2

I have a mode, a maximum and minimum value of X (Xmin and Xmax), and a percentage confidence (percentage).

I want to use the following functions in order to find the μ and σ of a theoretical log normal distribution:

The cumulative distribution function:

CDF

and the mode

mode

I began with the following Matlab script:

function [mu, sigma] = DefLog(Mode, Percentage, Xmin, Xmax)

syms s
eqn = 1/2+1/2*erf((log(Xmax)-(log(Mode)+s^2))/(sqrt(2)*s))-(1/2+1/2*erf((log(Xmin)-(log(Mode)+s^2))/(sqrt(2)*s)))==Percentage;
sigma = solve(eqn,s)

mu=log(Mode)+sigma^2

end

And this gives me a single numerical solution for mu and sigma.

For example if I run DefLog(2, 0.95, 1, 4) I get sigma = 0.33 and mu = 0.80

I needed to translate this equation into Python, so I used sympy to solve the same equation. The only way I could get a single numerical solution with sympy was to use the nsolve function. My code is as follows:

from sympy import *

def CalcScaleParamOPT(mode, percentage, Xmin, Xmax):

    s = Symbol('s', Real=True)

    eqn = (1/2+1/2*erf((log(Xmax)-(log(mode)+s**2))/(sqrt(2)*s))-(1/2+1/2*erf((log(Xmin)-(log(mode)+s**2))/(sqrt(2)*s)))) - 0.95

    sigma = nsolve(eqn, 0.6)

    mu=log(mode)+sigma**2

    print(sigma)
    print(mu.evalf())


CalcScaleParamOPT(2, 0.95, 1, 4)

This gives the same solution as the matlab script, but unlike the matlab solve() function nsolve() requires a "guess" close enough to the answer I am looking for. How can matlab find a single solution without the guess?

Community
  • 1
  • 1
cachemoi
  • 343
  • 2
  • 13
  • Those are two completely different functions. Matlab's `solve` is a symbolic solver (similar to Sympy's `solve`). Sympy's `nsolve` is a numeric solver (similar to Matlab's `vpasolve` or maybe `fsolve`). – horchler Jun 27 '16 at 16:10
  • the docs say `solve()` is to be replaced with `solvestet()` in sympy 1.0, but using `solveset` to solve this does not return any results. I am using nsolve following a recommendation I got when I asked about this difference, link [here](http://stackoverflow.com/questions/37818218/sympy-returns-a-conditionset-object-when-solving-an-equation-while-matlab-retur) – cachemoi Jun 27 '16 at 16:23
  • A guess implies a non-linear, iterative, numerical solution to me. – duffymo Jun 27 '16 at 18:30

1 Answers1

1

Based on the documentation, MATLAB's solve falls back to a numerical solution automatically. Assumedly it generates a guess value automatically (it doesn't mention how), but it does say that you can use vpasolve to pass in a guess interval manually, since the default solve only returns one numerical solution (the first one it finds).

asmeurer
  • 86,894
  • 26
  • 169
  • 240
  • Thanks, unfortunately that seems to be the case. Do you know of a sympy function that would generate the guess value automatically? I cannot seem to find one in the docs. `nsolve()` fails if the guess value is too far off. – cachemoi Jun 29 '16 at 11:01
  • Looks like `nsolve` also lets you pass in an interval for a guess, if that helps. `nsolve` is just a wrapper around `mpmath.findroot`, so you might look at [its docs](http://mpmath.org/doc/current/calculus/optimization.html#mpmath.findroot) for more information on what it can do. – asmeurer Jun 29 '16 at 16:31