Skip to content

Commit

Permalink
test: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chilango74 committed Jul 4, 2024
1 parent 70dac41 commit 3ee7576
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 37 deletions.
6 changes: 3 additions & 3 deletions okama/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -1609,7 +1609,7 @@ def monte_carlo_returns_ts(self, distr: str = "norm", years: int = 1, n: int = 1
raise ValueError('"distr" must be "norm" (default), "lognorm" or "t".')
return pd.DataFrame(data=random_returns, index=ts_index)

def _monte_carlo_wealth(self, distr: str = "norm", years: int = 1, n: int = 100) -> pd.DataFrame:
def monte_carlo_wealth(self, distr: str = "norm", years: int = 1, n: int = 100) -> pd.DataFrame:
"""
Generate portfolio wealth index with Monte Carlo simulation.
Expand Down Expand Up @@ -1791,7 +1791,7 @@ def percentile_wealth(
results = self.percentile_wealth_history(years=years, percentiles=percentiles).iloc[-1].to_dict()
elif distr in ["norm", "lognorm", "t"]:
results = {}
wealth_indexes = self._monte_carlo_wealth(distr=distr, years=years, n=n)
wealth_indexes = self.monte_carlo_wealth(distr=distr, years=years, n=n)
for percentile in percentiles:
value = wealth_indexes.iloc[-1, :].quantile(percentile / 100)
results.update({percentile: value})
Expand Down Expand Up @@ -2333,7 +2333,7 @@ def plot_forecast_monte_carlo(
>>> plt.show()
"""
s1 = self.wealth_index
s2 = self._monte_carlo_wealth(distr=distr, years=years, n=n)
s2 = self.monte_carlo_wealth(distr=distr, years=years, n=n)
s1[self.symbol].plot(legend=None, figsize=figsize)
for n in s2:
s2[n].plot(legend=None)
Expand Down
5 changes: 1 addition & 4 deletions tests/test_asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@ def test_get_symbol_data(init_asset_spy, init_asset_berkshire):


def test_usdrub(init_asset_usdrub):
# close_daily_sample = pd.read_pickle(conftest.data_folder / "usdrub_close_daily.pkl")
close_monthly_sample = pd.read_pickle(conftest.data_folder / "usdrub_close_monthly.pkl")
# adj_close_sample = pd.read_pickle(conftest.data_folder / "usdrub_adj_close.pkl")
# assert_series_equal(init_asset_usdrub.close_daily["2019-01":"2020-01"], close_daily_sample, rtol=1e-2)
assert_series_equal(init_asset_usdrub.close_monthly["2019-01":"2020-01"], close_monthly_sample, rtol=1e-1)
# assert_series_equal(init_asset_usdrub.adj_close["2019-01":"2020-01"], adj_close_sample, rtol=1e-2)


@mark.xfail
def test_eurusd(init_asset_eurusd):
close_daily_sample = pd.read_pickle(conftest.data_folder / "eurusd_close_daily.pkl")
assert_series_equal(init_asset_eurusd.close_daily["2019-01":"2020-01"], close_daily_sample, rtol=1e-2)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_asset_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_currencies(self):
}
assert self.currencies.names == {
"RUBUSD.FX": "Central Bank of Russia rate for RUBUSD (US Dollar)",
"EURUSD.FX": "EURUSD",
"EURUSD.FX": "EUR/USD",
"CNYUSD.FX": "Chinese Renminbi/US Dollar FX Cross Rate",
}
assert self.currencies.describe().iloc[1, -1] == approx(0.02485, rel=1e-2)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_frontier_reb.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_repr(init_efficient_frontier_reb):
def test_gmv_annual_weights(init_efficient_frontier_reb):
assert_allclose(
init_efficient_frontier_reb.gmv_annual_weights,
np.array([0.447667, 0.552333]),
np.array([0.384194, 0.615806]),
rtol=1e-2,
atol=1e-2,
)
Expand All @@ -52,10 +52,10 @@ def test_gmv_annual_values(init_efficient_frontier_reb):
@mark.rebalance
@mark.frontier
def test_max_return(init_efficient_frontier_reb):
assert init_efficient_frontier_reb.global_max_return_portfolio["CAGR"] == approx(0.1623, abs=1e-2)
assert init_efficient_frontier_reb.global_max_return_portfolio["CAGR"] == approx(0.1889, abs=1e-2)


@mark.rebalance
@mark.frontier
def test_ef_points_reb(init_efficient_frontier_reb):
assert init_efficient_frontier_reb.ef_points["CAGR"].iloc[1] == approx(0.1623, abs=1e-2)
assert init_efficient_frontier_reb.ef_points["CAGR"].iloc[1] == approx(0.1889, abs=1e-2)
28 changes: 14 additions & 14 deletions tests/test_macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ def test_get_infl_eur_data(self):
assert self.infl_eur.type == "inflation"

