Пример #1
0
    def __init__(self,
                 stochastic,
                 cov=None,
                 delay=1000,
                 scales=None,
                 interval=100,
                 greedy=True,
                 verbose=None):

        self.verbose = verbose

        if getattr(stochastic, '__class__') is pymc.PyMCObjects.Stochastic:
            stochastic = [stochastic]
        StepMethod.__init__(self, stochastic, verbose)

        self._id = 'AdaptiveMetropolis_' + '_'.join(
            [p.__name__ for p in self.stochastics])
        # State variables used to restore the state in a latter session.
        self._state += [
            '_trace_count', '_current_iter', 'C', '_sig', '_proposal_deviate',
            '_trace'
        ]

        self.delay = delay
        self.isdiscrete = {}
        self.interval = interval
        self.greedy = greedy

        self.check_type()
        self.dimension()
        self.C_0 = self.initialize_cov(cov, scales)

        #self._sig = msqrt(self.C_0)
        self._sig = np.linalg.cholesky(self.C_0)

        # Keep track of the internal trace length
        # It may be different from the iteration count since greedy
        # sampling can be done during warm-up period.
        self._trace_count = 0
        self._current_iter = 0

        self._proposal_deviate = np.zeros(self.dim)
        self.C = self.C_0.copy()
        self.chain_mean = np.asmatrix(np.zeros(self.dim))
        self._trace = []

        if self.verbose >= 1:
            print "Initialization..."
            print 'Dimension: ', self.dim
            print "C_0: ", self.C_0
            print "Sigma: ", self._sig
Пример #2
0
    def __init__(self, stochastic, cov, phi, verbose=-1, tally=False):
        # assign cooling step
        self.phi = phi

        # Verbosity flag
        self.verbose = verbose

        self.accepted = 0
        self.rejected = 0

        if not np.iterable(stochastic) or isinstance(stochastic, Variable):
            stochastic = [stochastic]

        # Initialize superclass
        StepMethod.__init__(self, stochastic, verbose, tally)

        self._id = 'SMC_Metropolis_' + '_'.join(
            [p.__name__ for p in self.stochastics])
        # State variables used to restore the state in a latter session.
        self._state += [
            'accepted', 'rejected', '_trace_count', '_current_iter', 'C',
            'proposal_sd', '_proposal_deviate', '_trace', 'shrink_if_necessary'
        ]
        self._tuning_info = ['C']

        self.proposal_sd = None
        self.shrink_if_necessary = False

        # Initialization methods
        self.check_type()
        self.dimension()

        # Set the initial covariance using cov
        self.C = cov
        self.updateproposal_sd()

        # Keep track of the internal trace length
        self._trace_count = 0
        self._current_iter = 0

        self._proposal_deviate = np.zeros(self.dim)
        self.chain_mean = np.asmatrix(np.zeros(self.dim))
        self._trace = []

        if self.verbose >= 2:
            print("Initialization...")
            print('Dimension: ', self.dim)
            print("C_0: ", self.C)
            print("Sigma: ", self.proposal_sd)
Пример #3
0
    def __init__(self, stochs, indicator, p, rp, g, q, rq, inv_q, Jacobian):

        StepMethod.__init__(self, nodes = stochs)

        self.g = g
        self.q = q
        self.rq = rq
        self.p = p
        self.rp = rp
        self.inv_q = inv_q
        self.Jacobian = Jacobian

        self.stoch_dict = {}
        for stoch in stochs:
            self.stoch_dict[stoch.__name__] = stoch

        self.indicator = indicator
Пример #4
0
    def __init__(self, stochs, indicator, p, rp, g, q, rq, inv_q, Jacobian):

        StepMethod.__init__(self, nodes=stochs)

        self.g = g
        self.q = q
        self.rq = rq
        self.p = p
        self.rp = rp
        self.inv_q = inv_q
        self.Jacobian = Jacobian

        self.stoch_dict = {}
        for stoch in stochs:
            self.stoch_dict[stoch.__name__] = stoch

        self.indicator = indicator
