样本:S = [s1, s2, ..., sn]
平均值:m = (s1+s2+...+sn)/n
离差:D = [d1, d2, ..., dn], di = si-m
离差方:Q = [q1, q2, ..., qn], qi = di**2
总体方差:v = (q1+q2+...+qn)/n
总体标准差:s = sqrt(v),方均根
样本方差:v' = (q1+q2+...+qn)/(n-1)
样本标准差:s' = sqrt(v'),方均根
np.std(array) # 总体标准差 np.std(array, ddof=1) # 样本标准差
# 中位数 import numpy as np import datetime as dt def dmy2ymd(dmy): """ 把日月年转年月日 :param day: :return: """ dmy = str(dmy, encoding='utf-8') t = dt.datetime.strptime(dmy, '%d-%m-%Y') s = t.date().strftime('%Y-%m-%d') return s dates, opening_prices, \ highest_prices, lowest_prices, \ closing_prices, volumes = \ np.loadtxt('aapl.csv', delimiter=',', usecols=(1, 3, 4, 5, 6, 7), unpack=True, dtype='M8[D],f8,f8,f8,f8,f8', converters={1: dmy2ymd}) # 日月年转年月日 #总体封盘标准差 std_c = np.std(closing_prices) print(std_c) # 7.080008325481608 #总体开盘标准差 std_o = np.std(opening_prices) print(std_o) # 7.099438350242144 std_c2 = np.std(closing_prices, ddof=1) # 样本标准差 print(std_c2) # 7.201042876260849 # 手动实现 m = np.mean(closing_prices) #算数平均值 d = closing_prices - m #利差 v = np.mean(d ** 2)#离差方 s = np.sqrt(v)#总体标准差 print(s) # 7.080008325481608 v2 = (d ** 2).sum() / (d.size - 1) s2 = np.sqrt(v2) print(s2) # 样本标准差 # 7.201042876260849