def test_cumulative_inflation(self):
assert self.infl_rub.cumulative_inflation[-1] == approx(19576.47386585591, rel=1e-4)
assert self.infl_eur.cumulative_inflation[-1] == approx(0.20267532488218776, abs=1e-4)
assert self.infl_usd.cumulative_inflation[-1] == approx(0.7145424753209466, abs=1e-4)
assert self.infl_rub.cumulative_inflation.iloc[-1] == approx(19576.47386585591, rel=1e-4)
assert self.infl_eur.cumulative_inflation.iloc[-1] == approx(0.20267532488218776, abs=1e-4)
assert self.infl_usd.cumulative_inflation.iloc[-1] == approx(0.7145424753209466, abs=1e-4)

def test_purchasing_power_1000(self):
assert self.infl_usd.purchasing_power_1000 == approx(583.2459763429362, rel=1e-4)
Expand All @@ -54,19 +54,19 @@ def test_rolling_inflation_fails(self):
self.infl_usd_less_year.rolling_inflation

def test_rolling_inflation(self):
assert self.infl_eur.rolling_inflation[-1] == approx(0.02317927930197139, abs=1e-4)
assert self.infl_usd.rolling_inflation[-1] == approx(-0.0058137, abs=1e-4)
assert self.infl_rub.rolling_inflation[-1] == approx(0.2070533602100877, abs=1e-4)
assert self.infl_eur.rolling_inflation.iloc[-1] == approx(0.02317927930197139, abs=1e-4)
assert self.infl_usd.rolling_inflation.iloc[-1] == approx(-0.0058137, abs=1e-4)
assert self.infl_rub.rolling_inflation.iloc[-1] == approx(0.2070533602100877, abs=1e-4)

def test_annual_inflation_ts(self):
assert self.infl_rub.annual_inflation_ts.iloc[-1] == approx(0.0276, abs=1e-4)
assert self.infl_usd.annual_inflation_ts[-1] == approx(-0.0059, abs=1e-4)
assert self.infl_eur.annual_inflation_ts[-1] == approx(-0.002015, abs=1e-4)
assert self.infl_usd.annual_inflation_ts.iloc[-1] == approx(-0.0059, abs=1e-4)
assert self.infl_eur.annual_inflation_ts.iloc[-1] == approx(-0.002015, abs=1e-4)

def test_values_monthly(self):
assert self.infl_eur.values_monthly[-1] == approx(0.003, abs=1e-4)
assert self.infl_usd.values_monthly[-1] == approx(-0.0059, abs=1e-4)
assert self.infl_rub.values_monthly[-1] == approx(0.0276, abs=1e-4)
assert self.infl_eur.values_monthly.iloc[-1] == approx(0.003, abs=1e-4)
assert self.infl_usd.values_monthly.iloc[-1] == approx(-0.0059, abs=1e-4)
assert self.infl_rub.values_monthly.iloc[-1] == approx(0.0276, abs=1e-4)

error_case_ids = ["invalid_date_format", "nonexistent_date", "invalid_value_type"]

Expand Down Expand Up @@ -108,10 +108,10 @@ def test_rates_init(self):
assert self.rates_rub.last_date == pd.to_datetime("2020-02")

def test_values_monthly(self):
assert self.rates_rub.values_monthly[-1] == 0.0639
assert self.rates_rub.values_monthly.iloc[-1] == 0.0639

def test_values_daily(self):
assert self.rates_ruonia.values_daily[-1] == 0.0605
assert self.rates_ruonia.values_daily.iloc[-1] == 0.0605
assert self.rates_ruonia.values_daily.shape[0] == 1846
assert self.rates_cbr_rate.values_daily.shape[0] == 62 # RUS_CBR.RATE has only monthly values

Expand All @@ -136,4 +136,4 @@ def test_indicator_init(self):
assert self.cape10_usd.last_date == pd.to_datetime("2022-02")

def test_values_monthly(self):
assert self.cape10_usd.values_monthly[-1] == approx(34.93, rel=1e-4)
assert self.cape10_usd.values_monthly.iloc[-1] == approx(34.93, rel=1e-4)
24 changes: 12 additions & 12 deletions tests/test_portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_wealth_index(portfolio_rebalanced_year):

def test_wealth_index_with_assets(portfolio_rebalanced_year, portfolio_no_inflation):
result = portfolio_rebalanced_year.wealth_index_with_assets.iloc[-1, :].values
assert_allclose(np.array(result), np.array([2259.244689, 2056.11199, 2889.930097, 1310.606208]), rtol=1e-02)
assert_allclose(np.array(result), np.array([2462.161392, 2056.11199, 2889.930097, 1310.606208]), rtol=1e-02)


def test_weights(portfolio_rebalanced_month):
Expand Down Expand Up @@ -114,37 +114,37 @@ def test_assets_close_monthly(portfolio_not_rebalanced):


