KatInterview

Momentum-Weighted Portfolio: Daily Log Return Statistics

Arrays & HashingAuto-graded · 18 test cases
MediumAuto-gradedArrays & Hashingsimulationfinancestatistics

Last updated

Tests simulation of a daily rebalancing strategy where portfolio weights are proportional to positive asset returns, requiring correct timeline logic and statistical output.

Momentum-Weighted Portfolio: Daily Log Return Statistics

You are given a price matrix prices with shape N × T, where prices[i][t] is the closing price of asset i on day t (days are indexed 0 through T−1). You also receive an initial cash amount capital, though as you will find, it has no effect on the answer.

Strategy rules

Simple return for asset i on day t (for t ≥ 1):

r_i(t) = prices[i][t] / prices[i][t-1] − 1

Portfolio weights set at the close of day t:

  • If every asset has r_i(t) ≤ 0, hold 100% cash the next day.
  • Otherwise, allocate weight proportional to each asset's positive return:
    • w_i(t) = r_i(t) / Σ_{j: r_j(t) > 0} r_j(t) when r_i(t) > 0
    • w_i(t) = 0 otherwise

Fractional shares are allowed and transaction costs are zero.

Timeline

There are exactly T−1 daily log return periods, one for each consecutive pair of days:

PeriodPortfolio heldReason
day 0 → day 1100% cashNo prior returns exist to set weights; log return = 0
day 1 → day 2weights w(1) from r(1)Rebalanced at close of day 1
day t → day t+1weights w(t) from r(t)Rebalanced at close of day t

For any period t → t+1 where weights have been set, the portfolio's gross return is:

1 + Σ_i w_i(t) · r_i(t+1)

So the log return for that period is:

ln(1 + Σ_i w_i(t) · r_i(t+1))

For a cash-held period, the log return is exactly 0.

What to return

Compute the population mean and population standard deviation (divide by n, not n−1) of the T−1 log returns and return them as [mean_log_return, stddev_log_return].

If T ≤ 1 or there are no assets, return [0.0, 0.0].

Constraints

  • 0 ≤ N ≤ 100 (number of assets)
  • 0 ≤ T ≤ 500 (number of days)
  • All prices are strictly positive floating-point values
  • The price matrix is rectangular: every asset has exactly T price entries
  • capital > 0, but it does not affect the output
  • A return of exactly 0 is not considered positive

Examples

Example 1

Input: momentum_weighted_portfolio_stats([[100, 110, 121], [100, 90, 99]], 1000)

Output: [0.04765508990216247, 0.04765508990216247]

Example 2

Input: momentum_weighted_portfolio_stats([[100, 90, 81], [100, 110, 121]], 500)

Output: [0.04765508990216247, 0.04765508990216247]

Example 3

Input: momentum_weighted_portfolio_stats([[10, 10, 11, 10], [20, 19, 19, 20]], 100)

Output: [-0.03177005993477496, 0.04492964963716485]

Hints

Hint 1

Sketch the timeline before writing any code. There are exactly T−1 log return periods. The very first one (day 0 → day 1) is always a cash period with log return 0, because no daily returns are observable until day 1.

Hint 2

The initial capital C never affects the answer. A fully rebalanced portfolio's gross return over one period equals the weighted sum of that period's gross returns, and the current portfolio value cancels out. You only need to track returns, not dollar amounts or share counts.

Hint 3

Use math.log1p(x) to compute ln(1 + x) instead of math.log(1 + x). For small daily returns it is significantly more numerically accurate. Also be careful to use population standard deviation (divide by n), and state that assumption clearly.