Skip to content

Commit

Permalink
refactor: rename Rebalance.return_ts to return_ror_ts
Browse files Browse the repository at this point in the history
  • Loading branch information
chilango74 committed Feb 22, 2024
1 parent ed18fb8 commit ed5245e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 27 deletions.
28 changes: 14 additions & 14 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@
discount_rate=0.01
)

print(portf.discount_rate)
print(portf)
print(f"{portf.get_cagr()}")
print(f"{portf.initial_amount_pv=}, {portf.cashflow_pv=}")
print(f"{portf.survival_period=}")
print(f"{portf.survival_date=}")
portf.wealth_index.plot()

portf.plot_forecast_monte_carlo(distr="norm", years=30, backtest=True, n=100)
# print(portf.discount_rate)
# print(portf)
# print(f"{portf.get_cagr()}")
# print(f"{portf.initial_amount_pv=}, {portf.cashflow_pv=}")
# print(f"{portf.survival_period=}")
# print(f"{portf.survival_date=}")
# portf.wealth_index.plot()
#
# portf.plot_forecast_monte_carlo(distr="norm", years=30, backtest=True, n=100)

s_periods = portf.monte_carlo_survival_period(distr="lognorm", years=25, n=100)
s_periods = portf.monte_carlo_survival_period(distr="lognorm", years=25, n=10)
print(f"медиана {s_periods.quantile(50 / 100)}")
print(f"первый порцентиль {s_periods.quantile(1 / 100)}")
print(f"99й порцентиль {s_periods.quantile(99 / 100)}")
print(f"минимум {s_periods.min()}")
print(f"среднее {s_periods.mean()}")
# print(f"первый порцентиль {s_periods.quantile(1 / 100)}")
# print(f"99й порцентиль {s_periods.quantile(99 / 100)}")
# print(f"минимум {s_periods.min()}")
# print(f"среднее {s_periods.mean()}")

plt.show()

Expand Down
2 changes: 1 addition & 1 deletion okama/common/helpers/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ def assets_weights_ts(self, weights: list, ror: pd.DataFrame) -> pd.DataFrame:
portfolio_wealth_index = self.wealth_ts(weights=weights, ror=ror)
return assets_wealth_indexes.divide(portfolio_wealth_index, axis=0)