def test_close_monthly(portfolio_not_rebalanced):
assert portfolio_not_rebalanced.close_monthly.iloc[-1] == approx(2269.20, rel=1e-2)
assert portfolio_not_rebalanced.close_monthly.iloc[-1] == approx(2473.0, rel=1e-2)


def test_get_assets_dividends(portfolio_dividends):
assert portfolio_dividends._get_assets_dividends().iloc[-1, 0] == approx(0, abs=1e-2)
# T.US 2020-01=$0.3927 , RUBUSD=63.03 ( http://joxi.ru/823dnYWizBvEOA )
# T.US 2020-01=$0.5200 , RUBUSD=63.03 ( http://joxi.ru/Grqjdaliz5Ow9m ) 04.09.2022
# T.US 2020-01-09, 0.5200 from EOD
assert portfolio_dividends._get_assets_dividends().iloc[-1, 1] == approx(32.77, rel=1e-2)
assert portfolio_dividends._get_assets_dividends().iloc[-1, 1] == approx(24.75, rel=1e-2)
assert portfolio_dividends._get_assets_dividends().iloc[-1, 2] == approx(0, rel=1e-2)


def test_number_of_securities(portfolio_not_rebalanced, portfolio_dividends):
assert portfolio_not_rebalanced.number_of_securities.iloc[-1, 0] == approx(1.6312, rel=1e-2) # RGBITR.INDX
assert portfolio_not_rebalanced.number_of_securities.iloc[-1, 1] == approx(0.2527, abs=1e-2) # MCFTR.INDX
assert portfolio_not_rebalanced.number_of_securities.iloc[-1, 0] == approx(1.7777, rel=1e-2) # RGBITR.INDX
assert portfolio_not_rebalanced.number_of_securities.iloc[-1, 1] == approx(0.2754, abs=1e-2) # MCFTR.INDX
# with dividends
assert portfolio_dividends.number_of_securities.iloc[-1, 0] == approx(3.63, rel=1e-2) # SBER.MOEX
assert portfolio_dividends.number_of_securities.iloc[-1, 1] == approx(0.3892, abs=1e-2) # T.US
assert portfolio_dividends.number_of_securities.iloc[-1, 0] == approx(4.12, rel=1e-2) # SBER.MOEX
assert portfolio_dividends.number_of_securities.iloc[-1, 1] == approx(0.5832, abs=1e-2) # T.US
assert portfolio_dividends.number_of_securities.iloc[-1, 2] == approx(0.004137, abs=1e-2) # GNS.LSE


def test_dividends(portfolio_dividends):
assert portfolio_dividends.dividends.iloc[-1] == approx(12.75, rel=1e-2)
assert portfolio_dividends.dividends.iloc[-1] == approx(14.43, rel=1e-2)


def test_dividend_yield(portfolio_dividends):
assert portfolio_dividends.dividend_yield.iloc[-1] == approx(0.0396, abs=1e-2)


def test_dividends_annual(portfolio_dividends):
assert portfolio_dividends.dividends_annual.iloc[-1].sum() == approx(32.778668, rel=1e-3)
assert portfolio_dividends.dividends_annual.iloc[-1].sum() == approx(24.754197, rel=1e-3)


def test_dividend_yield_annual(portfolio_dividends):
Expand Down Expand Up @@ -307,7 +307,7 @@ def test_get_rolling_cagr_failing_no_inflation(portfolio_no_inflation):


def test_monte_carlo_wealth(portfolio_rebalanced_month):
df = portfolio_rebalanced_month._monte_carlo_wealth(distr="norm", years=1, n=1000)
df = portfolio_rebalanced_month.monte_carlo_wealth(distr="norm", years=1, n=1000)
assert df.shape == (12, 1000)
assert df.iloc[-1, :].mean() == approx(2915.55, rel=1e-1)

Expand Down Expand Up @@ -385,9 +385,9 @@ def test_dcf_discount_rate(


def test_dcf_wealth_index(portfolio_cashflows_inflation, portfolio_cashflows_NO_inflation):
assert portfolio_cashflows_inflation.dcf.wealth_index.iloc[-1, 0] == approx(164459.78, rel=1e-2)
assert portfolio_cashflows_inflation.dcf.wealth_index.iloc[-1, 0] == approx(179950.30, rel=1e-2)
assert portfolio_cashflows_inflation.dcf.wealth_index.iloc[-1, 1] == approx(100050.78, rel=1e-2)
assert portfolio_cashflows_NO_inflation.dcf.wealth_index.iloc[-1, 0] == approx(139454.34, rel=1e-2)
assert portfolio_cashflows_NO_inflation.dcf.wealth_index.iloc[-1, 0] == approx(152642.54, rel=1e-2)


def test_survival_period(portfolio_cashflows_inflation):
Expand Down

0 comments on commit 3ee7576

Please sign in to comment.