Ejemplo n.º 1
0
    def set_dist(self, dist='uniform', kwds=None, dim=None):
        r"""
        TODO: Add this.
        """
        distribution = dist
        if (kwds is not None) and (dim is not None):
            self.dist.set_dist(distribution, kwds, dim)
        elif (kwds is None) and (dim is not None):
            self.dist.set_dist(dist=distribution, dim=dim)

        # Here we override the default errors printed by scipy.stats with our own.
        elif (kwds is None) and (
                distributions.supported_distributions(distribution) is 'chi2'):
            raise (AttributeError(
                "If you are using a chi2 distribution, please pass `df` as a key-value pair in a dictionary. Ex: {'df': 20}."
            ))
        elif (kwds is None) and (
                distributions.supported_distributions(distribution) is 'beta'):
            raise (AttributeError(
                "If you are using a Beta dist, please pass `a` and `b` as key-value pairs in a dictionary. Ex: {'a': 1, 'b': 1}"
            ))
        # the following allows for manual overrides not using the parametric object.
        # e.g. kwds = {'loc': [1,2,3]}
        elif dim is None:
            # print("INPUT: No dimension specified. You will be using `scipy.stats` for your distributions instead of the parametric object. Be warned that functions like `.pdf` may not work as expected.")
            if kwds is not None:
                self.dist = distributions.assign_dist(distribution, **kwds)
            else:
                self.dist = distributions.assign_dist(distribution)
        pass
Ejemplo n.º 2
0
 def test_supp_dist_norm(self):
     print('========== testing distributions.supported_distributions for Normal ==========\n')
     assert_equals(type(distributions.supported_distributions('Normal')), _continuous_distns.norm_gen)
     assert_equals(type(distributions.supported_distributions('Gauss')), _continuous_distns.norm_gen)
     assert_equals(type(distributions.supported_distributions('Gaussian')), _continuous_distns.norm_gen)
     assert_equals(type(distributions.supported_distributions('Norm')), _continuous_distns.norm_gen)
     assert_equals(type(distributions.supported_distributions('N')), _continuous_distns.norm_gen)
Ejemplo n.º 3
0
 def test_supp_dist_uni(self):
     print('========== testing distributions.supported_distributions for Uniform ==========\n')
     assert_equals(type(distributions.supported_distributions('Uniform')), _continuous_distns.uniform_gen)
     assert_equals(type(distributions.supported_distributions('U')), _continuous_distns.uniform_gen)
     assert_equals(type(distributions.supported_distributions('Uni')), _continuous_distns.uniform_gen)
 
     
Ejemplo n.º 4
0
    def set_observed_dist(self, distribution=None, kwds=None, dim=None):
        r"""
        TODO: Add this.
        """
        # If `distribution = None`, we query the pushforward density for the top 5% to get a MAP estimate
        # TODO print warning about the aforementioned.
        # TODO check sizes, ensure dimension agreement

        if distribution is not None:
            if (kwds is not None) and (dim is not None):
                self.observed_dist.set_dist(dim, distribution, kwds)
            elif (kwds is None) and (dim is not None):
                self.observed_dist.set_dist(dim, distribution)

            # Here we override the default errors printed by scipy.stats with our own.
            elif (kwds is None) and (
                    distributions.supported_distributions(distribution) is
                    'chi2'):
                raise (AttributeError(
                    "If you are using a chi2 distribution, please pass `df` as a key-value pair in a dictionary. Ex: {'df': 20}."
                ))
            elif (kwds is None) and (
                    distributions.supported_distributions(distribution) is
                    'beta'):
                raise (AttributeError(
                    "If you are using a Beta dist, please pass `a` and `b` as key-value pairs in a dictionary. Ex: {'a': 1, 'b': 1}"
                ))
            # the following allows for manual overrides not using the parametric object.
            # e.g. kwds = {'loc': [1,2,3]}
            elif dim is None:
                logging.warn(
                    "OBS: No dimension specified. You will be using `scipy.stats` for your distributions instead of the parametric object. Be warned that functions like `.pdf` may not work as expected."
                )
                if kwds is not None:
                    self.observed_dist = distributions.assign_dist(
                        distribution, **kwds)
                else:
                    self.observed_dist = distributions.assign_dist(
                        distribution)

        else:
            logging.warn("""No distribution specified. 
            Defaulting to normal around data_means with 0.5*data_standard_deviation"""
                         )
            loc = np.mean(self.output.samples, axis=0)
            scale = 0.5 * np.std(self.output.samples, axis=0)
            self.observed_dist = distributions.assign_dist(
                'normal', **{
                    'loc': loc,
                    'scale': scale
                })
        pass
