python 求 gamma 分布_从平均值拟合Gamma分布的参数b,在Python中为95%

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]]];

解决方案

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)

For example

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]}]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值