Esempio n. 1
0
 def test_bernoulli_data(self):
     data_dict = {'N': 10, 'y': [0, 1, 0, 0, 0, 0, 0, 0, 0, 1]}
     stan = os.path.join(datafiles_path, 'bernoulli.stan')
     output = os.path.join(TMPDIR, 'test3-bernoulli-output')
     model = compile_model(stan)
     post_sample = sample(model, data=data_dict, csv_output_file=output)
     for i in range(post_sample.chains):
         csv_file = post_sample.csv_files[i]
         txt_file = ''.join([os.path.splitext(csv_file)[0], '.txt'])
         self.assertTrue(os.path.exists(csv_file))
         self.assertTrue(os.path.exists(txt_file))
Esempio n. 2
0
 def test_bernoulli(self):
     stan = os.path.join(datafiles_path, 'bernoulli.stan')
     exe = os.path.join(datafiles_path, 'bernoulli')
     if not os.path.exists(exe):
         compile_model(stan)
     model = Model(stan, exe_file=exe)
     jdata = os.path.join(datafiles_path, 'bernoulli.data.json')
     post_sample = sample(model,
                          chains=4,
                          cores=2,
                          seed=12345,
                          sampling_iters=200,
                          data=jdata)
     df = summary(post_sample)
     self.assertTrue(df.shape == (2, 9))
Esempio n. 3
0
 def test_bernoulli(self):
     stan = os.path.join(datafiles_path, 'bernoulli.stan')
     exe = os.path.join(datafiles_path, 'bernoulli')
     if not os.path.exists(exe):
         compile_model(stan)
     model = Model(stan, exe_file=exe)
     jdata = os.path.join(datafiles_path, 'bernoulli.data.json')
     post_sample = sample(model,
                          chains=4,
                          cores=2,
                          seed=12345,
                          sampling_iters=200,
                          data=jdata)
     post_sample.assemble_sample()
     df = get_drawset(post_sample)
     self.assertEqual(df.shape, (post_sample.chains * post_sample.draws,
                                 len(post_sample.column_names)))
Esempio n. 4
0
 def diagnose_no_problems(self):
     stan = os.path.join(datafiles_path, 'bernoulli.stan')
     exe = os.path.join(datafiles_path, 'bernoulli')
     if not os.path.exists(exe):
         compile_model(stan)
     model = Model(stan, exe_file=exe)
     jdata = os.path.join(datafiles_path, 'bernoulli.data.json')
     post_sample = sample(model,
                          chains=4,
                          cores=2,
                          seed=12345,
                          sampling_iters=200,
                          data=jdata)
     capturedOutput = io.StringIO()
     sys.stdout = capturedOutput
     diagnose(post_sample)
     sys.stdout = sys.__stdout__
     self.assertEqual(capturedOutput.getvalue(), 'No problems detected.\n')
Esempio n. 5
0
    def test_bernoulli_1(self):
        stan = os.path.join(datafiles_path, 'bernoulli.stan')
        exe = os.path.join(datafiles_path, 'bernoulli')
        if not os.path.exists(exe):
            compile_model(stan)
        model = Model(stan, exe_file=exe)
        jdata = os.path.join(datafiles_path, 'bernoulli.data.json')
        output = os.path.join(datafiles_path, 'test1-bernoulli-output')
        post_sample = sample(
            model,
            chains=4,
            cores=2,
            seed=12345,
            sampling_iters=100,
            data=jdata,
            csv_output_file=output,
            max_treedepth=11,
            adapt_delta=0.95,
        )
        for i in range(post_sample.chains):
            csv_file = post_sample.csv_files[i]
            txt_file = ''.join([os.path.splitext(csv_file)[0], '.txt'])
            self.assertTrue(os.path.exists(csv_file))
            self.assertTrue(os.path.exists(txt_file))

        self.assertEqual(post_sample.chains, 4)
        self.assertEqual(post_sample.draws, 100)
        column_names = [
            'lp__',
            'accept_stat__',
            'stepsize__',
            'treedepth__',
            'n_leapfrog__',
            'divergent__',
            'energy__',
            'theta',
        ]
        self.assertEqual(post_sample.column_names, tuple(column_names))

        post_sample.assemble_sample()
        self.assertEqual(post_sample.sample.shape, (100, 4, len(column_names)))
        self.assertEqual(post_sample.metric_type, 'diag_e')
        self.assertEqual(post_sample.stepsize.shape, (4, ))
        self.assertEqual(post_sample.metric.shape, (4, 1))
Esempio n. 6
0
 def test_bernoulli_2(self):
     # tempfile for outputs
     stan = os.path.join(datafiles_path, 'bernoulli.stan')
     exe = os.path.join(datafiles_path, 'bernoulli')
     if not os.path.exists(exe):
         compile_model(stan)
     model = Model(stan, exe_file=exe)
     jdata = os.path.join(datafiles_path, 'bernoulli.data.json')
     post_sample = sample(model,
                          chains=4,
                          cores=2,
                          seed=12345,
                          sampling_iters=100,
                          data=jdata,
                          max_treedepth=11,
                          adapt_delta=0.95)
     for i in range(post_sample.chains):
         csv_file = post_sample.csv_files[i]
         txt_file = ''.join([os.path.splitext(csv_file)[0], '.txt'])
         self.assertTrue(os.path.exists(csv_file))
         self.assertTrue(os.path.exists(txt_file))
Esempio n. 7
0
    def test_bernoulli(self):
        stan = os.path.join(datafiles_path, 'bernoulli.stan')
        exe = os.path.join(datafiles_path, 'bernoulli')
        if not os.path.exists(exe):
            compile_model(stan)
        model = Model(stan, exe_file=exe)
        jdata = os.path.join(datafiles_path, 'bernoulli.data.json')
        post_sample = sample(model,
                             chains=4,
                             cores=2,
                             seed=12345,
                             sampling_iters=200,
                             data=jdata)
        for i in range(post_sample.chains):
            csv_file = post_sample.csv_files[i]
            txt_file = ''.join([os.path.splitext(csv_file)[0], '.txt'])
            self.assertTrue(os.path.exists(csv_file))
            self.assertTrue(os.path.exists(txt_file))

        basename = 'bern_save_csvfiles_test'
        save_csvfiles(post_sample, datafiles_path, basename)  # good
        for i in range(post_sample.chains):
            csv_file = post_sample.csv_files[i]
            self.assertTrue(os.path.exists(csv_file))

        with self.assertRaisesRegex(Exception, 'cannot save'):
            save_csvfiles(post_sample, os.path.join('no', 'such', 'dir'),
                          basename)

        with self.assertRaisesRegex(Exception, 'file exists'):
            save_csvfiles(post_sample, datafiles_path, basename)

        save_csvfiles(post_sample, basename=basename)  # default dir
        for i in range(post_sample.chains):
            csv_file = post_sample.csv_files[i]
            self.assertTrue(os.path.exists(csv_file))

        for i in range(post_sample.chains):  # cleanup
            os.remove(post_sample.csv_files[i])
Esempio n. 8
0
 def test_missing_input(self):
     stan = os.path.join(datafiles_path, 'bernoulli.stan')
     output = os.path.join(TMPDIR, 'test4-bernoulli-output')
     model = compile_model(stan)
     with self.assertRaisesRegex(Exception, 'Error during sampling'):
         post_sample = sample(model, csv_output_file=output)