Portfolio Rebalancing

Interview

Time limit1sMemory limit128 MB

Summary
Simulate each term's fixed fee, percentage fee, and return per instrument, pool and redistribute every NREBALANCE terms, and print each ending balance to two decimals.
Level

Medium4 of 10

Topics
Simulation, Implementation, Math, Array
Solved
No attempts yet

Problem

An investor spreads their assets across NINSTRUMENTS financial instruments. Consider one instrument during one term, and let PP be the amount it holds at the start of that term. If the instrument is still open, three things happen to it, in order:

  1. A fixed administrative cost FIXED_FEE is deducted.
  2. A percentage fee is deducted, equal to PERCENTAGE_FEE times PP — a fraction of the amount held at the start of the term.
  3. A return is added, equal to RETURN times PP, where RETURN may be positive or negative.

Writing the fixed cost as ff, the percentage fee as cc, and the return as rr, the instrument's value at the end of the term is P−f−cP+rP.P - f - cP + rP.

If an account's value becomes zero or negative after a term, it is closed: from then on it is treated as exactly zero and no fees are charged, until a rebalancing reopens it.

A rebalancing happens after every NREBALANCE terms: all instruments' balances are pooled into one total, which is then split among the instruments in proportion to their original starting principals (PRINCIPAL_START). Without rebalancing, the higher-return instruments would gradually dominate the portfolio and expose the investor to more risk than a balanced plan. Every instrument can reach zero at once; then the total is zero, a rebalancing leaves everything at zero, and all accounts stay closed for the remaining terms.

Report the ending value of each instrument after NTERMS terms. If a rebalancing would fall exactly on term NTERMS, report the values before that final rebalancing. Do all arithmetic in double precision without rounding intermediate values, and round only the final answers to the nearest penny (two decimal places).

Input

The first line contains three positive integers:

NINSTRUMENTS NTERMS NREBALANCE

There are at most 10 instruments and at most 20 terms (1≤NINSTRUMENTS≤101 \le \text{NINSTRUMENTS} \le 10, 1≤NTERMS≤201 \le \text{NTERMS} \le 20, NREBALANCE≥1\text{NREBALANCE} \ge 1). This is followed by 3 lines of space-separated floating-point numbers, in this format:

FIXED_FEE(1) .. FIXED_FEE(NINSTRUMENTS)
PERCENTAGE_FEE(1) .. PERCENTAGE_FEE(NINSTRUMENTS)
PRINCIPAL_START(1) .. PRINCIPAL_START(NINSTRUMENTS)

Finally there are NTERMS lines, each with NINSTRUMENTS floating-point numbers giving the return of each instrument in that term:

RETURN(1,1) .. RETURN(1,NINSTRUMENTS)
...
RETURN(NTERMS,1) .. RETURN(NTERMS,NINSTRUMENTS)

All ratios (PERCENTAGE_FEE and RETURN) are given as fractions with up to 4 decimal places. For example, a fee of 0.0002 means 0.02% of the amount invested in that instrument is deducted as a fee each term. FIXED_FEE and PRINCIPAL_START are non-negative floating-point numbers given to 2 decimal places. At least one PRINCIPAL_START value is positive.

Output

Print a single line with the ending principal of each instrument, separated by single spaces, after NTERMS terms. Round each value to the nearest penny (two decimal places), so every value has exactly two digits after the decimal point (a closed account prints as 0.00).

PRINCIPAL_END(1) .. PRINCIPAL_END(NINSTRUMENTS)

Examples2

  1. Example 1

    Input
    4 10 5
    5.00 10.00 20.00 50.00
    0.002 0.001 0.0008 0.0005
    150000.00 100000.00 75000.00 50000.00
    0.10 0.05 -0.05 -0.85
    0.10 0.05 -0.10 -0.85
    0.10 0.05 -0.20 -0.85
    0.10 0.05 -0.40 -0.85
    0.10 0.05 -0.80 -0.85
    0.10 0.05 -0.05 -0.90
    0.10 0.05 -0.05 -0.90
    0.10 0.05 -0.05 -0.90
    0.10 0.05 -0.05 -0.85
    0.10 0.05 -0.05 -0.85
    
    Expected output
    237698.69 126086.01 57298.74 0.00
    
  2. Example 2

    Input
    2 3 2
    0.00 0.00
    0.0000 0.0000
    100.00 300.00
    1.0000 -1.0000
    1.0000 -1.0000
    1.0000 1.0000
    
    Expected output
    200.00 600.00