PIXELBANKv9.1.0
Menu

Return Variance Under Stochastic Rewards

Problem Statement

Each timestep t gives an independent reward with mean mu[t] and variance var[t]. The discounted return is G = sum_t gamma^t * R_t. Because the rewards are independent, the variance of the discounted return is:

Var(G)=∑t=0T−1γ2t Var(Rt)\mathrm{Var}(G) = \sum_{t=0}^{T-1} \gamma^{2t}\, \mathrm{Var}(R_t)

Implement return_variance(var, gamma) returning Var(G) as a float. (mu is not needed for the variance.)

Example:

Input:
return_variance([1.0, 1.0], 0.5)
Output:
1.25
Reasoning:
  • Initialize the cumulative variance to 0.00.0 and the discount factor for the current step to 1.01.0. Since the variance formula requires squaring the discount rate at each step, calculate the squared discount factor: g2=0.52=0.25g^2 = 0.5^2 = 0.25.
  • Process the first variance value (1.01.0) at timestep t=0t=0. Multiply the current discount factor by this variance and add it to the total: 0.0+(1.0×1.0)=1.00.0 + (1.0 \times 1.0) = 1.0.
  • Update the discount factor for the next timestep by multiplying it by the squared discount factor: 1.0×0.25=0.251.0 \times 0.25 = 0.25.
  • Process the second variance value (1.01.0) at timestep t=1t=1. Multiply the updated discount factor by this variance and add it to the running total: 1.0+(0.25×1.0)=1.251.0 + (0.25 \times 1.0) = 1.25.
  • The final output is 1.25

Constraints:

  • 1 <= len(var) <= 10000, all var[t] >= 0, 0.0 <= gamma <= 1.0
  • Note the discount is squared: gamma**(2t).
  • Return a float.
solution.py

Test Results

0/0
Run code to see test results.
Return Variance Under Stochastic Rewards - Hard | PixelBank