概率论python代码
1 1.指数分布 2 import matplotlib 3 import matplotlib.pyplot as plt 4 import numpy as np 5 matplotlib.use('Agg') 6 x = np.linspace(0, 2) 7 8 9 def func(lmd,x): 10 return lmd*np.exp(-lmd*x) 11 12 13 lambd1 = 1 14 lambd2 = 2 15 16 fig,ax=plt.subplots(figsize=(6,5)) 17 plt.rcParams['font.sans-serif']=['SimSun'] 18 19 plt.title("指数分布",fontsize=20) 20 y1=func(lambd1,x) 21 y2=func(lambd2,x) 22 plt.plot(x,y1,label='lambda=1',linewidth=2,linestyle=':') 23 plt.plot(x,y2,label='lambda=2',linewidth=3) 24 plt.figtext(0.05,0.9,'$y$') 25 plt.figtext(0.97,0.05,'$x$') 26 plt.legend(['λ=1','λ=2']) 27 28 plt.savefig('new1.png') 29 30 31 2.均匀分布F(x) 32 import matplotlib 33 import matplotlib.pyplot as plt 34 import numpy as np 35 matplotlib.use('Agg') 36 x = np.linspace(0, 4,1000) 37 a = 1 38 b = 3 39 40 interval0 = [0 if (i <= a) else 0 for i in x] 41 interval1 = [1 if (a < i < b) else 0 for i in x] 42 interval2 = [1 if (i >= b) else 0 for i in x] 43 44 y = interval0 + (x - a)*interval1 / (b - a)+ interval2 45 46 fig, ax = plt.subplots() 47 ax.set_yticks([0, 1]) 48 ax.set_xticks([a,b]) 49 ax.set_xticklabels(['$a$','$b$']) 50 plt.figtext(0.05,0.9,'$F(x)$') 51 plt.figtext(0.9,0.05,'$x$') 52 53 plt.plot(x, y, label='1') 54 plt.show() 55 plt.savefig('new2.png') 56 均匀分布f(x) 57 import matplotlib 58 import matplotlib.pyplot as plt 59 import numpy as np 60 matplotlib.use('Agg') 61 x = np.linspace(0, 4, 1000) 62 a = 1 63 b = 3 64 y = [1 / (b - a) if (a < i < b) else 0 for i in x] 65 66 fig, ax = plt.subplots() 67 ax.set_yticks([0, 1]) 68 ax.set_xticks([a, b]) 69 ax.set_xticklabels(['$a$', '$b$']) 70 plt.figtext(0.05, 0.9, '$f(x)$') 71 plt.figtext(0.9, 0.05, '$x$') 72 73 plt.plot(x, y, label='1') 74 plt.savefig('new3.png')
所望隔山海