本文介绍了如何使用 Mu 和 Sigma 在 Python 中获得对数正态分布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试获得 使用 Scipy 的对数正态 分布.我已经有了 Mu 和 Sigma,所以我不需要做任何其他准备工作.如果我需要更具体(并且我试图以我有限的统计知识),我会说我正在寻找累积函数(Scipy 下的 cdf).问题是我无法弄清楚如何仅使用 0-1 范围内的均值和标准差(即返回的答案应该是 0-1 的值).我也不确定 dist 中的哪种方法,我应该使用它来获得答案.我试过阅读文档并浏览 SO,但相关问题(如 this这个) 似乎没有提供我一直在寻找的答案.

I have been trying to get the result of a lognormal distribution using Scipy. I already have the Mu and Sigma, so I don't need to do any other prep work. If I need to be more specific (and I am trying to be with my limited knowledge of stats), I would say that I am looking for the cumulative function (cdf under Scipy). The problem is that I can't figure out how to do this with just the mean and standard deviation on a scale of 0-1 (ie the answer returned should be something from 0-1). I'm also not sure which method from dist, I should be using to get the answer. I've tried reading the documentation and looking through SO, but the relevant questions (like this and this) didn't seem to provide the answers I was looking for.

这是我正在使用的代码示例.谢谢.

Here is a code sample of what I am working with. Thanks.

from scipy.stats import lognorm
stddev = 0.859455801705594
mean = 0.418749176686875
total = 37
dist = lognorm.cdf(total,mean,stddev)

更新:

所以经过一些工作和一些研究后,我走得更远了.但我仍然得到错误的答案.新代码如下.根据 R 和 Excel,结果应该是 .7434,但这显然不是正在发生的.是否有我遗漏的逻辑缺陷?

So after a bit of work and a little research, I got a little further. But I still am getting the wrong answer. The new code is below. According to R and Excel, the result should be .7434, but that's clearly not what is happening. Is there a logic flaw I am missing?

dist = lognorm([1.744],loc=2.0785)
dist.cdf(25)  # yields=0.96374596, expected=0.7434

更新 2:工作 lognorm 实现可产生正确的 0.7434 结果.

UPDATE 2:Working lognorm implementation which yields the correct 0.7434 result.

def lognorm(self,x,mu=0,sigma=1):
   a = (math.log(x) - mu)/math.sqrt(2*sigma**2)
   p = 0.5 + 0.5*math.erf(a)
   return p
lognorm(25,1.744,2.0785)
> 0.7434

推荐答案

听起来您想根据已知参数实例化冻结"分布.在您的示例中,您可以执行以下操作:

It sounds like you want to instantiate a "frozen" distribution from known parameters. In your example, you could do something like:

from scipy.stats import lognorm
stddev = 0.859455801705594
mean = 0.418749176686875
dist=lognorm([stddev],loc=mean)

这将为您提供一个对数分布对象,其中包含您指定的均值和标准差.然后,您可以像这样获取 pdf 或 cdf:

which will give you a lognorm distribution object with the mean and standard deviation you specify. You can then get the pdf or cdf like this:

import numpy as np
import pylab as pl
x=np.linspace(0,6,200)
pl.plot(x,dist.pdf(x))
pl.plot(x,dist.cdf(x))

这是你的想法吗?

这篇关于如何使用 Mu 和 Sigma 在 Python 中获得对数正态分布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 04:40