Example #1
0
    def test_constraints_greater_than_works(self):
        """
        Ensures that setting a constraint actually limits the trace
        """

        # Database size
        N = 10
        # Specify distributions
        age = pv.Normal("age", mu=55.2, std=3.5, num_elements=N)

        # Create dataset. Refer to "age_alice" as "age1" in the trace and "age" as "age2" in the trace. This is the general naming convention.
        ds = pv.Dataset(input_specs=[age])

        # For now output type can be: Int, Float, List(Float), List(Int)
        program = pv.Program("output",
                             dataset=ds,
                             output_type=pv.Float,
                             function=program_alpha)

        # Add observations
        program.add_observation("57>output>56")

        # Call infer and specify program output
        trace = pv.infer(program, cores=2, draws=1000)
        os.remove("typed.py")

        self.assertTrue(
            all(57 > (np.array(trace.posterior["output"] > 56).flatten())))
Example #2
0
    def test_sampling_samples_correct_order(self):
        """
        Ensures that when sampling for distribution A and B then output is made of [(a_0,b_0), ..., (a_i, b_i)]
        """
        a = pv.Normal("age", mu=10, std=3.5)
        b = pv.Normal("height", mu=40, std=3.5)

        # Create dataset and specify program output
        ds = pv.Dataset(input_specs=[a, b])

        prog = pv.Program("output",
                          dataset=ds,
                          output_type=pv.Float,
                          function=program_addition)
        #Program
        # self.create_file(lambda a,b: a+b)

        # Call infer
        trace = pv.infer(prog, draws=1000, cores=1)
        os.remove("typed.py")
        for a, b, o in zip(trace.posterior["age"].values,
                           trace.posterior["height"].values,
                           trace.posterior["output"].values):
            for i in range(len(a)):
                self.assertEqual(a[i] + b[i], o[i])
Example #3
0
    def test_uniform_cutoff(self):
        """
        Ensures that when sampling for continuous uniform, no value exceeds domain
        """
        a = pv.Uniform("age", 10, 50)

        # Create dataset and specify program output
        ds = pv.Dataset(input_specs=[a])

        prog = pv.Program("output",
                          dataset=ds,
                          output_type=pv.Float,
                          function=program_identity)
        #Program
        # self.create_file(lambda a: a)

        # Call infer
        trace = pv.infer(prog, draws=1000, cores=1)
        os.remove("typed.py")
        for ai, oi in zip(trace.posterior["age"], trace.posterior["output"]):
            for i in range(len(ai)):
                self.assertTrue(50 >= ai[i] >= 10)
                self.assertTrue(50 >= oi[i] >= 10)
Example #4
0
    def test_k_samples_gives_k_samples(self):
        """
        Ensures that the number of samples given also corresponds to the number of samples returned
        """
        sample_size = 1000

        # Specify distributions
        age = pv.Normal("age", mu=55.2, std=3.5)

        # Create dataset and specify program output
        ds = pv.Dataset(input_specs=[age])

        #Program
        prog = pv.Program("output",
                          dataset=ds,
                          output_type=pv.Float,
                          function=program_identity)

        # Call infer
        trace = pv.infer(prog, draws=sample_size, cores=1, chains=1)
        os.remove("typed.py")
        self.assertEqual(len(trace.posterior["age"][0]), sample_size)
        self.assertEqual(len(trace.posterior["output"][0]), sample_size)