コード例 #1
0
def test_diag(data):
    x, diag, y, t = data

    term = terms.SHOTerm(S0=1.0, w0=0.5, Q=3.0)
    gp1 = celerite2.GaussianProcess(term, t=x, diag=diag)
    gp2 = celerite2.GaussianProcess(term, t=x, yerr=np.sqrt(diag))
    assert np.allclose(gp1.log_likelihood(y), gp2.log_likelihood(y))

    gp1 = celerite2.GaussianProcess(term, t=x, diag=np.zeros_like(x))
    gp2 = celerite2.GaussianProcess(term, t=x)
    assert np.allclose(gp1.log_likelihood(y), gp2.log_likelihood(y))
コード例 #2
0
def test_mean(data):
    x, diag, y, t = data

    term = terms.SHOTerm(S0=1.0, w0=0.5, Q=3.0)
    gp = celerite2.GaussianProcess(term, t=x, diag=diag, mean=lambda x: 2 * x)

    cond1 = gp.condition(y, include_mean=True)
    cond2 = gp.condition(y, x, include_mean=True)
    assert np.allclose(cond1.mean, cond2.mean)

    cond1 = gp.condition(y, include_mean=False)
    cond2 = gp.condition(y, x, include_mean=False)
    assert np.allclose(cond1.mean, cond2.mean)

    cond_mean = gp.condition(y, include_mean=True)
    cond_no_mean = gp.condition(y, include_mean=False)
    assert np.allclose(cond_mean.mean, cond_no_mean.mean + 2 * x)

    cond_mean = gp.condition(y, t, include_mean=True)
    cond_no_mean = gp.condition(y, t, include_mean=False)
    assert np.allclose(cond_mean.mean, cond_no_mean.mean + 2 * t)

    np.random.seed(42)
    s1 = gp.sample(size=5, include_mean=True)
    np.random.seed(42)
    s2 = gp.sample(size=5, include_mean=False)
    assert np.allclose(s1, s2 + 2 * x)
コード例 #3
0
ファイル: gp.py プロジェクト: dioph/periodicity
    def __init__(self, signal, err, init_period=None, period_ppf=None):
        if not isinstance(signal, TSeries):
            signal = TSeries(data=signal)
        self.signal = signal
        self.err = err
        self.t = self.signal.time
        self.y = self.signal.values
        self.sigma = np.std(self.y)
        self.jitter = np.min(self.err) ** 2
        self.mean = np.mean(self.y)
        if init_period is None:
            init_period = np.sqrt(signal.size) * signal.median_dt
        if period_ppf is None:

            def period_ppf(u):
                sigma_period = 0.5 * np.log(signal.size)
                return np.exp(norm.ppf(u, np.log(init_period), sigma_period))

        self.period_ppf = period_ppf
        init_params = self.prior_transform(np.full(self.ndim, 50.0))
        init_params["period"] = init_period
        mean = init_params.pop("mean")
        jitter = init_params.pop("jitter")
        self.gp = celerite2.GaussianProcess(self.kernel(**init_params), mean=mean)
        self.gp.compute(self.t, diag=self.err ** 2 + jitter)
コード例 #4
0
def test_mean(data):
    x, diag, y, t = data

    term = terms.SHOTerm(S0=1.0, w0=0.5, Q=3.0)
    gp = celerite2.GaussianProcess(term, t=x, diag=diag, mean=lambda x: 2 * x)

    mu1 = gp.predict(y, include_mean=True)
    mu2 = gp.predict(y, x, include_mean=True)
    assert np.allclose(mu1, mu2)

    mu1 = gp.predict(y, include_mean=False)
    mu2 = gp.predict(y, x, include_mean=False)
    assert np.allclose(mu1, mu2)

    mu_mean = gp.predict(y, include_mean=True)
    mu_no_mean = gp.predict(y, include_mean=False)
    assert np.allclose(mu_mean, mu_no_mean + 2 * x)

    mu_mean = gp.predict(y, t, include_mean=True)
    mu_no_mean = gp.predict(y, t, include_mean=False)
    assert np.allclose(mu_mean, mu_no_mean + 2 * t)

    np.random.seed(42)
    s1 = gp.sample(size=5, include_mean=True)
    np.random.seed(42)
    s2 = gp.sample(size=5, include_mean=False)
    assert np.allclose(s1, s2 + 2 * x)