Ejemplo n.º 5
0
 def test_supp_dist_uni(self):
     print(
         "========== testing distributions.supported_distributions for Uniform ==========\n"
     )
     assert_equals(
         type(distributions.supported_distributions("Uniform")),
         _continuous_distns.uniform_gen,
     )
     assert_equals(
         type(distributions.supported_distributions("U")),
         _continuous_distns.uniform_gen,
     )
     assert_equals(
         type(distributions.supported_distributions("Uni")),
         _continuous_distns.uniform_gen,
     )
Ejemplo n.º 6
0
 def test_supp_dist_norm(self):
     print(
         "========== testing distributions.supported_distributions for Normal ==========\n"
     )
     assert_equals(
         type(distributions.supported_distributions("Normal")),
         _continuous_distns.norm_gen,
     )
     assert_equals(
         type(distributions.supported_distributions("Gauss")),
         _continuous_distns.norm_gen,
     )
     assert_equals(
         type(distributions.supported_distributions("Gaussian")),
         _continuous_distns.norm_gen,
     )
     assert_equals(
         type(distributions.supported_distributions("Norm")),
         _continuous_distns.norm_gen,
     )
     assert_equals(
         type(distributions.supported_distributions("N")),
         _continuous_distns.norm_gen,
     )
 def test_generate_samples(self):
     print(
         '\n========== testing `sample.sample_set.generate_samples` ==========\n'
     )
     D = distributions.supported_distributions(
     )  # dictionary of distributions
     for n in [100, 200, 500]:  # num_samples
         for d in [1, 2, 3]:  # dim
             self.S.set_num_samples(n)
             self.S.set_dim(d)
             for dist_key in D.keys():
                 if dist_key not in ['chi2', 'beta', 'gamma']:
                     self.S.set_dist(dist_key)
                     self.S.generate_samples(self.S.num_samples)
                     # ensure the samples were generated with the correct shape
                     assert self.S.samples.shape == (n, d)
Ejemplo n.º 8
0
    def test_set_dist(
            self):  # this essentially functions as a test of assign_dist
        print('\n========== testing sample.sample_set.set_dist` ==========\n')
        self.S.setup()
        D = distributions.supported_distributions(
        )  # dictionary of distributions
        # test `self.S.set_dist()` with no arguments.
        # print(type(self.S.dist))
        assert type(self.S.dist) is _distn_infrastructure.rv_frozen
        for dist_key in D.keys(
        ):  # the dist_keys are strings representing the distributions supported.
            # self.S.set_dist(dist_key)
            # assert type(self.S.dist) is type(D[dist_key]()) # check that it got instantiated to the expected type.
            # now check that argument parameters are passed, stored, and recovered correctly
            sz = (4, 5)  # test argument passing
            if dist_key in ['normal', 'uniform']:
                for (l, s) in zip(range(0, 5), range(1, 6)):
                    self.S.set_dist(dist_key, {'loc': l, 'scale': s})
                    kwd_dict = self.S.dist.kwds
                    loc = kwd_dict['loc']
                    scale = kwd_dict['scale']
                    assert (loc == l and scale == s)
                    # assert self.S.dist.args == sz
            if dist_key in ['chi2']:
                for df in range(1):
                    self.S.set_dist(dist_key, {'df': df})
                    kwd_dict = self.S.dist.kwds
                    deg_freedom = kwd_dict['df']
                    assert df == deg_freedom
            if dist_key in ['gamma']:
                for a in range(1):
                    self.S.set_dist(dist_key, {'a': a})
                    kwd_dict = self.S.dist.kwds
                    shape = kwd_dict['a']
                    assert a == shape

            if dist_key in ['beta']:
                for (a, b) in zip(range(0, 5), range(1, 6)):
                    self.S.set_dist(dist_key, {'a': a, 'b': b})
                    kwd_dict = self.S.dist.kwds
                    aa = kwd_dict['a']
                    bb = kwd_dict['b']
                    assert (a == aa and b == bb)