2

Math isn't really my thing, but what I'm trying to figure out is how to predict/estimate the next number from a dataset.

Let's say I have an array:

var values = new Array(1,4,3,5,6,10,4,15);

Does anyone know a formula in javascript that could guess the next number after 15 based off the previous values in the array.

Basically I have an array of total numbers from daily sales, each item in the array is the total for a single day. So I'd like something that could guess what tomorrow's sale might be.

Joe
  • 3,043
  • 9
  • 43
  • 56
  • I guess you can try averaging the last (maybe 5) values of the array? So the next estimate will be (5+6+10+4+15) / 5 = 8 in this case – irrelephant Oct 30 '10 at 23:34
  • 6
    If you get a good answer to this question, I plan to use it to extrapolate tomorrow's stock market values. :-) – xscott Oct 30 '10 at 23:36
  • 1
    You should not be asking this question if math isn't your thing. – David Titarenco Oct 30 '10 at 23:39
  • Lol @xscott. @irrelephant, the problem averaging the numbers is if say the array is (1,2,3,4,20,30) the trend shows it's on the rise but using the average is not close to accurate – Joe Oct 30 '10 at 23:41
  • Maybe a quadratic regression will show the trend better? http://stackoverflow.com/questions/2075013/best-way-to-find-quadratic-regression-curve-in-java – irrelephant Oct 31 '10 at 00:26
  • 3
    @DavidTitarenco We're all here to learn, aren't we? – Luc Jan 09 '14 at 23:51

2 Answers2

2

Based on the data you're providing, it seems you can only predict what tomorrow's sale might be by taking the average of your dataset.

If you had additional data, say, day of the week, you could take the average of all sales on Tuesdays, and then make a prediction based off of that average.

Calvin
  • 8,697
  • 7
  • 43
  • 51
  • that's what I'm thinking about doing, it may not be 100% accurate especially if sales are on the rise or fall. – Joe Oct 30 '10 at 23:46
  • Well, like xscott says, if you can get something 100% accurate, then let me know :) – Calvin Oct 30 '10 at 23:47
  • The point is that the more data you have, the more accurate your prediction will be. You'll never be able to get to 100% though. – Calvin Oct 30 '10 at 23:48
1

Have a look at the various moving average methods - you can choose whichever suits your application best.

casablanca
  • 69,683
  • 7
  • 133
  • 150
  • you can't predict future with calculating an average. That's nonsense! Try tensorflow. – Grasper Mar 23 '22 at 12:43
  • 1
    @Grasper: TensorFlow wasn't even around back when I posted this answer :) Also not everything needs a fancy ML model, exponential moving average is a simple but effective technique that is commonly used for predictions. – casablanca Mar 24 '22 at 08:06