def test_run_forecast_basic(self): """ Test Forecaster.run_forecast with simple arguments. """ # Run a simple forecast with $10,000 income, $500 in annual # contributions, and $1000 in starting balances with no growth: self.forecaster = Forecaster( living_expenses_strategy=LivingExpensesStrategy( strategy=LivingExpensesStrategy.strategy_const_contribution, base_amount=500, inflation_adjust=None), settings=self.settings) forecast = self.forecaster.run_forecast(people={self.person}, accounts={self.account}, debts={}) # Test that it starts and ends in the right place and that # income and total balance (principal) are correct self.assertEqual(forecast.scenario, self.scenario) # Pylint has trouble with attributes added by metaclass # pylint: disable=no-member self.assertEqual(len(forecast.principal_history), self.scenario.num_years) # pylint: enable=no-member # Test that the $500 in contributions have been added to the # initial $1000 principal by the start of year 2: self.assertAlmostEqual(forecast.principal, 1500, places=2) # Gross income should be unchanged at $10,000: self.assertAlmostEqual(forecast.income_forecast.gross_income, 10000, places=2)
def test_get_forecast_exception(): conn = sqlite3.connect('src/weather.db') cur = conn.cursor() forecaster = Forecaster('2019-05-04', '2019-04-21', 5) with pytest.raises(ForecasterException): forecaster.get_forecast(cur)
class HKOModel(nn.Module): def __init__(self, inplanes, input_num_seqs, output_num_seqs): super(HKOModel, self).__init__() self.input_num_seqs = input_num_seqs self.output_num_seqs = output_num_seqs self.encoder = Encoder(inplanes=inplanes, num_seqs=input_num_seqs) self.forecaster = Forecaster(num_seqs=output_num_seqs) if cuda_flag == True: self.encoder = self.encoder.cuda() self.forecaster = self.forecaster.cuda() def forward(self, data): self.encoder.init_h0() for time in range(self.input_num_seqs): self.encoder(data[time]) all_pre_data = [] self.forecaster.set_h0(self.encoder) for time in range(self.output_num_seqs): pre_data = self.forecaster(None) # print h_next.size() all_pre_data.append(pre_data) return all_pre_data
def define_graph(self): with tf.variable_scope("Graph", reuse=tf.AUTO_REUSE): self.in_data = tf.placeholder(self._dtype, shape=(self._batch, self._in_seq, self._h, self._w, self._in_c), name="input") self.gt_data = tf.placeholder(self._dtype, shape=(self._batch, self._out_seq, self._h, self._w, 1), name="gt") self.global_step = tf.Variable(0, trainable=False) with tf.device('/device:GPU:0'): encoder_net = Encoder(self._batch, self._in_seq, gru_filter=c.ENCODER_GRU_FILTER, gru_in_chanel=c.ENCODER_GRU_INCHANEL, conv_kernel=c.CONV_KERNEL, conv_stride=c.CONV_STRIDE, h2h_kernel=c.H2H_KERNEL, i2h_kernel=c.I2H_KERNEL, height=self._h, width=self._w) for i in range(self._in_seq): encoder_net.rnn_encoder(self.in_data[:, i, ...]) states = encoder_net.rnn_states with tf.device('/device:GPU:1'): forecaster_net = Forecaster( self._batch, self._out_seq, gru_filter=c.DECODER_GRU_FILTER, gru_in_chanel=c.DECODER_GRU_INCHANEL, deconv_kernel=c.DECONV_KERNEL, deconv_stride=c.DECONV_STRIDE, h2h_kernel=c.H2H_KERNEL, i2h_kernel=c.I2H_KERNEL, rnn_states=states, height=self._h, width=self._w) for i in range(self._out_seq): forecaster_net.rnn_forecaster() pred = tf.concat(forecaster_net.pred, axis=1) with tf.variable_scope("loss"): gt = self.gt_data weights = get_loss_weight_symbol(pred) self.result = pred self.mse = weighted_mse(pred, gt, weights) self.mae = weighted_mae(pred, gt, weights) self.gdl = gdl_loss(pred, gt) loss = c.L1_LAMBDA * self.mse + c.L2_LAMBDA * self.mae + c.GDL_LAMBDA * self.gdl self.optimizer = tf.train.AdamOptimizer(c.LR).minimize( loss, global_step=self.global_step) tf.summary.scalar('mse', self.mse) tf.summary.scalar('mae', self.mae) tf.summary.scalar('gdl', self.gdl) tf.summary.scalar('combine_loss', loss) self.summary = tf.summary.merge_all()
def __init__(self, inplanes, input_num_seqs, output_num_seqs): super(HKOModel, self).__init__() self.input_num_seqs = input_num_seqs self.output_num_seqs = output_num_seqs self.encoder = Encoder(inplanes=inplanes, num_seqs=input_num_seqs) self.forecaster = Forecaster(num_seqs=output_num_seqs) if cuda_flag == True: self.encoder = self.encoder.cuda() self.forecaster = self.forecaster.cuda()
def main( *, sequence_length: int, forecast_steps: int, ) -> None: print("Starting...") X = linspace(0, 50, num=501) Y_pure = [sin(x) for x in X] pure_sine_model = LSTMModel( input_dim=1, hidden_dim=5 * sequence_length, learning_rate=1e-3, num_layers=2, dropout_prob=0, ) forecaster = Forecaster(pure_sine_model) training_set, validation_set, test_set = [ SequentialForecastSet( data, sequence_length=sequence_length, forecast_steps=forecast_steps, ) for data in split_series(Y_pure, reserved_ratio=0.2, test_ratio=0.5) ] forecaster.fit(training_set=training_set, validation_set=validation_set) from torch.utils.data import DataLoader loader = DataLoader( test_set, batch_size=128, # num_workers=cpu_count(), ) for batch in loader: x, y = batch x = x.transpose(-3, 1) y_hat = pure_sine_model(x) loss = pure_sine_model.loss_fn(y, y_hat) # print(sequence) print(f"loss: {loss}") # pure_sine_model.fit_progressively( # pd.DataFrame(pure_sine_train), # to_dataset=to_dataset, # training_steps=200, # validation_steps=100, # ) print("Finished.")
def test_init_default(self): """ Tests Forecaster.__init__ with default parameters. """ self.forecaster = Forecaster() # For most params, not being passed means they should be None: self.assertEqual(self.forecaster.living_expenses_strategy, None) self.assertEqual(self.forecaster.saving_strategy, None) self.assertEqual(self.forecaster.withdrawal_strategy, None) self.assertEqual(self.forecaster.allocation_strategy, None) # For two of the params, they should be initialized to whatever # is provided by default by the Settings class: self.assertEqual(self.forecaster.settings, Settings())
def define_graph(self): with tf.variable_scope("Graph", reuse=tf.AUTO_REUSE): self.in_data_480 = tf.placeholder(self._dtype, shape=(self._batch, self._in_seq, self._h, self._w, self._in_c), name="input") self.in_data_720 = tf.placeholder(self._dtype, shape=(self._batch, self._in_seq, c.PRED_H, c.PRED_W, self._in_c), name="input2") self.gt_data_480 = tf.placeholder(self._dtype, shape=(self._batch, self._out_seq, self._h, self._w, self._in_c), name="gt_480") self.gt_data_720 = tf.placeholder(self._dtype, shape=(self._batch, self._out_seq, c.PRED_H, c.PRED_W, c.IN_CHANEL), name="gt_700") with tf.device('/device:GPU:0'): encoder_net = Encoder(self._batch, self._in_seq, mode=self.mode) if self.mode == 'online': encoder_net.stack_rnn_encoder(self.in_data_720) else: encoder_net.stack_rnn_encoder(self.in_data_480) with tf.device('/device:GPU:1'): forecaster_net = Forecaster(self._batch, self._out_seq, mode=self.mode) forecaster_net.stack_rnn_forecaster(encoder_net.rnn_states) with tf.variable_scope("loss"): pred = forecaster_net.pred gt = self.gt_data_480 with tf.name_scope('loss_weights'): weights = get_loss_weight_symbol(pred) self.result = pred if self.mode == 'train': self.mse = weighted_mse(pred, gt, weights) self.mae = weighted_mae(pred, gt, weights) self.gdl = gdl_loss(pred, gt) loss = c.L1_LAMBDA * self.mse + c.L2_LAMBDA * self.mae + c.GDL_LAMBDA * self.gdl self.optimizer = tf.train.AdamOptimizer( c.LR).minimize(loss) self.mse_summary = tf.summary.scalar('mse', self.mse) self.mae_summary = tf.summary.scalar('mae', self.mae) self.gdl_summary = tf.summary.scalar('gdl', self.gdl) self.merged = tf.summary.merge( [self.mse_summary, self.mae_summary])
def main(): print "starting" if len(sys.argv) < 4: print("Not enough arguments") return training_file_loc, eval_file_loc, tick_type, analyse_changes_str = sys.argv[ 1:] if analyse_changes_str == 'True': analyse_changes = True else: analyse_changes = False if tick_type == "enrollment": tick_builder = Enrollment_tick elif tick_type == "olhc": tick_builder = Forex_Tick elif tick_type == "taiex": tick_builder = Taiex_tick moving_window_len = 10 confidence_threshold = 1 time_series = Time_Series(tick_builder, moving_window_len) time_series.import_history(training_file_loc, analyse_changes) forecaster = Forecaster(analyse_changes) fts = Fuzzy_time_series(confidence_threshold) fts.build_fts(1, time_series) for i in xrange(1, 10): fts.add_order(i) result = list(forecaster.evaluate_model(fts, eval_file_loc, order=i))[-1] print "Order-" + str(i) print "RMSE:\t" + str(result.rmse) print "%:\t\t" + str(result.percent) print "" result = list( forecaster.evaluate_buy_and_hold_model(fts, eval_file_loc, order=i))[-1] print "Buy and hold:\t" print "RMSE:\t" + str(result.rmse) print "%:\t\t" + str(result.percent) print "" walk = Random_walk() walk.build(time_series) result = list(forecaster.evaluate_model(walk, eval_file_loc, order=i))[-1] print "Random walk:\t" print "RMSE:\t" + str(result.rmse) print "%:\t\t" + str(result.percent) print "-----------------------------------------"
def test_forecast(reading, expected_forecast, monkeypatch, mock_ws): ''' 1. Assume that the forecaster module imports an external module weatherservice and then instantiates it in the constructor. 2. In this case you cannot inject a mock into forecaster's constructor. 3. Therefore, use a monkeypatching. Pytest will only patch a value for the duration of the test and then remove it. ''' WS = Mock(return_value=mock_ws) monkeypatch.setattr('forecaster.WeatherService', WS) forecaster = Forecaster() mock_ws.barometer.return_value = reading assert forecaster.forecast() == expected_forecast
def main(): print "starting" if len(sys.argv) < 4: print ("Not enough arguments") return training_file_loc, eval_file_loc, tick_type, analyse_changes_str = sys.argv[1:] if analyse_changes_str == 'True': analyse_changes = True else: analyse_changes = False if tick_type == "enrollment": tick_builder = Enrollment_tick elif tick_type == "olhc": tick_builder = Forex_Tick elif tick_type == "taiex": tick_builder = Taiex_tick moving_window_len = 10 confidence_threshold = 1 time_series = Time_Series(tick_builder, moving_window_len) time_series.import_history(training_file_loc, analyse_changes) forecaster = Forecaster(analyse_changes) fts = Fuzzy_time_series(confidence_threshold) fts.build_fts(1, time_series) for i in xrange(1, 10): fts.add_order(i) result = list(forecaster.evaluate_model(fts, eval_file_loc, order=i))[-1] print "Order-" + str(i) print "RMSE:\t" + str(result.rmse) print "%:\t\t" + str(result.percent) print "" result = list(forecaster.evaluate_buy_and_hold_model(fts, eval_file_loc, order=i))[-1] print "Buy and hold:\t" print "RMSE:\t" + str(result.rmse) print "%:\t\t" + str(result.percent) print "" walk = Random_walk() walk.build(time_series) result = list(forecaster.evaluate_model(walk, eval_file_loc, order=i))[-1] print "Random walk:\t" print "RMSE:\t" + str(result.rmse) print "%:\t\t" + str(result.percent) print "-----------------------------------------"
def __eval(self, name, training_file_loc, eval_file_loc, tick_builder, analyse_changes, confidence_threshold): results = [] for window_len in xrange(1, self.order_max): time_series = Time_Series(tick_builder, window_len) time_series.import_history(training_file_loc, analyse_changes) forecaster = Forecaster(analyse_changes) fts = Fuzzy_time_series(confidence_threshold) fts.build_fts(0, time_series) for order in xrange(1, self.moving_window_max): results.append( self.__get_resuts(name, order, window_len, forecaster, fts, eval_file_loc)) return itertools.izip_longest(*results, fillvalue=None)
def chat_messages(): if request.form['action'] == 'join': store[request.form['user_id']] = { "messages": ["Hello %s!" % (request.form['name'])] } reply = store[request.form['user_id']]['messages'][-1] elif request.form['action'] == 'message': location = QueryParser(request.form['text']).location() if location: lat, lng = geocoder.encode(location) reply = Forecaster(lat, lng).weather_summary() if not reply: reply = "Hrm... couldn't find any weather info for that place." else: reply = RESPONSES[randint(0, len(RESPONSES) - 1)] return jsonify({"messages": [{"type": "text", "text": reply}]})
def ts_forecasting(): args = input_cmd() # get energy consumption data load = args.load f_steps = args.steps data = get_dataset(load_to_predict=load) c_target = data["energy"] t_target, f_target, fcast_range = forecast_split(c_target, n_steps=f_steps) # ML methods features, target = get_features(t_target) lags = [int(f.split("_")[1]) for f in features if "lag" in f] forecaster = Forecaster(f_steps, lags=lags) print("Forecast with Linear Regression model") model, cv_score, test_score = linear_model(features, target) if args.fcast == "direct": fcast_linear = forecaster.direct(t_target, linear_model) elif args.fcast == "recursive": fcast_linear = forecaster.recursive(t_target, model) fcast_score = mape(f_target, fcast_linear) print(f""" Linear Regression scores -------------- Cross-validation MAPE: {round(cv_score, 2)}% Test MAPE: {round(test_score, 2)}% Direct Forecast MAPE: {round(fcast_score, 2)}% """) print("Forecast with XGBoost model") model, cv_score, test_score = xgboost_model(features, target, max_evals=25) if args.fcast == "direct": fcast_xgb = forecaster.direct(t_target, xgboost_model) elif args.fcast == "recursive": fcast_xgb = forecaster.recursive(t_target, model) fcast_score = mape(f_target, fcast_xgb) print(f""" XGBoost scores -------------- Cross-validation MAPE: {round(cv_score, 2)}% Test MAPE: {round(test_score, 2)}% Recursive Forecast MAPE: {round(fcast_score, 2)}% """)
true_img = target_image[pre_id, 0, 0, ...] encode_img = input_image[pre_id, 0, 0, ...] cv2.imwrite(os.path.join(save_path, 'a_%s.png' % pre_id), encode_img) cv2.imwrite(os.path.join(save_path, 'c_%s.png' % pre_id), tmp_img) cv2.imwrite(os.path.join(save_path, 'b_%s.png' % pre_id), true_img) # for pre_data in pre_list: # temp = pre_data.cpu().data.numpy() # print temp.mean() train_arr, test_arr, train_imgs_maps, test_imgs_maps = load_data( ['AZ9010', 'AZ9200']) if __name__ == '__main__': # m = HKOModel(inplanes=1, input_num_seqs=input_num_seqs, output_num_seqs=output_num_seqs) m_e = Encoder(inplanes=input_channels_img, num_seqs=input_num_seqs) m_e = m_e.cuda() m_f = Forecaster(num_seqs=output_num_seqs) m_f = m_f.cuda() test(input_channels_img, output_channels_img, size_image, max_epoch, model_e=m_e, model_f=m_f, cuda_test=cuda_flag)
def define_graph(self): with tf.variable_scope("Graph", reuse=tf.AUTO_REUSE): self.in_data = tf.placeholder(self._dtype, shape=(self._batch, self._in_seq, self._h, self._w, self._in_c), name="input") self.gt_data = tf.placeholder(self._dtype, shape=(self._batch, self._out_seq, self._h, self._w, 1), name="gt") self.global_step = tf.Variable(0, trainable=False) with tf.device('/device:GPU:0'): encoder_net = Encoder(self._batch, self._in_seq, gru_filter=c.ENCODER_GRU_FILTER, gru_in_chanel=c.ENCODER_GRU_INCHANEL, conv_kernel=c.CONV_KERNEL, conv_stride=c.CONV_STRIDE, h2h_kernel=c.H2H_KERNEL, i2h_kernel=c.I2H_KERNEL, height=self._h, width=self._w) if c.SEQUENCE_MODE: for i in range(self._in_seq): encoder_net.rnn_encoder_step(self.in_data[:, i, ...]) else: encoder_net.rnn_encoder(self.in_data) states = encoder_net.rnn_states with tf.device('/device:GPU:1'): forecaster_net = Forecaster( self._batch, self._out_seq, gru_filter=c.DECODER_GRU_FILTER, gru_in_chanel=c.DECODER_GRU_INCHANEL, deconv_kernel=c.DECONV_KERNEL, deconv_stride=c.DECONV_STRIDE, h2h_kernel=c.H2H_KERNEL, i2h_kernel=c.I2H_KERNEL, rnn_states=states, height=self._h, width=self._w) if c.SEQUENCE_MODE: for i in range(self._out_seq): forecaster_net.rnn_forecaster_step() pred = tf.concat(forecaster_net.pred, axis=1) else: forecaster_net.rnn_forecaster() pred = forecaster_net.pred with tf.variable_scope("loss"): gt = self.gt_data weights = get_loss_weight_symbol(pred) self.result = pred self.mse = tf.reduce_mean(tf.square(pred - gt)) self.mae = weighted_mae(pred, gt, weights) self.gdl = gdl_loss(pred, gt) self.d_loss = self.result if c.ADVERSARIAL: self.d_pred = tf.placeholder( self._dtype, (self._batch, self._out_seq, self._h, self._w, 1)) self.d_loss = tf.reduce_mean(tf.square(self.d_pred - gt)) self.loss = tf.cond(self.global_step > self.adv_involve, lambda: c.L1_LAMBDA * self.mae \ + c.L2_LAMBDA * self.mse \ + c.GDL_LAMBDA * self.gdl \ + c.ADV_LAMBDA * self.d_loss, lambda: c.L1_LAMBDA * self.mae \ + c.L2_LAMBDA * self.mse \ + c.GDL_LAMBDA * self.gdl ) # self.loss = c.L1_LAMBDA * self.mae \ # + c.L2_LAMBDA * self.mse \ # + c.GDL_LAMBDA * self.gdl \ # + c.ADV_LAMBDA * self.d_loss else: self.loss = c.L1_LAMBDA * self.mae + c.L2_LAMBDA * self.mse + c.GDL_LAMBDA * self.gdl self.d_loss = self.loss self.optimizer = tf.train.AdamOptimizer(c.LR).minimize( self.loss, global_step=self.global_step) self.summary = tf.summary.merge([ tf.summary.scalar('mse', self.mse), tf.summary.scalar('mae', self.mae), tf.summary.scalar('gdl', self.gdl), tf.summary.scalar('combine_loss', self.loss) ])
class TestForecaster(ForecasterTester): """ Tests Forecaster. """ def setUp(self): """ Builds default strategies, persons, etc. """ # Use a default settings object: # (This is conditional so that subclasses can assign their own # settings object before calling super().setUp()) if not hasattr(self, 'settings'): self.settings = Settings() # To simplify tests, modify Settings so that forecasts are # just 2 years with easy-to-predict contributions ($1000/yr) self.settings.num_years = 2 self.settings.living_expenses_strategy = ( LivingExpensesStrategy.strategy_const_contribution) self.settings.living_expenses_base_amount = 1000 # Allow subclasses to use subclasses of Forecaster by assigning # to forecaster_type if not hasattr(self, 'forecaster_type'): self.forecaster_type = Forecaster # Build default `SubForecast` inputs based on `settings`: self.initial_year = self.settings.initial_year self.scenario = Scenario(inflation=self.settings.inflation, stock_return=self.settings.stock_return, bond_return=self.settings.bond_return, other_return=self.settings.other_return, management_fees=self.settings.management_fees, initial_year=self.settings.initial_year, num_years=self.settings.num_years) self.living_expenses_strategy = LivingExpensesStrategy( strategy=self.settings.living_expenses_strategy, base_amount=self.settings.living_expenses_base_amount, rate=self.settings.living_expenses_rate, inflation_adjust=self.scenario.inflation_adjust) self.saving_strategy = TransactionStrategy( strategy=self.settings.saving_strategy, weights=self.settings.saving_weights) self.withdrawal_strategy = TransactionStrategy( strategy=self.settings.withdrawal_strategy, weights=self.settings.withdrawal_weights) self.allocation_strategy = AllocationStrategy( strategy=self.settings.allocation_strategy, min_equity=self.settings.allocation_min_equity, max_equity=self.settings.allocation_max_equity, target=self.settings.allocation_target, standard_retirement_age=( self.settings.allocation_std_retirement_age), risk_transition_period=self.settings.allocation_risk_trans_period, adjust_for_retirement_plan=( self.settings.allocation_adjust_retirement)) self.debt_payment_strategy = DebtPaymentStrategy( strategy=self.settings.debt_payment_strategy) self.tax_treatment = Tax( tax_brackets=self.settings.tax_brackets, personal_deduction=self.settings.tax_personal_deduction, credit_rate=self.settings.tax_credit_rate, inflation_adjust=self.scenario.inflation_adjust) # Now build some Ledger objects to test against: # A person making $10,000/yr self.person = Person(initial_year=self.initial_year, name="Test 1", birth_date="1 January 1980", retirement_date="31 December 2040", gross_income=10000, raise_rate=0, spouse=None, tax_treatment=self.tax_treatment) # An account with $1000 in it (and no interest) self.account = Account(owner=self.person, balance=1000) # A debt with a $100 balance (and no interest) self.debt = Debt(owner=self.person, balance=100) # Init a Forecaster object here for convenience: self.forecaster = self.forecaster_type(settings=self.settings) def setUp_decimal(self): """ Builds default strategies/persons/etc. with Decimal inputs. """ # pylint: disable=invalid-name # This name is based on `setUp`, which doesn't follow Pylint's rules # pylint: enable=invalid-name # Use a default settings object: # (This is conditional so that subclasses can assign their own # settings object before calling super().setUp()) if not hasattr(self, 'settings'): self.settings = Settings() # To simplify tests, modify Settings so that forecasts are # just 2 years with easy-to-predict contributions ($1000/yr) self.settings.num_years = 2 self.settings.living_expenses_strategy = ( LivingExpensesStrategy.strategy_const_contribution) self.settings.living_expenses_base_amount = Decimal(1000) # Allow subclasses to use subclasses of Forecaster by assigning # to forecaster_type if not hasattr(self, 'forecaster_type'): self.forecaster_type = Forecaster # Build default `SubForecast` inputs based on `settings`: self.initial_year = self.settings.initial_year self.scenario = Scenario( inflation=Decimal(self.settings.inflation), stock_return=Decimal(self.settings.stock_return), bond_return=Decimal(self.settings.bond_return), other_return=Decimal(self.settings.other_return), management_fees=Decimal(self.settings.management_fees), initial_year=self.settings.initial_year, num_years=self.settings.num_years) self.living_expenses_strategy = LivingExpensesStrategy( strategy=self.settings.living_expenses_strategy, base_amount=Decimal(self.settings.living_expenses_base_amount), rate=Decimal(self.settings.living_expenses_rate), inflation_adjust=self.scenario.inflation_adjust) self.saving_strategy = TransactionStrategy( strategy=self.settings.saving_strategy, weights={ year: Decimal(val) for (year, val) in self.settings.saving_weights.items() }) self.withdrawal_strategy = TransactionStrategy( strategy=self.settings.withdrawal_strategy, weights={ year: Decimal(val) for (year, val) in self.settings.withdrawal_weights.items() }) self.allocation_strategy = AllocationStrategy( strategy=self.settings.allocation_strategy, min_equity=Decimal(self.settings.allocation_min_equity), max_equity=Decimal(self.settings.allocation_max_equity), target=Decimal(self.settings.allocation_target), standard_retirement_age=( self.settings.allocation_std_retirement_age), risk_transition_period=self.settings.allocation_risk_trans_period, adjust_for_retirement_plan=( self.settings.allocation_adjust_retirement)) self.debt_payment_strategy = DebtPaymentStrategy( strategy=self.settings.debt_payment_strategy, high_precision=Decimal) self.tax_treatment = Tax( tax_brackets={ year: { Decimal(lower): Decimal(upper) } for (year, vals) in self.settings.tax_brackets.items() for (lower, upper) in vals.items() }, personal_deduction={ year: Decimal(val) for (year, val) in self.settings.tax_personal_deduction.items() }, credit_rate={ year: Decimal(val) for (year, val) in self.settings.tax_credit_rate.items() }, inflation_adjust=self.scenario.inflation_adjust, high_precision=Decimal) # Now build some Ledger objects to test against: # A person making $10,000/yr self.person = Person(initial_year=self.initial_year, name="Test 1", birth_date="1 January 1980", retirement_date="31 December 2040", gross_income=Decimal(10000), raise_rate=Decimal(0), spouse=None, tax_treatment=self.tax_treatment, high_precision=Decimal) # An account with $1000 in it (and no interest) self.account = Account(owner=self.person, balance=Decimal(1000), high_precision=Decimal) # A debt with a $100 balance (and no interest) self.debt = Debt(owner=self.person, balance=Decimal(100), high_precision=Decimal) # Init a Forecaster object here for convenience: self.forecaster = self.forecaster_type(settings=self.settings, high_precision=Decimal) def test_init_default(self): """ Tests Forecaster.__init__ with default parameters. """ self.forecaster = Forecaster() # For most params, not being passed means they should be None: self.assertEqual(self.forecaster.living_expenses_strategy, None) self.assertEqual(self.forecaster.saving_strategy, None) self.assertEqual(self.forecaster.withdrawal_strategy, None) self.assertEqual(self.forecaster.allocation_strategy, None) # For two of the params, they should be initialized to whatever # is provided by default by the Settings class: self.assertEqual(self.forecaster.settings, Settings()) def test_build_living_exp_strat(self): """ Test Forecaster.build_param for living_expenses_strategy. """ param = self.forecaster.get_param(Parameter.LIVING_EXPENSES_STRATEGY) self.assertEqual(param, self.living_expenses_strategy) def test_build_saving_strat(self): """ Test Forecaster.build_param for contribution_strategy. """ param = self.forecaster.get_param(Parameter.SAVING_STRATEGY) self.assertEqual(param, self.saving_strategy) def test_build_withdraw_strat(self): """ Test Forecaster.build_param for withdrawal_strategy. """ param = self.forecaster.get_param(Parameter.WITHDRAWAL_STRATEGY) self.assertEqual(param, self.withdrawal_strategy) def test_build_allocation_strat(self): """ Test Forecaster.build_param for allocation_strategy. """ param = self.forecaster.get_param(Parameter.ALLOCATION_STRATEGY) self.assertEqual(param, self.allocation_strategy) def test_build_tax_treatment(self): """ Test Forecaster.build_param for tax_treatment. """ param = self.forecaster.get_param(Parameter.TAX_TREATMENT) self.assertEqual(param, self.tax_treatment) def test_run_forecast_basic(self): """ Test Forecaster.run_forecast with simple arguments. """ # Run a simple forecast with $10,000 income, $500 in annual # contributions, and $1000 in starting balances with no growth: self.forecaster = Forecaster( living_expenses_strategy=LivingExpensesStrategy( strategy=LivingExpensesStrategy.strategy_const_contribution, base_amount=500, inflation_adjust=None), settings=self.settings) forecast = self.forecaster.run_forecast(people={self.person}, accounts={self.account}, debts={}) # Test that it starts and ends in the right place and that # income and total balance (principal) are correct self.assertEqual(forecast.scenario, self.scenario) # Pylint has trouble with attributes added by metaclass # pylint: disable=no-member self.assertEqual(len(forecast.principal_history), self.scenario.num_years) # pylint: enable=no-member # Test that the $500 in contributions have been added to the # initial $1000 principal by the start of year 2: self.assertAlmostEqual(forecast.principal, 1500, places=2) # Gross income should be unchanged at $10,000: self.assertAlmostEqual(forecast.income_forecast.gross_income, 10000, places=2) def test_run_forecast_mutation(self): """ Test that Forecaster.run_forecast doesn't mutate arguments. """ # Run a forecast and check whether the inputs were mutated: forecast = self.forecaster.run_forecast(people={self.person}, accounts={self.account}, debts={self.debt}) # The originally-provided Person's history dicts should have # length 1 (since they haven't been mutated). They should be # length 2 for the Person held by the Forecast. # pylint: disable=no-member self.assertEqual(len(self.person.gross_income_history), 1) # pylint: enable=no-member self.assertEqual(len(next(iter(forecast.people)).gross_income_history), 2) def test_decimal(self): """ Test Forecaster.run_forecast with Decimal arguments. """ # Convert values to Decimal: self.setUp_decimal() # This test is based on test_run_forecast_basic # Run a simple forecast with $10,000 income, $500 in annual # contributions, and $1000 in starting balances with no growth: living_expenses_strategy = LivingExpensesStrategy( strategy=LivingExpensesStrategy.strategy_const_contribution, base_amount=Decimal(500), inflation_adjust=None, high_precision=Decimal) self.forecaster = Forecaster( living_expenses_strategy=living_expenses_strategy, settings=self.settings, high_precision=Decimal) forecast = self.forecaster.run_forecast(people={self.person}, accounts={self.account}, debts={}) # Test that it starts and ends in the right place and that # income and total balance (principal) are correct self.assertEqual(forecast.scenario, self.scenario) # Pylint has trouble with attributes added by metaclass # pylint: disable=no-member self.assertEqual(len(forecast.principal_history), self.scenario.num_years) # pylint: enable=no-member # Test that the $500 in contributions have been added to the # initial $1000 principal by the start of year 2: self.assertAlmostEqual(forecast.principal, Decimal(1500), places=2) # Gross income should be unchanged at $10,000: self.assertAlmostEqual(forecast.income_forecast.gross_income, Decimal(10000), places=2)
class TestForecaster(unittest.TestCase): """ Tests Forecaster. """ def setUp(self): """ Builds default strategies, persons, etc. """ # Use a default settings object: # (This is conditional so that subclasses can assign their own # settings object before calling super().setUp()) if not hasattr(self, 'settings'): self.settings = Settings() # To simplify tests, modify Settings so that forecasts are # just 2 years with easy-to-predict contributions ($1000/yr) self.settings.num_years = 2 self.settings.living_expenses_strategy = ( LivingExpensesStrategy.strategy_const_contribution) self.settings.living_expenses_base_amount = Decimal(1000) # Allow subclasses to use subclasses of Forecaster by assigning # to forecaster_type if not hasattr(self, 'forecaster_type'): self.forecaster_type = Forecaster # Build default `SubForecast` inputs based on `settings`: self.initial_year = self.settings.initial_year self.scenario = Scenario(inflation=self.settings.inflation, stock_return=self.settings.stock_return, bond_return=self.settings.bond_return, other_return=self.settings.other_return, management_fees=self.settings.management_fees, initial_year=self.settings.initial_year, num_years=self.settings.num_years) self.living_expenses_strategy = LivingExpensesStrategy( strategy=self.settings.living_expenses_strategy, base_amount=self.settings.living_expenses_base_amount, rate=self.settings.living_expenses_rate, inflation_adjust=self.scenario.inflation_adjust) self.saving_strategy = TransactionStrategy( strategy=self.settings.saving_strategy, weights=self.settings.saving_weights) self.withdrawal_strategy = TransactionStrategy( strategy=self.settings.withdrawal_strategy, weights=self.settings.withdrawal_weights) self.allocation_strategy = AllocationStrategy( strategy=self.settings.allocation_strategy, min_equity=self.settings.allocation_min_equity, max_equity=self.settings.allocation_max_equity, target=self.settings.allocation_target, standard_retirement_age=( self.settings.allocation_std_retirement_age), risk_transition_period=self.settings.allocation_risk_trans_period, adjust_for_retirement_plan=( self.settings.allocation_adjust_retirement)) self.debt_payment_strategy = DebtPaymentStrategy( strategy=self.settings.debt_payment_strategy) self.tax_treatment = Tax( tax_brackets=self.settings.tax_brackets, personal_deduction=self.settings.tax_personal_deduction, credit_rate=self.settings.tax_credit_rate, inflation_adjust=self.scenario.inflation_adjust) # Now build some Ledger objects to test against: # A person making $10,000/yr self.person = Person(initial_year=self.initial_year, name="Test 1", birth_date="1 January 1980", retirement_date="31 December 2040", gross_income=Money(10000), raise_rate=Decimal(0), spouse=None, tax_treatment=self.tax_treatment) # An account with $1000 in it (and no interest) self.account = Account(owner=self.person, balance=Money(1000)) # A debt with a $100 balance (and no interest) self.debt = Debt(owner=self.person, balance=Money(100)) # Init a Forecaster object here for convenience: self.forecaster = self.forecaster_type(settings=self.settings) def assertEqual_dict(self, first, second, msg=None, memo=None): """ Extends equality testing for dicts with complex members. """ # We're mimicking the name of assertEqual, so we can live with # the unusual method name. # pylint: disable=invalid-name # For dicts, first confirm they represent the same keys: # (The superclass can handle this) if first.keys() != second.keys(): super().assertEqual(first, second) # Then recursively check each pair of values: for key in first: self.assertEqual(first[key], second[key], msg=msg, memo=memo) def assertEqual_list(self, first, second, msg=None, memo=None): """ Extends equality testing for lists with complex members. """ # We're mimicking the name of assertEqual, so we can live with # the unusual method name. # pylint: disable=invalid-name # First confirm that they have the same length. if len(first) != len(second): super().assertEqual(first, second) # Then iterate over the elements in sequence: for first_value, second_value in zip(first, second): self.assertEqual(first_value, second_value, msg=msg, memo=memo) def assertEqual_set(self, first, second, msg=None, memo=None): """ Extends equality testing for sets with complex members. """ # We're mimicking the name of assertEqual, so we can live with # the unusual method name. # pylint: disable=invalid-name # First confirm that they have the same length. if len(first) != len(second): super().assertEqual(first, second, msg=msg) # For sets or other unordered iterables, we can't rely on # `in` (because complex objects might not have equality or # hashing implemented beyond the standard id() # implementation), so we want to test each element in one # set against every element in the other set. for val1 in first: match = False for val2 in second: try: # Each pair of compared objects is automatically # added to the memo, so make a copy (which will # be discarded if the objects are not equal). memo_copy = copy(memo) self.assertEqual(val1, val2, msg=msg, memo=memo_copy) except AssertionError: # If we didn't find a match, advance to the next # value in second and try that. continue # If we did find a match, record that fact and # advance to the next value in second. match = True memo.update(memo_copy) break if not match: # If we couldn't find a match, the sets are not # equal; the entire test should fail. raise AssertionError(str(first) + ' != ' + str(second)) def assertEqual_complex(self, first, second, msg=None, memo=None): """ Extends equality testing for complex objects. """ # We're mimicking the name of assertEqual, so we can live with # the unusual method name. # pylint: disable=invalid-name # For complicated objects, recurse onto the attributes dict: self.assertEqual(first.__dict__, second.__dict__, msg=msg, memo=memo) def assertEqual(self, first, second, msg=None, memo=None): """ Tests complicated class instances for equality. This method is used (instead of __eq__) because equality semantics are only needed for testing code and can mess up things like set membership, require extensive (and inefficient) comparisons, and/or can result in infinite recursion. """ # We add a memo argument to avoid recursion. We don't pass it # to the superclass, so pylint's objection isn't helpful. # pylint: disable=arguments-differ # The memo dict maps each object to the set of objects that it's # been compared to. If they've been compared, that means that we # don't need to re-evaluate their equality - if they're unequal, # that'll be discovered at a higher level of recursion: if memo is None: memo = collections.defaultdict(set) if id(second) in memo[id(first)]: # We've previously compared these objects and found them to # be equal, so return without failing. return else: memo[id(first)].add(id(second)) memo[id(second)].add(id(first)) try: # If these are equal under ordinary comparison, accept that # and don't so any further special testing. super().assertEqual(first, second, msg=msg) return except AssertionError as error: # If the superclass assertEqual doesn't find equality, run # a few additional equality tests based on object type: # 1) Dicts; keys and values both need to be checked. # 2) Ordered iterables; values need to be checked in order. # 3) Unordered iterables; check values for membership. # 4) Complex objects; compare attributes via __dict__. # Most of these tests won't work if the objects are # different types, and we don't deal with the case anyways. # In that case, accept the error and raise it on up. if (not isinstance(first, type(second)) and not isinstance(second, type(first))): raise error elif isinstance(first, dict): self.assertEqual_dict(first, second, msg=msg, memo=memo) elif isinstance(first, collections.abc.Sequence): self.assertEqual_list(first, second, msg=msg, memo=memo) elif isinstance(first, collections.abc.Iterable): self.assertEqual_set(first, second, msg=msg, memo=memo) elif hasattr(first, '__dict__'): self.assertEqual_complex(first, second, msg=msg, memo=memo) else: # If none of our special tests apply, accept the error. raise error def assertNotEqual(self, first, second, msg=None): """ Overloaded to test non-equality of complex objects. """ try: self.assertEqual(first, second, msg=msg) except AssertionError: # We want assertEqual to throw an error (since we're # expecting non-equality) return # Raise a suitable error if the equality test didn't fail: raise AssertionError(str(first) + ' == ' + str(second)) def test_assertEqual(self): # pylint: disable=invalid-name """ Tests overloaded TestForecaster.assertEqual. """ # Compare an object to itself person1 = self.person self.assertEqual(person1, self.person) # Compare two idential instances of an object: person2 = deepcopy(person1) self.assertEqual(person1, person2) # Compare two instances of an object that differ only in a # complicated attribute. (Simple case: set it to None) person2.tax_treatment = None self.assertNotEqual(person1, person2) def test_init_default(self): """ Tests Forecaster.__init__ with default parameters. """ self.forecaster = Forecaster() # For most params, not being passed means they should be None: self.assertEqual(self.forecaster.living_expenses_strategy, None) self.assertEqual(self.forecaster.saving_strategy, None) self.assertEqual(self.forecaster.withdrawal_strategy, None) self.assertEqual(self.forecaster.allocation_strategy, None) # For two of the params, they should be initialized to whatever # is provided by default by the Settings class: self.assertEqual(self.forecaster.settings, Settings()) def test_build_living_exp_strat(self): """ Test Forecaster.build_param for living_expenses_strategy. """ param = self.forecaster.get_param(Parameter.LIVING_EXPENSES_STRATEGY) self.assertEqual(param, self.living_expenses_strategy) def test_build_saving_strat(self): """ Test Forecaster.build_param for contribution_strategy. """ param = self.forecaster.get_param(Parameter.SAVING_STRATEGY) self.assertEqual(param, self.saving_strategy) def test_build_withdraw_strat(self): """ Test Forecaster.build_param for withdrawal_strategy. """ param = self.forecaster.get_param(Parameter.WITHDRAWAL_STRATEGY) self.assertEqual(param, self.withdrawal_strategy) def test_build_allocation_strat(self): """ Test Forecaster.build_param for allocation_strategy. """ param = self.forecaster.get_param(Parameter.ALLOCATION_STRATEGY) self.assertEqual(param, self.allocation_strategy) def test_build_tax_treatment(self): """ Test Forecaster.build_param for tax_treatment. """ param = self.forecaster.get_param(Parameter.TAX_TREATMENT) self.assertEqual(param, self.tax_treatment) def test_run_forecast_basic(self): """ Test Forecaster.run_forecast with simple arguments. """ # Run a simple forecast with $10,000 income, $500 in annual # contributions, and $1000 in starting balances with no growth: self.forecaster = Forecaster( living_expenses_strategy=LivingExpensesStrategy( strategy=LivingExpensesStrategy.strategy_const_contribution, base_amount=Money(500), inflation_adjust=None), settings=self.settings) forecast = self.forecaster.run_forecast(people={self.person}, accounts={self.account}, debts={}) # Test that it starts and ends in the right place and that # income and total balance (principal) are correct self.assertEqual(forecast.scenario, self.scenario) # Pylint has trouble with attributes added by metaclass # pylint: disable=no-member self.assertEqual(len(forecast.principal_history), self.scenario.num_years) # pylint: enable=no-member # Test that the $500 in contributions have been added to the # initial $1000 principal by the start of year 2: self.assertAlmostEqual(forecast.principal, Money(1500), places=2) # Gross income should be unchanged at $10,000: self.assertAlmostEqual(forecast.income_forecast.gross_income, Money(10000), places=2) def test_run_forecast_mutation(self): """ Test that Forecaster.run_forecast doesn't mutate arguments. """ # Run a forecast and check whether the inputs were mutated: forecast = self.forecaster.run_forecast(people={self.person}, accounts={self.account}, debts={self.debt}) # The originally-provided Person's history dicts should have # length 1 (since they haven't been mutated). They should be # length 2 for the Person held by the Forecast. # pylint: disable=no-member self.assertEqual(len(self.person.gross_income_history), 1) # pylint: enable=no-member self.assertEqual(len(next(iter(forecast.people)).gross_income_history), 2)
def test_time_difference_none(): forecaster = Forecaster('2019-05-04', '2019-04-21', 5) assert forecaster.get_time_difference(None, 5) == 0
def setup_class(cls): print("Setting up CLASS {0}".format(cls.__name__)) cls.d = DataLoader('dev.csv', '2009-01-01', '2016-06-30') cls.d.load_stock_data() cls.f = Forecaster(cls.d, 'GOOGL')
def tmp_data(): train_arr = torch.rand(input_num_seqs, batch_size, input_channels_img, size_image, size_image) test_arr = torch.rand(input_num_seqs, batch_size, input_channels_img, size_image, size_image) train_imgs_maps = torch.rand(input_num_seqs, batch_size, output_channels_img, size_image, size_image) test_imgs_maps = torch.rand(input_num_seqs, batch_size, output_channels_img, size_image, size_image) return train_arr, test_arr, train_imgs_maps, test_imgs_maps # train_arr, test_arr, train_imgs_maps, test_imgs_maps = load_data(['AZ9010','AZ9200']) train_arr, test_arr, train_imgs_maps, test_imgs_maps = tmp_data() if __name__ == '__main__': # m = HKOModel(inplanes=1, input_num_seqs=input_num_seqs, output_num_seqs=output_num_seqs) m_e = Encoder(inplanes=input_channels_img, num_seqs=input_num_seqs) # m_e = m_e.cuda() m_f = Forecaster(num_seqs=output_num_seqs) # m_f = m_f.cuda() mtest(input_channels_img, output_channels_img, size_image, max_epoch, model_e=m_e, model_f=m_f, cuda_test=cuda_flag)
def __init__(self): self.forecaster = Forecaster() self.window = CloudoWindow(self.forecaster.get_weather)
def test_day_increase_n(): forecaster = Forecaster('2000-06-13', '2000-06-13', 5) assert forecaster.day_increase('2000-06-13', 30) == '2000-07-13'
def test_day_increase_1(): forecaster = Forecaster('2000-06-13', '2000-06-13', 5) assert forecaster.day_increase('2000-06-13', 1) == '2000-06-14'
def test_get_forecast(): conn = sqlite3.connect('src/weather.db') cur = conn.cursor() forecaster = Forecaster('2019-05-01', '2019-04-21', 5) assert len(forecaster.get_forecast(cur)) > 0
from forecaster import Forecaster import pandas as pd import numpy as np covid = pd.read_csv("covid_19_data.csv") covid["ObservationDate"] = pd.to_datetime(covid["ObservationDate"]) covid.drop(columns=["SNo"], inplace=True) def incidence_denv(xdata, c1, c2, c3, c4=0): ans = [c1 * np.exp(-((t - c2)**2) / c3) + c4 for t in xdata] return ans caster = Forecaster(covid, "Mainland China", incidence_denv) print("Done 1") caster.forecast() caster.plot()
def test_time_difference_hour(): minute = 60 forecaster = Forecaster('2019-05-04', '2019-04-21', 5) assert forecaster.get_time_difference('13:30:00', '14:30:00') == 60 * minute