Problem · Array

Proportional Momentum Investment Statistics

Learn this problem
MediumOptiver logoOptiverFULLTIMEOA

Problem statement

You are given an N x T matrix of positive asset prices. Row i contains the prices of asset i on consecutive days.

Compute one portfolio log return for every day transition from day 0 to day T - 1:

  • For the first transition, hold cash, so the portfolio return is 0.
  • For transition t, where t >= 2, compute each asset's simple return over the preceding transition t - 1. Ignore nonpositive returns. If no asset had a positive return, hold cash.
  • Otherwise, assign each positive-return asset a weight equal to its preceding return divided by the sum of all positive preceding returns. Apply those weights to the asset's simple return over transition t.
  • Convert the resulting portfolio simple return r to ln(1 + r). A cash transition has log return 0.

Return [mean, standardDeviation] over all T - 1 daily log returns. Use the population standard deviation, divide by T - 1, and round both outputs to five digits after the decimal point using half-up rounding.

Function

proportionalMomentumStatistics(prices: double[][]) → double[]

Examples

Example 1

prices = [[100.0,115.0,117.3],[200.0,210.0,199.5]]return = [0.00125,0.00125]

The first transition stays in cash. The preceding positive returns for the second transition are 15% and 5%, so the weights are 0.75 and 0.25. The portfolio simple return is 0.0025 and its log return is about 0.00249688; the mean and population deviation of that value with the initial zero both round to 0.00125.

Constraints

  • 1 <= N <= 200
  • 2 <= T <= 10000
  • Every row has exactly T values.
  • Every price is finite and in [10^-6, 10^9].
  • Every weighted portfolio simple return is greater than -1.
  • Each unrounded result is at least 10^-10 away from a half-way five-decimal rounding boundary.

More Optiver problems

drafts saved locally
public double[] proportionalMomentumStatistics(double[][] prices) {
  // write your code here
}
prices[[100.0,115.0,117.3],[200.0,210.0,199.5]]
expected[0.00125,0.00125]
checking account