Statistics

Problem Statement for "Predicting"

Problem Statement

We would like to be able to predict tomorrow's price of a share of stock. We have data on past daily prices. Based on that we will make a prediction. Our plan is to use a weighted average of the 5 most recent prices as the prediction.

We will choose the appropriate weights as the ones that would have best predicted prices in the past. The weights must add up to one to be a weighted average, but some of them may be negative. We will restrict consideration to weights that are chosen from the following 21 values:

     -1.0, -0.9, ... , -0.1, 0.0, 0.1, ..., 0.9, 1.0
We define the "error" of a prediction to be the absolute value of the difference between the prediction and the price.We will evaluate a possible weighting by using it to predict each of the known prices (except for the first 5, which don't have enough predecessors). We will then choose the weighting that has the smallest average error for its predictions.

Before we use our weighted averaging scheme to make our fortune on the stock market we need to have some idea of how well it predicted past data. Create a class Predicting that contains a method avgError that will be given a double[] data and will return the average error made by our best weighting.

Definition

Class:
Predicting
Method:
avgError
Parameters:
double[]
Returns:
double
Method signature:
double avgError(double[] data)
(be sure your method is public)

Notes

  • The returned value must be accurate to within a relative or absolute value of 1E-9.

Constraints

  • data will contain between 6 and 50 elements inclusive.
  • Each element of data will be between 10.0 and 100.0 inclusive.

Examples

  1. {10,10,10,10,10,10}

    Returns: 0.0

    A weighting of .2,.2,.2,.2,.2 will exactly predict the only past price that had 5 predecessors.

  2. {50,10,50,10,50,10,50,10,50,10,50,10}

    Returns: 0.0

    A weighting of -1,0,0,1,1 predicts price correctly every time (in the past). For example, the prediction of the most recent price is -1*50 + 0*10 + 0*50 + 1*10 + 1*50 = 10 which was exactly right.

  3. {50,10,50,10,50,10.4,50,10,10.1,10,50,10}

    Returns: 5.814285714285714

  4. {50,10,50,10,50,10,48,22,19.2,30.0,50,10,50,10,50,10}

    Returns: 6.654545454545455

  5. {99.50,10.10,15.0,10,50,10,48,22,19.2,30.0,50,10,50,10,50,10}

    Returns: 8.481818181818182

  6. {50,60,50,60,50,60,60}

    Returns: 5.0

    The best choice of weights is -1.0,-1.0,1.0,1.0,1.0 which gives a prediction of 50 for the next to the last price (-1*50 + -1*60 + 1*50 + 1*60 + 1*50 = 50) and a prediction of 60 for the last price (-1*60 + -1*50 + 1*60 + 1*50 + 1*60 = 60). So the errors are 10 and 0 for the two predictions with an average error of 5.

  7. {50,10,50,10,50,10.4,50,10,50,10,50,10}

    Returns: 0.11428571428571413

  8. {30,50,70,19,22,42,40,38}

    Returns: 0.5333333333333338

  9. {30,30,30,30,30,44.444444,55.555555,92.000001}

    Returns: 15.851852333333332

  10. {35.165,68.355,76.267,72.331,38.335,74.623,39.897,87.271,77.835,22.677}

    Returns: 6.725439999999997

  11. {82.932,73.964,76.249,72.616,10.437,17.237,13.298,62.588,45.838,64.325,84.712,58.193,39.839,45.470,77.586,76.780,61.538,59.157,24.883,25.858,80.865,39.984,36.920,84.033,89.860,54.582,54.294,55.322,28.520,42.298,16.247,88.777,71.847,12.848,45.622,71.955,81.046}

    Returns: 20.22365625

  12. {28.105,64.279,39.121,56.279,16.293,83.553,89.273,24.489,77.454,27.516,34.167,64.207,74.262,77.110,72.248,46.357,79.122,66.862,38.167,35.312,56.255,51.953,40.309,50.456,34.341,75.425,45.088,62.486,44.741,73.110,36.430,79.052}

    Returns: 17.616466666666664

  13. {62.128,89.341,58.595,51.020,66.208,65.688}

    Returns: 3.9999999999906777E-4

  14. {67.380,30.839,84.666,84.759,61.974,39.229,85.407,44.910,41.659,56.668,77.957,36.250,41.039,62.483,21.231,42.211,15.666,37.225,89.010,89.639,48.973,61.016,54.548,52.269,17.820,86.404,77.996,75.694,44.827,51.755,78.462,20.565,12.252,75.093,13.542,83.965,43.213,51.692,16.305,65.087,43.636,84.546,47.849}

    Returns: 20.948971052631578

  15. {21.536,54.289,65.080,34.626,69.705,17.825,27.372}

    Returns: 0.7820000000000018

  16. {88.270,48.394,56.403,84.908,46.960,48.401,22.100,60.570,14.721,25.479,89.688,82.741,36.692,61.451,80.256,65.175,62.117,18.816,41.187,23.855,60.915,29.167,36.688,84.390,64.160,71.975,70.050,57.382,31.270}

    Returns: 18.373599999999996

  17. {57.920,69.960,29.037,43.019,18.199,46.090,45.844,11.562,71.529,42.813,65.398,40.302}

    Returns: 13.414157142857144

  18. {55.0288, 94.9763, 16.0207, 27.6995, 30.7785, 64.4884, 37.6864, 50.6287, 62.6151, 13.9415, 93.9985, 49.0274, 11.3953, 77.785, 45.5089, 54.5427, 87.2143, 15.6829, 28.4246, 17.9159, 34.8106, 47.4783, 76.0463, 55.8501, 69.2868, 37.3156, 12.5324, 60.9726, 32.3579, 56.1467, 71.756, 16.5755, 34.503, 86.5358, 39.0432, 62.5684, 43.2814, 98.9288, 68.6743}

    Returns: 20.514395588235292

  19. {63.1809, 63.3869, 74.89, 40.6967, 77.2549, 45.8989, 39.2575, 93.5398, 45.9209, 43.2731, 22.1979, 84.0172, 33.1709, 65.7216, 61.4808, 98.5525, 35.5495, 31.1988, 95.5119, 30.1825, 34.1981, 70.5063, 94.2045, 97.1819, 25.2687, 87.2939, 85.2724, 55.4189, 99.0497, 71.9593, 78.749, 66.0979, 53.0677, 13.5569, 27.532, 15.334, 49.5328, 98.7558}

    Returns: 24.632385454545453

  20. {67.2433, 92.3917, 41.8806, 29.96, 56.9955, 14.3342, 35.1814, 44.3855, 55.5095, 89.06, 93.2954, 64.0324, 72.2422, 89.071, 11.0163, 82.4378, 57.7041, 54.4163, 38.4719, 83.5337, 62.2608, 52.5513, 82.6576, 63.1864, 92.7296, 34.7612, 59.5032, 19.2727, 63.4062, 39.3481, 15.4988, 93.1196, 13.0323, 47.0003, 32.8907}

    Returns: 19.705228333333334

  21. {84.2753, 98.5635, 67.5893, 72.0664, 77.3098, 70.5722, 93.809, 59.9701, 54.0181, 78.8754, 74.489, 98.5333, 94.4325, 95.5998, 41.6086, 21.7228, 51.9114, 19.8303, 75.4091, 31.3224, 18.6657, 47.3025, 16.5893, 86.5221, 98.1845, 59.3823, 50.6012, 97.3797, 16.0949, 36.6866, 17.9901, 69.957, 46.0802, 37.6672, 29.9847}

    Returns: 22.91622733333334

  22. {92.3341, 37.4227, 63.2606, 14.2656, 25.1891, 57.0696, 33.039, 38.623, 91.4579, 69.0341, 88.5519, 36.7992, 86.0057}

    Returns: 19.706005

  23. {19.9127, 96.5639, 66.2078, 76.3073, 40.4935, 54.7432, 35.75, 23.8679, 33.8356, 12.7247, 29.4327, 44.9431, 69.1906, 16.1003, 95.4543, 25.0352, 84.7313, 71.2864, 76.7934, 83.4953, 12.483, 96.9265, 38.2522, 68.6798}

    Returns: 17.24972

  24. {82.9102, 70.6848, 21.503, 61.4588, 54.7789, 48.9889, 57.6766, 91.1859, 26.3674, 55.4601, 53.9357, 87.2005, 78.4771, 65.0102, 18.619, 90.296, 26.3894, 53.8588, 91.8369, 58.8028, 74.0577, 28.2406, 65.609, 59.4867, 27.7544, 54.6992, 69.2428, 22.6264, 87.0083, 58.5116, 60.286, 20.4318, 65.6475, 11.8348, 36.3488, 92.8092, 60.7392, 98.124, 48.1292, 39.5459, 52.2657, 34.3519, 38.9279, 93.0152, 11.3157}

    Returns: 22.0175905

  25. {30.9928, 10.379, 69.6603, 47.9946, 53.9851, 19.4568, 78.4387, 93.1114, 65.8919, 77.2384, 88.8513, 97.8823, 71.6791, 65.1421}

    Returns: 12.59912888888889

  26. {54.0812, 60.036, 80.0812, 15.1637, 32.4403, 53.4742, 25.3786, 98.8299, 33.473, 72.1351, 90.6394, 59.841, 98.6129, 36.5767, 64.5488, 35.923, 88.258, 39.9222, 59.808, 87.8954, 18.2785, 24.5793, 54.0977, 12.9087, 91.5403, 13.9525, 16.2761, 12.1946, 55.9819, 24.3788, 91.8012, 11.0245, 85.8931, 87.64, 96.4211, 63.8429, 27.8451, 49.3487, 42.2761, 92.2489, 17.9269, 23.5383, 45.4045, 38.7494, 31.792, 88.7442, 78.7298}

    Returns: 25.836595476190475

  27. {24.1069, 15.8943, 79.606, 40.5045, 18.9679, 43.3665, 65.9771, 10.217, 97.8191, 69.4296, 50.2139, 19.4678, 96.4568, 77.4883, 34.687, 68.4024, 37.6919, 64.3703, 58.1353, 88.1317, 31.207}

    Returns: 18.314545624999997

  28. {36.9036, 23.2664, 37.181, 57.7563, 90.112, 82.7015, 25.2467, 93.8447, 12.7934, 36.379, 25.3676, 10.9668, 26.1119, 80.9738, 62.118, 96.6573, 28.6608, 82.1577, 99.0469, 23.5081, 17.9598, 76.4171, 74.5659, 57.7013, 76.1946, 10.0879, 66.032, 83.0613, 69.2236, 68.5397, 33.4922, 66.021, 62.9914, 23.217, 79.7378, 51.7631, 76.3347, 29.4711, 48.1567, 76.8621, 84.2973}

    Returns: 25.734153888888894

  29. {24.9995, 68.7512, 60.9836, 16.8365, 27.6089, 27.3205, 76.0326, 29.592, 85.1405, 18.9486, 53.7544, 72.8465, 73.393, 98.2861, 19.031, 90.4663, 60.2777, 48.11, 78.9935, 89.6396, 73.4864, 42.5672, 64.568, 12.4912, 46.9234, 54.095, 60.9561, 90.903, 27.499, 99.3435, 41.0978, 22.3655, 48.7802, 44.4267, 11.1838, 29.7073, 23.1263, 74.8021, 52.2794, 72.4454, 48.8241, 10.0714, 47.1322}

    Returns: 21.97105894736842

  30. {43.8252, 99.0689, 96.2206, 82.9679, 92.6142, 35.4726, 62.0685, 70.0696, 57.7783, 89.3567, 55.9874, 60.5441, 22.8681, 14.8506, 36.1812, 42.8886, 28.0758, 79.3973, 61.3791, 92.8697, 35.294, 54.7377, 88.9914, 45.2452, 24.17, 65.0926, 51.9086, 39.7024, 75.6426, 32.7561, 92.9575, 53.6885, 74.5714, 86.437, 71.0858, 90.1038, 58.0282, 22.6759, 18.3773, 32.5227}

    Returns: 20.415989142857146

  31. {95.6658, 31.9074, 25.8922, 94.5231, 55.699, 19.5199, 35.4012, 72.4949, 61.6923, 33.6213, 71.4017, 15.9383, 27.9797, 81.9929, 93.1828, 54.0565, 40.7572, 59.7366, 89.4143, 73.9424, 24.4447, 41.3916, 68.0316, 32.1409, 33.3412, 47.2805, 93.4327, 34.33, 68.8061, 63.1755}

    Returns: 16.5132788

  32. {84.7066, 77.34, 57.3003, 96.1217, 81.6385, 19.8578, 53.7956, 52.4607, 51.1753, 37.4639, 96.6326, 14.0568, 24.9281, 64.0269, 73.0964, 37.3074, 93.9106, 80.9162, 40.8945, 78.6941, 44.8909, 94.1496, 37.9199, 81.7072, 10.8679, 39.2987, 18.7591}

    Returns: 23.16801363636364

  33. {99.0, 39.0, 19.0, 38.0, 83.0, 87.0, 54.0, 57.0, 39.0, 60.0, 78.0, 29.0, 10.0, 22.0, 36.0, 55.0, 64.0, 79.0, 80.0, 70.0 }

    Returns: 13.253333333333332

  34. {50.0, 60.0, 50.0, 60.0, 50.0, 60.0, 60.0 }

    Returns: 5.0

  35. {42.25180591152108, 78.17980235984584, 96.02634922482405, 40.58619042157095, 28.99690772946481, 95.42930700433745, 60.698867736447575, 65.4750568371753, 60.114536947006016, 65.70844075899055, 97.92331414587117, 17.892536648972055 }

    Returns: 8.880320508649147

  36. {20.0, 20.0, 20.0, 20.0, 10.0, 100.0 }

    Returns: 70.0

  37. {14.586716505257238, 17.575290010646093, 18.804511433353387, 15.522547896254405, 13.502284781928106, 11.296175351413897, 19.811484956328687, 18.765414696185104, 17.874809050989082, 18.342972086923538, 19.987332068666067 }

    Returns: 2.0136267194726973

  38. {82.9102, 70.6848, 21.503, 61.4588, 54.7789, 48.9889, 57.6766, 91.1859, 26.3674, 55.4601, 53.9357, 87.2005, 78.4771, 65.0102, 18.619, 90.296, 26.3894, 53.8588, 91.8369, 58.8028, 74.0577, 28.2406, 65.609, 59.4867, 27.7544, 54.6992, 69.2428, 22.6264, 87.0083, 58.5116, 60.286, 20.4318, 65.6475, 11.8348, 36.3488, 92.8092, 60.7392, 98.124, 48.1292, 39.5459, 52.2657, 34.3519, 38.9279, 93.0152, 11.3157 }

    Returns: 22.0175905

  39. {44.0, 66.0, 77.0, 88.0, 77.0, 55.0, 12.0, 12.0, 12.0, 45.0, 66.0, 78.0, 78.0, 45.0, 33.0, 45.0, 66.0, 12.0, 55.0, 12.0, 45.0, 78.0, 78.0, 55.0, 12.0, 12.0, 45.0, 45.0, 45.0, 87.0, 78.0, 88.0, 78.0, 54.0, 45.0, 12.0, 45.0, 77.0, 44.0, 66.0, 98.0, 78.0, 44.0, 54.0, 68.0, 68.0, 77.0, 85.0, 55.0, 10.0 }

    Returns: 19.9

  40. {20.0, 20.0, 20.0, 20.0, 20.0, 100.0 }

    Returns: 80.0

  41. {25.0, 25.0, 10.0, 10.0, 10.0, 100.0 }

    Returns: 60.0

  42. {51.0, 99.0, 17.0, 45.0, 67.0, 69.0, 89.0, 52.0, 65.0, 62.0, 12.0, 30.0, 60.0, 37.0, 92.0, 63.0, 71.0, 16.0, 72.0, 18.0 }

    Returns: 21.3

  43. {40.0, 40.0, 40.0, 10.0, 10.0, 100.0 }

    Returns: 0.0

  44. {11.0, 21.0, 49.0, 17.0, 32.0, 39.0, 32.0, 32.0, 32.0, 32.0, 32.0, 32.0, 11.0, 19.0, 99.0, 99.0, 99.0, 11.0, 11.0, 11.0, 11.0, 29.0, 95.0, 48.0, 97.0, 59.0 }

    Returns: 19.16190476190476

  45. {10.0, 10.0, 10.0, 10.0, 25.0, 100.0 }

    Returns: 75.0

  46. {10.0, 10.0, 10.0, 10.0, 10.0, 50.0 }

    Returns: 40.0

  47. {10.0, 11.0, 12.0, 13.0, 14.0, 24.0 }

    Returns: 6.0

  48. {10.0, 30.0, 30.0, 30.0, 10.0, 60.0, 60.0 }

    Returns: 11.0

  49. {99.0, 99.0, 99.0, 20.0, 15.0, 19.0 }

    Returns: 0.0

  50. {90.2, 70.9, 70.5, 30.3, 90.9, 50.9, 30.7 }

    Returns: 0.07999999999999652


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: