我有一个这样的问题:Question screenshot。我一直在使用Python,但结果似乎很可疑。你能否检查代码有什么问题?
import numpy as np
import statsmodels.api as sm
import statsmodels.formula.api as smf
sig = 1 # standard deviation of the distributions
nsim = 100 # number of simulations
tvec = [10, 25, 50, 100, 250, 1000] # how many different inputs are used? store the number in the vector.
nvec = [1, 2, 3, 5, 10] # how many observations for each sample?
holding_place = np.zeros((nsim, len(tvec), len(nvec))) # stores the rsquared number of the regression.
for isim in range(nsim):
for tdx, t in enumerate(tvec):
y = np.zeros((t, 1))
for i in range(t): # y[0] = 0 + np.random.normal(0, sig)
y[i] = y[i-1] + np.random.normal(0, sig) # get new numbers
for ndx, n in enumerate(nvec):
x = np.zeros((t, n))
for i in range(t):
x[i, ndx] = x[i-1, ndx] + np.random.normal(0, sig)
x = sm.add_constant(x)
model = smf.OLS(y, x).fit()
holding_place[isim, tdx, ndx] = model.rsquared
print(holding_place.mean(0)) # the average rsquared for the (n,t) combination