Skip to content

Commit

Permalink
chore: change .dropna() to .iloc[1:] in all expanding risk time series
Browse files Browse the repository at this point in the history
dropna() is not a good solution as it may loose information
  • Loading branch information
chilango74 committed Jan 25, 2024
1 parent ffd4763 commit 888a7ac
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 27 deletions.
10 changes: 5 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@

# Rolling / Expanding Risk
al = ok.AssetList(['SPY.US',
'BND.US'
# 'BND.US'
])
# al.risk_annual.plot()
#
# plt.show()

pf = ok.Portfolio(['SPY.US',
'BND.US'
])
print(pf.describe())
# pf = ok.Portfolio(['SPY.US',
# 'BND.US'
# ])
print(type(al.risk_monthly))
30 changes: 18 additions & 12 deletions okama/asset_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def wealth_indexes(self) -> pd.DataFrame:
return helpers.Frame.get_wealth_indexes(df)

@property
def risk_monthly(self) -> pd.Series:
def risk_monthly(self) -> pd.DataFrame:
"""
Calculate monthly risk expanding time series for each asset.
Expand All @@ -89,12 +89,12 @@ def risk_monthly(self) -> pd.Series:
Returns
-------
Series
Monthly risk (standard deviation) values for each asset in form of Series.
DataFrame
Monthly risk (standard deviation) expanding time series for each asset in form of Series.
See Also
--------
risk_annual : Calculate annualized risks.
risk_annual : Calculate annualized risks expanding time series.
semideviation_monthly : Calculate semideviation monthly values.
semideviation_annual : Calculate semideviation annualized values.
get_var_historic : Calculate historic Value at Risk (VaR).
Expand All @@ -105,11 +105,17 @@ def risk_monthly(self) -> pd.Series:
--------
>>> al = ok.AssetList(['GC.COMM', 'SHV.US'], ccy='USD', last_date='2021-01')
>>> al.risk_monthly
GC.COMM 0.050864
SHV.US 0.001419
dtype: float64
Symbols GC.COMM SHV.US
date
2007-03 0.025668 0.000141
2007-04 0.020872 0.000153
2007-05 0.027513 0.000451
2007-06 0.025988 0.000406
... ...
2020-09 0.051006 0.001380
2020-10 0.050861 0.001377
"""
return self.assets_ror.expanding().std()
return self.assets_ror.expanding().std().iloc[1:]

@property
def risk_annual(self) -> pd.DataFrame:
Expand All @@ -123,12 +129,12 @@ def risk_annual(self) -> pd.DataFrame:
Returns
-------
Series
Annualized risk (standard deviation) values for each asset in form of Series.
DataFrame
Annualized risk (standard deviation) expanding time series for each asset.
See Also
--------
risk_monthly : Calculate montly risk for each asset.
risk_monthly : Calculate montly risk expanding time series for each asset.
risk_annual : Calculate annualized risks.
semideviation_monthly : Calculate semideviation monthly values.
semideviation_annual : Calculate semideviation annualized values.
Expand All @@ -152,7 +158,7 @@ def risk_annual(self) -> pd.DataFrame:
"""
risk_ts = self.assets_ror.expanding().std()
mean_return_ts = self.assets_ror.expanding().mean()
return helpers.Float.annualize_risk(risk_ts, mean_return_ts).dropna()
return helpers.Float.annualize_risk(risk_ts, mean_return_ts).iloc[1:]

@property
def semideviation_monthly(self) -> pd.Series:
Expand Down
41 changes: 31 additions & 10 deletions okama/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,15 +986,15 @@ def real_mean_return(self) -> float:
@property
def risk_monthly(self) -> pd.Series:
"""
Calculate monthly risk (standard deviation of return) for Portfolio.
Calculate monthly risk expanding time series for Portfolio.
Monthly risk of portfolio is a standard deviation of the rate of return time series.
Standard deviation (sigma σ) is normalized by N-1.
Returns
-------
float
Standard deviation value of the monthly return time series.
Series
Standard deviation of the monthly return expanding time series.
See Also
--------
Expand All @@ -1009,34 +1009,55 @@ def risk_monthly(self) -> pd.Series:
--------
>>> pf = ok.Portfolio(['MSFT.US', 'AAPL.US'])
>>> pf.risk_monthly
0.09415483565833212
date
1986-05 0.020117
1986-06 0.122032
1986-07 0.130113
1986-08 0.116642
...
2023-08 0.092875
2023-09 0.092861
2023-10 0.092759
2023-11 0.092763
2023-12 0.092665
Freq: M, Name: portfolio_1094.PF, Length: 453, dtype: float64
"""
return self.ror.expanding().std()
return self.ror.expanding().std().iloc[1:]

@property
def risk_annual(self) -> pd.Series:
"""
Calculate annualized risk expanding time series for portfolio.
Risk in this case is a standard deviation of return.
Risk is a standard deviation of the rate of return.
Annualized risk is calculated for rate of retirun time series for the sample from 'first_date' to
'last_date'.
Returns
-------
float
Annualized standard deviation value of the monthly return time series.
Series
Annualized standard deviation of the monthly return expanding time series.
Examples
--------
>>> pf = ok.Portfolio(['MSFT.US', 'AAPL.US'])
>>> pf.risk_annual
0.4374591902169046
date
1986-05 0.285175
1986-06 0.890909
1986-07 0.616876
1986-08 0.632270
1986-09 0.509642
...
2023-08 0.428297
2023-09 0.427350
2023-10 0.426961
2023-11 0.427930
"""
risk_ts = self.ror.expanding().std()
mean_return_ts = self.ror.expanding().mean()
return helpers.Float.annualize_risk(risk_ts, mean_return_ts).dropna()
return helpers.Float.annualize_risk(risk_ts, mean_return_ts).iloc[1:]

@property
def semideviation_monthly(self) -> float:
Expand Down

0 comments on commit 888a7ac

Please sign in to comment.