In [1]:
import random
import numpy as np
import matplotlib.pyplot as plt
X遵循随机分布¶
In [2]:
X = [random.uniform(0, 10) for _ in range(0, 1000)]
plt.figure()
plt.scatter(range(0, 1000), X)
plt.show()
In [3]:
sum(X)
Out[3]:
4889.740271640322
In [4]:
sum_x = []
for test in range(0, 1000):
X = [random.uniform(0, 10) for _ in range(0, 1000)]
sum_x.append(sum(X))
plt.figure()
plt.hist(sum_x, bins=20)
plt.xlabel('Sum of X_i from uniform distribution (0, 10)')
plt.ylabel('Count')
plt.show()
每个x都在0和10之间随机选一个(50%~50%)¶
In [5]:
X = [random.choice([0, 10]) for _ in range(0, 1000)]
plt.figure()
plt.scatter(range(0, 1000), X)
plt.show()
In [6]:
sum_x = []
for test in range(0, 1000):
X = [random.choice([0, 10]) for _ in range(0, 1000)]
sum_x.append(sum(X))
plt.figure()
plt.hist(sum_x, bins=20)
plt.xlabel('Sum of X_i from 50% choice of 0 and 50% choice of 10')
plt.ylabel('Count')
plt.show()
每个x都有99%的概率选择0和1%的概率选择10¶
In [7]:
X = [(10 if random.random() > 0.99 else 0) for _ in range(0, 1000)]
plt.figure()
plt.scatter(range(0, 1000), X)
plt.show()
In [8]:
sum_x = []
for test in range(0, 1000):
X = [(10 if random.random() > 0.99 else 0) for _ in range(0, 1000)]
sum_x.append(sum(X))
plt.figure()
plt.hist(sum_x, bins=20)
plt.xlabel('Sum of X_i from 99% choice of 0 and 1% choice of 10')
plt.ylabel('Count')
plt.show()
In [ ]: