def test_gp_product_creation(data_Periodic, models): m, qp = models period, mscale, lscale, decay, sigv = np.random.uniform(0.3, 3.0, 5) par_Periodic = [ dict(name='period', value=period, transform='log'), dict(name='mscale', value=mscale, transform='log'), dict(name='lscale', value=lscale, transform='log'), dict(name='sigv', value=sigv, transform='log'), ] par_Matern = [ dict(name='mscale', value=1.0, transform='fixed'), dict(name='lscale', value=decay, transform='log'), dict(name='sigv', value=0.0, transform='fixed'), ] par_QuasiPeriodic = [ dict(name='period', value=period, transform='log'), dict(name='mscale', value=mscale, transform='log'), dict(name='lscale', value=lscale, transform='log'), dict(name='sigv', value=sigv, transform='log'), dict(name='decay', value=decay, transform='log'), ] reg1 = Regressor(ss=qp(par_QuasiPeriodic)) reg2 = Regressor(ss=Periodic(par_Periodic) * m(par_Matern)) check_grad(data_Periodic, reg1, reg2)
def test_Periodic(data_Periodic): p = [ dict(name='period', transform='log'), dict(name='mscale', transform='log'), dict(name='lscale', transform='log'), dict(name='sigv', transform='log'), ] regressor = Regressor(ss=Periodic(parameters=p)) regressor.ss.parameters.theta = np.random.uniform(0.3, 3.0, 4) check_grad_fd(data_Periodic, regressor)
def generate_regressor_sum(*statespaces): """State-space generator for gradient test""" values = np.random.uniform(0.3, 3.0, 6) par_Periodic = [ dict(name="period", value=values[0], bounds=(0.0, None)), dict(name='mscale', value=values[1], bounds=(0.0, None)), dict(name='lscale', value=values[2], bounds=(0.0, None)), dict(name='sigv', value=values[3], bounds=(0.0, None)), ] par_Matern = [ dict(name="mscale", value=values[4], bounds=(0.0, None)), dict(name="lscale", value=values[5], bounds=(0.0, None)), dict(name="sigv", value=0.0, transform='fixed'), ] for ssm in statespaces: yield Regressor(Periodic(par_Periodic) + ssm(par_Matern))
def generate_regressor_product(*statespaces): """State-space generator for gradient test""" values = np.random.uniform(1.0, 2.0, 5) par_Periodic = [ dict(name='period', value=values[0], bounds=(0.0, None)), dict(name='mscale', value=values[1], bounds=(0.0, None)), dict(name='lscale', value=values[2], bounds=(0.0, None)), dict(name='sigv', value=values[3], bounds=(0.0, None)), ] par_Matern = [ dict(name='mscale', value=1.0, transform='fixed'), dict(name='lscale', value=values[4], bounds=(0.0, None)), dict(name='sigv', value=0.0, transform='fixed'), ] for ssm in statespaces: yield Regressor(ss=Periodic(parameters=par_Periodic) * ssm(parameters=par_Matern))
amplitude = alim[0] + (alim[1] - alim[0]) * np.random.random() noise = 0.2 * np.random.randn(n) y = amplitude * np.sin(2.0 * np.pi / period * t) + noise y[y <= 0] = 0.0 data = pd.DataFrame(index=t, data=y, columns=['y']) # Parameter settings for the Periodic covariance function parameters = [ dict(name='period', value=1.0, transform='fixed'), dict(name='mscale', value=1.0, transform='log'), dict(name='lscale', value=1.0, transform='log'), dict(name='sigv', value=0.1, transform='log'), ] # Instantiate regressor with the Periodic covariance function reg = Regressor(Periodic(parameters)) fit_summary, corr_matrix, opt_summary = reg.fit(df=data, outputs='y') # Fit results print(f'\n{fit_summary}') # Predict on test data tnew = np.linspace(xlim[0], xlim[1] + 1, 500) ym, ysd = reg.predict(df=data, outputs='y', tnew=tnew, smooth=True) # Plot output mean and 95% credible intervals sns.set_style('darkgrid') sns.set_context('talk') plt.plot(t, y, linestyle='', marker='+', mew=2, label='data', color='darkred') plt.plot(tnew, ym, color='navy', label='mean')
p3 = [ dict(name='mscale', value=1.0, transform='fixed'), dict(name='lscale', value=2.607e02, transform='log'), dict(name='sigv', value=0.0, transform='fixed'), ] # Matérn 3/2 for short-term effects p4 = [ dict(name='mscale', value=4.595e-01, transform='log'), dict(name='lscale', value=6.359e-01, transform='log'), dict(name='sigv', value=0.0, transform='fixed'), ] k1 = Matern52(p1, name='k1') k2 = Periodic(p2, name='k2') k3 = Matern32(p3, name='k3') k4 = Matern32(p4, name='k4') # Compose covariance function K = k1 + k2 * k3 + k4 reg = Regressor(K) fit_summary, corr_matrix, opt_summary = reg.fit(df=df, outputs='CO2_fit') print(f'\n{fit_summary}') # generate new prediction time instants tnew = np.arange(1958, 2030, 0.01) ym, ysd = reg.predict(df=df, outputs='CO2_pred', smooth=True, tnew=tnew) # Plot output mean and 95% credible intervals
import os import pytest import numpy as np from pysip.regressors import FreqRegressor as Regressor from pysip.statespace import TwTi_RoRiAwAicv, TwTiTm_RoRiRmRbAwAicv, Matern12, Matern52, Periodic from pysip.utils import save_model, load_model @pytest.mark.parametrize( 'reg', [ pytest.param(TwTi_RoRiAwAicv(), id="rc_model"), pytest.param(Matern12(), id="gp_model"), pytest.param(Periodic() * Matern12(), id="gp_product"), pytest.param(Periodic() + Matern12(), id="gp_sum"), pytest.param(TwTi_RoRiAwAicv(latent_forces='Qv') <= Matern12(), id="lfm_rc_gp"), pytest.param( TwTi_RoRiAwAicv(latent_forces='Qv') <= Periodic() * Matern12(), id="lfm_rc_gp_product"), pytest.param( TwTi_RoRiAwAicv(latent_forces='Qv') <= Periodic() + Matern12(), id="lfm_rc_gp_sum"), ], ) def test_save_model_to_pickle(reg): reg = Regressor(reg) reg.ss.parameters.theta = np.random.uniform(1e-1, 1, len(reg.ss.parameters.theta)) reg.ss.update_continuous_dssm()