def test_get_posterior_summary_variational(self): FILENAME = 'tests/test_output_variational.csv' assert os.path.isfile(FILENAME) result = get_posterior_summary(stan_read_csv(FILENAME)) keys = ['mu', 'theta', 'tau'] subkeys = ['mean', 'std', 'p5', 'p25', 'p50', 'p75', 'p95'] self.assertListEqual(keys, list(result.keys())) for key in keys: self.assertListEqual(subkeys, list(result[key].keys())) assert_allclose(result['mu']['mean'], np.array([1.84578])) assert_allclose(result['mu']['std'], np.array([3.574338])) assert_allclose(result['mu']['p5'], np.array([-3.888642])) assert_allclose(result['mu']['p25'], np.array([-0.645503]), rtol=1e-6) assert_allclose(result['mu']['p50'], np.array([1.88189])) assert_allclose(result['mu']['p75'], np.array([4.229085])) assert_allclose(result['mu']['p95'], np.array([7.662146])) assert_allclose(result['theta']['mean'], np.array([4.64194, 2.73732, -0.087907, 2.4676, -0.007914, 1.06119, 5.76268, 1.77541]), rtol=1e-4) assert_allclose(result['tau']['mean'], np.array([9.59118]))
def test_get_posterior_summary_variational(self): FILENAME = 'tests/test_output_variational.csv' assert os.path.isfile(FILENAME) result = get_posterior_summary(stan_read_csv(FILENAME)) keys = ['mu', 'theta', 'tau'] subkeys = ['mean', 'std', 'p5', 'p25', 'p50', 'p75', 'p95'] self.assertListEqual(keys, list(result.keys())) for key in keys: self.assertListEqual(subkeys, list(result[key].keys())) assert_allclose(result['mu']['mean'], np.array([1.84578])) assert_allclose(result['mu']['std'], np.array([3.574338])) assert_allclose(result['mu']['p5'], np.array([-3.888642])) assert_allclose(result['mu']['p25'], np.array([-0.645503]), rtol=1e-6) assert_allclose(result['mu']['p50'], np.array([1.88189])) assert_allclose(result['mu']['p75'], np.array([4.229085])) assert_allclose(result['mu']['p95'], np.array([7.662146])) assert_allclose(result['theta']['mean'], np.array([ 4.64194, 2.73732, -0.087907, 2.4676, -0.007914, 1.06119, 5.76268, 1.77541 ]), rtol=1e-4) assert_allclose(result['tau']['mean'], np.array([9.59118]))
def test_read_csv_optimize(self): FILENAME = 'tests/test_output_optimize.csv' assert os.path.isfile(FILENAME) result = stan_read_csv(FILENAME) keys = ['lp__', 'mu', 'theta', 'tau'] self.assertListEqual(keys, list(result.keys())) self.assertEqual(result['lp__'].shape, (1,)) self.assertEqual(result['mu'].shape, (1,)) self.assertEqual(result['theta'].shape, (8,)) self.assertEqual(result['tau'].shape, (1,))
def test_get_posterior_summary_sample(self): FILENAME = 'tests/test_output_sample.csv' assert os.path.isfile(FILENAME) result = get_posterior_summary(stan_read_csv(FILENAME)) keys = [ 'lp__', 'accept_stat__', 'stepsize__', 'treedepth__', 'n_leapfrog__', 'divergent__', 'energy__', 'mu', 'theta', 'tau' ] subkeys = ['mean', 'std', 'p5', 'p25', 'p50', 'p75', 'p95'] self.assertListEqual(keys, list(result.keys())) for key in keys: self.assertListEqual(subkeys, list(result[key].keys())) assert_allclose(result['mu']['mean'], np.array([8.073827])) assert_allclose(result['mu']['std'], np.array([5.25104899])) assert_allclose(result['mu']['p5'], np.array([-0.4602165])) assert_allclose(result['mu']['p25'], np.array([4.81581])) assert_allclose(result['mu']['p50'], np.array([8.11732])) assert_allclose(result['mu']['p75'], np.array([11.429175])) assert_allclose(result['mu']['p95'], np.array([16.19353])) assert_allclose( result['theta']['mean'], np.array([ 12.419558, 7.816183, 5.874417, 7.385253, 4.876437, 5.872688, 11.357495, 8.674288 ])) assert_allclose( result['theta']['std'], np.array([ 8.61228728, 6.68721892, 7.91427956, 7.39105258, 6.32559623, 7.03889131, 6.97848441, 8.82055328 ])) assert_allclose( result['theta']['p5'], np.array([ 0.1057379, -2.863044, -8.5084845, -5.1880425, -5.63355, -6.229635, 0.9704559, -4.523796 ])) assert_allclose( result['theta']['p50'], np.array([ 11.26905, 7.880545, 6.40003, 7.525235, 5.318285, 6.118715, 10.94805, 8.87839 ])) assert_allclose( result['theta']['p95'], np.array([ 28.27765, 19.00013, 17.481145, 19.8772, 14.34689, 16.32348, 24.068015, 23.40457 ]))
def test_read_csv_optimize(self): FILENAME = 'tests/test_output_optimize.csv' assert os.path.isfile(FILENAME) result = stan_read_csv(FILENAME) keys = ['lp__', 'mu', 'theta', 'tau'] self.assertListEqual(keys, list(result.keys())) self.assertEqual(result['lp__'].shape, (1, )) self.assertEqual(result['mu'].shape, (1, )) self.assertEqual(result['theta'].shape, (8, )) self.assertEqual(result['tau'].shape, (1, ))
def test_get_posterior_estimates_variational(self): FILENAME = 'tests/test_output_variational.csv' assert os.path.isfile(FILENAME) result = get_posterior_estimates(stan_read_csv(FILENAME)) keys = ['mu', 'theta', 'tau'] self.assertListEqual(keys, list(result.keys())) assert_allclose(result['mu'], np.array([1.84578])) assert_allclose(result['theta'], np.array([4.64194, 2.73732, -0.087907, 2.4676, -0.007914, 1.06119, 5.76268, 1.77541]), rtol=1e-4) assert_allclose(result['tau'], np.array([9.59118]))
def test_read_csv_sample(self): FILENAME = 'tests/test_output_sample.csv' assert os.path.isfile(FILENAME) result = stan_read_csv(FILENAME) keys = ['lp__', 'accept_stat__', 'stepsize__', 'treedepth__', 'n_leapfrog__', 'divergent__', 'energy__', 'mu', 'theta', 'tau'] self.assertListEqual(keys, list(result.keys())) self.assertEqual(result['lp__'].shape, (1000, 1)) self.assertEqual(result['mu'].shape, (1000, 1)) self.assertEqual(result['theta'].shape, (1000, 8)) self.assertEqual(result['tau'].shape, (1000, 1))
def test_read_csv_sample(self): FILENAME = 'tests/test_output_sample.csv' assert os.path.isfile(FILENAME) result = stan_read_csv(FILENAME) keys = [ 'lp__', 'accept_stat__', 'stepsize__', 'treedepth__', 'n_leapfrog__', 'divergent__', 'energy__', 'mu', 'theta', 'tau' ] self.assertListEqual(keys, list(result.keys())) self.assertEqual(result['lp__'].shape, (1000, 1)) self.assertEqual(result['mu'].shape, (1000, 1)) self.assertEqual(result['theta'].shape, (1000, 8)) self.assertEqual(result['tau'].shape, (1000, 1))
def test_get_posterior_estimates_optimize(self): FILENAME = 'tests/test_output_optimize.csv' assert os.path.isfile(FILENAME) result = get_posterior_estimates(stan_read_csv(FILENAME)) keys = ['lp__', 'mu', 'theta', 'tau'] self.assertListEqual(keys, list(result.keys())) assert_allclose(result['lp__'], np.array([276.575])) assert_allclose(result['mu'], np.array([0.765896])) assert_allclose(result['theta'], np.array([0.765896, 0.765896, 0.765896, 0.765896, 0.765896, 0.765896, 0.765896, 0.765896])) assert_allclose(result['tau'], np.array([4.335870e-16]))
def test_get_posterior_estimates_variational(self): FILENAME = 'tests/test_output_variational.csv' assert os.path.isfile(FILENAME) result = get_posterior_estimates(stan_read_csv(FILENAME)) keys = ['mu', 'theta', 'tau'] self.assertListEqual(keys, list(result.keys())) assert_allclose(result['mu'], np.array([1.84578])) assert_allclose(result['theta'], np.array([ 4.64194, 2.73732, -0.087907, 2.4676, -0.007914, 1.06119, 5.76268, 1.77541 ]), rtol=1e-4) assert_allclose(result['tau'], np.array([9.59118]))
def test_get_posterior_estimates_optimize(self): FILENAME = 'tests/test_output_optimize.csv' assert os.path.isfile(FILENAME) result = get_posterior_estimates(stan_read_csv(FILENAME)) keys = ['lp__', 'mu', 'theta', 'tau'] self.assertListEqual(keys, list(result.keys())) assert_allclose(result['lp__'], np.array([276.575])) assert_allclose(result['mu'], np.array([0.765896])) assert_allclose( result['theta'], np.array([ 0.765896, 0.765896, 0.765896, 0.765896, 0.765896, 0.765896, 0.765896, 0.765896 ])) assert_allclose(result['tau'], np.array([4.335870e-16]))
def test_get_posterior_estimates_sample(self): FILENAME = 'tests/test_output_sample.csv' assert os.path.isfile(FILENAME) result = get_posterior_estimates(stan_read_csv(FILENAME)) keys = ['lp__', 'accept_stat__', 'stepsize__', 'treedepth__', 'n_leapfrog__', 'divergent__', 'energy__', 'mu', 'theta', 'tau'] self.assertListEqual(keys, list(result.keys())) assert_allclose(result['lp__'], np.array([-18.94640541])) assert_allclose(result['mu'], np.array([8.073827])) assert_allclose(result['theta'], np.array([12.419558, 7.816183, 5.874417, 7.385253, 4.876437, 5.872688, 11.357495, 8.674288])) assert_allclose(result['tau'], np.array([7.735637]))
def test_read_csv_variational(self): FILENAME = 'tests/test_output_variational.csv' assert os.path.isfile(FILENAME) result = stan_read_csv(FILENAME) keys = ['mean_pars', 'sampled_pars'] self.assertListEqual(keys, list(result.keys())) keys_param = ['mu', 'theta', 'tau'] self.assertListEqual(keys_param, list(result['mean_pars'].keys())) self.assertListEqual(keys_param, list(result['sampled_pars'].keys())) self.assertEqual(result['mean_pars']['mu'].shape, (1,)) self.assertEqual(result['mean_pars']['theta'].shape, (8,)) self.assertEqual(result['mean_pars']['tau'].shape, (1,)) self.assertEqual(result['sampled_pars']['mu'].shape, (1000, 1)) self.assertEqual(result['sampled_pars']['theta'].shape, (1000, 8)) self.assertEqual(result['sampled_pars']['tau'].shape, (1000, 1))
def test_read_csv_variational(self): FILENAME = 'tests/test_output_variational.csv' assert os.path.isfile(FILENAME) result = stan_read_csv(FILENAME) keys = ['mean_pars', 'sampled_pars'] self.assertListEqual(keys, list(result.keys())) keys_param = ['mu', 'theta', 'tau'] self.assertListEqual(keys_param, list(result['mean_pars'].keys())) self.assertListEqual(keys_param, list(result['sampled_pars'].keys())) self.assertEqual(result['mean_pars']['mu'].shape, (1, )) self.assertEqual(result['mean_pars']['theta'].shape, (8, )) self.assertEqual(result['mean_pars']['tau'].shape, (1, )) self.assertEqual(result['sampled_pars']['mu'].shape, (1000, 1)) self.assertEqual(result['sampled_pars']['theta'].shape, (1000, 8)) self.assertEqual(result['sampled_pars']['tau'].shape, (1000, 1))
def test_get_posterior_summary_sample(self): FILENAME = 'tests/test_output_sample.csv' assert os.path.isfile(FILENAME) result = get_posterior_summary(stan_read_csv(FILENAME)) keys = ['lp__', 'accept_stat__', 'stepsize__', 'treedepth__', 'n_leapfrog__', 'divergent__', 'energy__', 'mu', 'theta', 'tau'] subkeys = ['mean', 'std', 'p5', 'p25', 'p50', 'p75', 'p95'] self.assertListEqual(keys, list(result.keys())) for key in keys: self.assertListEqual(subkeys, list(result[key].keys())) assert_allclose(result['mu']['mean'], np.array([8.073827])) assert_allclose(result['mu']['std'], np.array([5.25104899])) assert_allclose(result['mu']['p5'], np.array([-0.4602165])) assert_allclose(result['mu']['p25'], np.array([4.81581])) assert_allclose(result['mu']['p50'], np.array([8.11732])) assert_allclose(result['mu']['p75'], np.array([11.429175])) assert_allclose(result['mu']['p95'], np.array([16.19353])) assert_allclose(result['theta']['mean'], np.array([12.419558, 7.816183, 5.874417, 7.385253, 4.876437, 5.872688, 11.357495, 8.674288])) assert_allclose(result['theta']['std'], np.array([8.61228728, 6.68721892, 7.91427956, 7.39105258, 6.32559623, 7.03889131, 6.97848441, 8.82055328])) assert_allclose(result['theta']['p5'], np.array([0.1057379, -2.863044, -8.5084845, -5.1880425, -5.63355, -6.229635, 0.9704559, -4.523796])) assert_allclose(result['theta']['p50'], np.array([11.26905, 7.880545, 6.40003, 7.525235, 5.318285, 6.118715, 10.94805, 8.87839])) assert_allclose(result['theta']['p95'], np.array([28.27765, 19.00013, 17.481145, 19.8772, 14.34689, 16.32348, 24.068015, 23.40457]))
def test_get_posterior_estimates_sample(self): FILENAME = 'tests/test_output_sample.csv' assert os.path.isfile(FILENAME) result = get_posterior_estimates(stan_read_csv(FILENAME)) keys = [ 'lp__', 'accept_stat__', 'stepsize__', 'treedepth__', 'n_leapfrog__', 'divergent__', 'energy__', 'mu', 'theta', 'tau' ] self.assertListEqual(keys, list(result.keys())) assert_allclose(result['lp__'], np.array([-18.94640541])) assert_allclose(result['mu'], np.array([8.073827])) assert_allclose( result['theta'], np.array([ 12.419558, 7.816183, 5.874417, 7.385253, 4.876437, 5.872688, 11.357495, 8.674288 ])) assert_allclose(result['tau'], np.array([7.735637]))
def test_variational_invalid(self): FILENAME = 'tests/test_output_variational_invalid.csv' assert os.path.isfile(FILENAME) with self.assertRaises(RuntimeError): stan_read_csv(FILENAME)
def test_no_method(self): FILENAME = 'tests/test_output_no_method.csv' assert os.path.isfile(FILENAME) with self.assertRaises(RuntimeError): stan_read_csv(FILENAME)