def cm_test(X): """ Conditional moment test. X is a flat numpy array. """ betahat, alphahat, shat = ar1_functions.fit(X) n = len(X) xL = X[:(n-1)] # All but the last one xF = X[1:] # All but the first one Z = (xF - betahat - alphahat * xL)**2 XX = sm.add_constant(xL) out = sm.OLS(Z, XX).fit() return np.abs(out.tvalues[0]) > 1.96
def compute_power(alt_mod=None, ts_length=500, replications=1000, cv_reps=1000): """ Computes the rejection frequency for a given null and alternative. """ outcomes = [] for m in range(replications): # Generate time series from alternative X = alt_mod.ts(ts_length) # Estimate parameters under the null betahat, alphahat, shat = ar1_functions.fit(X) cvs = v_test.compute_critical(betahat, alphahat, shat, num_reps=cv_reps, ts_length=ts_length) T = v_test.test_stat(betahat, alphahat, shat, X) outcomes.append(T > cvs) return sum(outcomes) / replications
def compute_critical(self, beta, alpha, s, ep=True, num_reps=500, ts_length=264): """ Compute the critical value of the test stat associated with parameters (beta, alpha, s) and AR1 null by Monte Carlo. The flag ep determines whether or not the test is with estimated parameters. (Note that the test statistic has a different asymptotic distribution when parameters are estimated---see the paper.) """ # Step 1: Compute num_reps observations of the test statistic T = np.empty(num_reps) a1 = ar1_functions.AR1(beta=beta, alpha=alpha, s=s) data = a1.sim_data(num_reps, ts_length) # Simulate under null for k in range(num_reps): if ep: betahat, alphahat, shat = ar1_functions.fit(data[k,:]) T[k] = self.test_stat(betahat, alphahat, shat, data[k,:]) else: T[k] = self.test_stat(beta, alpha, s, data[k,:]) # Step 2: Compute and return (1 - size) quantile of test stat observations T.sort() return T[int((1 - self.test_size) * num_reps)]