我的问题涉及到统计学和python,我是这两方面的初学者。我正在运行一个模拟,对于自变量(X)的每个值,我为因变量(Y)生成1000个值。我所做的是计算每个X值的Y平均值,并使用scipy.optimize.curve_fit拟合这些平均值。曲线拟合得很好,但我也想画出置信区间。我不确定我所做的是否正确,也不确定我想做的是否可以做到,但我的问题是如何从曲线拟合产生的协方差矩阵中得到置信区间。代码首先从文件中读取平均值,然后只使用曲线拟合。
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
def readTDvsTx(L, B, P, fileformat):
# L should be '_Fixed_' or '_'
TD = []
infile = open(fileformat.format(L, B, P), 'r')
infile.readline() # To remove header
for line in infile:
l = line.split() # each line contains TxR followed by CD followed by TD
if eval(l[0]) >= 70 and eval(l[0]) <=190:
td = eval(l[2])
TD.append(td)
infile.close()
tdArray = np.array(TD)
return tdArray
def rec(x, a, b):
return a * (1 / (x**2)) + b
fileformat = 'Densities_file{}BS{}_PRNTS{}.txt'
txR = np.array(range(70, 200, 20))
parents = np.array(range(1,6))
disc_p1 = readTDvsTx('_Fixed_', 5, 1, fileformat)
popt, pcov = curve_fit(rec, txR, disc_p1)
plt.plot(txR, rec(txR, popt[0], popt[1]), 'r-')
plt.plot(txR, disc_p1, '.')
print(popt)
plt.show()
目前没有回答
相关问题 更多 >
编程相关推荐