Portfolio Rebalancing
InterviewTime limit1sMemory limit128 MB
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 be the amount it holds at the start of that term. If the instrument is still open, three things happen to it, in order:
- A fixed administrative cost
FIXED_FEEis deducted. - A percentage fee is deducted, equal to
PERCENTAGE_FEEtimes — a fraction of the amount held at the start of the term. - A return is added, equal to
RETURNtimes , whereRETURNmay be positive or negative.
Writing the fixed cost as , the percentage fee as , and the return as , the instrument's value at the end of the term is
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 (, , ). 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)