Skip to content

Commit

Permalink
feat: get_wealth_indexes_with_cashflow has use_discounted_values para…
Browse files Browse the repository at this point in the history
…meters
  • Loading branch information
chilango74 committed Oct 10, 2024
1 parent c1df18d commit b2ff033
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
6 changes: 3 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
ind = ok.IndexationStrategy(pf)
ind.initial_investment = 10_000
ind.frequency = "year"
ind.amount = -1_000
ind.amount = -500
ind.indexation = "inflation"

# # TimeSeries strategy
Expand All @@ -44,12 +44,12 @@
# Assign a strategy
pf.dcf.cashflow_parameters = ind
pf.dcf.discount_rate = 0.10
pf.dcf.use_discounted_values = False
pf.dcf.use_discounted_values = True

# Set Monte Carlo
pf.dcf.set_mc_parameters(distribution="t", period=50, number=100)

pf.dcf.plot_forecast_monte_carlo(backtest=False)
pf.dcf.plot_forecast_monte_carlo(backtest=True)

plt.yscale("log") # log or linear
plt.legend("")
Expand Down
8 changes: 4 additions & 4 deletions okama/common/helpers/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,19 +197,19 @@ def get_wealth_indexes_with_cashflow(
portfolio_symbol: Optional[str],
inflation_symbol: Optional[str],
cashflow_parameters: type[CashFlow],
use_discounted_values: bool,
) -> Union[pd.Series, pd.DataFrame]:
"""
Returns wealth index for a series of returns with cash flows (withdrawals/contributions).
Values of the wealth index correspond to the beginning of the month.
"""
# TODO: add use_discounted_values parameters to setup initial conditions without modifying cashflow_parameters
pf_object = cashflow_parameters.parent
dcf_object = cashflow_parameters.parent.dcf
amount = getattr(cashflow_parameters, "amount", None)
period_initial_amount = (
dcf_object.initial_investment_pv
if dcf_object.use_discounted_values
if use_discounted_values
else cashflow_parameters.initial_investment
)
period_initial_amount_cached = period_initial_amount
Expand All @@ -218,7 +218,7 @@ def get_wealth_indexes_with_cashflow(
else:
try:
# amount is not defined in TimeSeriesStrategy & PercentageStrategy
amount = dcf_object.cashflow_pv if dcf_object.use_discounted_values else cashflow_parameters.amount
amount = dcf_object.cashflow_pv if use_discounted_values else cashflow_parameters.amount
except AttributeError:
pass
if isinstance(ror, pd.DataFrame):
Expand All @@ -240,7 +240,7 @@ def get_wealth_indexes_with_cashflow(
elif cashflow_parameters.NAME == "time_series":
try:
cashflow = cashflow_parameters.time_series[date]
if dcf_object.use_discounted_values:
if use_discounted_values:
last_date = pf_object.last_date
first_date = date.to_timestamp(how="End")
period_length = Date.get_period_length(last_date, first_date)
Expand Down
15 changes: 9 additions & 6 deletions okama/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -2505,6 +2505,7 @@ def wealth_index(self) -> pd.DataFrame:
portfolio_symbol=self.parent.symbol,
inflation_symbol=infl,
cashflow_parameters=self.cashflow_parameters,
use_discounted_values=self.use_discounted_values,
)
self._wealth_index = self.parent._make_df_if_series(df)
return self._wealth_index
Expand Down Expand Up @@ -2553,6 +2554,7 @@ def wealth_index_with_assets(self) -> pd.DataFrame:
None, # symbol
None, # inflation_symbol
self.cashflow_parameters,
self.use_discounted_values
),
)
return wealth_df
Expand Down Expand Up @@ -2776,6 +2778,7 @@ def monte_carlo_wealth(self) -> pd.DataFrame:
None, # portfolio_symbol
None, # inflation_symbol
self.cashflow_parameters,
False, # use_discounted_values
),
)

Expand Down Expand Up @@ -2876,12 +2879,12 @@ def plot_forecast_monte_carlo(
>>> plt.yscale("log") # Y-axis has logarithmic scale
>>> plt.show()
"""
backup_obj = self.cashflow_parameters
backup = self.use_discounted_values
self.use_discounted_values = False # we need to start with not discounted values
if backtest:
if self.cashflow_parameters is None:
raise AttributeError("'cashflow_parameters' is not defined.")
backup_obj = self.cashflow_parameters
backup = self.use_discounted_values
self.use_discounted_values = False # we need to start with not discounted values
s1 = self.wealth_index[self.parent.symbol]
s1.plot(legend=None, figsize=figsize)
last_backtest_value = s1.iloc[-1]
Expand All @@ -2895,12 +2898,12 @@ def plot_forecast_monte_carlo(
s2 = self.monte_carlo_wealth
for s in s2:
s2[s].plot(legend=None)
self.cashflow_parameters = backup_obj
self.use_discounted_values = backup
self.cashflow_parameters._clear_cf_cache()
else:
s2 = self.monte_carlo_wealth
s2.plot(legend=None)
self.cashflow_parameters = backup_obj
self.cashflow_parameters._clear_cf_cache()
self.use_discounted_values = backup

def monte_carlo_survival_period(self, threshold: float = 0) -> pd.Series:
"""
Expand Down

0 comments on commit b2ff033

Please sign in to comment.