def test_save(): reg = Regressor(TwTi_RoRi()) save_model('test', reg) load_reg = load_model('test') assert id(reg) != id(load_reg) for a, b in zip(reg.ss.parameters, load_reg.ss.parameters): assert a == b
def test_check_model(data, ss): reg = Regressor(ss=ss) summary, corr = reg.fit(df=data, outputs='T_int', inputs=['T_ext', 'P_hea']) assert not check_model(reg, summary, corr, verbose=True)
* ((df['vent_SUA_VFR'] / 3600.0) * df['vent_SUA_AT'] - (df['vent_EHA_VFR'] / 3600.0) * df['vent_EHA_AT'])) y = df['south_T'][start_fit:stop_fit].values[np.newaxis, :] y0 = y[0, 0] / 100.0 u = df[['Ambient temperature', 'north_T', 'Solar radiation: global horizontal', 'south_H', 'vent2_H']][start_fit:stop_fit].values.T dt = 600 t = np.arange(0, dt * y.shape[1], dt) reg = Regressor(TwTi_RwRbAwAicv()) reg.ss.parameters.set_parameter("Rw", value=1e-3, transform="lowup", bounds=(1e-7, 1.0)) reg.ss.parameters.set_parameter("Rb", value=1e-3, transform="lowup", bounds=(1e-7, 1.0)) reg.ss.parameters.set_parameter("Cw", value=1e-3, transform="lowup", bounds=(1e-7, 1.0)) reg.ss.parameters.set_parameter("Ci", value=1e-3, transform="lowup", bounds=(1e-7, 1.0)) reg.ss.parameters.set_parameter("Aw", value=10.0, transform="lowup",
df['vent2_H'] = (1005.0 * 1.2 * ((df['vent_SUA_VFR'] / 3600.0) * df['vent_SUA_AT'] - (df['vent_EHA_VFR'] / 3600.0) * df['vent_EHA_AT'])) y = df['south_T'][start_fit:stop_fit].values[np.newaxis, :] y0 = y[0, 0] / 100.0 u = df[[ 'Ambient temperature', 'north_T', 'Solar radiation: global horizontal', 'south_H', 'vent2_H' ]][start_fit:stop_fit].values.T dt = 600 t = np.arange(0, dt * y.shape[1], dt) reg = Regressor(TwTiTb_RoRiRbAwAicv()) reg.ss.parameters.set_parameter("Ro", value=1.0, transform="lower", bounds=(1e-8, 1.0)) reg.ss.parameters.set_parameter("Ri", value=5e-3, transform="lowup", bounds=(1e-8, 1.0)) reg.ss.parameters.set_parameter("Rb", value=5e-3, transform="lowup", bounds=(1e-8, 1.0)) reg.ss.parameters.set_parameter("Cw", value=5e-2,
dict(name="Ro", value=1e-2, transform="log"), dict(name="Ri", value=1e-3, transform="log"), dict(name="Cw", value=1e-2, transform="log"), dict(name="Ci", value=1e-3, transform="log"), dict(name="Aw", value=1.0, transform="log"), dict(name="Ai", value=1.0, transform="log"), dict(name="sigw_w", value=0.1, transform="log"), dict(name="sigw_i", value=1e-8, transform="fixed"), dict(name="sigv", value=0.1, transform="log"), dict(name="x0_w", value=0.28, transform="log"), dict(name="x0_i", value=y0, transform="fixed"), dict(name="sigx0_w", value=1.0, transform="fixed"), dict(name="sigx0_i", value=1.0, transform="fixed") ] reg = Regressor(TwTi_RoRiAwAi(parameters, hold_order='foh')) inputs = [ 'Ambient temperature', 'Solar radiation: global horizontal', 'south_H' ] outputs = 'south_T' out = reg.fit(df=df[start_fit:stop_fit], inputs=inputs, outputs=outputs) # compute log-likelihood on validation dataset x, P = reg.estimate_states(df=df[start_fit:stop_fit], inputs=inputs, outputs=outputs) x0v = x[-1, :, :] P0v = np.linalg.cholesky(P[-1, :, :]).T
def regressor(statespace): return Regressor(ss=statespace)
y0 = 0.2670106194217502 parameters = [ dict(name="Ro", value=0.1, transform="log"), dict(name="Ri", value=0.01, transform="log"), dict(name="Cw", value=0.1, transform="log"), dict(name="Ci", value=0.01, transform="log"), dict(name="sigw_w", value=0.01, transform="log"), dict(name="sigw_i", value=0.0, transform="fixed"), dict(name="sigv", value=0.01, transform="log"), dict(name="x0_w", value=0.25, transform="log"), dict(name="x0_i", value=y0, transform="fixed"), dict(name="sigx0_w", value=1.0, transform="fixed"), dict(name="sigx0_i", value=1.0, transform="fixed") ] reg = Regressor(TwTi_RoRi(parameters, hold_order='foh')) out = reg.fit(df=df, inputs=['T_ext', 'P_hea'], outputs='T_int') dt = 60 tnew = np.arange(df.index[0], df.index[-1] + dt, dt) y_mean_f, y_std_f = reg.predict(df=df, inputs=['T_ext', 'P_hea'], tpred=tnew) y_mean_s, y_std_s = reg.predict(df=df, outputs='T_int', inputs=['T_ext', 'P_hea'], tpred=tnew, smooth=True) # plot filtered and smoothed output plt.close("all") fig, axes = plt.subplots(nrows=1, ncols=1, sharex=True, sharey=True)
def regressor(parameters): return Regressor(TwTi_RoRi(parameters))
# Generate df np.random.seed(1) N = 20 t = np.sort(np.random.rand(1, N), axis=1).flatten() y = np.sin(12 * t) + 0.66 * np.cos(25 * t) + np.random.randn(1, N) * 0.01 y = y.flatten() df = pd.DataFrame(index=t, data=y, columns=['y']) parameters = [ dict(name="mscale", value=0.5, transform="log"), dict(name="lscale", value=0.5, transform="log"), dict(name="sigv", value=0.1, transform="log") ] reg12 = Regressor(Matern12(parameters)) reg32 = Regressor(Matern32(parameters)) reg52 = Regressor(Matern52(parameters)) # fit print("\nMatérn(1/2)") print("-" * 11) reg12.fit(df=df, outputs='y') print("\nMatérn(3/2)") print("-" * 11) reg32.fit(df=df, outputs='y') print("\nMatérn(5/2)") print("-" * 11) reg52.fit(df=df, outputs='y')
df = pd.read_csv('../data/armadillo/armadillo_data_H2.csv').set_index('Time') y0 = 0.2670106194217502 parameters = [ dict(name="Ro", value=0.1, bounds=(1e-6, 0.3), prior=Normal(0.1, 0.04)), dict(name="Ri", value=0.01, bounds=(1e-6, 0.3), prior=Normal(0.05, 0.02)), dict(name="Cw", value=0.1, bounds=(1e-6, 0.3), prior=Normal(0.1, 0.04)), dict(name="Ci", value=0.01, bounds=(1e-6, 0.3), prior=Normal(0.05, 0.02)), dict(name="sigw_w", value=0.01, bounds=(1e-6, 0.3), prior=Normal(0.05, 0.02)), dict(name="sigw_i", value=0.0, transform="fixed"), dict(name="sigv", value=0.01, bounds=(1e-6, 0.3), prior=Normal(0.05, 0.02)), dict(name="x0_w", value=0.25, bounds=(0.1, 0.5), prior=Normal(0.25, 0.07)), dict(name="x0_i", value=y0, transform="fixed"), dict(name="sigx0_w", value=1.0, transform="fixed"), dict(name="sigx0_i", value=1.0, transform="fixed") ] reg = Regressor(TwTi_RoRi(parameters, hold_order='foh')) mh = reg.fit(df=df, inputs=['T_ext', 'P_hea'], outputs='T_int', options={ 'method': 'sMMALA', 'n_chains': 6, 'n_samples': 2000 })
# Generate data np.random.seed(1) N = 20 t = np.sort(np.random.rand(1, N), axis=1).flatten() y = np.sin(12 * t) + 0.66 * np.cos(25 * t) + np.random.randn(1, N) * 0.01 y = y.flatten() df = pd.DataFrame(index=t, data=y, columns=['y']) # Set Matern(3/2) parameters = [ dict(name="mscale", value=0.5, transform="log"), dict(name="lscale", value=0.5, transform="log"), dict(name="sigv", value=0.1, transform="log") ] reg = Regressor(Matern32(parameters)) results = reg.fit(df=df, outputs='y') # new data Nnew = 500 tnew = np.linspace(-0.1, 1.1, Nnew) ym_f, ys_f = reg.predict(df=df, outputs='y', tpred=tnew, smooth=False) ym_s, ys_s = reg.predict(df=df, outputs='y', tpred=tnew, smooth=True) # plot filtered and smoothed output plt.close("all") fig, axes = plt.subplots(nrows=2, ncols=1, sharex=True, sharey=True) fig.suptitle("filtered vs. smoothed state ") plt.xlim(-0.1, 1.1)
par_Periodic = [ dict(name="period", value=1.5, transform="log"), dict(name="mscale", value=1.0, transform="log"), dict(name="lscale", value=1.0, transform="log"), dict(name="sigv", value=0.1, transform="log") ] par_Matern12 = [ dict(name="mscale", value=1.0, transform="log"), dict(name="lscale", value=10.0, transform="log"), dict(name="sigv", value=0.1, transform="log") ] quasi_periodic = GPProduct(Periodic(par_Periodic), Matern12(par_Matern12)) reg = Regressor(quasi_periodic) results = reg.fit(df=data, outputs='y') Nnew = 500 tnew = np.linspace(xlim[0], xlim[1] + 3, Nnew) y_mean_f, y_std_f = reg.predict(df=data, outputs='y', tpred=tnew, smooth=False) y_mean_s, y_std_s = reg.predict(df=data, outputs='y', tpred=tnew, smooth=True) # plot filtered and smoothed output plt.close("all") fig, axes = plt.subplots(nrows=2, ncols=1, sharex=True, sharey=False) fig.suptitle("filtered vs. smoothed state ") axes[0].set_title("filtered") axes[0].plot(t, y, 'kx', mew=2) axes[0].plot(tnew, y_mean_f, 'C0', lw=2)
def regressor(matern_statespace): return Regressor(ss=matern_statespace)
period = 1 t = np.sort(xlim[0] + (xlim[1] - xlim[0]) * np.random.random(n)) amplitude = alim[0] + (alim[1] - alim[0]) * np.random.random() noise = 0.2 * np.random.randn(len(t)) 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']) 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") ] reg = Regressor(Periodic(parameters, J=6)) results = reg.fit(df=data, outputs='y') Nnew = 500 tnew = np.linspace(xlim[0], xlim[1] + 3, Nnew) y_mean_f, y_std_f = reg.predict(df=data, outputs='y', tpred=tnew, smooth=False) y_mean_s, y_std_s = reg.predict(df=data, outputs='y', tpred=tnew, smooth=True) # plot filtered and smoothed output # plt.close("all") # fig, axes = plt.subplots(nrows=2, ncols=1, sharex=True, sharey=False) # fig.suptitle("filtered vs. smoothed state ") # axes[0].set_title("filtered") # axes[0].plot(t, y, 'kx', mew=2)
df = pd.read_csv('data/armadillo/armadillo_data_H2.csv').set_index('Time') parameters = [ dict(name="Ro", value=0.1), dict(name="Ri", value=0.01), dict(name="Cw", value=0.1), dict(name="Ci", value=0.01), dict(name="sigw_w", value=0.01), dict(name="sigw_i", value=0.0, transform="fixed"), dict(name="sigv", value=0.01), dict(name="x0_w", value=0.25), dict(name="x0_i", value=0.2670106194217502, transform="fixed"), dict(name="sigx0_w", value=1.0, transform="fixed"), dict(name="sigx0_i", value=1.0, transform="fixed") ] reg = Regressor(TwTi_RoRi(parameters, hold_order='foh')) Nsamples = mh._N - mh.warmup yhat = np.empty((Nsamples, df.index.shape[0])) chains = np.empty((mh._Np + 1, Nsamples * mh._M)) for i in range(mh._Np + 1): chains[i, :] = mh.trace[:, i, mh.warmup:].ravel() # chains[-1, :] = np.abs(chains[-1, :]) # posterior = ( chains[-1, :] - chains[-1, :].min() ) / ( chains[-1, :].max() - chains[-1, :].min() ) ecdf = ECDF(chains[-1, :]) min_ecdf = ecdf.x[np.where(ecdf.y <= 0.025)[0][-1]] max_ecdf = ecdf.x[np.where(ecdf.y >= 0.975)[0][0]] for i in range(Nsamples):
parameters = [ dict(name="Ro", value=1e-2), dict(name="Ri", value=1e-3), dict(name="Cw", value=1e-2), dict(name="Ci", value=1e-3), dict(name="sigw_w", value=0.1), dict(name="sigw_i", value=0.2), dict(name="sigv", value=0.1), dict(name="x0_w", value=0.28), dict(name="x0_i", value=0.25), dict(name="sigx0_w", value=1.0), dict(name="sigx0_i", value=1.0) ] reg = Regressor(TwTi_RoRi(parameters)) reg.ss.jacobian = False reg.ss.update() dt = 600 hold = 'foh' Ad, B0d, B1d, Qd = reg.ss.discretization(dt, hold) AA = np.zeros((reg.ss.Nx + reg.ss.Nu, reg.ss.Nx + reg.ss.Nu)) AA[:reg.ss.Nx, :reg.ss.Nx] = reg.ss.A AA[:reg.ss.Nx, reg.ss.Nx:] = reg.ss.B AAd = expm(AA * dt) Ad2 = AAd[:reg.ss.Nx, :reg.ss.Nx] B0d2 = AAd[:reg.ss.Nx, reg.ss.Nx:] assert np.allclose(Ad, Ad2)