Пример #5
0
    def __init__(self, stochastic, cov=None, delay=1000, scales=None, interval=100, greedy=True, verbose=None):

        self.verbose = verbose

        if getattr(stochastic, "__class__") is pymc.PyMCObjects.Stochastic:
            stochastic = [stochastic]
        StepMethod.__init__(self, stochastic, verbose)

        self._id = "AdaptiveMetropolis_" + "_".join([p.__name__ for p in self.stochastics])
        # State variables used to restore the state in a latter session.
        self._state += ["_trace_count", "_current_iter", "C", "_sig", "_proposal_deviate", "_trace"]

        self.delay = delay
        self.isdiscrete = {}
        self.interval = interval
        self.greedy = greedy

        self.check_type()
        self.dimension()
        self.C_0 = self.initialize_cov(cov, scales)

        # self._sig = msqrt(self.C_0)
        self._sig = np.linalg.cholesky(self.C_0)

        # Keep track of the internal trace length
        # It may be different from the iteration count since greedy
        # sampling can be done during warm-up period.
        self._trace_count = 0
        self._current_iter = 0

        self._proposal_deviate = np.zeros(self.dim)
        self.C = self.C_0.copy()
        self.chain_mean = np.asmatrix(np.zeros(self.dim))
        self._trace = []

        if self.verbose >= 1:
            print "Initialization..."
            print "Dimension: ", self.dim
            print "C_0: ", self.C_0
            print "Sigma: ", self._sig
Пример #6
0
    def __init__(self,
                 stochastic,
                 cov=None,
                 delay=1000,
                 interval=200,
                 greedy=True,
                 drscale=0.1,
                 shrink_if_necessary=False,
                 scales=None,
                 verbose=-1,
                 tally=False):

        # Verbosity flag
        self.verbose = verbose

        self.accepted = 0
        self.rejected_then_accepted = 0
        self.rejected_twice = 0

        if not np.iterable(stochastic) or isinstance(stochastic, Variable):
            stochastic = [stochastic]

        # Initialize superclass
        StepMethod.__init__(self, stochastic, verbose, tally)

        self._id = 'DelayedRejectionAdaptiveMetropolis_' + '_'.join(
            [p.__name__ for p in self.stochastics])
        # State variables used to restore the state in a latter session.
        self._state += [
            'accepted', 'rejected_then_accepted', 'rejected_twice',
            '_trace_count', '_current_iter', 'C', 'proposal_sd',
            '_proposal_deviate', '_trace', 'shrink_if_necessary'
        ]
        self._tuning_info = ['C']

        self.proposal_sd = None
        self.shrink_if_necessary = shrink_if_necessary

        # Number of successful steps before the empirical covariance is
        # computed
        self.delay = delay
        # Interval between covariance updates
        self.interval = interval
        # Flag for tallying only accepted jumps until delay reached
        self.greedy = greedy
        # Scale for second attempt
        self.drscale = drscale

        # Initialization methods
        self.check_type()
        self.dimension()

        # Set the initial covariance using cov, or the following fallback
        # mechanisms:
        # 1. If scales is provided, use it.
        # 2. If a trace is present, compute the covariance matrix empirically
        #    from it.
        # 3. Use the stochastics value as a guess of the variance.
        if cov is not None:
            self.C = cov
        elif scales:
            self.C = self.cov_from_scales(scales)
        else:
            try:
                self.C = self.cov_from_trace()
            except AttributeError:
                self.C = self.cov_from_value(100.)

        self.updateproposal_sd()

        # Keep track of the internal trace length
        # It may be different from the iteration count since greedy
        # sampling can be done during warm-up period.
        self._trace_count = 0
        self._current_iter = 0

        self._proposal_deviate = np.zeros(self.dim)
        self.chain_mean = np.asmatrix(np.zeros(self.dim))
        self._trace = []

        if self.verbose >= 2:
            print("Initialization...")
            print('Dimension: ', self.dim)
            print("C_0: ", self.C)
            print("Sigma: ", self.proposal_sd)