Skip to content

Commit

Permalink
test: move PortfolioDCF tests to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
chilango74 committed Oct 8, 2024
1 parent c593a51 commit 1aab23e
Show file tree
Hide file tree
Showing 3 changed files with 681 additions and 209 deletions.
770 changes: 613 additions & 157 deletions main_notebook.ipynb

Large diffs are not rendered by default.

68 changes: 68 additions & 0 deletions tests/test_cash_flow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import numpy as np
import pandas as pd
import pytest
from pytest import approx
from pytest import mark
from numpy.testing import assert_array_equal, assert_allclose
from pandas.testing import assert_series_equal, assert_frame_equal

import okama as ok
from okama.common.error import LongRollingWindowLengthError, RollingWindowLengthBelowOneYearError

from tests import conftest

# DCF Methods
def test_dcf_discount_rate(
portfolio_cashflows_inflation, portfolio_cashflows_NO_inflation, portfolio_cashflows_NO_inflation_NO_discount_rate
):
assert portfolio_cashflows_inflation.discount_rate == approx(0.0554, abs=1e-3) # average inflation
assert portfolio_cashflows_NO_inflation.discount_rate == approx(0.09, abs=1e-3) # defined discount rate
assert portfolio_cashflows_NO_inflation_NO_discount_rate.discount_rate == approx(0.05, abs=1e-3) # default rate


def test_dcf_wealth_index(portfolio_cashflows_inflation, portfolio_cashflows_NO_inflation):
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(152642.54, rel=1e-2)


def test_dcf_survival_date(portfolio_cashflows_inflation):
assert portfolio_cashflows_inflation.dcf.survival_date_hist == pd.to_datetime("2020-01-31")


def test_dcf_cashflow_pv(portfolio_cashflows_inflation, portfolio_cashflows_NO_inflation_NO_discount_rate):
assert portfolio_cashflows_inflation.dcf.cashflow_pv == approx(-76.33, rel=1e-2)
assert portfolio_cashflows_NO_inflation_NO_discount_rate.dcf.cashflow_pv == approx(-78.35, rel=1e-2)


def test_dcf_initial_amount_pv(portfolio_cashflows_inflation, portfolio_cashflows_NO_inflation_NO_discount_rate):
assert portfolio_cashflows_inflation.dcf.initial_amount_pv == approx(76339.31, rel=1e-2)
assert portfolio_cashflows_NO_inflation_NO_discount_rate.dcf.initial_amount_pv == approx(78352.61, rel=1e-2)


def test_dcf_survival_period(portfolio_cashflows_inflation):
assert portfolio_cashflows_inflation.dcf.survival_period_hist == approx(5.1, rel=1e-2)


@mark.parametrize(
"distribution, expected",
[("norm", 93899.64), ("lognorm", 92155.15), ("t", 93123.36)],
)
def test_dcf_monte_carlo_wealth(portfolio_cashflows_inflation_large_cf, distribution, expected):
result = portfolio_cashflows_inflation_large_cf.dcf.monte_carlo_wealth(
first_value=100_000, distr=distribution, years=1, n=100
)
assert result.iloc[-1].mean() == approx(expected, rel=1e-1)


@mark.parametrize(
"distribution, expected",
[("norm", 6.2), ("lognorm", 6.2), ("t", 5.9)],
)
def test_dcf_monte_carlo_survival_period(portfolio_cashflows_inflation_large_cf, distribution, expected):
result = portfolio_cashflows_inflation_large_cf.dcf.monte_carlo_survival_period(distr=distribution, years=25, n=100)
assert result.mean() == approx(expected, rel=1e-1)


def test_find_the_largest_withdrawals_size():
assert False
52 changes: 0 additions & 52 deletions tests/test_portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,55 +375,3 @@ def test_init_portfolio_failing():
with pytest.raises(ValueError, match="Weights sum is not equal to one."):
ok.Portfolio(["RGBITR.INDX", "MCFTR.INDX"], weights=[0.1, 0.2])


# DCF Methods
def test_dcf_discount_rate(
portfolio_cashflows_inflation, portfolio_cashflows_NO_inflation, portfolio_cashflows_NO_inflation_NO_discount_rate
):
assert portfolio_cashflows_inflation.discount_rate == approx(0.0554, abs=1e-3) # average inflation
assert portfolio_cashflows_NO_inflation.discount_rate == approx(0.09, abs=1e-3) # defined discount rate
assert portfolio_cashflows_NO_inflation_NO_discount_rate.discount_rate == approx(0.05, abs=1e-3) # default rate


def test_dcf_wealth_index(portfolio_cashflows_inflation, portfolio_cashflows_NO_inflation):
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(152642.54, rel=1e-2)


def test_dcf_survival_date(portfolio_cashflows_inflation):
assert portfolio_cashflows_inflation.dcf.survival_date_hist == pd.to_datetime("2020-01-31")


def test_dcf_cashflow_pv(portfolio_cashflows_inflation, portfolio_cashflows_NO_inflation_NO_discount_rate):
assert portfolio_cashflows_inflation.dcf.cashflow_pv == approx(-76.33, rel=1e-2)
assert portfolio_cashflows_NO_inflation_NO_discount_rate.dcf.cashflow_pv == approx(-78.35, rel=1e-2)


def test_dcf_initial_amount_pv(portfolio_cashflows_inflation, portfolio_cashflows_NO_inflation_NO_discount_rate):
assert portfolio_cashflows_inflation.dcf.initial_amount_pv == approx(76339.31, rel=1e-2)
assert portfolio_cashflows_NO_inflation_NO_discount_rate.dcf.initial_amount_pv == approx(78352.61, rel=1e-2)


def test_dcf_survival_period(portfolio_cashflows_inflation):
assert portfolio_cashflows_inflation.dcf.survival_period_hist == approx(5.1, rel=1e-2)


@mark.parametrize(
"distribution, expected",
[("norm", 93899.64), ("lognorm", 92155.15), ("t", 93123.36)],
)
def test_dcf_monte_carlo_wealth(portfolio_cashflows_inflation_large_cf, distribution, expected):
result = portfolio_cashflows_inflation_large_cf.dcf.monte_carlo_wealth(
first_value=100_000, distr=distribution, years=1, n=100
)
assert result.iloc[-1].mean() == approx(expected, rel=1e-1)


@mark.parametrize(
"distribution, expected",
[("norm", 6.2), ("lognorm", 6.2), ("t", 5.9)],
)
def test_dcf_monte_carlo_survival_period(portfolio_cashflows_inflation_large_cf, distribution, expected):
result = portfolio_cashflows_inflation_large_cf.dcf.monte_carlo_survival_period(distr=distribution, years=25, n=100)
assert result.mean() == approx(expected, rel=1e-1)

0 comments on commit 1aab23e

Please sign in to comment.