从平均值拟合Gamma分布的参数b,在Python中为95% [英] Fitting parameter b of the Gamma distribution from mean, 95% in Python
问题描述
此Mathematica函数可找到Gamma分布的参数b, 给定平均值和95%的值,并用平均值[Mu]进行缩放; 两个值括号[Beta]使其速度很快,并且最大pg95 = 5.8 [Mu] * gb [[Mu] ,p95 ]有一个限制. 我需要将此代码转换为Python:
This Mathematica function finds parameter b of the Gamma distribution, given mean and 95% values and scaled by the mean [Mu]; The two values bracket [Beta] makes it fast and there is a restriction for max pg95= 5.8[Mu]* gb[[Mu], p95]. I need to translate this code into Python:
gb[\[Mu]_, p95_] := Block[{p = Min[p95/\[Mu], 5.8]},
\[Mu] FindRoot[CDF[GammaDistribution[1/\[Beta], \[Beta]], p] - .95 == 0,
{\[Beta], 1, If[p == 1, 1.1, p]}][[1, 2]]];
推荐答案
即使您找不到完全等效的伽马函数,您也应该能够使用SciPy的集成和求根函数来翻译gb
.可以获得所需的功能,例如(说明一些演示值)
Even if you cannot find exactly equivalent gamma functions you ought to be able to translate gb
with SciPy's integration and root finding functions. The functions required can be obtained, e.g. (illustrating with some demo values)
例如
如您所见,虽然速度较慢,但由更基本的函数构造的代码会产生相同的答案.
As you can see, the code constructed from the more basic functions produces the same answer, albeit more slowly.
代码
Code
gamma[z_] := \!\(
\*SubsuperscriptBox[\(\[Integral]\), \(0\), \(\[Infinity]\)]\(
\*SuperscriptBox[\(t\), \(z - 1\)]
\*SuperscriptBox[\(E\), \(-t\)] \[DifferentialD]t\)\)
gamma[a_, z0_, z1_] := \!\(
\*SubsuperscriptBox[\(\[Integral]\), \(z0\), \(z1\)]\(
\*SuperscriptBox[\(t\), \(a - 1\)]
\*SuperscriptBox[\(E\), \(-t\)] \[DifferentialD]t\)\)
gammaregularized[a_, z1_] := gamma[a, 0, z1]/gamma[a]
cdf[\[Beta]_, p_] :=
Piecewise[{{gammaregularized[1/\[Beta], p/\[Beta]], p > 0}}]
p = 1.2;
FindRoot[cdf[\[Beta], p] - .95, {\[Beta], 1, If[p == 1, 1.1, p]}]
FindRoot[CDF[GammaDistribution[1/\[Beta], \[Beta]],
p] - .95, {\[Beta], 1, If[p == 1, 1.1, p]}]
这篇关于从平均值拟合Gamma分布的参数b,在Python中为95%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!