def _assert_data(result_data, check_city=True): # city data if check_city: assert result_data['city']['id'] == city_info['_id'] assert result_data['city']['name'] == city_info['name'] assert result_data['city']['country'] == city_info['country'] assert isclose( result_data['city']['coord']['latitude'], city_info['coord']['lat'], rel_tol=1e-02) assert isclose( result_data['city']['coord']['longitude'], city_info['coord']['lon'], rel_tol=1e-02) # forecast data datas = result_data['datas'] assert datas is not None assert result_data['cnt'] == len(datas) assert len(datas) > 0 assert isinstance(datas[0]['dt_value'], dt.datetime) assert datas[0]['main']['temp'] is not None assert datas[0]['main']['temp_min'] is not None assert datas[0]['main']['temp_max'] is not None assert datas[0]['main']['humidity'] is not None assert datas[0]['main']['pressure'] is not None assert datas[0]['main']['sea_level'] is not None assert datas[0]['main']['grnd_level'] is not None
def test_linlogspace_correct_range(start, stop, num, base): space = utils.linlogspace(start=start, stop=stop, num=num, base=base) note(space) assert isclose(space[0], start) assert isclose(space[-1], stop) assert isclose(space.max(), max(start, stop)) assert isclose(space.min(), min(start, stop)) assert len(space) == num
def test_omega_ratio(self, test_risk_free, test_required_return): res_a = empyrical.omega_ratio(ret['a'], risk_free=test_risk_free, required_return=test_required_return) if np.isnan(res_a): res_a = np.inf res_b = empyrical.omega_ratio(ret['b'], risk_free=test_risk_free, required_return=test_required_return) if np.isnan(res_b): res_b = np.inf res_c = empyrical.omega_ratio(ret['c'], risk_free=test_risk_free, required_return=test_required_return) if np.isnan(res_c): res_c = np.inf assert isclose( ret['a'].vbt.returns.omega_ratio( risk_free=test_risk_free, required_return=test_required_return), res_a) pd.testing.assert_series_equal( ret.vbt.returns.omega_ratio(risk_free=test_risk_free, required_return=test_required_return), pd.Series([res_a, res_b, res_c], index=ret.columns).rename('omega_ratio')) pd.testing.assert_series_equal( ret.vbt.returns.rolling_omega_ratio( ret.shape[0], minp=1, risk_free=test_risk_free, required_return=test_required_return).iloc[-1], pd.Series([res_a, res_b, res_c], index=ret.columns).rename(ret.index[-1]))
def test_max_drawdown(self): res_a = empyrical.max_drawdown(ret['a']) res_b = empyrical.max_drawdown(ret['b']) res_c = empyrical.max_drawdown(ret['c']) assert isclose(ret['a'].vbt.returns.max_drawdown(), res_a) pd.testing.assert_series_equal( ret.vbt.returns.max_drawdown(), pd.Series([res_a, res_b, res_c], index=ret.columns))
def test_beta(self): res_a = empyrical.beta(ret['a'], benchmark_rets['a']) res_b = empyrical.beta(ret['b'], benchmark_rets['b']) res_c = empyrical.beta(ret['c'], benchmark_rets['c']) assert isclose(ret['a'].vbt.returns.beta(benchmark_rets['a']), res_a) pd.testing.assert_series_equal( ret.vbt.returns.beta(benchmark_rets), pd.Series([res_a, res_b, res_c], index=ret.columns))
def test_annualized_return(self): res_a = empyrical.annual_return(ret['a']) res_b = empyrical.annual_return(ret['b']) res_c = empyrical.annual_return(ret['c']) assert isclose(ret['a'].vbt.returns.annualized(), res_a) pd.testing.assert_series_equal( ret.vbt.returns.annualized(), pd.Series([res_a, res_b, res_c], index=ret.columns))
def test_tail_ratio(self): res_a = empyrical.tail_ratio(ret['a']) res_b = empyrical.tail_ratio(ret['b']) res_c = empyrical.tail_ratio(ret['c']) assert isclose(ret['a'].vbt.returns.tail_ratio(), res_a) pd.testing.assert_series_equal( ret.vbt.returns.tail_ratio(), pd.Series([res_a, res_b, res_c], index=ret.columns))
def test_sortino_ratio(self, test_required_return): res_a = empyrical.sortino_ratio(ret['a'], required_return=test_required_return) res_b = empyrical.sortino_ratio(ret['b'], required_return=test_required_return) res_c = empyrical.sortino_ratio(ret['c'], required_return=test_required_return) assert isclose(ret['a'].vbt.returns.sortino_ratio(required_return=test_required_return), res_a) pd.testing.assert_series_equal( ret.vbt.returns.sortino_ratio(required_return=test_required_return), pd.Series([res_a, res_b, res_c], index=ret.columns).rename('sortino_ratio') )
def test_annualized_volatility(self, test_alpha): res_a = empyrical.annual_volatility(ret['a'], alpha=test_alpha) res_b = empyrical.annual_volatility(ret['b'], alpha=test_alpha) res_c = empyrical.annual_volatility(ret['c'], alpha=test_alpha) assert isclose(ret['a'].vbt.returns.annualized_volatility(levy_alpha=test_alpha), res_a) pd.testing.assert_series_equal( ret.vbt.returns.annualized_volatility(levy_alpha=test_alpha), pd.Series([res_a, res_b, res_c], index=ret.columns).rename('annualized_volatility') )
def test_information_ratio(self): res_a = empyrical.excess_sharpe(ret['a'], benchmark_rets['a']) res_b = empyrical.excess_sharpe(ret['b'], benchmark_rets['b']) res_c = empyrical.excess_sharpe(ret['c'], benchmark_rets['c']) assert isclose( ret['a'].vbt.returns.information_ratio(benchmark_rets['a']), res_a) pd.testing.assert_series_equal( ret.vbt.returns.information_ratio(benchmark_rets), pd.Series([res_a, res_b, res_c], index=ret.columns))
def test_alpha(self, test_risk_free): res_a = empyrical.alpha(ret['a'], benchmark_rets['a'], risk_free=test_risk_free) res_b = empyrical.alpha(ret['b'], benchmark_rets['b'], risk_free=test_risk_free) res_c = empyrical.alpha(ret['c'], benchmark_rets['c'], risk_free=test_risk_free) assert isclose(ret['a'].vbt.returns.alpha(benchmark_rets['a'], risk_free=test_risk_free), res_a) pd.testing.assert_series_equal( ret.vbt.returns.alpha(benchmark_rets, risk_free=test_risk_free), pd.Series([res_a, res_b, res_c], index=ret.columns).rename('alpha') )
def test_downside_risk(self, test_required_return): res_a = empyrical.downside_risk(ret['a'], required_return=test_required_return) res_b = empyrical.downside_risk(ret['b'], required_return=test_required_return) res_c = empyrical.downside_risk(ret['c'], required_return=test_required_return) assert isclose(ret['a'].vbt.returns.downside_risk(required_return=test_required_return), res_a) pd.testing.assert_series_equal( ret.vbt.returns.downside_risk(required_return=test_required_return), pd.Series([res_a, res_b, res_c], index=ret.columns).rename('downside_risk') )
def test_total_return(self): res_a = empyrical.cum_returns_final(ret['a']) res_b = empyrical.cum_returns_final(ret['b']) res_c = empyrical.cum_returns_final(ret['c']) assert isclose(ret['a'].vbt.returns.total(), res_a) pd.testing.assert_series_equal( ret.vbt.returns.total(), pd.Series([res_a, res_b, res_c], index=ret.columns).rename('total_return') )
def test_down_capture(self): res_a = empyrical.down_capture(ret['a'], factor_returns['a']) res_b = empyrical.down_capture(ret['b'], factor_returns['b']) res_c = empyrical.down_capture(ret['c'], factor_returns['c']) assert isclose(ret['a'].vbt.returns.down_capture(factor_returns['a']), res_a) pd.testing.assert_series_equal( ret.vbt.returns.down_capture(factor_returns), pd.Series([res_a, res_b, res_c], index=ret.columns))
def test_sharpe_ratio(self, test_risk_free): res_a = empyrical.sharpe_ratio(ret['a'], risk_free=test_risk_free) res_b = empyrical.sharpe_ratio(ret['b'], risk_free=test_risk_free) res_c = empyrical.sharpe_ratio(ret['c'], risk_free=test_risk_free) assert isclose( ret['a'].vbt.returns.sharpe_ratio(risk_free=test_risk_free), res_a) pd.testing.assert_series_equal( ret.vbt.returns.sharpe_ratio(risk_free=test_risk_free), pd.Series([res_a, res_b, res_c], index=ret.columns))
def test_down_capture(self): res_a = empyrical.down_capture(ret['a'], benchmark_rets['a']) res_b = empyrical.down_capture(ret['b'], benchmark_rets['b']) res_c = empyrical.down_capture(ret['c'], benchmark_rets['c']) assert isclose(ret['a'].vbt.returns.down_capture(benchmark_rets['a']), res_a) pd.testing.assert_series_equal( ret.vbt.returns.down_capture(benchmark_rets), pd.Series([res_a, res_b, res_c], index=ret.columns).rename('down_capture') )
def test_drawdowns(self): assert type(ret['a'].vbt.returns.drawdowns) is Drawdowns assert ret['a'].vbt.returns.drawdowns.wrapper.freq == ret['a'].vbt.wrapper.freq assert ret['a'].vbt.returns.drawdowns.wrapper.ndim == ret['a'].ndim assert ret.vbt.returns.drawdowns.wrapper.ndim == ret.ndim assert isclose(ret['a'].vbt.returns.drawdowns.max_drawdown(), ret['a'].vbt.returns.max_drawdown()) pd.testing.assert_series_equal( ret.vbt.returns.drawdowns.max_drawdown(), ret.vbt.returns.max_drawdown() )
def test_value_at_risk(self, test_cutoff): # empyrical can't tolerate NaNs here res_a = empyrical.value_at_risk(ret['a'].iloc[1:], cutoff=test_cutoff) res_b = empyrical.value_at_risk(ret['b'].iloc[1:], cutoff=test_cutoff) res_c = empyrical.value_at_risk(ret['c'].iloc[1:], cutoff=test_cutoff) assert isclose(ret['a'].vbt.returns.value_at_risk(cutoff=test_cutoff), res_a) pd.testing.assert_series_equal( ret.vbt.returns.value_at_risk(cutoff=test_cutoff), pd.Series([res_a, res_b, res_c], index=ret.columns))
def _assert_data(result_data, check_city=True): # city data if check_city: assert result_data['city_id'] == city_info['_id'] assert result_data['city_name'] == city_info['name'] assert result_data['sys']['country'] == city_info['country'] assert isclose( result_data['city_coord']['latitude'], city_info['coord']['lat'], rel_tol=1e-02) assert isclose( result_data['city_coord']['longitude'], city_info['coord']['lon'], rel_tol=1e-02) # current weather data assert isinstance(result_data['dt_value'], dt.datetime) assert result_data['main']['temp'] is not None assert result_data['main']['temp_min'] is not None assert result_data['main']['temp_max'] is not None assert result_data['main']['humidity'] is not None assert result_data['main']['pressure'] is not None
def test_beta(self): res_a = empyrical.beta(ret['a'], benchmark_rets['a']) res_b = empyrical.beta(ret['b'], benchmark_rets['b']) res_c = empyrical.beta(ret['c'], benchmark_rets['c']) assert isclose(ret['a'].vbt.returns.beta(benchmark_rets['a']), res_a) pd.testing.assert_series_equal( ret.vbt.returns.beta(benchmark_rets), pd.Series([res_a, res_b, res_c], index=ret.columns).rename('beta')) pd.testing.assert_series_equal( ret.vbt.returns.rolling_beta(ret.shape[0], benchmark_rets, minp=1).iloc[-1], pd.Series([res_a, res_b, res_c], index=ret.columns).rename(ret.index[-1]))
def test_annualized_return(self): res_a = empyrical.annual_return(ret['a']) res_b = empyrical.annual_return(ret['b']) res_c = empyrical.annual_return(ret['c']) assert isclose(ret['a'].vbt.returns.annualized(), res_a) pd.testing.assert_series_equal( ret.vbt.returns.annualized(), pd.Series([res_a, res_b, res_c], index=ret.columns).rename('annualized_return')) pd.testing.assert_series_equal( ret.vbt.returns.rolling_annualized(ret.shape[0], minp=1).iloc[-1], pd.Series([res_a, res_b, res_c], index=ret.columns).rename(ret.index[-1]))
def test_tail_ratio(self): res_a = empyrical.tail_ratio(ret['a']) res_b = empyrical.tail_ratio(ret['b']) res_c = empyrical.tail_ratio(ret['c']) assert isclose(ret['a'].vbt.returns.tail_ratio(), res_a) pd.testing.assert_series_equal( ret.vbt.returns.tail_ratio(), pd.Series([res_a, res_b, res_c], index=ret.columns).rename('tail_ratio')) pd.testing.assert_series_equal( ret.vbt.returns.rolling_tail_ratio(ret.shape[0], minp=1).iloc[-1], pd.Series([res_a, res_b, res_c], index=ret.columns).rename(ret.index[-1]))
def test_sharpe_ratio(self, test_risk_free): res_a = empyrical.sharpe_ratio(ret['a'], risk_free=test_risk_free) res_b = empyrical.sharpe_ratio(ret['b'], risk_free=test_risk_free) res_c = empyrical.sharpe_ratio(ret['c'], risk_free=test_risk_free) assert isclose( ret['a'].vbt.returns.sharpe_ratio(risk_free=test_risk_free), res_a) pd.testing.assert_series_equal( ret.vbt.returns.sharpe_ratio(risk_free=test_risk_free), pd.Series([res_a, res_b, res_c], index=ret.columns).rename('sharpe_ratio')) pd.testing.assert_series_equal( ret.vbt.returns.rolling_sharpe_ratio( ret.shape[0], minp=1, risk_free=test_risk_free).iloc[-1], pd.Series([res_a, res_b, res_c], index=ret.columns).rename(ret.index[-1]))
def test_total_return(self): assert isclose(rets['a'].vbt.returns.total(), 4.0) pd.testing.assert_series_equal( rets.vbt.returns.total(), pd.Series([4.0, -0.8, 0.0], index=rets.columns, name='total_return')) pd.testing.assert_frame_equal( rets.vbt.returns.rolling_total(), pd.DataFrame( [[np.nan, np.nan, np.nan], [1.0, -0.19999999999999996, 1.0], [2.0, -0.3999999999999999, 2.0], [3.0, -0.6, 1.0], [4.0, -0.8, 0.0]], index=rets.index, columns=rets.columns))
def test_down_capture(self): assert isclose(rets['a'].vbt.returns.down_capture(benchmark_rets['a']), np.nan) pd.testing.assert_series_equal( rets.vbt.returns.down_capture(benchmark_rets), pd.Series([np.nan, np.nan, np.nan], index=rets.columns, name='down_capture')) pd.testing.assert_frame_equal( rets.vbt.returns.rolling_down_capture(benchmark_rets), pd.DataFrame([[np.nan, np.nan, np.nan], [np.nan, 1.0, np.nan], [np.nan, 1.0, np.nan], [np.nan, 1.0, 1.0], [np.nan, np.nan, np.nan]], index=rets.index, columns=rets.columns))
def test_information_ratio(self): res_a = empyrical.excess_sharpe(ret['a'], benchmark_rets['a']) res_b = empyrical.excess_sharpe(ret['b'], benchmark_rets['b']) res_c = empyrical.excess_sharpe(ret['c'], benchmark_rets['c']) assert isclose( ret['a'].vbt.returns.information_ratio(benchmark_rets['a']), res_a) pd.testing.assert_series_equal( ret.vbt.returns.information_ratio(benchmark_rets), pd.Series([res_a, res_b, res_c], index=ret.columns).rename('information_ratio')) pd.testing.assert_series_equal( ret.vbt.returns.rolling_information_ratio(ret.shape[0], benchmark_rets, minp=1).iloc[-1], pd.Series([res_a, res_b, res_c], index=ret.columns).rename(ret.index[-1]))
def test_max_drawdown(self): assert isclose( rets['a'].vbt.returns.max_drawdown(), rets['a'].vbt.returns.drawdowns.max_drawdown(fill_value=0.)) pd.testing.assert_series_equal( rets.vbt.returns.max_drawdown(), rets.vbt.returns.drawdowns.max_drawdown(fill_value=0.)) pd.testing.assert_frame_equal( rets.vbt.returns.rolling_max_drawdown(), pd.DataFrame( [[np.nan, np.nan, np.nan], [0.0, -0.19999999999999996, 0.0], [0.0, -0.3999999999999999, 0.0], [0.0, -0.6, -0.33333333333333337], [0.0, -0.8, -0.6666666666666667]], index=rets.index, columns=rets.columns))
def test_calmar_ratio(self): assert isclose(rets['a'].vbt.returns.calmar_ratio(), np.nan) pd.testing.assert_series_equal( rets.vbt.returns.calmar_ratio(), pd.Series([np.nan, -1.25, 0.0], index=rets.columns, name='calmar_ratio')) pd.testing.assert_frame_equal( rets.vbt.returns.rolling_calmar_ratio(), pd.DataFrame([[np.nan, np.nan, np.nan], [np.nan, -5.000000000000001, np.nan], [np.nan, -2.5000000000000004, np.nan], [np.nan, -1.6666666666666667, 8.833002615989533e+27], [np.nan, -1.25, 0.0]], index=rets.index, columns=rets.columns))
def test_value_at_risk(self, test_cutoff): # empyrical can't tolerate NaN here res_a = empyrical.value_at_risk(ret['a'].iloc[1:], cutoff=test_cutoff) res_b = empyrical.value_at_risk(ret['b'].iloc[1:], cutoff=test_cutoff) res_c = empyrical.value_at_risk(ret['c'].iloc[1:], cutoff=test_cutoff) assert isclose(ret['a'].vbt.returns.value_at_risk(cutoff=test_cutoff), res_a) pd.testing.assert_series_equal( ret.vbt.returns.value_at_risk(cutoff=test_cutoff), pd.Series([res_a, res_b, res_c], index=ret.columns).rename('value_at_risk')) pd.testing.assert_series_equal( ret.vbt.returns.rolling_value_at_risk(ret.shape[0], minp=1, cutoff=test_cutoff).iloc[-1], pd.Series([res_a, res_b, res_c], index=ret.columns).rename(ret.index[-1]))
def test_capture(self): assert isclose(rets['a'].vbt.returns.capture(benchmark_rets['a']), 0.0007435597416888084) pd.testing.assert_series_equal( rets.vbt.returns.capture(benchmark_rets), pd.Series([0.0007435597416888084, 1.0, -0.0], index=rets.columns, name='capture')) pd.testing.assert_frame_equal( rets.vbt.returns.rolling_capture(benchmark_rets), pd.DataFrame([[np.nan, np.nan, np.nan], [1.443034545422564e-07, 1.0, 4.0670014163453153e-66], [6.758314743559073e-07, 1.0, 2.2829041301869233e-75], [9.623155594782632e-06, 1.0, 43620380068493.234], [0.0007435597416888084, 1.0, -0.0]], index=rets.index, columns=rets.columns))