C# Nmath 到 Python SciPy

发布于 2024-07-19 08:01:19 字数 591 浏览 5 评论 0原文

我需要将一些函数从 C# 移植到 Python,但我无法正确实现下一个代码:

[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);
}

此代码使用 CenterSpace Nmath 库。

任何人都可以帮我在 python 中编写一个与此代码类似的正确函数吗?

对不起我的英语不好。

UPD 实际上,我不明白哪些 scipy.stats.lognorm.cdf attrs 与 C# 概率相似,意思是 stddev

如果只是将现有订单复制到 python,就像下面的答案一样,我会得到错误的数字。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

嗼ふ静 2024-07-26 08:01:19

Scipy 在 scipy.stats 包中定义了一堆发行版

import scipy.stats

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

更新

好吧,看起来 Scipy 的统计定义有点不标准。 这是 scipy.stats.lognormal 文档字符串的结尾

对数正态分布

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

如果 log x 服从均值 mu 和方差 sigma**2 的正态分布,
那么 x 是具有形状参数 sigma 和尺度的对数正态分布
参数exp(mu)。

所以也许可以尝试一下,

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

如果这仍然不起作用,请尝试获取一些样本点,我会看看是否可以找到工作关系。

Udpate 2

哎呀,我没有意识到比例参数是一个关键字。 这个现在应该可以工作了:

import scipy.stats

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

干杯,祝你的项目好运!

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!

笑,眼淚并存 2024-07-26 08:01:19

Python 文档描述了一种方法 random.lognormvariate(mu, sigma):

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

也许这就是您想要的。

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

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

Maybe that's what you want.

倾其所爱 2024-07-26 08:01:19

也许您可以使用 Python.NET(这不是 IronPython),它允许访问 .NET 组件和服务:

http://pythonnet.sourceforge.net/

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

http://pythonnet.sourceforge.net/

半透明的墙 2024-07-26 08:01:19

Ivan,

我们没有兴趣让人们局限于 NMath。 这就是我们在 NMath 中所做的事情。

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

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

应该有帮助......

  • 特雷弗

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文