我在python上有一个代码,它试图在累积每个循环中的条目时计算样本方差。
y_hat = y_df.loc[n-1]
var = []
var_sum = 0
for i in range(n):
var_i = (g_i[i] - y_hat)**2
var_sum += var_i
if i == 0:
var_avg = var_sum
var.append(var_avg)
else:
var_avg = var_sum/i
var.append(var_avg)
结果的输出会产生非常奇怪的第一行(当我为1时),而其余的行都很好。有人可以帮忙吗?
这是当前输出:
下面是我的整个脚本,本质上,我正在测试Monte Carlo仿真以评估pi。
import numpy as np
import math
import matplotlib.pyplot as plt
import random
import pandas as pd
import statistics as stats
n = 1000
k = 100
# generate u r.v. with size k*n -> (100,1000)
u = []
for i in range(k):
u_i = np.random.uniform(size = n)
u.append(u_i)
# put into dataframe (k*n)
u_df = pd.DataFrame(u)
# calculate g_i, g_i is a df with k*n
g_i = 4*np.sqrt(1-u_df**2)
g_sum = 0
y = []
for i in range(n):
g_sum += g_i[i]
y_i = g_sum/(i+1)
y.append(y_i)
# put y into df -> n*k
y_df = pd.DataFrame(y)
y_df = y_df.reset_index(drop=True)
y_hat = y_df.loc[n-1]
var = []
var_sum = 0
for i in range(n):
var_i = (g_i[i] - y_hat)**2
var_sum += var_i
if i == 0:
var_avg = var_sum
var.append(var_avg)
else:
var_avg = var_sum/i
var.append(var_avg)
var_df = pd.DataFrame(var)
var_df = var_df.reset_index(drop=True)
var_df.head()
答案 0 :(得分:0)
当在i == 0 if语句中运行var.append(var_avg)时,您将通过引用var_sum附加var_sum。因此,每次更改var_sum时,都将更改var [0]。您可以通过在i == 0时显式复制var_sum来解决此问题。更正的if-else语句应为
if i == 0:
var_avg = var_sum
var.append(var_avg.copy())
else:
var_avg = var_sum/(i+1)
var.append(var_avg)