コード例 #1
0
def pCN(iterations,
        propose,
        phi,
        kappa_0,
        adapt_frequency=None,
        adapt_function=None,
        progress_object=None,
        storage=None):
    progress_object = progress.factory(progress_object)

    if adapt_frequency is not None and adapt_function is None:
        raise Exception(
            'Adapt frequency supplied but no adapt function specified.')

    # create an empty numpy if the array is not supplied
    return_array = storage is None
    if storage is None:
        storage = st.ArrayStorage(iterations, kappa_0.shape[0])

    acceptances = np.empty(iterations, dtype=np.bool)

    cur_kappa = kappa_0
    cur_phi = as_single_number(phi(cur_kappa))

    progress_object.initialise(iterations)

    for i in xrange(iterations):
        if adapt_frequency is not None and i > 0 and i % adapt_frequency == 0:
            propose = adapt_function(propose, storage, acceptances)
        try:
            new_kappa = propose(cur_kappa)
            new_phi = as_single_number(phi(new_kappa))

            if np.isnan(new_phi):
                progress_object.report_error(
                    i,
                    'About to reject proposal because new value of phi is NaN.'
                    .format(i))
                accept = False
            else:
                alpha = min(1, np.exp(cur_phi - new_phi))
                accept = alpha > np.random.uniform()
        except Exception as ex:
            progress_object.report_error(i, ex)
            accept = False

        if accept:
            cur_kappa = new_kappa
            cur_phi = new_phi

        storage.add_sample(cur_kappa.ravel())
        acceptances[i] = accept

        progress_object.update(i, acceptances[:i])
    progress_object.update(iterations, acceptances)
    if return_array:
        return storage.array, acceptances
    return storage, acceptances
コード例 #2
0
ファイル: rwm.py プロジェクト: jcockayne/mcmc
def rwm(iterations, propose, log_likelihood, log_prior, init_theta, progress_object=None):
    progress_object = progress.factory(progress_object)

    adapt = hasattr(propose, 'adapt')

    if type(init_theta) is np.ndarray:
        theta_shape = init_theta.shape[0]
    else:
        theta_shape = 1
    samples = np.empty((iterations, theta_shape))
    acceptances = np.empty(iterations, dtype=np.bool)

    cur_theta = init_theta
    cur_log_likelihood = as_single_number(log_likelihood(cur_theta))
    cur_log_prior = as_single_number(log_prior(cur_theta))

    progress_object.initialise(iterations)

    for i in xrange(iterations):
        new_theta = propose(cur_theta)
        new_log_prior = as_single_number(log_prior(new_theta))

        if np.isinf(new_log_prior) and new_log_prior < 0:
            accept = False
        else:
            try:
                new_log_likelihood = log_likelihood(new_theta)
                new_log_likelihood = as_single_number(new_log_likelihood)

                if np.isnan(new_log_likelihood):
                    progress_object.report_error(i, 'Proposal is about to be rejected because likelihood is NaN')
                    accept = False
                elif np.isnan(new_log_prior):
                    progress_object.report_error(i, 'Proposal is about to be rejected because prior is NaN')
                    accept = False
                else:
                    alpha = min(1, np.exp(new_log_likelihood + new_log_prior - cur_log_likelihood - cur_log_prior))
                    accept = np.random.uniform() < alpha
            except Exception as ex:
                accept = False
                progress_object.report_error(i, ex)

        if accept:
            cur_theta = new_theta
            cur_log_likelihood = new_log_likelihood
            cur_log_prior = new_log_prior
        samples[i, :] = cur_theta
        acceptances[i] = accept

        if adapt:
            propose.adapt(i, accept)

        progress_object.update(i, acceptances[:(i+1)])
    progress_object.update(iterations, acceptances)
    return samples, acceptances
コード例 #3
0
ファイル: pcn.py プロジェクト: jcockayne/mcmc
def pCN(
    iterations, propose, phi, kappa_0, adapt_frequency=None, adapt_function=None, progress_object=None, storage=None
):
    progress_object = progress.factory(progress_object)

    if adapt_frequency is not None and adapt_function is None:
        raise Exception("Adapt frequency supplied but no adapt function specified.")

    # create an empty numpy if the array is not supplied
    return_array = storage is None
    if storage is None:
        storage = st.ArrayStorage(iterations, kappa_0.shape[0])

    acceptances = np.empty(iterations, dtype=np.bool)

    cur_kappa = kappa_0
    cur_phi = as_single_number(phi(cur_kappa))

    progress_object.initialise(iterations)

    for i in xrange(iterations):
        if adapt_frequency is not None and i > 0 and i % adapt_frequency == 0:
            propose = adapt_function(propose, storage, acceptances)
        try:
            new_kappa = propose(cur_kappa)
            new_phi = as_single_number(phi(new_kappa))

            if np.isnan(new_phi):
                progress_object.report_error(i, "About to reject proposal because new value of phi is NaN.".format(i))
                accept = False
            else:
                alpha = min(1, np.exp(cur_phi - new_phi))
                accept = alpha > np.random.uniform()
        except Exception as ex:
            progress_object.report_error(i, ex)
            accept = False

        if accept:
            cur_kappa = new_kappa
            cur_phi = new_phi

        storage.add_sample(cur_kappa.ravel())
        acceptances[i] = accept

        progress_object.update(i, acceptances[:i])
    progress_object.update(iterations, acceptances)
    if return_array:
        return storage.array, acceptances
    return storage, acceptances
