def __init__(self): super(self.__class__, self).__init__() #initialize DGP nobs = self.nobs y_true, x, exog = self.y_true, self.x, self.exog np.random.seed(8765993) sigma_noise = 0.1 y = y_true + sigma_noise * np.random.randn(nobs) m = AdditiveModel(x) m.fit(y) res_gam = m.results #TODO: currently attached to class res_ols = OLS(y, exog).fit() #Note: there still are some naming inconsistencies self.res1 = res1 = Dummy() #for gam model #res2 = Dummy() #for benchmark self.res2 = res2 = res_ols #reuse existing ols results, will add additional res1.y_pred = res_gam.predict(x) res2.y_pred = res_ols.model.predict(res_ols.params, exog) res1.y_predshort = res_gam.predict(x[:10]) slopes = [i for ss in m.smoothers for i in ss.params[1:]] const = res_gam.alpha + sum([ss.params[1] for ss in m.smoothers]) #print const, slopes res1.params = np.array([const] + slopes)
x2 = R.standard_normal(500) x2.sort() y = R.standard_normal((500, )) f1 = lambda x1: (x1 + x1**2 - 3 - 1.5 * x1**3 + np.exp(-x1)) f2 = lambda x2: (x2 + x2**2 - np.exp(x2)) z = standardize(f1(x1)) + standardize(f2(x2)) z = standardize(z) * 0.1 y += z d = np.array([x1, x2]).T if example == 1: print "normal" m = AdditiveModel(d) m.fit(y) x = np.linspace(-2, 2, 50) print m import scipy.stats, time if example == 2: print "binomial" f = family.Binomial() b = np.asarray([scipy.stats.bernoulli.rvs(p) for p in f.link.inverse(y)]) b.shape = y.shape m = GAM(b, d, family=f) toc = time.time() m.fit(b) tic = time.time()
x2 = np.sin(2 * x1) x = np.column_stack((x1 / x1.max() * 2, x2)) exog = (x[:, :, None]**np.arange(order + 1)[None, None, :]).reshape(nobs, -1) idx = range((order + 1) * 2) del idx[order + 1] exog_reduced = exog[:, idx] #remove duplicate constant y_true = exog.sum(1) / 2. z = y_true #alias check d = x y = y_true + sigma_noise * np.random.randn(nobs) example = 1 if example == 1: m = AdditiveModel(d) m.fit(y) y_pred = m.results.predict(d) for ss in m.smoothers: print ss.params res_ols = OLS(y, exog_reduced).fit() print res_ols.params #assert_almost_equal(y_pred, res_ols.fittedvalues, 3) if example > 0: import matplotlib.pyplot as plt plt.figure()
x2.sort() y = R.standard_normal((nobs,)) f1 = lambda x1: (x1 + x1**2 - 3 - 1 * x1**3 + 0.1 * np.exp(-x1/4.)) f2 = lambda x2: (x2 + x2**2 - 0.1 * np.exp(x2/4.)) z = standardize(f1(x1)) + standardize(f2(x2)) z = standardize(z) * 2 # 0.1 y += z d = np.array([x1,x2]).T if example == 1: print "normal" m = AdditiveModel(d) m.fit(y) x = np.linspace(-2,2,50) print m y_pred = m.results.predict(d) plt.figure() plt.plot(y, '.') plt.plot(z, 'b-', label='true') plt.plot(y_pred, 'r-', label='AdditiveModel') plt.legend() plt.title('gam.AdditiveModel') import scipy.stats, time if example == 2:
x2 = np.sin(2*x1) x = np.column_stack((x1/x1.max()*2, x2)) exog = (x[:,:,None]**np.arange(order+1)[None, None, :]).reshape(nobs, -1) idx = range((order+1)*2) del idx[order+1] exog_reduced = exog[:,idx] #remove duplicate constant y_true = exog.sum(1) / 2. z = y_true #alias check d = x y = y_true + sigma_noise * np.random.randn(nobs) example = 1 if example == 1: m = AdditiveModel(d) m.fit(y) y_pred = m.results.predict(d) for ss in m.smoothers: print ss.params res_ols = OLS(y, exog_reduced).fit() print res_ols.params #assert_almost_equal(y_pred, res_ols.fittedvalues, 3) if example > 0: import matplotlib.pyplot as plt