def test_TA_odp(self): model = apc.Model() model.data_from_df(apc.loss_TA(), data_format='CL') model.fit('od_poisson_response', 'AC') model.simulate(repetitions=10) model.simulate(repetitions=10, sigma2=10) model.simulate(repetitions=10, od_poisson_dgp='neg_binomial')
def test_Belgian_gauss_rates(self): model = apc.Model() model.data_from_df(**apc.Belgian_lung_cancer()) model.fit('gaussian_rates', 'AC') model.simulate(repetitions=10) model.simulate(repetitions=10, fitted_values=model.fitted_values * 10) model.simulate(repetitions=10, sigma2=10)
def test_VNJ_gln(self): model = apc.Model() model.data_from_df(apc.loss_VNJ(), data_format='CL') model.fit('gen_log_normal_response', 'APC') model.simulate(repetitions=10) model.simulate(repetitions=10, fitted_values=model.fitted_values * 10) model.simulate(repetitions=10, sigma2=10)
def test_BZ(self): model = apc.Model() model.data_from_df(apc.loss_BZ(), data_format='CL') model.fit('od_poisson_response', 'AC') model.forecast(method='n_poisson') fc = model.forecast(attach_to_self=False) self.assertEqual(fc['method'], 't_odp') model.forecast([0.9])
def test_asbestos(self): model = apc.Model() model.data_from_df(apc.asbestos(), data_format='PA') clone = model.clone() for att in ('data_format', 'I', 'J', 'K', 'L', 'n', 'time_adjust'): self.assertEqual(getattr(model, att), getattr(clone, att)) model.K = None self.assertFalse(clone.K == model.K)
def test_Belgian_bin_dose_response(self): data = apc.Belgian_lung_cancer() dose = ((data['response'] / data['rate']) * 10**5).astype(int) model = apc.Model() model.data_from_df(data['response'], dose=dose, data_format='AP') model.fit('binomial_dose_response', 'Ad') model.simulate(repetitions=10) model.simulate(repetitions=10, fitted_values=model.fitted_values * 10)
def test_Belgian(self): model = apc.Model() model.data_from_df(**apc.Belgian_lung_cancer()) model.plot_data_sums() model.plot_data_sums(logy=True) for simplify_ranges in ('start', 'mean', 'end', False): model.plot_data_sums(simplify_ranges) plt.close('all')
def test_TA(self): model = apc.Model() model.data_from_df(apc.loss_TA(), data_format='CL') model.plot_data_sums() model.plot_data_sums(logy=True) for simplify_ranges in ('start', 'mean', 'end', False): model.plot_data_sums(simplify_ranges) plt.close('all')
def test_VNJ(self): model = apc.Model() model.data_from_df(apc.loss_VNJ(), data_format='CL') model.fit('log_normal_response', 'AC') sub_models = [model.sub_model(coh_from_to=(1,5)), model.sub_model(coh_from_to=(6,10))] f = apc.f_test(model, sub_models) self.assertEqual(round(f['F_stat'], 3), 0.242) self.assertEqual(round(f['p_value'], 3), 0.912)
def test_BZ(self): model = apc.Model() model.data_from_df(apc.loss_BZ(), data_format='CL') model.fit('od_poisson_response', 'APC') sub_models = [model.sub_model(per_from_to=(1977,1981)), model.sub_model(per_from_to=(1982,1984)), model.sub_model(per_from_to=(1985,1987))] f = apc.f_test(model, sub_models) self.assertEqual(round(f['F_stat'], 3), 1.855) self.assertEqual(round(f['p_value'], 3), 0.133)
def test_TA_odp(self): model = apc.Model() model.data_from_df(apc.loss_TA(), data_format='CL') model.fit(family='od_poisson_response', predictor='AC') self.assertAlmostEqual(model.deviance, 1903014.004, 3) self.assertTrue( np.allclose( model.parameters.sum().values, np.array([10.99021854, 6.56495772, 0.37934664, 8.3525477]))) self.assertAlmostEqual(model.fitted_values.sum(), 34358090.000, 3)
def test_TA(self): model = apc.Model() model.data_from_df(apc.loss_TA(), data_format='CL') model.fit('od_poisson_response', 'Ad') model.forecast() model.plot_forecast() model.plot_forecast(ic=True) model.plot_forecast('Age', aggregate=True) model.plot_forecast('Cohort') model.plot_forecast(from_to=(8, 13)) plt.close('all')
def test_asbestos(self): model = apc.Model() model.data_from_df(apc.asbestos()) self.assertEqual(model.data_format, 'PA') self.assertEqual(model.I, 70) self.assertEqual(model.J, 41) self.assertEqual(model.K, 110) self.assertEqual(model.L, 69) self.assertEqual(model.n, 2870) self.assertEqual(model.data_vector.sum()['response'], 32164)
def test_BZ_gln(self): model = apc.Model() model.data_from_df(apc.loss_BZ(), data_format='CL') model.fit(family='gen_log_normal_response', predictor='APC') self.assertAlmostEqual(model.deviance, -287.459, 3) self.assertTrue( np.allclose( model.parameters.sum().values, np.array([11.83624119, 1.26201587, 312.66337107, 12.1751463]))) self.assertAlmostEqual(model.fitted_values.sum(), 10214114.721, 3)
def test_TA(self): model = apc.Model() model.data_from_df(apc.loss_TA(), data_format='CL') model.plot_data_heatmaps() for simplify_ranges in ('start', 'mean', 'end', False): model.plot_data_heatmaps(simplify_ranges) plt.close('all') for space in ('AC', 'AP', 'PA', 'PC', 'CA', 'CP'): model.plot_data_heatmaps(space=space) plt.close('all') model.plot_data_heatmaps(vmin=20)
def test_Belgian(self): model = apc.Model() model.data_from_df(**apc.Belgian_lung_cancer()) model.plot_data_heatmaps() for simplify_ranges in ('start', 'mean', 'end', False): model.plot_data_heatmaps(simplify_ranges=simplify_ranges) plt.close('all') for space in ('AC', 'AP', 'PA', 'PC', 'CA', 'CP'): model.plot_data_heatmaps(space=space) plt.close('all') model.plot_data_heatmaps(vmax=50)
def test_Belgian_ln_rates(self): model = apc.Model() model.data_from_df(**apc.Belgian_lung_cancer()) model.fit(family='log_normal_rates', predictor='APC') self.assertAlmostEqual(model.deviance, -44.854, 3) self.assertTrue( np.allclose( model.parameters.sum().values, np.array([1.07781409, 7.31144396, 9.98467135, 15.2844013]))) self.assertAlmostEqual(model.fitted_values.sum(), 552.365, 3)
def test_Belgian_gauss_rates(self): model = apc.Model() model.data_from_df(**apc.Belgian_lung_cancer()) predictors = [ 'APC', 'AP', 'AC', 'PC', 'Ad', 'Pd', 'Cd', 'A', 'P', 'C', 't', 'tA', 'tP', 'tC', '1' ] for p in predictors: model.fit('gaussian_rates', p) model.identify('sum_sum') model.identify('detrend')
def test_asbestos(self): model = apc.Model() model.data_from_df(apc.asbestos(), data_format='PA') model.fit('poisson_response', 'AC') model.simulate(repetitions=10) model.simulate(repetitions=10, poisson_dgp='multinomial') model.simulate(repetitions=10, seed=1) self.assertTrue( np.allclose( model.draws, model.simulate(repetitions=10, seed=1, attach_to_self=False))) model.simulate(repetitions=10, fitted_values=model.fitted_values * 10)
def test_Belgian(self): model = apc.Model() model.data_from_df(**apc.Belgian_lung_cancer()) model.fit('log_normal_rates', 'APC') for sr in ('start', 'mean', 'end', False): model.plot_residuals(simplify_ranges=sr) plt.close('all') for sp in ('AC', 'AP', 'PA', 'PC', 'CA', 'CP'): model.plot_data_heatmaps(space=sp) plt.close('all') for r in ('anscombe', 'deviance', 'pearson', 'response'): model.plot_residuals(kind=r)
def test_Belgian_poisson_dose_response(self): model = apc.Model() model.data_from_df(**apc.Belgian_lung_cancer()) model.fit_table('poisson_dose_response', 'APC') self.assertTrue( np.allclose( model.deviance_table.astype(float).sum().values, np.array([ 2.33052840e+04, 5.08000000e+02, 2.12588574e+00, 2.30019096e+04, 2.38000000e+02, 2.44351741e+00 ])))
def test_asbestos(self): model = apc.Model() model.data_from_df(apc.asbestos(), data_format='PA') model.fit('poisson_response', 'AC') model.plot_parameters() for style in ('detrend', 'sum_sum'): model.plot_parameters(style) plt.close('all') for sr in ('start', 'mean', 'end', False): model.plot_parameters(simplify_ranges=sr) plt.close('all') model.plot_parameters(around_coef=False)
def test_asbestos(self): model = apc.Model() model.data_from_df(apc.asbestos(), data_format='PA') model.fit('poisson_response', 'AC') model.forecast() model.plot_forecast() model.plot_forecast(ic=True) model.plot_forecast('Age', aggregate=True) model.plot_forecast('Cohort') model.plot_forecast(from_to=(1986, 2032)) model.plot_forecast(figsize=(8, 6)) plt.close('all')
def test_TA(self): model = apc.Model() model.data_from_df(apc.loss_TA(), data_format='CL') model.fit('od_poisson_response', 'AC') for sr in ('start', 'mean', 'end', False): model.plot_residuals(simplify_ranges=sr) plt.close('all') for sp in ('AC', 'AP', 'PA', 'PC', 'CA', 'CP'): model.plot_data_heatmaps(space=sp) plt.close('all') for r in ('anscombe', 'deviance', 'pearson', 'response'): model.plot_residuals(kind=r)
def test_TA(self): model = apc.Model() model.data_from_df(apc.loss_TA(), data_format='CL') model.plot_data_within() for simplify_ranges in ('start', 'mean', 'end', False): model.plot_data_within(simplify_ranges=simplify_ranges) plt.close('all') for aggregate in ('mean', 'sum'): model.plot_data_within(aggregate=aggregate) plt.close('all') model.plot_data_within(logy=True) model.plot_data_within(n_groups='all')
def test_TA(self): model = apc.Model() model.data_from_df(apc.loss_TA(), data_format='CL') model.fit('od_poisson_response', 'Ad') model.plot_fit() for style in ('detrend', 'sum_sum'): model.plot_fit(style) plt.close('all') for sr in ('start', 'mean', 'end', False): model.plot_fit(simplify_ranges=sr) plt.close('all') model.plot_fit(around_coef=False)
def test_Belgian(self): model = apc.Model() model.data_from_df(**apc.Belgian_lung_cancer()) model.plot_data_within() for simplify_ranges in ('start', 'mean', 'end', False): model.plot_data_within(simplify_ranges=simplify_ranges) plt.close('all') for aggregate in ('mean', 'sum'): model.plot_data_within(aggregate=aggregate) plt.close('all') model.plot_data_within(logy=True) model.plot_data_within(n_groups='all')
def test_Belgian(self): model = apc.Model() model.data_from_df(**apc.Belgian_lung_cancer()) model.fit('log_normal_rates', 'APC') model.plot_fit() for style in ('detrend', 'sum_sum'): model.plot_fit(style) plt.close('all') for sr in ('start', 'mean', 'end', False): model.plot_fit(simplify_ranges=sr) plt.close('all') model.plot_fit(around_coef=False)
def test_TA(self): model = apc.Model() model.data_from_df(apc.loss_TA()) self.assertEqual(model.data_format, 'CA') self.assertEqual(model.I, 10) self.assertEqual(model.J, 10) self.assertEqual(model.K, 10) self.assertEqual(model.L, 0) self.assertEqual(model.n, 55) self.assertEqual(model.time_adjust, 1) self.assertAlmostEqual(model.data_vector.sum()['response'], 34358090.0, 3)
def test_BZ(self): model = apc.Model() model.data_from_df(apc.loss_BZ(), data_format='CL') self.assertEqual(model.data_format, 'CL') self.assertEqual(model.I, 11) self.assertEqual(model.J, 11) self.assertEqual(model.K, 11) self.assertEqual(model.L, 0) self.assertEqual(model.n, 66) self.assertEqual(model.time_adjust, 0) self.assertAlmostEqual(model.data_vector.sum()['response'], 10221194.0, 3)