我需要在 Latex 中绘制伽马分布的 cdf 图表。我能够使用此处解释的 erf 函数绘制正态分布的图表: 如何在 Tikz 中绘制正态分布的 CDF(第三条评论)
我该如何对 gamma 进行操作?
答案1
您可以使用sagetex
包,文档位于这里在 CTAN 上,可以解决这类问题。这可以让你访问计算机代数系统,智者,它类似于 Mathematica(除了它是免费的)。您还可以获得 Python 编程语言作为奖励。查看维基百科页面中的伽马分布这里我在代码中使用了第二种形式(涉及 alpha 和 beta 而不是 k 和 theta)来获取函数f = (beta^(alpha)/gamma(alpha))*x^(alpha-1)*e^(-beta*x)
,并且出于说明目的我将其输入alpha = 7.5 beta = 3.8
。Sage 通过 计算 CDF。SageN = lambda a: integrate(f,x,0,a)
确定的值由 pgfplots 用于绘图。
\documentclass{article}
\usepackage{sagetex}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{sagesilent}
t = var('t')
LowerY = 0
UpperY = 1
LowerX = 0
UpperX = 10
step = .1
var("x, alpha, beta", domain="positive")
assume(alpha,"noninteger")
alpha = 7.5
beta = 3.8
f = (beta^(alpha)/gamma(alpha))*x^(alpha-1)*e^(-beta*x)
N = lambda a: integrate(f,x,0,a)
x_coords = [t for t in srange(LowerX,UpperX,step)]
y_coords = [N(t).n(digits=6) for t in srange(LowerX,UpperX,step)]
output = r""
output += r"\begin{tikzpicture}[scale=.7]"
output += r"\begin{axis}[xmin=%f,xmax=%f,ymin= %f,ymax=%f]"%(LowerX,UpperX,LowerY, UpperY)
output += r"\addplot[thin, blue, unbounded coords=jump] coordinates {"
for i in range(0,len(x_coords)-1):
if (y_coords[i])<LowerY or (y_coords[i])>UpperY:
output += r"(%f , inf) "%(x_coords[i])
else:
output += r"(%f , %f) "%(x_coords[i],y_coords[i])
output += r"};"
output += r"\end{axis}"
output += r"\end{tikzpicture}"
\end{sagesilent}
\begin{center}
\sagestr{output}
\end{center}
\end{document}
Sage 不是 LaTeX 的一部分,因此需要将其下载到您的计算机,或者您可以使用免费的Cocalc 账户。免费的 Cocalc 帐户是迄今为止最简单的入门方式。
答案2
- 寻找在线
gamma
函数发生器,例如这里或者使用您最喜欢的软件包(Matlab,Python等)进行计算。 x
生成包含和数据的文本文件(表格)y
。- 用于
pgfplots
绘制表格。 - 由于您似乎对该网站不太熟悉,请查看这(“巡回赛”)和这(“MWE”)。
\documentclass{article}
\begin{filecontents}{data1.txt}
0 0
0.1 0.00452418709
0.2 0.01637461506
0.3 0.03333681993
0.4 0.05362560368
0.5 0.07581633246
0.6 0.0987860945
0.7 0.1216633994
0.8 0.1437852685
0.9 0.1646607122
1 0.1839397206
1.1 0.2013870056
1.2 0.2168598326
1.3 0.2302893651
1.4 0.2416650247
1.5 0.2510214302
1.6 0.258427543
1.7 0.2639776923
1.8 0.2677841989
1.9 0.2699713577
2 0.2706705665
2.1 0.2700164243
2.2 0.2681436432
2.3 0.2651846416
2.4 0.2612677055
2.5 0.2565156207
2.6 0.2510446944
2.7 0.2449640939
2.8 0.2383754455
2.9 0.2313726403
3 0.2240418077
3.1 0.2164614175
3.2 0.2087024844
3.3 0.2008288465
3.4 0.1928975004
3.5 0.1849589735
3.6 0.1770577215
3.7 0.1692325387
3.8 0.1615169728
3.9 0.1539397365
4 0.1465251111
4.1 0.1392933368
4.2 0.1322609876
4.3 0.1254413281
4.4 0.1188446503
4.5 0.11247859
4.6 0.1063484222
4.7 0.1004573356
4.8 0.094806686
4.9 0.08939622977
5 0.08422433749
5.1 0.07928818908
5.2 0.07458395097
5.3 0.07010693642
5.4 0.06585175014
5.5 0.06181241801
5.6 0.05798250307
5.7 0.05435520886
5.8 0.05092347082
5.9 0.04768003707
6 0.04461753918
6.1 0.04172855392
6.2 0.03900565683
6.3 0.0364414683
6.4 0.03402869295
6.5 0.03176015295
6.6 0.02962881586
6.7 0.02762781766
6.8 0.02575048142
6.9 0.02399033214
7 0.02234110816
7.1 0.02079676959
7.2 0.01935150415
7.3 0.01799973067
7.4 0.0167361006
7.5 0.01555549791
7.6 0.0144530374
7.7 0.01342406184
7.8 0.01246413806
7.9 0.01156905218
8 0.01073480409
8.1 0.009957601425
8.2 0.009233853022
8.3 0.00856016211
8.4 0.007933319197
8.5 0.007350294831
8.6 0.00680823225
8.7 0.006304440017
8.8 0.005836384668
8.9 0.005401683433
9 0.004998097066
9.1 0.004623522801
9.2 0.004275987486
9.3 0.00395364089
9.4 0.003654749216
9.5 0.003377688824
9.6 0.003120940177
9.7 0.002883082025
9.8 0.002662785805
9.9 0.002458810294
10 0.002269996488
\end{filecontents}
\begin{filecontents}{data2.txt}
x gamma distribution
0 0
0.1 1.546530703E-4
0.2 0.001148481245
0.3 0.003599493183
0.4 0.007926331867
0.5 0.01438767797
0.6 0.02311528775
0.7 0.03414158413
0.8 0.04742259607
0.9 0.0628569343
1 0.08030139707
1.1 0.0995837186
1.2 0.1205129012
1.3 0.1428875109
1.4 0.1665022619
1.5 0.1911531695
1.6 0.2166415102
1.7 0.2427767928
1.8 0.2693789141
1.9 0.2962796466
2 0.3233235838
2.1 0.3503686481
2.2 0.37728625
2.3 0.4039611741
2.4 0.4302912533
2.5 0.4561868841
2.6 0.4815704241
2.7 0.5063755089
2.8 0.5305463165
2.9 0.5540368014
3 0.5768099189
3.1 0.5988368527
3.2 0.6200962589
3.3 0.6405735337
3.4 0.6602601118
3.5 0.6791528011
3.6 0.6972531553
3.7 0.7145668869
3.8 0.7311033223
3.9 0.7468748974
4 0.7618966944
4.1 0.7761860187
4.2 0.789762013
4.3 0.8026453092
4.4 0.8148577143
4.5 0.8264219291
4.6 0.8373612977
4.7 0.8476995849
4.8 0.8574607811
4.9 0.8666689301
5 0.8753479805
5.1 0.8835216569
5.2 0.8912133496
5.3 0.898446022
5.4 0.9052421318
5.5 0.9116235676
5.6 0.9176115964
5.7 0.9232268226
5.8 0.9284891569
5.9 0.9334177937
6 0.9380311956
6.1 0.9423470853
6.2 0.9463824426
6.3 0.9501535068
6.4 0.9536757832
6.5 0.9569640531
6.6 0.9600323871
6.7 0.9628941607
6.8 0.9655620724
6.9 0.968048163
7 0.9703638361
7.1 0.9725198805
7.2 0.9745264922
7.3 0.9763932975
7.4 0.9781293762
7.5 0.9797432849
7.6 0.9812430803
7.7 0.9826363417
7.8 0.9839301941
7.9 0.9851313303
8 0.9862460323
8.1 0.9872801924
8.2 0.9882393341
8.3 0.9891286314
8.4 0.989952928
8.5 0.9907167557
8.6 0.9914243521
8.7 0.9920796776
8.8 0.9926864312
8.9 0.9932480662
9 0.9937678049
9.1 0.9942486525
9.2 0.9946934106
9.3 0.9951046895
9.4 0.9954849205
9.5 0.995836367
9.6 0.9961611352
9.7 0.9964611846
9.8 0.9967383369
9.9 0.9969942857
10 0.9972306043
\end{filecontents}
\usepackage{pgfplots}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}
\addplot table [x index=0, y index=1]{data1.txt}; % use \addplot[mark=none] table ... if you do not like the "dots"
\end{axis}
\end{tikzpicture}
\caption{Gamma Function}
\end{figure}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}
\addplot table [x index=0, y index=1]{data2.txt};
\end{axis}
\end{tikzpicture}
\caption{CDF of Gamma Function}
\end{figure}
\end{document}