Operations on Lists (Iterators & Amend) · General · Practice Question
Calculate Compounded Returns Using Scan
Question
A portfolio starts with a value of 100.
The portfolio then experiences the following daily returns:
returns:0.01 -0.02 0.03 -0.01where:
| Return | Meaning |
|---|---|
0.01 | +1% |
-0.02 | -2% |
0.03 | +3% |
-0.01 | -1% |
Each day's return should be applied to the portfolio value produced by the previous day.
Calculate the portfolio value after each daily return is applied.
Expected result:
101 98.98 101.9494 100.9299Solution
{x*(1+y)}\[100;returns]Alternative Solution
100*prds 1+returns1+returns turns each return into a growth factor (1.01 0.98 1.03 0.99), prds takes the running product of those factors, and multiplying by 100 scales them to the starting value.
Explanation
The scan iterator:
\returns every intermediate result.
The function:
{x*(1+y)}takes:
xas the current portfolio valueyas the next daily return
For each iteration, the portfolio value is updated using:
newValue = currentValue * (1 + return)The calculation proceeds as follows:
Day 1: +1%
100*(1+0.01)Result:
101Day 2: -2%
The 2% loss is applied to the new portfolio value of 101:
101*(1-0.02)Result:
98.98Day 3: +3%
98.98*(1+0.03)Result:
101.9494Day 4: -1%
101.9494*(1-0.01)Result:
100.9299(q displays floats to 7 significant digits by default; the full value is 100.929906. Use \P 10 to show more digits.)
The scan expression:
{x*(1+y)}\[100;returns]returns every intermediate portfolio value:
101 98.98 101.9494 100.9299This is useful when modelling:
- Cumulative portfolio value
- Compounded returns
- Growth of capital over time
- Running NAV
Unlike a simple sum of returns, each return is applied to the current portfolio value, making this a natural example of when the scan iterator is required to carry state forward between iterations.