计算拟合函数的标准差、方差
#计算拟合函数的标准差、方差
from sympy import *
X = [3.2,2.99,2.7,2.28,1.53]
Y = [7.06,6.94,6.73,6.52,6.06] #实际Y值
n = len(X)
def f(x):
y = 0.5955*x + 5.1486
return y
Y_v = [] #拟合函数的Y值
for i in range(n):
Y_v.append(f(X[i]))
S = 0
for i in range(n):
S += (Y[i] - Y_v[i])**2
S = S/n
print('拟合函数方差:',S)
print('拟合函数标准差:',sqrt(S))
结果:
拟合函数方差: 0.00020755006999999574
拟合函数标准差: 0.0144065981411295
计算样本的标准差、方差
#计算样本的标准差、方差
from sympy import *
X = [13.64,13.72,13.6,13.34,13.28,13.34,13.7,13.81,13.62,13.93]
n = len(X)
X_Sum = 0
for i in range(n):
X_Sum += X[i]
X_Ave = X_Sum/n
S = 0
for i in range(n):
S += (X[i] - X_Ave)**2
S = S/(n-1)
print('样本方差:',S)
print('样本标准差:',sqrt(S))
结果:
样本方差: 0.04632888888888896
样本标准差: 0.215241466471702