コード例 #5
0
def test_errors():
    # Generate fake data
    np.random.seed(40582)
    x = np.sort(np.random.uniform(0, 10, 50))
    t = np.sort(np.random.uniform(-1, 12, 100))
    diag = np.random.uniform(0.1, 0.3, len(x))
    y = np.sin(x)

    term = terms.SHOTerm(S0=1.0, w0=0.5, Q=3.0)
    gp = celerite2.GaussianProcess(term)

    # Need to call compute first
    with pytest.raises(RuntimeError):
        gp.recompute()

    with pytest.raises(RuntimeError):
        gp.log_likelihood(y)

    with pytest.raises(RuntimeError):
        gp.sample()

    # Sorted
    with pytest.raises(ValueError):
        gp.compute(x[::-1], diag=diag)

    # 1D
    with pytest.raises(ValueError):
        gp.compute(np.tile(x[:, None], (1, 5)), diag=diag)

    # Only one of diag and yerr
    with pytest.raises(ValueError):
        gp.compute(x, diag=diag, yerr=np.sqrt(diag))

    # Not positive definite
    # with pytest.raises(celerite2.driver.LinAlgError):
    with pytest.raises(Exception):
        gp.compute(x, diag=-10 * diag)

    # Not positive definite with `quiet`
    gp.compute(x, diag=-10 * diag, quiet=True)
    assert np.isinf(gp._log_det)
    assert gp._log_det < 0

    # Compute correctly
    gp.compute(x, diag=diag)
    gp.log_likelihood(y)

    # Dimension mismatch
    with pytest.raises(ValueError):
        gp.log_likelihood(y[:-1])

    with pytest.raises(ValueError):
        gp.log_likelihood(np.tile(y[:, None], (1, 5)))

    with pytest.raises(ValueError):
        gp.predict(y, t=np.tile(t[:, None], (1, 5)))
コード例 #6
0
def test_consistency(oterm, mean, data):
    x, diag, y, t = data

    # Setup the original GP
    original_gp = original_celerite.GP(oterm, mean=mean)
    original_gp.compute(x, np.sqrt(diag))

    # Setup the new GP
    term = terms.OriginalCeleriteTerm(oterm)
    gp = celerite2.GaussianProcess(term, mean=mean)
    gp.compute(x, diag=diag)

    # "log_likelihood" method
    assert np.allclose(original_gp.log_likelihood(y), gp.log_likelihood(y))

    # "predict" method
    for args in [
            dict(return_cov=False, return_var=False),
            dict(return_cov=False, return_var=True),
            dict(return_cov=True, return_var=False),
    ]:
        assert all(
            np.allclose(a, b) for a, b in zip(
                original_gp.predict(y, **args),
                gp.predict(y, **args),
            ))
        assert all(
            np.allclose(a, b) for a, b in zip(
                original_gp.predict(y, t=t, **args),
                gp.predict(y, t=t, **args),
            ))

    # "sample" method
    seed = 5938
    np.random.seed(seed)
    a = original_gp.sample()
    np.random.seed(seed)
    b = gp.sample()
    assert np.allclose(a, b)

    np.random.seed(seed)
    a = original_gp.sample(size=10)
    np.random.seed(seed)
    b = gp.sample(size=10)
    assert np.allclose(a, b)

    # "sample_conditional" method, numerics make this one a little unstable;
    # just check the shape
    a = original_gp.sample_conditional(y, t=t)
    b = gp.sample_conditional(y, t=t)
    assert a.shape == b.shape

    a = original_gp.sample_conditional(y, size=10)
    b = gp.sample_conditional(y, size=10)
    assert a.shape == b.shape
