예제 #1
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import division, print_function


import numpy as np
import matplotlib.pyplot as pl
from plot_setup import setup, SQUARE_FIGSIZE, COLORS, savefig

setup()  # initialize the plotting styles

np.random.seed(42)


def compute_stats(K, a=0.0, b=1.0):
    a, b = sorted((a, b))
    x = np.random.uniform(a, b, size=K)

    # Analytic results:
    p = 1. / (b - a)
    a_mu = 0.5 * p * (b**2 - a**2)
    a_var = p * ((b - a_mu)**3 - (a - a_mu)**3) / 3.
    a_skew = 0.25 * p * ((b-a_mu)**4-(a-a_mu)**4) / a_var**(3./2)
    a_kurt = p * ((b-a_mu)**5-(a-a_mu)**5) / a_var**2 / 5.0

    # Sample estimates of stats:
    s_mu = np.mean(x)
    s_var = np.var(x)
    s_skew = np.mean((x - s_mu)**3) / s_var**(3./2)
    s_kurt = np.mean((x - s_mu)**4) / s_var**2
예제 #2
0
from __future__ import division, print_function

import emcee3
import fitsio
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import minimize

from plot_setup import setup, get_figsize, COLORS

import celerite
from celerite import terms

np.random.seed(123)
setup()


# Define the custom kernel
class RotationTerm(terms.Term):
    parameter_names = ("log_amp", "log_timescale", "log_period", "log_factor")

    def get_real_coefficients(self):
        f = np.exp(self.log_factor)
        return (
            np.exp(self.log_amp) * (1.0 + f) / (2.0 + f),
            np.exp(-self.log_timescale),
        )

    def get_complex_coefficients(self):
        f = np.exp(self.log_factor)
예제 #3
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import division, print_function

import corner
import numpy as np

from plot_setup import setup, savefig

setup()  # initialize the plotting styles
np.random.seed(42)


def log_p_gauss(x):
    V = np.array([[2.0, 1.2], [1.2, 2.0]])
    alpha = np.linalg.solve(V, x)
    return -0.5 * np.dot(x, alpha)


def run_mcmc(log_p, x, prop_sigma=1.0, nsteps=2e4, thin=1):
    lp = log_p(x)
    chain = np.empty((nsteps // thin, len(x)))
    acc = 0
    for step in range(len(chain)):
        for i in range(thin):
            x_prime = np.array(x)
            x_prime[np.random.randint(
                len(x))] += prop_sigma * np.random.randn()
            lp_prime = log_p(x_prime)
            if np.random.rand() <= np.exp(lp_prime - lp):