Here are the annual returns for a hypothetical S&P 500 stock index fund for each calendar year from 1928 to 2017, expressed as a multiplier. So in 1928 you might say "the index went up by 37.88%" which I've represented here by 1.3788.
1.3788, 0.8809, 0.7152, 0.5293, 0.8485, 1.4659, 0.9406, 1.4137, 1.2792, 0.6141, 1.2521, 0.9455, 0.8471, 0.8214, 1.1243, 1.1945, 1.138, 1.3072, 0.8813, 1, 0.9935, 1.1026, 1.2178, 1.1646, 1.1178, 0.9338, 1.4502, 1.264, 1.0262, 0.8569, 1.3806, 1.0848, 0.9703, 1.2313, 0.8819, 1.1889, 1.1297, 1.0906, 0.8691, 1.2009, 1.0766, 0.8864, 1.001, 1.1079, 1.1563, 0.8263, 0.7028, 1.3155, 1.1915, 0.885, 1.0106, 1.1231, 1.2577, 0.9027, 1.1476, 1.1727, 1.014, 1.2633, 1.1462, 1.0203, 1.124, 1.2725, 0.9344, 1.2631, 1.0446, 1.0706, 0.9846, 1.3411, 1.2026, 1.3101, 1.2667, 1.1953, 0.8986, 0.8696, 0.7663, 1.2638, 1.0899, 1.03, 1.1362, 1.0353, 0.6151, 1.2345, 1.1278, 1, 1.1341, 1.296, 1.1139, 0.9927, 1.0954, 1.1942
Source: https://www.macrotrends.net/2526/sp-500-historical-annual-returns
Challenge
Given as inputs:
- the array of annual returns (but see Rule 2. below)
- an array \$X\$ of \$1\le K \le 90\$ positive numbers (the amounts to invest in each year of the investment period)
write a program or function that outputs what would have been the "best" \$K\$ consecutive year period to have invested the amounts in \$X\$, where:
- each amount is invested at the beginning of a year.
- anything left after each year is reinvested at the beginning of each subsequent year.
- "best" means the largest amount at the end of the \$K\$ year period.
Rules
This is code-golf, so the fewest bytes in each language wins. Standard rules apply. Explanations encouraged.
If you don't like the way I've represented the array of annual returns, you can change them to any other array of 90 numbers that you prefer.
You can output the best \$K\$ year period in any consistent manner (e.g. 0-indexed or 1-indexed, the first and/or the last year to have invested, etc.) but you need to say what your output represents if it isn't obvious.
In case of a tie, output any or all correct answers.
Example calculation
Suppose \$X = [ 10000, 20000, 30000 ]\$.
If you had invested these amounts in 1928, 1929, and 1930 (the 1-indices 1, 2, and 3) you would have ended up with 42743.10. Sad.
But if you had invested those amounts in 2014, 2015, and 2016 (the 1-indices 87, 88, and 89) you would have ended up with 66722.66. A bit better.
It turns out the best three year period to have invested these amounts would have been 1995, 1996, and 1997 (1-indices 68, 69, and 70), resulting in 91942.91. Nice.
Test cases
1-indexed, first year of optimal period
[ 1, 2, 3 ] -> 68
[ 1 ] -> 6
* any array of length 90 * -> 1
[ 1, 1 ] -> 27
[ 1, 2 ] -> 8
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ] -> 62
[ 1, 2, 3, 4, 5, 4, 3, 2, 1 ] -> 64
* 1 repeated 20 times * -> 53