Skip to content

Commit

Permalink
'Refactored by Sourcery'
Browse files Browse the repository at this point in the history
  • Loading branch information
Sourcery AI committed Jan 8, 2024
1 parent 5b8f1a5 commit 3d6a85b
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions okama/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -1118,9 +1118,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

def describe(self, years: Tuple[int] = (1, 5, 10)) -> pd.DataFrame:
"""
Expand Down Expand Up @@ -1332,7 +1330,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"}:
if not n:
n = 1000
cagr_distr = self._get_cagr_distribution(distr=distr, years=years, n=n)
Expand Down Expand Up @@ -1381,7 +1379,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
df = pd.DataFrame(returns_dict, index=list(period_range))
df.index.rename("years", inplace=True)
return df
Expand Down Expand Up @@ -1613,7 +1611,7 @@ def percentile_distribution_cagr(
results = {}
for percentile in percentiles:
value = cagr_distr.quantile(percentile / 100)
results.update({percentile: value})
results[percentile] = value
return results

def percentile_wealth(
Expand Down Expand Up @@ -1669,12 +1667,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
else:
raise ValueError('distr should be "norm", "lognorm" or "hist".')
if today_value:
Expand Down

0 comments on commit 3d6a85b

Please sign in to comment.