-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vkhomyakov (Sourcery refactored) #70
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -728,20 +728,20 @@ def describe(self, years: Tuple[int, ...] = (1, 5, 10), tickers: bool = True) -> | |
for ti in self.symbols: | ||
# short_ticker = ti.split(".", 1)[0] | ||
value = self.assets_first_dates[ti].strftime("%Y-%m") | ||
row.update({ti: value}) | ||
row[ti] = value | ||
row.update(period=None, property="Inception date") | ||
if hasattr(self, "inflation"): | ||
row.update({self.inflation: self.inflation_first_date.strftime("%Y-%m")}) | ||
row[self.inflation] = self.inflation_first_date.strftime("%Y-%m") | ||
description = pd.concat([description, pd.DataFrame(row, index=[0])], ignore_index=True) | ||
# last asset date | ||
row = {} | ||
for ti in self.symbols: | ||
# short_ticker = ti.split(".", 1)[0] | ||
value = self.assets_last_dates[ti].strftime("%Y-%m") | ||
row.update({ti: value}) | ||
row[ti] = value | ||
row.update(period=None, property="Last asset date") | ||
if hasattr(self, "inflation"): | ||
row.update({self.inflation: self.inflation_last_date.strftime("%Y-%m")}) | ||
row[self.inflation] = self.inflation_last_date.strftime("%Y-%m") | ||
description = pd.concat([description, pd.DataFrame(row, index=[0])], ignore_index=True) | ||
# last data date | ||
row = {x: self.last_date.strftime("%Y-%m") for x in df.columns} | ||
|
@@ -1157,16 +1157,15 @@ def tracking_difference_annualized(self, rolling_window: Optional[int] = None) - | |
>>> x.tracking_difference_annualized(rolling_window = 12*5).plot() | ||
>>> plt.show() | ||
""" | ||
if rolling_window: | ||
rolling_cagr = helpers.Frame.get_rolling_fn( | ||
self.assets_ror, | ||
window=rolling_window, | ||
fn=helpers.Frame.get_cagr, | ||
window_below_year=False, # small windows below 12 months are not allowed (CAGR is not defined) | ||
) | ||
return rolling_cagr.subtract(rolling_cagr.iloc[:, 0], axis=0).iloc[:, 1:] | ||
else: | ||
if not rolling_window: | ||
return helpers.Index.tracking_difference_annualized(self.tracking_difference()) | ||
rolling_cagr = helpers.Frame.get_rolling_fn( | ||
self.assets_ror, | ||
window=rolling_window, | ||
fn=helpers.Frame.get_cagr, | ||
window_below_year=False, # small windows below 12 months are not allowed (CAGR is not defined) | ||
) | ||
return rolling_cagr.subtract(rolling_cagr.iloc[:, 0], axis=0).iloc[:, 1:] | ||
Comment on lines
-1160
to
+1168
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
@property | ||
def tracking_difference_annual(self) -> pd.DataFrame: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1145,9 +1145,7 @@ def recovery_period(self) -> int: | |
s1 = s.where(s == 0).notnull().astype(int) | ||
s1_1 = s.where(s == 0).isnull().astype(int).cumsum() | ||
s2 = s1.groupby(s1_1).cumsum() | ||
# Max recovery period date should not be in the border (means it's not recovered) | ||
max_period = s2.max() if s2.idxmax().to_timestamp() != self.last_date else np.NAN | ||
return max_period | ||
return s2.max() if s2.idxmax().to_timestamp() != self.last_date else np.NAN | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ):
|
||
|
||
def describe(self, years: Tuple[int] = (1, 5, 10)) -> pd.DataFrame: | ||
""" | ||
|
@@ -1359,7 +1357,7 @@ def percentile_inverse_cagr( | |
""" | ||
if distr == "hist": | ||
cagr_distr = self.get_rolling_cagr(years * settings._MONTHS_PER_YEAR).loc[:, [self.symbol]].squeeze() | ||
elif distr in ["norm", "lognorm"]: | ||
elif distr in {"norm", "lognorm"}: | ||
Comment on lines
-1362
to
+1360
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
if not n: | ||
n = 1000 | ||
cagr_distr = self._get_cagr_distribution(distr=distr, years=years, n=n) | ||
|
@@ -1408,7 +1406,7 @@ def percentile_history_cagr(self, years: int, percentiles: List[int] = [10, 50, | |
self.get_rolling_cagr(years * 12).loc[:, self.symbol].quantile(percentile / 100) | ||
for years in period_range | ||
] | ||
returns_dict.update({percentile: percentile_returns_list}) | ||
returns_dict[percentile] = percentile_returns_list | ||
Comment on lines
-1411
to
+1409
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
df = pd.DataFrame(returns_dict, index=list(period_range)) | ||
df.index.rename("years", inplace=True) | ||
return df | ||
|
@@ -1640,7 +1638,7 @@ def percentile_distribution_cagr( | |
results = {} | ||
for percentile in percentiles: | ||
value = cagr_distr.quantile(percentile / 100) | ||
results.update({percentile: value}) | ||
results[percentile] = value | ||
Comment on lines
-1643
to
+1641
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return results | ||
|
||
def percentile_wealth( | ||
|
@@ -1696,12 +1694,12 @@ def percentile_wealth( | |
""" | ||
if distr == "hist": | ||
results = self.percentile_wealth_history(years=years, percentiles=percentiles).iloc[-1].to_dict() | ||
elif distr in ["norm", "lognorm"]: | ||
elif distr in {"norm", "lognorm"}: | ||
results = {} | ||
wealth_indexes = self._monte_carlo_wealth(distr=distr, years=years, n=n) | ||
for percentile in percentiles: | ||
value = wealth_indexes.iloc[-1, :].quantile(percentile / 100) | ||
results.update({percentile: value}) | ||
results[percentile] = value | ||
Comment on lines
-1699
to
+1702
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
else: | ||
raise ValueError('distr should be "norm", "lognorm" or "hist".') | ||
if today_value: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
AssetList.describe
refactored with the following changes:simplify-dictionary-update
)