コード例 #7
0
ファイル: test_celerite2.py プロジェクト: zeta1999/celerite2
def test_consistency(oterm, mean, data):
    x, diag, y, t = data

    # Setup the original GP
    original_gp = original_celerite.GP(oterm, mean=mean)
    original_gp.compute(x, np.sqrt(diag))

    # Setup the new GP
    term = terms.OriginalCeleriteTerm(oterm)
    gp = celerite2.GaussianProcess(term, mean=mean)
    gp.compute(x, diag=diag)

    # "log_likelihood" method
    assert np.allclose(original_gp.log_likelihood(y), gp.log_likelihood(y))

    # Apply inverse
    assert np.allclose(
        np.squeeze(original_gp.apply_inverse(y)), gp.apply_inverse(y)
    )

    conditional_t = gp.condition(y, t=t)
    mu, cov = original_gp.predict(y, t=t, return_cov=True)
    assert np.allclose(conditional_t.mean, mu)
    assert np.allclose(conditional_t.variance, np.diag(cov))
    assert np.allclose(conditional_t.covariance, cov)

    conditional = gp.condition(y)
    mu, cov = original_gp.predict(y, return_cov=True)
    assert np.allclose(conditional.mean, mu)
    assert np.allclose(conditional.variance, np.diag(cov))
    assert np.allclose(conditional.covariance, cov)

    # "sample" method
    seed = 5938
    np.random.seed(seed)
    a = original_gp.sample()
    np.random.seed(seed)
    b = gp.sample()
    assert np.allclose(a, b)

    np.random.seed(seed)
    a = original_gp.sample(size=10)
    np.random.seed(seed)
    b = gp.sample(size=10)
    assert np.allclose(a, b)

    # "sample_conditional" method, numerics make this one a little unstable;
    # just check the shape
    a = original_gp.sample_conditional(y, t=t)
    b = conditional_t.sample()
    assert a.shape == b.shape

    a = original_gp.sample_conditional(y, size=10)
    b = conditional.sample(size=10)
    assert a.shape == b.shape
コード例 #8
0
def test_consistency(name, args, mean, data):
    x, diag, y, t = data

    term = getattr(terms, name)(**args)
    gp = GaussianProcess(term, mean=mean)
    gp.compute(x, diag=diag)

    pyterm = getattr(pyterms, name)(**args)
    pygp = celerite2.GaussianProcess(pyterm, mean=mean)
    pygp.compute(x, diag=diag)

    check_gp_models(lambda x: x.eval(), gp, pygp, y, t)
コード例 #9
0
def test_consistency(name, args, mean):
    # Generate fake data
    np.random.seed(40582)
    x = np.sort(np.random.uniform(0, 10, 50))
    t = np.sort(np.random.uniform(-1, 12, 100))
    diag = np.random.uniform(0.1, 0.3, len(x))
    y = np.sin(x)

    term = getattr(terms, name)(**args)
    gp = GaussianProcess(term, mean=mean)
    gp.compute(x, diag=diag)

    pyterm = getattr(pyterms, name)(**args)
    pygp = celerite2.GaussianProcess(pyterm, mean=mean)
    pygp.compute(x, diag=diag)

    check_gp_models(lambda x: x.detach().numpy(), gp, pygp, y, t)
コード例 #10
0
def test_predict_kernel(data):
    x, diag, y, t = data

    term1 = terms.SHOTerm(S0=1.0, w0=0.5, Q=3.0)
    term2 = terms.SHOTerm(S0=0.3, w0=0.1, Q=0.1)
    term = term1 + term2
    gp = celerite2.GaussianProcess(term, t=x, diag=diag)

    cond0 = gp.condition(y)
    cond1 = gp.condition(y, kernel=term)
    assert np.allclose(cond0.mean, cond1.mean)

    cond0 = gp.condition(y, t)
    cond1 = gp.condition(y, t, kernel=term)
    assert np.allclose(cond0.mean, cond1.mean)

    cond0 = gp.condition(y, t)
    cond1 = gp.condition(y, t, kernel=term1)
    cond2 = gp.condition(y, t, kernel=term2)
    assert np.allclose(cond0.mean, cond1.mean + cond2.mean)

    cond1 = gp.condition(y, kernel=term1)
    mu2 = term1.dot(x, np.zeros_like(x), gp.apply_inverse(y))
    assert np.allclose(cond1.mean, mu2)
