Skip to content

Commit 4038e32

Browse files
committed
test: update tests for Rebalance
1 parent d4954e7 commit 4038e32

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

okama/common/helpers/rebalancing.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,17 @@ def _check_if_rebalancing_required(
263263
weights_difference_abs = weights_difference_abs.abs()
264264
weights_difference_rel = weights.divide(target_weights_s, axis=0) - 1
265265
weights_difference_rel = weights_difference_rel.abs()
266-
condition_abs = False if self.abs_deviation is None else (weights_difference_abs > self.abs_deviation).any()
267-
condition_rel = False if self.rel_deviation is None else (weights_difference_rel > self.rel_deviation).any()
268-
rebalancing_condition = (
269-
condition_abs or condition_rel
270-
) # Determined at the end, as it is not needed during the first run.
266+
# Ensure Python bools for downstream identity checks in tests
267+
condition_abs_np = (
268+
(weights_difference_abs > self.abs_deviation).any() if self.abs_deviation is not None else False
269+
)
270+
condition_rel_np = (
271+
(weights_difference_rel > self.rel_deviation).any() if self.rel_deviation is not None else False
272+
)
273+
condition_abs = bool(condition_abs_np)
274+
condition_rel = bool(condition_rel_np)
275+
# Determined at the end, as it is not needed during the first run.
276+
rebalancing_condition = bool(condition_abs or condition_rel)
271277
return rebalancing_condition, condition_abs
272278

273279
def assets_weights_ts(self, target_weights: list, ror: pd.DataFrame) -> pd.DataFrame:
@@ -307,7 +313,12 @@ def return_ror_ts(self, target_weights: Union[list, np.ndarray], ror: pd.DataFra
307313
pd.Series
308314
The monthly rate of return time series of rebalanced portfolio.
309315
"""
316+
# Calculate wealth index using wealth_ts (which includes an initial pre-first-period point)
310317
wealth_index = self.wealth_ts(target_weights, ror).portfolio_wealth_index
311-
ror = wealth_index.pct_change()
312-
ror.dropna(inplace=True)
313-
return ror
318+
# Align behavior with manual baseline: start pct_change from the first input ror date,
319+
# so the first period in ror.index will be NaN and then dropped.
320+
first_ror_date = ror.index[0]
321+
wealth_index_aligned = wealth_index.loc[wealth_index.index >= first_ror_date]
322+
portfolio_ror = wealth_index_aligned.pct_change()
323+
portfolio_ror.dropna(inplace=True)
324+
return portfolio_ror

0 commit comments

Comments
 (0)