def market_return(self): # 1. Regression: On the period before the window dr_data = bu.daily_returns(self.data) dr_market = bu.daily_returns(self.market) dr_market['intercept'] = 1 reg_results = sm.OLS(dr_data[self.start_period:self.end_period], dr_market[self.start_period:self.end_period]).fit() del dr_market['intercept'] # Solution to the regresion slope = reg_results.params[0] intercept = reg_results.params[1] std_error = dr_market[self.start_period:self.end_period].std()['Daily Return'] expected_return = lambda x: x * slope + intercept # 2. Analysis on the event window # Expexted Return: self.er = dr_market['Daily Return'][self.start_window:self.end_window].apply(expected_return) self.er.name = 'Expected Return' # Abnormal return: Return of the data - expected return self.ar = dr_data['Daily Return'][self.start_window:self.end_window] - self.er self.ar.name = 'Abnormal Return' # Cumulative abnormal return self.car = self.ar.apply(np.cumsum) self.car.name = 'Cumulative Abnormal Return' # t-test t_test_calc = lambda x: x / std_error self.t_test = self.car.apply(t_test_calc) self.t_test.name = 't test' self.prob = self.t_test.apply(stats.norm.cdf) self.prob.name = 'Probability'
def test_total_return(self): sim = MarketSimulator('./data') sim.initial_cash = 1000000 sim.simulate("../../sim/test/orders.csv") # Test without giving the column tr1 = BasicUtils.total_return(sim.portfolio) self.assertEquals(tr1, 0.1332629999999999) # Test giving the column we want to calculate tr2 = BasicUtils.total_return(sim.portfolio, 'Portfolio') self.assertEquals(tr2, 0.1332629999999999)
def test_sharpe_ratio(self): sim = MarketSimulator('./data') sim.initial_cash = 1000000 sim.simulate("../../sim/test/orders.csv") # Test without giving the column sr1 = BasicUtils.sharpe_ratio(sim.portfolio, extraAnswers=True) self.assertEquals(sr1['sharpe_ratio'], 1.1825359272456812) self.assertEquals(sr1['std'], 0.0071658104790118396) self.assertEquals(sr1['mean'], 0.00054698326727656884) # Test giving the column we want to calculate sr2 = BasicUtils.sharpe_ratio(sim.portfolio, 'Portfolio', extraAnswers=True) self.assertEquals(sr2['sharpe_ratio'], 1.1825359272456812) self.assertEquals(sr2['std'], 0.0071658104790118396) self.assertEquals(sr2['mean'], 0.00054698326727656884)