Simulate return-weighted rebalancing strategy

Quick Overview

Simulate return-weighted rebalancing strategy evaluates metric design, causal reasoning, experiment setup, diagnostics, SQL/statistical checks, and recommendations in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

Simulate return-weighted rebalancing strategy

Company: Optiver

Role: Software Engineer

Category: Analytics & Experimentation

Difficulty: medium

Interview Round: Technical Screen

You are given an N×T matrix prices where prices[i][t] is the end‑of‑day price of asset i on day t (t = 0…T− 1). Start with total capital C in cash on day 0. For each day t ≥ 1, compute simple returns r_i(t) = prices[i][t]/prices[i][t−1] − 1. If all r_i(t) ≤ 0, hold 100% cash for the next day. Otherwise, set next‑day portfolio weights proportional to the positive returns: w_i(t) = r_i(t) / Σ_{j: r_j(t) > 0} r_j(t) for r_i(t) > 0, and w_i(t) = 0 otherwise. Rebalance at the close of day t using these weights; fractional shares and zero transaction costs are allowed. Let V_t be portfolio value at the close of day t. The daily log return for period t→t+1 is ln(V_{t+1}/V_t). Compute and return [mean_log_return, stddev_log_return] over all T−1 daily log returns.

Overview: Simulate return-weighted rebalancing strategy evaluates metric design, causal reasoning, experiment setup, diagnostics, SQL/statistical checks, and recommendations in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

Community answers

Answer by Michal Hug

import math def simulate_return_weighted_rebalancing(prices): """ prices[i][t] = closing price of asset i on day t Returns: [mean_log_return, stddev_log_return] """ if not prices or len(prices[0]) < 2: return [0.0, 0.0] N = len(prices) T = len(prices[0]) # Validate rectangular input if any(len(row) != T for row in prices): raise ValueError("prices must be a rectangular matrix") # Portfolio value at close of day 0 V = 1.0 log_returns = [] # Period 0 -> 1 is 100% cash log_returns.append(0.0) # At close of day t, calculate returns from t-1 -> t, # then apply those weights to the NEXT period t -> t+1. # # We only need periods through T-1. for t in range(1, T - 1): returns = [ prices[i][t] / prices[i][t - 1] - 1.0 for i in range(N) ] positive_returns = [r for r in returns if r > 0] total_positive = sum(positive_returns) if total_positive > 0: weights = [ r / total_positive if r > 0 else 0.0 for r in returns ] else: weights = [0.0] * N # Portfolio return during t -> t+1 portfolio_return = sum( weights[i] * ( prices[i][t + 1] / prices[i][t] - 1.0 ) for i in range(N) ) V_next = V * (1.0 + portfolio_return) log_returns.append(math.log(V_next / V)) V = V_next mean = sum(log_returns) / len(log_returns) variance = sum( (x - mean) ** 2 for x in log_returns ) / len(log_returns) stddev = math.sqrt(variance) return [mean, stddev]
|Home/Analytics & Experimentation/Optiver
Optiver logo
Optiver
Jul 16, 2025
mediumSoftware EngineerTechnical ScreenAnalytics & Experimentation
8
0

Simulate return-weighted rebalancing strategy

Problem: Momentum-weighted daily log-return statistics

You have N assets with end-of-day prices over T trading days. Let prices[i][t] be the closing price of asset i on day t (i = 0..N−1, t = 0..T−1). You start with total capital C in cash at the close of day 0. Fractional shares and zero transaction costs are allowed.

At each day t ≥ 1:

  1. Compute simple returns r_i(t) = prices[i][t] / prices[i][t−1] − 1.
  2. Set next-day portfolio weights for period t→t+1 as follows:
    • If all r_i(t) ≤ 0, hold 100% cash for the next day (i.e., all asset weights are 0).
    • Otherwise, assign weights only to assets with positive returns, proportional to those returns: w_i(t) = r_i(t) / Σ_{j: r_j(t) > 0} r_j(t) if r_i(t) > 0; else w_i(t) = 0.
  3. Rebalance at the close of day t using these weights.

Let V_t denote portfolio value at the close of day t. The daily log return for period t→t+1 is ln(V_{t+1}/V_t). Note:

  • For t = 0, there are no prior-day returns; treat period 0→1 as 100% cash (log return 0).

Task: Compute and return [mean_log_return, stddev_log_return] over all T−1 daily log returns L_t = ln(V_{t+1}/V_t), for t = 0..T−2.

Assume prices are positive. If T < 2, return [0.0, 0.0].

Clarifying Questions to Ask Guidance

  • Clarify the business objective, unit of analysis, time window, exposure definition, and primary metric.
  • State assumptions about instrumentation, randomization, sample size, and data quality.
  • Separate descriptive analysis from causal claims.

What a Strong Answer Covers Guidance

  • A metric framework with primary, guardrail, and diagnostic metrics.
  • A credible analysis or experiment design with clear assumptions and bias checks.
  • SQL/statistical logic for segmentation, variance, confidence, and data validation where relevant.
  • An actionable recommendation that explains trade-offs and next steps.

Follow-up Questions Guidance

  • What sanity checks would you run before trusting the result?
  • How would you handle novelty effects, seasonality, or selection bias?
  • What decision would you make if metrics disagree?
Loading comments...