def hmc(iterations,
        log_pi,
        epsilon,
        L,
        q_0,
        grad_log_pi=None,
        progress_object=None):
    progress_object = progress.factory(progress_object)
    if grad_log_pi is None:
        try:
            import autograd
            grad_log_pi = autograd.grad(log_pi)
        except Exception as ex:
            raise Exception(
                'Gradient of log target not passed and unable to use autograd. {}'
                .format(ex))

    res = np.empty((iterations, q_0.shape[0]))
    acceptances = np.empty(iterations, dtype=np.bool)

    U = lambda x: -log_pi(x)
    grad_U = lambda x: -grad_log_pi(x)

    cur_q = q = q_0

    progress_object.initialise(iterations)

    for i in xrange(iterations):
        p = np.random.normal(size=q.shape[0])
        cur_p = p
        # half step
        try:
            p -= epsilon * grad_U(q) / 2.

            # alternate full steps for position and momentum
            for j in xrange(L):
                q += epsilon * p
                if i != L:
                    p -= epsilon * grad_U(q)

            # now half-step
            p -= epsilon * grad_U(q) / 2.

            # negate momentum to make the proposal symmetric
            p = -p

            # evaluate potential and kinetic energies at start and end of trajectory
            cur_U = U(cur_q)
            cur_K = np.sum(cur_p**2) / 2.
            new_U = U(q)
            new_K = np.sum(p**2) / 2.
            alpha = min(1, np.exp(-new_U + cur_U - new_K + cur_K))

            accept = np.random.uniform() < alpha
        except Exception as ex:
            progress_object.report_error(i, ex)
            accept = False

        # accept / reject step
        if accept:
            res[i, :] = q
            cur_q = q
        else:
            res[i, :] = cur_q
        acceptances[i] = accept

        progress_object.update(i, acceptances[:(i + 1)])

    progress_object.update(iterations, acceptances)
    return res
コード例 #5
0
def rwm(iterations,
        propose,
        log_likelihood,
        log_prior,
        init_theta,
        progress_object=None):
    progress_object = progress.factory(progress_object)

    adapt = hasattr(propose, 'adapt')

    if type(init_theta) is np.ndarray:
        theta_shape = init_theta.shape[0]
    else:
        theta_shape = 1
    samples = np.empty((iterations, theta_shape))
    acceptances = np.empty(iterations, dtype=np.bool)

    cur_theta = init_theta
    cur_log_likelihood = as_single_number(log_likelihood(cur_theta))
    cur_log_prior = as_single_number(log_prior(cur_theta))

    progress_object.initialise(iterations)

    for i in xrange(iterations):
        new_theta = propose(cur_theta)
        new_log_prior = as_single_number(log_prior(new_theta))

        if np.isinf(new_log_prior) and new_log_prior < 0:
            accept = False
        else:
            try:
                new_log_likelihood = log_likelihood(new_theta)
                new_log_likelihood = as_single_number(new_log_likelihood)

                if np.isnan(new_log_likelihood):
                    progress_object.report_error(
                        i,
                        'Proposal is about to be rejected because likelihood is NaN'
                    )
                    accept = False
                elif np.isnan(new_log_prior):
                    progress_object.report_error(
                        i,
                        'Proposal is about to be rejected because prior is NaN'
                    )
                    accept = False
                else:
                    alpha = min(
                        1,
                        np.exp(new_log_likelihood + new_log_prior -
                               cur_log_likelihood - cur_log_prior))
                    accept = np.random.uniform() < alpha
            except Exception as ex:
                accept = False
                progress_object.report_error(i, ex)

        if accept:
            cur_theta = new_theta
            cur_log_likelihood = new_log_likelihood
            cur_log_prior = new_log_prior
        samples[i, :] = cur_theta
        acceptances[i] = accept

        if adapt:
            propose.adapt(i, accept)

        progress_object.update(i, acceptances[:(i + 1)])
    progress_object.update(iterations, acceptances)
    return samples, acceptances