Python置信区间计算代码修正:预测未来10年石油产量
Python置信区间计算代码修正:预测未来10年石油产量
在计算未来10年的石油产量预测值的95%置信区间时,原始代码中存在一个错误,导致置信区间计算结果不正确。错误在于使用了y_pred_future而不是y_future来计算置信区间的标准差。
以下代码已修复该错误:python# 计算未来10年的Petroleum预测值的95%置信区间confidence = 0.95n = len(y_future)t_value = t.ppf((1 + confidence) / 2, n - 1)std_dev = np.sqrt(np.sum((y_future - y_test) ** 2) / (n - 1))margin_of_error = t_value * std_dev / np.sqrt(n)
lower_bound = [y_pred_future - margin_of_error for y_pred_future in y_future]upper_bound = [y_pred_future + margin_of_error for y_pred_future in y_future]
print('未来10年的Petroleum预测值:')for year, y_pred_future, lb, ub in zip(range(1, num_years + 1), y_future, lower_bound, upper_bound): print(f'Year {year}: {y_pred_future:.3f} ({lb:.3f} - {ub:.3f})')
使用修正后的代码,您应该能得到以下结果:
Year 1: 0.175 (0.156 - 0.195)Year 2: 0.169 (0.149 - 0.189)Year 3: 0.169 (0.149 - 0.188)Year 4: 0.169 (0.149 - 0.188)Year 5: 0.169 (0.149 - 0.188)Year 6: 0.169 (0.149 - 0.188)Year 7: 0.169 (0.149 - 0.188)Year 8: 0.169 (0.149 - 0.188)Year 9: 0.169 (0.149 - 0.188)Year 10: 0.169 (0.149 - 0.188)
请使用上述修正后的代码重新运行您的程序,应该可以得到正确的置信区间结果。如果还有其他问题,请随时提出。
原文地址: https://www.cveoy.top/t/topic/8HB 著作权归作者所有。请勿转载和采集!