def test_run_simulation(self): '''Investor: Running a simulation returns well formed daily returns''' inv = Investor([1, 10], 10, 1000) test_array = inv._run_simulation(10) success = np.array([0, 0, -.2, -.4, .4, .2, 0, .2, -.4, .4]) print test_array.tolist() self.assertTrue(np.allclose(test_array, success))
def sweep_variations(funds_and_expense_ratios, leverage_ratio, num_samples, num_trajectories_to_save_as_figures, outfilepath): for scenario in LEV_ETF_SCENARIOS.keys(): dir = path.join(outfilepath, LEV_ETF_SCENARIOS[scenario]) if not os.path.isdir(dir): os.mkdir(dir) tax_rate = 0 funds_and_expense_ratios_to_use = copy.copy(funds_and_expense_ratios) leverage_ratio_to_use = leverage_ratio if "Match theory" in scenario: investor = Investor.Investor(monthly_probability_of_layoff=0, only_paid_in_first_month_of_sim=True, initial_emergency_savings=0) market = Market.Market(inflation_rate=0,medium_black_swan_prob=0, large_black_swan_prob=0) elif "Default" in scenario: market = Market.Market() investor = Investor.Investor() else: raise Exception("scenario type not supported") if "3X" in scenario: leverage_ratio_to_use = 3.0 if "no expense ratios" in scenario: for key in funds_and_expense_ratios.keys(): funds_and_expense_ratios_to_use[key] = 0 if "taxes" in scenario: tax_rates = TaxRates.TaxRates() if "moderate taxes" in scenario: tax_rate = MODERATE_ANNUAL_FRACTION_OF_SHORT_TERM_CAP_GAINS * tax_rates.short_term_cap_gains_rate_plus_state() elif "high taxes" in scenario: tax_rate = HIGH_ANNUAL_FRACTION_OF_SHORT_TERM_CAP_GAINS * tax_rates.short_term_cap_gains_rate_plus_state() print "\n==Scenario: %s==" % scenario many_runs(funds_and_expense_ratios_to_use, tax_rate, leverage_ratio_to_use, num_samples, investor, market, path.join(dir,""), num_trajectories_to_save_as_figures)
def test_returns_correct_results(self): '''Investor: Returned results are correct values for mean/std''' inv = Investor([1, 10], 10000, 1000) test = inv.get_results() success = np.array([[ 1., 0.0332, 0.99944873], [10., 0.02084, 0.31293081]]) self.assertTrue(np.allclose(test, success))