コード例 #1
0
 def set_dim(self, dimension=None):
     r"""
     TODO: Add this.
     """
     if self.dim is None:
         if dimension is not None:
             self.dim = int(abs(dimension))
         else:
             self.dim = 1 # default option is self.dim not yet set.
     elif self.dim is not None:
         if dimension is not None:
             self.dim = int(abs(dimension))
         # otherwise, if nothing specified, leave it alone.
     else:
         assert TypeError("Please specify an integer-valued `dimension` greater than zero.")
     self.dist = distributions.parametric_dist(self.dim)
     pass
コード例 #2
0
    def __init__(self, input_set=None, output_set=None, seed=None):
        self.input = input_set
        self.output = output_set
        self.model = None
        self.prior_dist = self.input.dist
        self.pushforward_dist = self.output.dist # kde object. should have rvs functionality. TODO: double check sizing with test.
        self.posterior_dist = None # this will be the dictionary object which we can use with .rvs(num_samples)
        
        if self.output.dim is not None:
            self.observed_dist = distributions.parametric_dist(self.output.dim)
        else:
            self.observed_dist = None

        self.accept_inds = None # indices into input_sample_set object associated with accepted samples from accept/reject
        self.ratio = None # the ratio is the posterior density evaluated on the `input_set.samples`
        self.pf_pr_eval = None
        if seed is None:
            self.seed = 12112
        else:
            self.seed = seed
コード例 #3
0
def generate_sample_set_from_dict(U, num_samples=1, seed=None):
    r"""
     This module takes a nested dictionary (one inside another) 
     which contains the description of the distribution in that parameter
     and generates the samples described therein and generates a sample set.
     Descriptions of each parameter name are in `sample_set.dist.names`, which
     describes the keys in the outer dictionary, and `sample_set.dist.vars`, 
     which descirbes the inner layers (assumed to match). 
     :returns: samples ordered by columns, `sample_set.dist.params` contains the ordering as strings
     :returns: :class:`~np.array` 
    """
    unit_names = list(U.keys())
    try:
        assert (len(np.unique([len(U[n].keys()) for n in unit_names])) == 1)
    except AssertionError:
        print(
            'Something is amiss in your dictionary. Perhaps extra or missing variables.'
        )
    unit_variables = list(U[unit_names[0]].keys())
    #     print(unit_variables) # the order appears to be preserved.
    dim = len(unit_variables) * len(unit_names)
    P = distributions.parametric_dist(dim)
    # both of these attributes will now belong to the sample set.
    param_names = []
    di = 0
    for n in unit_names:
        for v in unit_variables:
            P.set_dist(dim=di, **U[n][v])
            param_names.append(v + '-' + n)  # FORMATTING FOR NAMES
            di += 1

    P.names = unit_names
    P.vars = unit_variables
    P.params = param_names
    S = sample_set((num_samples, dim))
    S.dist = P
    S.generate_samples(seed=seed)
    return S
コード例 #4
0
    def __init__(self, size=(None, None), seed=121):
        r"""

        Initialization

        :param size: Dimension of the space in which these samples reside.
        :type size: :class:`numpy.ndarray` of sample values of shape (num, dim)

        :param int seed: random number generator seed
        """
        # tuple `size` should be of format (num_samples, dim).
        # Will write these attributes to class `sample_set`
        # If `size` is given as an integer, it is inferred to be dimension.
        if type(size) is tuple:
            self.num_samples = size[0]
            if len(size) == 1:
                self.dim = 1
            else:
                self.dim = size[1]  # dimension
        elif type(size) is int:
            self.dim = size
            self.num_samples = None  # used as a default.
            # will infer/set `num_samples` from call to `generate_samples`
        else:
            logging.warning(
                " Please specify a valid size parameter. Defaulting to None."
            )
            self.dim = None
            self.num_samples = None
        #: dist TODO description
        if self.dim != None:
            self.dist = distributions.parametric_dist(self.dim)
        else:
            self.dist = None
        #: :class:`numpy.ndarray` of samples of shape (num, dim)
        self.samples = None  # this holds the actual samples we generate.
        #: :param int seed: random number generator seed
        self.seed = seed