Binominal Distribution
Poisson Distribution
Gaussian Distribution
Exponential Distribution
Uniform Distribution
dim=1
dim=2
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(0)
Num = 10000
x = np.zeros(Num)
y = np.zeros(Num)
z = np.zeros(Num)
Times = 0
while Times < Num:
u,v = np.random.rand(2)
w = (2*u-1)**2 + (2*v-1)**2
if w<=1:
z[Times] = (-2*np.log(w)/w)**0.5
x[Times] = u * z[Times]
y[Times] = v * z[Times]
Times += 1
else:
continue
fig = plt.figure(figsize=(12, 6))
ax1 = fig.add_subplot(1, 2, 1)
ax2 = fig.add_subplot(1, 2, 2)
ax1.hist(x, np.arange(0, max(x), 1))
ax1.set_title("distribution x")
ax2.hist(y, np.arange(0, max(y), 1))
ax2.set_title("distribution y")
plt.savefig("3.jpg")
...
其中
...
近似替代
最小二乘拟合
...
import matplotlib.pyplot as plt
import numpy as np
import scipy.integrate as integrate
np.random.seed(0)
a = 0
b = 5
N = 100000
f = lambda x:x**2 + np.exp(x)
result1 = integrate.quad(f, a, b)
points = np.random.rand(N)*(b-a) + a
ybar = sum(f(points))/N
Sbar = (sum((points-ybar)**2)/N)**0.5
result2 = ((b-a)*ybar, (b-a)*Sbar/N**0.5)
X = np.linspace(a, b, N+1)
result3 = f(X[0]) + f(X[N])
flag = 1
for i in range(1, N-1):
if flag == 1:
result3 += 4*f(X[i])
flag *= -1
elif flag == -1:
result3 += 2*f(X[i])
flag *= -1
else:
print("ERROR!")
result3 *= (b-a)/N/3
print("result1:", result1)
print("result2:", result2)
print("result3:", result3)
result1: (189.07982576924329, 2.099207760611269e-12)
result2: (188.98624801068067, 0.5586055984291508)
result3: 189.06826542000084
>>>
import matplotlib.pyplot as plt
import numpy as np
import scipy.integrate as integrate
np.random.seed(0)
a = 0
b = 5
N = 100000
f = lambda x:x**2 + np.exp(x)
result1 = integrate.quad(f, a, b)
points = np.random.rand(N)*(b-a) + a
ybar = sum(f(points))/N
Sbar = (sum((points-ybar)**2)/N)**0.5
result2 = ((b-a)*ybar, (b-a)*Sbar/N**0.5)
X = np.linspace(a, b, N+1)
result3 = f(X)
coeff = np.ones(N+1)
coeff[1::2] = 4
coeff[2::2] = 2
result3 *= coeff
result3 = sum(result3)
result3 *= (b-a)/N/3
print("result1:", result1)
print("result2:", result2)
print("result3:", result3)
import matplotlib.pyplot as plt
import numpy as np
import scipy.integrate as integrate
import time
np.random.seed(0)
a = 0
b = 5
N = 100000
f = lambda x:x**2 + np.exp(x)
s1 = time.time()
for i in range(100):
result1 = integrate.quad(f, a, b)
e1 = time.time()
s2 = time.time()
for i in range(100):
points = np.random.rand(N)*(b-a) + a
ybar = sum(f(points))/N
Sbar = (sum((points-ybar)**2)/N)**0.5
result2 = ((b-a)*ybar, (b-a)*Sbar/N**0.5)
e2 = time.time()
X = np.linspace(a, b, N+1)
coeff = np.ones(N+1)
coeff[1::2] = 4
coeff[2::2] = 2
s3 = time.time()
for i in range(100):
result3 = f(X)
result3 *= coeff
result3 = sum(result3)
result3 *= (b-a)/N/3
e3 = time.time()
print("result1:", result1, "used time:", round(e1 - s1, 2))
print("result2:", result2, "used time:", round(e2 - s2, 2))
print("result3:", result3, "used time:", round(e3 - s3, 2))
result1: (189.07982576924329, 2.099207760611269e-12) used time: 0.0
result2: (188.83626394308098, 0.5580722224407848) used time: 1.8
result3: 189.0827159885614 used time: 0.9
>>>