1

I need to port some functions from C# to Python, but i can't implement next code right:

[SqlFunction(IsDeterministic = true, DataAccess = DataAccessKind.None)]
public static SqlDouble LogNormDist(double probability, double mean, double stddev)
{
    LognormalDistribution lnd = new LognormalDistribution(mean,stddev);
    return (SqlDouble)lnd.CDF(probability);
}

This code uses CenterSpace Nmath library.

Anyone can help me to write a right function in python, which will be similar to this code?

Sorry for my English.

UPD Actually, i don't understand which scipy.stats.lognorm.cdf attrs are simillar to C# probability, mean, stddev

If just copy existing order to python, like in answer below, i get wrong number.

Matthew Strawbridge
  • 19,940
  • 10
  • 72
  • 93
Ivan
  • 45
  • 6

4 Answers4

4

Scipy has a bunch of distributions defined in the scipy.stats package

import scipy.stats

def LogNormDist(prob, mean=0, stddev=1):
    return scipy.stats.lognorm.cdf(prob,stddev,mean)

Update

Okay, it looks like Scipy's stat definitions are a little nonstandard. Here's the end of the docstring for scipy.stats.lognormal

Lognormal distribution

lognorm.pdf(x,s) = 1/(sxsqrt(2*pi)) * exp(-1/2*(log(x)/s)**2) for x > 0, s > 0.

If log x is normally distributed with mean mu and variance sigma**2, then x is log-normally distributed with shape paramter sigma and scale parameter exp(mu).

So maybe try

return scipy.stats.lognorm.cdf(prob,stddev,scipy.exp(mean))

If that still doesn't work, try getting a few sample points and I'll see if I can find a working relationship.

Udpate 2

Oops, I didn't realize that the scale param is a keyword. This one should now work:

import scipy.stats

def LogNormDist(prob, mean=0, stddev=1):
    return scipy.stats.lognorm.cdf(prob,stddev,scale=scipy.exp(mean))

Cheers and good luck with your project!

Tim Lin
  • 3,244
  • 2
  • 19
  • 17
  • Thanks, but that still doesn't work, so i've run C# code with different attributes and this is what i have: prob | stddev | mean | result 0.3 | 1.0 | 3.2 | 5,31431365968782E-06 0.8 | 2.3 | 7.0 | 0,000843306609677019 0.1 | 0.2 | 0.3 | 0 1 | 4.1 | 6.8 | 0,0486046047161232 – Ivan Apr 29 '09 at 12:56
2

The Python docs describe a method random.lognormvariate(mu, sigma):

http://docs.python.org/library/random.html

Maybe that's what you want.

duffymo
  • 305,152
  • 44
  • 369
  • 561
0

Ivan,

We've got no interest in keeping people locked into NMath. Here's what we're doing in NMath.

  double t = ( Math.Log( x ) - mu_ ) / sigmaRoot2_;
  return ( 0.5 + 0.5 * Erf( t ) );

where

private static double Erf( double x )
{
  return ( x < 0.0 ? -StatsFunctions.IncompleteGamma( 0.5, x * x ) : StatsFunctions.IncompleteGamma( 0.5, x * x ) );
}

That should help...

  • Trevor
Trevor Misfeldt
  • 216
  • 2
  • 8
0

Maybe you can use Python.NET (this is NOT IronPython), it allows to access .NET components and services:

http://pythonnet.sourceforge.net/

theller
  • 2,809
  • 19
  • 19
  • I use Python.NET with Python 2.6 (had to build it myself). It's somewhat unstable, lacks .NET 3.0, and it seems the project is inactive for quite some time. I would not recommend it for production code, but it's quite nice to have .NET components handy from Python. – Josip Apr 29 '09 at 11:41