Skip to content

Commit

Permalink
feat: redefine dividend_yield for Portfolio
Browse files Browse the repository at this point in the history
Portfolio dividend yield calculated as a weighted sum of the assets dividend yields (adjusted to the portfolio base currency).
  • Loading branch information
chilango74 committed May 30, 2021
1 parent 1df9d2e commit 245e3c7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 30 deletions.
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import okama as ok
x = ok.Portfolio(['SBERP.MOEX'], ccy='RUB', inflation=True)
print(x.assets_dividend_yield)
y = ok.AssetList(['T.US', 'CLDN.LSE'], ccy='GBP')
print(y.assets_ror)
5 changes: 4 additions & 1 deletion okama/common/make_asset_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,10 @@ def assets_dividend_yield(self) -> pd.DataFrame:
"""
Calculate last twelve months (LTM) dividend yield time series (monthly) for each asset.
All yields are calculated in the asset list base currency after adjusting the dividends time series.
LTM dividend yield is the sum trailing twelve months of common dividends per share divided by
the current price per share.
All yields are calculated in the asset list base currency after adjusting the dividends and price time series.
Forecasted (future) dividends are removed.
Zero value time series are created for assets without dividends.
Expand Down
52 changes: 34 additions & 18 deletions okama/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __init__(
self.assets_weights = dict(zip(self.symbols, self.weights))
self._rebalancing_period = None
self.rebalancing_period = rebalancing_period
self._symbol = symbol if symbol else f'portfolio_{randint(1000, 9999)}.PF'
self._symbol = symbol or f'portfolio_{randint(1000, 9999)}.PF'

def __repr__(self):
dic = {
Expand Down Expand Up @@ -128,7 +128,10 @@ def weights_ts(self) -> pd.DataFrame:
-------
DataFrame
"""
return Rebalance.assets_weights_ts(ror=self.assets_ror, period=self.rebalancing_period, weights=self.weights)
if self.rebalancing_period != 'month':
return Rebalance.assets_weights_ts(ror=self.assets_ror, period=self.rebalancing_period, weights=self.weights)
values = np.tile(self.weights, (self.ror.shape[0], 1))
return pd.DataFrame(values, index=self.ror.index, columns=self.symbols)

@property
def rebalancing_period(self) -> str:
Expand Down Expand Up @@ -491,23 +494,36 @@ def annual_return_ts(self) -> pd.DataFrame:
@property
def dividend_yield(self) -> pd.Series:
"""
Calculates dividend yield time series in all base currencies of portfolio assets.
For every currency dividend yield is a weighted sum of the assets dividend yields.
# TODO: finish the docstring
"""
div_yield_assets = self.assets_dividend_yield
Calculate last twelve months (LTM) dividend yield time series (monthly) for the portfolio.
for currency in currencies_list:
assets_with_the_same_currency = [
x for x in currencies_dict if currencies_dict[x] == currency
]
weights = self.weights_ts[assets_with_the_same_currency]
normalized_weights = weights.divide(weights.sum(axis=1), axis=0)
df = div_yield_assets[assets_with_the_same_currency] @ normalized_weights.T
div_yield_series = pd.Series(np.diag(df), index=df.index)
div_yield_series.rename(currency, inplace=True)
div_yield_df = pd.concat([div_yield_df, div_yield_series], axis=1)
return div_yield_df
Portfolio dividend yield is a weighted sum of the assets dividend yields (adjusted to
the portfolio base currency).
For an asset LTM dividend yield is the sum trailing twelve months of common dividends per share divided by
the current price per share.
Returns
-------
Series
Portfolio LTM dividend yield monthly time series.
Examples
--------
>>> pf = ok.Portfolio(['T.US', 'XOM.US'], weights=[0.8, 0.2], first_date='2010-01', last_date='2021-01', ccy='USD')
>>> pf.dividend_yield
2010-01 0.013249
2010-02 0.014835
2010-03 0.014257
...
2020-11 0.076132
2020-12 0.074743
2021-01 0.073643
Freq: M, Name: LTM dividend yild, Length: 133, dtype: float64
"""
df = self.assets_dividend_yield @ self.weights_ts.T
div_yield_series = pd.Series(np.diag(df), index=df.index)
div_yield_series.rename('LTM dividend yield', inplace=True)
return div_yield_series

@property
def real_mean_return(self) -> float:
Expand Down
11 changes: 2 additions & 9 deletions tests/test_portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,9 @@ def test_get_rolling_cumulative_return(portfolio_rebalanced_month):
] == approx(0.1226, rel=1e-2)


@mark.xfail
def test_dividend_yield(portfolio_dividends):
assert portfolio_dividends.assets_dividend_yield["USD"].iloc[-1] == approx(
0.0544, rel=1e-2
)
assert portfolio_dividends.assets_dividend_yield["GBX"].iloc[-1] == approx(
8.9935e-05, rel=1e-2
)
assert portfolio_dividends.assets_dividend_yield["RUB"].iloc[-1] == approx(
0.06344, rel=1e-2
assert portfolio_dividends.dividend_yield.iloc[-1] == approx(
0.03949, rel=1e-2
)


Expand Down

0 comments on commit 245e3c7

Please sign in to comment.