def _noise_generator(self, set_a, set_b): set_a = np.asarray(set_a) set_b = np.asarray(set_b) model_a = pf.LocalLevel(set_a, family = pf.Normal()) model_b = pf.LocalLevel(set_b, family = pf.Normal()) model_a_fit = model_a.fit() model_b_fit = model_b.fit() noise_a = model_a.return_noise() noise_b = model_b.return_noise() return noise_a, noise_b
def __init__(self, model_inputs): ''' Initialize a time series model class takes a dictionary model_inputs and builds parameters to be used to estimate model NOTE: The time series model object is not initialized here, since this class will be called multiple times during cross validation. The fit method will do three things 1. Receive a unique dataset (passed into the method) 2. Initialize the ts model (for example ARIMAX) on the data 3. Fit the method The predict_one method will take the fitted class and predict a one day forecast ''' self.model_class = model_inputs['model_class'] self.model_name = model_inputs['name'] self.target = model_inputs['target'] self.dep_vars = model_inputs['dep_vars'] self.pca = None self.components = None self.shocks = None if self.model_class == 'ARIMA': self.model_type = model_inputs['model_type'] hyper_params = model_inputs['hyper_params'] self.ar = hyper_params['ar'] self.ma = hyper_params['ma'] self.diff_order = hyper_params['diff_ord'] self.family = pf.Normal() self.formula = None self.num_components = model_inputs['num_components'] elif self.model_class == 'ARIMAX': self.model_type = model_inputs['model_type'] hyper_params = model_inputs['hyper_params'] self.ar = hyper_params['ar'] self.ma = hyper_params['ma'] self.diff_order = hyper_params['diff_ord'] self.family = pf.Normal() self.formula = model_inputs['formula'] self.num_components = model_inputs['num_components'] else: # this will be gaussian self.model_type = None self.ar = None self.ma = None self.diff_order = None self.family = None self.formula = None self.formula = None # This is the string for ARIMAX models that needs to be used self.num_components = model_inputs['num_components']
def test_bbvi_elbo(): """ Tests that the ELBO increases """ model = pf.GASLLEV(data=data, family=pf.Normal()) x = model.fit('BBVI', iterations=100, record_elbo=True) assert (x.elbo_records[-1] > x.elbo_records[0])
def test_predict_is_length(): """ Tests that the prediction IS dataframe length is equal to the number of steps h """ model = pf.NNAR(data=data, ar=2, family=pf.Normal(), units=2, layers=1) x = model.fit(iterations=50, map_start=False) assert (model.predict_is(h=5).shape[0] == 5)
def test2_normal_predict_is_length(): """ Tests that the length of the predict IS dataframe is equal to no of steps h """ model = pf.GASReg(formula="y ~ x1 + x2", data=data, family=pf.Normal()) x = model.fit() assert (model.predict_is(h=5).shape[0] == 5)
def test_predict_is_length(): """ Tests that the prediction IS dataframe length is equal to the number of steps h """ model = pf.GASLLEV(data=data, family=pf.Normal()) x = model.fit() assert (model.predict_is(h=5).shape[0] == 5)
def test_normal_bbvi_mini_batch_elbo(): """ Tests that the ELBO increases """ model = pf.GASReg(formula="y ~ x1 + x2", data=data, family=pf.Normal()) x = model.fit('BBVI', iterations=100, mini_batch=32, record_elbo=True) assert (x.elbo_records[-1] > x.elbo_records[0])
def test_bbvi_mini_batch_elbo(): """ Tests that the ELBO increases """ model = pf.GAS(data=data, ar=1, sc=1, family=pf.Normal()) x = model.fit('BBVI', iterations=100, mini_batch=32, record_elbo=True) assert (x.elbo_records[-1] > x.elbo_records[0])
def test_bbvi_elbo(): """ Tests that the ELBO increases """ model = pf.NNAR(data=data, ar=2, family=pf.Normal(), units=2, layers=1) x = model.fit('BBVI', iterations=100, record_elbo=True, map_start=False) assert (x.elbo_records[-1] > x.elbo_records[0])
def run_aram(df, maxar, maxma, test_size=14): data = df.dropna() data['log'] = np.log(data[data.columns[0]]) # test_size = int(len(data) * 0.33) train_size = len(data) - int(test_size) train, test = data[:train_size], data[train_size:] if test_stationarity(train[train.columns[1]]) < 0.01: print('平稳,不需要差分') else: diffn = best_diff(train, maxdiff=8) train = produce_diffed_timeseries(train, diffn) print('差分阶数为' + str(diffn) + ',已完成差分') print('开始进行ARMA拟合') order = choose_order(train[train.columns[2]], maxar, maxma) print('模型的阶数为:' + str(order)) _ar = order[0] _ma = order[1] model = pf.ARIMA(data=train, ar=_ar, ma=_ma, target='diff', family=pf.Normal()) model.fit("MLE") test = test['payment_times'] test_predict = model.predict(int(test_size)) test_predict = predict_recover(test_predict, train, diffn) RMSE = np.sqrt( ((np.array(test_predict) - np.array(test))**2).sum() / test.size) print("测试集的RMSE为:" + str(RMSE))
def test2_predict_nans(): """ Tests that the predictions are not nans """ model = pf.NNAR(data=data, ar=2, family=pf.Normal(), units=2, layers=2) x = model.fit(iterations=50, map_start=False) assert (len( model.predict(h=5).values[np.isnan(model.predict(h=5).values)]) == 0)
def test_ppc(): """ Tests PPC value """ model = pf.GASLLEV(data=data, family=pf.Normal()) x = model.fit('BBVI', iterations=100) p_value = model.ppc() assert (0.0 <= p_value <= 1.0)
def test_predict_length(): """ Tests that the prediction dataframe length is equal to the number of steps h """ model = pf.GAS(data=data, ar=2, sc=2, family=pf.Normal()) x = model.fit() x.summary() assert (model.predict(h=5).shape[0] == 5)
def test_normal_predict_length(): """ Tests that the length of the predict dataframe is equal to no of steps h """ model = pf.GASReg(formula="y ~ x1", data=data, family=pf.Normal()) x = model.fit() x.summary() assert (model.predict(h=5, oos_data=data_oos).shape[0] == 5)
def test2_ppc(): """ Tests PPC value """ model = pf.GASReg(formula="y ~ x1 + x2", data=data, family=pf.Normal()) x = model.fit('BBVI', iterations=100) p_value = model.ppc() assert (0.0 <= p_value <= 1.0)
def test2_ppc(): """ Tests PPC value """ model = pf.NNAR(data=data, ar=2, family=pf.Normal(), units=2, layers=2) x = model.fit('BBVI', iterations=100, map_start=False) p_value = model.ppc(nsims=40) assert (0.0 <= p_value <= 1.0)
def test_predict_nans(): """ Tests that the predictions are not nans """ model = pf.GASLLEV(data=data, family=pf.Normal()) x = model.fit() x.summary() assert (len( model.predict(h=5).values[np.isnan(model.predict(h=5).values)]) == 0)
def test_sample_model(): """ Tests sampling function """ model = pf.GASLLEV(data=data, family=pf.Normal()) x = model.fit('BBVI', iterations=100) sample = model.sample(nsims=100) assert (sample.shape[0] == 100) assert (sample.shape[1] == len(data) - 1)
def test2_predict_is_nonconstant(): """ We should not really have predictions that are constant (should be some difference)... This captures bugs with the predict function not iterating forward """ model = pf.NNAR(data=data, ar=2, family=pf.Normal(), units=2, layers=2) x = model.fit(iterations=50, map_start=False) predictions = model.predict_is(h=10, intervals=False) assert (not np.all(predictions.values == predictions.values[0]))
def test_predict_is_nonconstant(): """ We should not really have predictions that are constant (should be some difference)... This captures bugs with the predict function not iterating forward """ model = pf.GAS(data=data, ar=1, sc=1, family=pf.Normal()) x = model.fit() predictions = model.predict_is(h=10, intervals=False) assert (not np.all(predictions.values == predictions.values[0]))
def test_bbvi_mini_batch(): """ Tests an ARIMA model estimated with BBVI and that the length of the latent variable list is correct, and that the estimated latent variables are not nan """ model = pf.NNAR(data=data, ar=2, family=pf.Normal(), units=2, layers=1) x = model.fit('BBVI', iterations=100, mini_batch=50, map_start=False) lvs = np.array([i.value for i in model.latent_variables.z_list]) assert (len(lvs[np.isnan(lvs)]) == 0)
def test2_sample_model(): """ Tests sampling function """ model = pf.NNAR(data=data, ar=2, family=pf.Normal(), units=2, layers=2) x = model.fit('BBVI', iterations=100, map_start=False) sample = model.sample(nsims=40) assert (sample.shape[0] == 40) assert (sample.shape[1] == len(data) - 2)
def flux_hyperparams(s, r: R_TYPE): # Interpret hyper-parameters - r is only used once at initiation of model integ_bounds, ar_bounds, ma_bounds =(0,2), (0.55, 2.45), (0.66, 10.33) s['n_burn'] = 25 s['alpha'] = 0.25 # Defines confidence interval s['buffer_len'] = 5000 s['integ'], s['ar'], s['ma'] = to_int_log_space(r, bounds=[integ_bounds, ar_bounds, ma_bounds]) s['family'] = pf.Normal() return s
def ARIMAX_model(df, target, ar, integ, ma): pfarima_model = pf.ARIMA(data=df, ar=ar, ma=ma, integ=integ, target=target, family=pf.Normal()) arima_x_mh = pfarima_model.fit("M-H") arima_x_mh.summary()
def test2_sample_model(): """ Tests sampling function """ model = pf.GASReg(formula="y ~ x1 + x2", data=data, family=pf.Normal()) x = model.fit('BBVI', iterations=100) sample = model.sample(nsims=100) assert (sample.shape[0] == 100) assert (sample.shape[1] == len(data))
def test2_normal_predict_is_nans(): """ Tests that the predictions in-sample are not NaNs """ model = pf.GASReg(formula="y ~ x1 + x2", data=data, family=pf.Normal()) x = model.fit() x.summary() assert (len( model.predict_is(h=5).values[np.isnan( model.predict_is(h=5).values)]) == 0)
def test_normal_laplace(): """ Tests an GASReg model estimated with Laplace approximation, and tests that the latent variable vector length is correct, and that value are not nan """ model = pf.GASReg(formula="y ~ x1", data=data, family=pf.Normal()) x = model.fit('Laplace') assert (len(model.latent_variables.z_list) == 3) lvs = np.array([i.value for i in model.latent_variables.z_list]) assert (len(lvs[np.isnan(lvs)]) == 0)
def test_normal_mh(): """ Tests an GASReg model estimated with Metropolis-Hastings, and tests that the latent variable vector length is correct, and that value are not nan """ model = pf.GASReg(formula="y ~ x1", data=data, family=pf.Normal()) x = model.fit('M-H', nsims=300) assert (len(model.latent_variables.z_list) == 3) lvs = np.array([i.value for i in model.latent_variables.z_list]) assert (len(lvs[np.isnan(lvs)]) == 0)
def test2_normal_pml(): """ Tests an GASReg model estimated with PML, with multiple predictors, and tests that the latent variable vector length is correct, and that value are not nan """ model = pf.GASReg(formula="y ~ x1 + x2", data=data, family=pf.Normal()) x = model.fit('PML') assert (len(model.latent_variables.z_list) == 4) lvs = np.array([i.value for i in model.latent_variables.z_list]) assert (len(lvs[np.isnan(lvs)]) == 0)
def test_normal_bbvi_mini_batch(): """ Tests an ARIMA model estimated with BBVI and that the length of the latent variable list is correct, and that the estimated latent variables are not nan """ model = pf.GASReg(formula="y ~ x1", data=data, family=pf.Normal()) x = model.fit('BBVI', iterations=100, mini_batch=32) assert (len(model.latent_variables.z_list) == 3) lvs = np.array([i.value for i in model.latent_variables.z_list]) assert (len(lvs[np.isnan(lvs)]) == 0)