![](/img/trans.png)
[英]How to calculate the standard deviation of a list of m x n matrices in Python?
[英]How to calculate Standard Deviation in Python when x and P(x) are known
澄清一下,如果假设所有 6 个项具有相等的概率分布,则 [1000, 2000, 3000, 0, -1000, -2000] 的标准差确实是 1707.8。
但是在帖子中,这 6 个术语的概率分布不均 [0.1, 0.1, 0.2, 0.2, 0.3, 0.1]
df = pd.DataFrame([
{'x':-2000, 'P(x)':0.1},
{'x':-1000, 'P(x)':0.1},
{'x':0, 'P(x)':0.2},
{'x':1000, 'P(x)':0.2},
{'x':2000, 'P(x)':0.3},
{'x':3000, 'P(x)':0.1} ])
df['E(x)'] = df['x'] * df['P(x)'] # E(x) = x . P(x)
df['E(x^2)'] = df['x']**2 * df['P(x)'] # E(x^2) = x^2 . P(x)
variance = df['E(x^2)'].sum() - df['E(x)'].sum() **2
std_dev = variance **0.5
display(df)
print('Standard Deviation is: {:.2f}'.format(std_dev))
Output
x P(x) E(x) E(x^2)
0 -2000 0.1 -200.0 400000.0
1 -1000 0.1 -100.0 100000.0
2 0 0.2 0.0 0.0
3 1000 0.2 200.0 200000.0
4 2000 0.3 600.0 1200000.0
5 3000 0.1 300.0 900000.0
Standard Deviation is: 1469.69
要确认,可以 go 到https://www.rapidtables.com/calc/math/standard-deviation-calculator.html
尝试这个:
import math
df['x_squared'] = df['x']**2
df['E_of_x_squared'] = df['x_squared'] * df['P(x)']
df['E_of_x'] = df['x'] * df['P(x)']
sum_E_x_square = df['E_of_x_squared'].values.sum()
square_of_E_x_sum = df['E_of_x'].values.sum()**2
var = sum_E_x_square - square_of_E_x_sum
std_dev = math.sqrt(var)
print('Standard Deviation is: ' + str(std_dev))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.