def return_ts(self, weights: Union[list, np.ndarray], ror: pd.DataFrame) -> pd.Series:
def return_ror_ts(self, weights: Union[list, np.ndarray], ror: pd.DataFrame) -> pd.Series:
"""
Return monthly rate of return time series of rebalanced portfolio given returns time series of the assets.
Default rebalancing period is a Year (end of year)
Expand Down
2 changes: 1 addition & 1 deletion okama/common/make_asset_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def __init__(
# Add inflation to the date range dict
self.assets_first_dates.update({self.inflation: macro.Inflation(self.inflation).first_date})
self.assets_last_dates.update({self.inflation: macro.Inflation(self.inflation).last_date})
self.assets_ror: pd.DataFrame = self.assets_ror[self.first_date : self.last_date]
self.assets_ror: pd.DataFrame = self.assets_ror[self.first_date: self.last_date]
self.period_length: float = round((self.last_date - self.first_date) / np.timedelta64(365, "D"), ndigits=1)
self.pl = settings.PeriodLength(
self.assets_ror.shape[0] // settings._MONTHS_PER_YEAR,
Expand Down
20 changes: 10 additions & 10 deletions okama/frontier/multi_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def gmv_monthly_weights(self) -> np.ndarray:

# Set the objective function
def objective_function(w):
risk = helpers.Rebalance(period=period).return_ts(w, ror).std()
risk = helpers.Rebalance(period=period).return_ror_ts(w, ror).std()
return risk

# construct the constraints
Expand Down Expand Up @@ -272,7 +272,7 @@ def gmv_annual_weights(self) -> np.ndarray:

# Set the objective function
def objective_function(w):
ts = helpers.Rebalance(period=period).return_ts(w, ror)
ts = helpers.Rebalance(period=period).return_ror_ts(w, ror)
mean_return = ts.mean()
risk = ts.std()
return helpers.Float.annualize_risk(risk=risk, mean_return=mean_return)
Expand All @@ -297,13 +297,13 @@ def _get_gmv_monthly(self) -> Tuple[float, float]:
"""
return (
helpers.Rebalance(period=self.rebalancing_period)
.return_ts(
.return_ror_ts(
self.gmv_monthly_weights,
self.assets_ror,
)
.std(),
helpers.Rebalance(period=self.rebalancing_period)
.return_ts(
.return_ror_ts(
self.gmv_monthly_weights,
self.assets_ror,
)
Expand Down Expand Up @@ -332,7 +332,7 @@ def gmv_annual_values(self) -> Tuple[float, float]:
>>> frontier.gmv_annual_values
(0.03695845106087943, 0.04418318557516887)
"""
returns = helpers.Rebalance(period=self.rebalancing_period).return_ts(self.gmv_annual_weights, self.assets_ror)
returns = helpers.Rebalance(period=self.rebalancing_period).return_ror_ts(self.gmv_annual_weights, self.assets_ror)
return (
helpers.Float.annualize_risk(returns.std(), returns.mean()),
(returns + 1.0).prod() ** (settings._MONTHS_PER_YEAR / returns.shape[0]) - 1.0,
Expand Down Expand Up @@ -369,7 +369,7 @@ def global_max_return_portfolio(self) -> dict:
# Set the objective function
def objective_function(w):
# Accumulated return for rebalanced portfolio time series
objective_function.returns = helpers.Rebalance(period=period).return_ts(w, ror)
objective_function.returns = helpers.Rebalance(period=period).return_ror_ts(w, ror)
accumulated_return = (objective_function.returns + 1.0).prod() - 1.0
return -accumulated_return

Expand All @@ -395,7 +395,7 @@ def objective_function(w):
return point

def _get_cagr(self, weights):
ts = helpers.Rebalance(period=self.rebalancing_period).return_ts(weights, self.assets_ror)
ts = helpers.Rebalance(period=self.rebalancing_period).return_ror_ts(weights, self.assets_ror)
acc_return = (ts + 1.0).prod() - 1.0
return (1.0 + acc_return) ** (settings._MONTHS_PER_YEAR / ts.shape[0]) - 1.0

Expand Down Expand Up @@ -426,7 +426,7 @@ def minimize_risk(self, target_value: float) -> Dict[str, float]:

def objective_function(w):
# annual risk
ts = helpers.Rebalance(period=self.rebalancing_period).return_ts(w, self.assets_ror)
ts = helpers.Rebalance(period=self.rebalancing_period).return_ror_ts(w, self.assets_ror)
risk_monthly = ts.std()
mean_return = ts.mean()
return helpers.Float.annualize_risk(risk_monthly, mean_return)
Expand Down Expand Up @@ -482,7 +482,7 @@ def _maximize_risk(self, target_return: float) -> Dict[str, float]:

def objective_function(w):
# annual risk
ts = helpers.Rebalance(period=self.rebalancing_period).return_ts(w, self.assets_ror)
ts = helpers.Rebalance(period=self.rebalancing_period).return_ror_ts(w, self.assets_ror)
risk_monthly = ts.std()
mean_return = ts.mean()
result = -helpers.Float.annualize_risk(risk_monthly, mean_return)
Expand Down Expand Up @@ -799,7 +799,7 @@ def get_monte_carlo(self, n: int = 100) -> pd.DataFrame:

# Portfolio risk and cagr for each set of weights
portfolios_ror = weights_df.aggregate(
helpers.Rebalance(period=self.rebalancing_period).return_ts,
helpers.Rebalance(period=self.rebalancing_period).return_ror_ts,
ror=self.assets_ror,
)
random_portfolios = pd.DataFrame()
Expand Down
2 changes: 1 addition & 1 deletion okama/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def ror(self) -> pd.Series:
if self.rebalancing_period == "month":
s = helpers.Frame.get_portfolio_return_ts(self.weights, self.assets_ror)
else:
s = helpers.Rebalance(period=self.rebalancing_period).return_ts(self.weights, self.assets_ror)
s = helpers.Rebalance(period=self.rebalancing_period).return_ror_ts(self.weights, self.assets_ror)
return s.rename(self.symbol, inplace=True)

@property
Expand Down

0 comments on commit ed5245e

Please sign in to comment.