|
| 1 | +import numpy as np |
| 2 | +import pandas as pd |
| 3 | +import pytest |
| 4 | +from pytest import approx |
| 5 | +from pytest import mark |
| 6 | +from numpy.testing import assert_array_equal, assert_allclose |
| 7 | +from pandas.testing import assert_series_equal, assert_frame_equal |
| 8 | + |
| 9 | +import okama as ok |
| 10 | +from okama.common.error import LongRollingWindowLengthError, RollingWindowLengthBelowOneYearError |
| 11 | + |
| 12 | +from tests import conftest |
| 13 | + |
| 14 | +# DCF Methods |
| 15 | +def test_dcf_discount_rate( |
| 16 | + portfolio_cashflows_inflation, portfolio_cashflows_NO_inflation, portfolio_cashflows_NO_inflation_NO_discount_rate |
| 17 | +): |
| 18 | + assert portfolio_cashflows_inflation.discount_rate == approx(0.0554, abs=1e-3) # average inflation |
| 19 | + assert portfolio_cashflows_NO_inflation.discount_rate == approx(0.09, abs=1e-3) # defined discount rate |
| 20 | + assert portfolio_cashflows_NO_inflation_NO_discount_rate.discount_rate == approx(0.05, abs=1e-3) # default rate |
| 21 | + |
| 22 | + |
| 23 | +def test_dcf_wealth_index(portfolio_cashflows_inflation, portfolio_cashflows_NO_inflation): |
| 24 | + assert portfolio_cashflows_inflation.dcf.wealth_index.iloc[-1, 0] == approx(179950.30, rel=1e-2) |
| 25 | + assert portfolio_cashflows_inflation.dcf.wealth_index.iloc[-1, 1] == approx(100050.78, rel=1e-2) |
| 26 | + assert portfolio_cashflows_NO_inflation.dcf.wealth_index.iloc[-1, 0] == approx(152642.54, rel=1e-2) |
| 27 | + |
| 28 | + |
| 29 | +def test_dcf_survival_date(portfolio_cashflows_inflation): |
| 30 | + assert portfolio_cashflows_inflation.dcf.survival_date_hist == pd.to_datetime("2020-01-31") |
| 31 | + |
| 32 | + |
| 33 | +def test_dcf_cashflow_pv(portfolio_cashflows_inflation, portfolio_cashflows_NO_inflation_NO_discount_rate): |
| 34 | + assert portfolio_cashflows_inflation.dcf.cashflow_pv == approx(-76.33, rel=1e-2) |
| 35 | + assert portfolio_cashflows_NO_inflation_NO_discount_rate.dcf.cashflow_pv == approx(-78.35, rel=1e-2) |
| 36 | + |
| 37 | + |
| 38 | +def test_dcf_initial_amount_pv(portfolio_cashflows_inflation, portfolio_cashflows_NO_inflation_NO_discount_rate): |
| 39 | + assert portfolio_cashflows_inflation.dcf.initial_amount_pv == approx(76339.31, rel=1e-2) |
| 40 | + assert portfolio_cashflows_NO_inflation_NO_discount_rate.dcf.initial_amount_pv == approx(78352.61, rel=1e-2) |
| 41 | + |
| 42 | + |
| 43 | +def test_dcf_survival_period(portfolio_cashflows_inflation): |
| 44 | + assert portfolio_cashflows_inflation.dcf.survival_period_hist == approx(5.1, rel=1e-2) |
| 45 | + |
| 46 | + |
| 47 | +@mark.parametrize( |
| 48 | + "distribution, expected", |
| 49 | + [("norm", 93899.64), ("lognorm", 92155.15), ("t", 93123.36)], |
| 50 | +) |
| 51 | +def test_dcf_monte_carlo_wealth(portfolio_cashflows_inflation_large_cf, distribution, expected): |
| 52 | + result = portfolio_cashflows_inflation_large_cf.dcf.monte_carlo_wealth( |
| 53 | + first_value=100_000, distr=distribution, years=1, n=100 |
| 54 | + ) |
| 55 | + assert result.iloc[-1].mean() == approx(expected, rel=1e-1) |
| 56 | + |
| 57 | + |
| 58 | +@mark.parametrize( |
| 59 | + "distribution, expected", |
| 60 | + [("norm", 6.2), ("lognorm", 6.2), ("t", 5.9)], |
| 61 | +) |
| 62 | +def test_dcf_monte_carlo_survival_period(portfolio_cashflows_inflation_large_cf, distribution, expected): |
| 63 | + result = portfolio_cashflows_inflation_large_cf.dcf.monte_carlo_survival_period(distr=distribution, years=25, n=100) |
| 64 | + assert result.mean() == approx(expected, rel=1e-1) |
| 65 | + |
| 66 | + |
| 67 | +def test_find_the_largest_withdrawals_size(): |
| 68 | + assert False |
0 commit comments