コード例 #11
0
def test_predict_kernel(data):
    x, diag, y, t = data

    term1 = terms.SHOTerm(S0=1.0, w0=0.5, Q=3.0)
    term2 = terms.SHOTerm(S0=0.3, w0=0.1, Q=0.1)
    term = term1 + term2
    gp = celerite2.GaussianProcess(term, t=x, diag=diag)

    mu0 = gp.predict(y)
    mu1 = gp.predict(y, kernel=term)
    assert np.allclose(mu0, mu1)

    mu0 = gp.predict(y, t)
    mu1 = gp.predict(y, t, kernel=term)
    assert np.allclose(mu0, mu1)

    mu0 = gp.predict(y, t)
    mu1 = gp.predict(y, t, kernel=term1)
    mu2 = gp.predict(y, t, kernel=term2)
    assert np.allclose(mu0, mu1 + mu2)

    mu1 = gp.predict(y, kernel=term1)
    mu2 = term1.dot(x, np.zeros_like(x), gp.apply_inverse(y))
    assert np.allclose(mu1, mu2)
コード例 #12
0
    def _compute(self, light_curve: pd.DataFrame, **kwargs) -> pd.Series:
        time = light_curve["hmjd"]
        mag = light_curve["mag"]
        err = light_curve["magerr"]

        time = time - time.min()
        mag = mag - mag.mean()
        sq_error = err**2

        kernel = celerite2.terms.RealTerm(a=1.0, c=10.0)
        gp = celerite2.GaussianProcess(kernel, mean=0.0)

        initial_params = np.array([np.log(1.0), np.log(1.0)])
        sol = minimize(neg_log_like,
                       initial_params,
                       method="L-BFGS-B",
                       args=(gp, time, mag, sq_error))

        optimal_params = np.exp(sol.x)
        out_data = [optimal_params[0], 1.0 / optimal_params[1]]

        out = pd.Series(data=out_data,
                        index=self.get_features_keys_without_band())
        return out
コード例 #13
0
# Now, let's fit this dataset using a mixture of [SHOTerm](../api/python.rst#celerite2.terms.SHOTerm) terms: one quasi-periodic component and one non-periodic component.
# First let's set up an initial model to see how it looks:

# +
import celerite2
from celerite2 import terms

# Quasi-periodic term
term1 = terms.SHOTerm(sigma=1.0, rho=1.0, tau=10.0)

# Non-periodic component
term2 = terms.SHOTerm(sigma=1.0, rho=5.0, Q=0.25)
kernel = term1 + term2

# Setup the GP
gp = celerite2.GaussianProcess(kernel, mean=0.0)
gp.compute(t, yerr=yerr)

print("Initial log likelihood: {0}".format(gp.log_likelihood(y)))
# -

# Let's look at the underlying power spectral density of this initial model:

# +
freq = np.linspace(1.0 / 8, 1.0 / 0.3, 500)
omega = 2 * np.pi * freq


def plot_psd(gp):
    for n, term in enumerate(gp.kernel.terms):
        plt.loglog(freq, term.get_psd(omega), label="term {0}".format(n + 1))
コード例 #14
0
def sim_signal(time, amp=1e-3, w=10.):
    kernel = celerite.terms.SHOTerm(S0=1, Q=1, w0=2 * np.pi / w)
    gp = celerite.GaussianProcess(kernel)
    gp.compute(time)
    return 1 + utils.rescale(gp.sample()) * amp / 2
コード例 #15
0
name = parts[6]
filt_disp = '{0}/{1}'.format(filt, disp)

wl = result['noise_dic']['All noise']['wl']
means = result['noise_dic']['All noise']['signal_mean_stack']
stds = result['noise_dic']['All noise']['signal_std_stack']

t = np.arange(0, nhours * 60 * 60, cycle_time)

import celerite2
from celerite2 import terms

S0 = float(sys.argv[2])
w0 = 886
term = terms.SHOTerm(S0=S0, w0=w0, Q=1 / np.sqrt(2))
gp = celerite2.GaussianProcess(term, mean=0.0)
gp.compute(t / (60 * 60 * 24), yerr=0)
fk = (gp.dot_tril(np.random.randn(len(t))) + 1)

import sys

sys.path.append('./')
import generate_noise

factors, data, wl = generate_noise.variability_factors(
    fk,
    wl,
    cold_temp=int(sys.argv[3]),
    hot_temp=int(sys.argv[4]),
    effective_temp=int(sys.argv[3]),
    spec_path='..')