<jupyter_start><jupyter_text>GP rotation periods<jupyter_code>import corner
import emcee3
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import minimize
from astropy.stats import LombScargle

import george
from george.kernels import ExpSquaredKernel, ExpSine2Kernel
import kplr
import kepler_data as kd

%matplotlib inline<jupyter_output><empty_output><jupyter_text>Download a light curve<jupyter_code>kepid = "1026146".zfill(9)
247520207
client = kplr.API()
star = client.star(kepid)
star.get_light_curves(fetch=True, short_cadence=False)
LC_DIR = "/Users/ruthangus/.kplr/data/lightcurves/{}".format(kepid)
x, y, yerr = kd.load_kepler_data(LC_DIR)
x -= x[0]
plt.plot(x, y, "k.")
plt.xlabel("Time [Days]")
plt.ylabel("Normalised flux")
plt.xlim(0, 20)

# Thin data
x, y, yerr = x[:1000:10], y[:1000:10], yerr[:1000:10]
plt.plot(x, y, "r.")
a, l, g, p = .1, 1, 1, 8
k = a * ExpSquaredKernel(l) * ExpSine2Kernel(g, p)
gp = george.GP(k)
gp.compute(x, yerr)

x_pred = np.linspace(min(x), max(x), 500)
mu, var = gp.predict(y, x_pred, return_var=True)

plt.plot(x, y, "k.")
plt.plot(x_pred, mu, color="CornFlowerBlue")
plt.fill_between(x_pred, mu - np.sqrt(var), mu + np.sqrt(var), color="CornFlowerBlue", alpha=0.2)
plt.xlabel("Time [Days]")
plt.ylabel("Normalised flux")
plt.xlim(0, 20)<jupyter_output><empty_output><jupyter_text>Now optimise the GP parameters<jupyter_code>from scipy.optimize import minimize

def neg_ln_like(p):
    gp.set_parameter_vector(p)
    return -gp.log_likelihood(y)

def grad_neg_ln_like(p):
    gp.set_parameter_vector(p)
    return -gp.grad_log_likelihood(y)

result = minimize(neg_ln_like, gp.get_parameter_vector(), jac=grad_neg_ln_like)
print(result)

gp.set_parameter_vector(result.x)
print("A = ", result.x[0])
print("l = ", result.x[1])
print("gamma = ", result.x[2])
print("period = ", result.x[3])
gp.compute(x, yerr)
mu, var = gp.predict(y, x_pred, return_var=True)

plt.plot(x, y, "k.")
plt.plot(x_pred, mu, color="CornFlowerBlue")
plt.fill_between(x_pred, mu - np.sqrt(var), mu + np.sqrt(var), color="CornFlowerBlue", alpha=0.2)
plt.xlabel("Time [Days]")
plt.ylabel("Normalised flux")
plt.xlim(0, 20)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16286496350364962, 'fraction_numerical': 0.02874087591240876, 'mean_word_length': 6.285714285714286, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '371656', 'license': 'MIT', 'n_tokens_mistral': 982, 'n_tokens_neox': 915, 'n_words': 178, 'path': 'Simple_george_demo.ipynb', 'repo_name': 'RuthAngus/GPtutorial', 'size': '2192'}