Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8362045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:51:02+00:00 2026-06-09T11:51:02+00:00

I’m writing some analytics modules for the site I’m working on and I need

  • 0

I’m writing some analytics modules for the site I’m working on and I need to estimate the total views after the current hour. I have data for every minute up to the current minute, so if the time is 12:28, I will have an array that looks something like this:

0: "21410"
1: "21886"
2: "21837"
3: "21895"
4: "21564"
5: "21714"
6: "21571"
7: "21324"
8: "21310"
9: "21390"
10: "21764"
11: "21598"
12: "21493"
13: "21352"
14: "21478"
15: "21058"
16: "20942"
17: "20825"
18: "21321"
19: "20950"
20: "21039"
21: "21117"
22: "20733"
23: "20773"
24: "20929"
25: "20900"
26: "20687"
27: "20999"

Currently I am projecting the hour’s value like this:

(60/minsSoFar)*totalSoFar

This works reasonably well, but I’d rather do it a bit more mathematically. I’d like to calculate the line of best fit for the data I have so far and project that up to the 60th minute. This would take into account acceleration and deceleration.

With the method I’m currently using, I’m effectively assuming the trend is a straight line. How would I calculate the formula for a polynomial or power trend?

I’m writing this in NodeJS so JavaScript would be ideal, but I’ll take pseudocode too!

Here’s the array in an easier format in case you want it:

[21410, 21886, 21837, 21895, 21564, 21714, 21571, 21324, 21310, 21390, 21764, 21598, 21493, 21352, 21478, 21058, 20942, 20825, 21321, 20950, 21039, 21117, 20733, 20773, 20929, 20900, 20687, 20999]

Thanks for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:51:04+00:00Added an answer on June 9, 2026 at 11:51 am

    You can do a least-squares fit of a line.

    function LineFitter()
    {
        this.count = 0;
        this.sumX = 0;
        this.sumX2 = 0;
        this.sumXY = 0;
        this.sumY = 0;
    }
    
    LineFitter.prototype = {
        'add': function(x, y)
        {
            this.count++;
            this.sumX += x;
            this.sumX2 += x*x;
            this.sumXY += x*y;
            this.sumY += y;
        },
        'project': function(x)
        {
            var det = this.count * this.sumX2 - this.sumX * this.sumX;
            var offset = (this.sumX2 * this.sumY - this.sumX * this.sumXY) / det;
            var scale = (this.count * this.sumXY - this.sumX * this.sumY) / det;
            return offset + x * scale;
        }
    };
    
    function linearProject(data, x)
    {
        var fitter = new LineFitter();
        for (var i = 0; i < data.length; i++)
        {
            fitter.add(i, data[i]);
        }
        return fitter.project(x);
    }
    

    Example:

    >>> linearProject([
            21410, 21886, 21837, 21895, 21564, 21714, 21571, 21324, 21310, 21390,
            21764, 21598, 21493, 21352, 21478, 21058, 20942, 20825, 21321, 20950,
            21039, 21117, 20733, 20773, 20929, 20900, 20687, 20999
        ], 60);
    19489.614121510676
    

    Doing something similar for a square polynomial is a little more complicated:

    function SquareFitter()
    {
        this.count = 0;
        this.sumX = 0;
        this.sumX2 = 0;
        this.sumX3 = 0;
        this.sumX4 = 0;
        this.sumY = 0;
        this.sumXY = 0;
        this.sumX2Y = 0;
    }
    
    SquareFitter.prototype = {
        'add': function(x, y)
        {
            this.count++;
            this.sumX += x;
            this.sumX2 += x*x;
            this.sumX3 += x*x*x;
            this.sumX4 += x*x*x*x;
            this.sumY += y;
            this.sumXY += x*y;
            this.sumX2Y += x*x*y;
        },
        'project': function(x)
        {
            var det = this.count*this.sumX2*this.sumX4 - this.count*this.sumX3*this.sumX3 - this.sumX*this.sumX*this.sumX4 + 2*this.sumX*this.sumX2*this.sumX3 - this.sumX2*this.sumX2*this.sumX2;
            var offset = this.sumX*this.sumX2Y*this.sumX3 - this.sumX*this.sumX4*this.sumXY - this.sumX2*this.sumX2*this.sumX2Y + this.sumX2*this.sumX3*this.sumXY + this.sumX2*this.sumX4*this.sumY - this.sumX3*this.sumX3*this.sumY;
            var scale = -this.count*this.sumX2Y*this.sumX3 + this.count*this.sumX4*this.sumXY + this.sumX*this.sumX2*this.sumX2Y - this.sumX*this.sumX4*this.sumY - this.sumX2*this.sumX2*this.sumXY + this.sumX2*this.sumX3*this.sumY;
            var accel = this.sumY*this.sumX*this.sumX3 - this.sumY*this.sumX2*this.sumX2 - this.sumXY*this.count*this.sumX3 + this.sumXY*this.sumX2*this.sumX - this.sumX2Y*this.sumX*this.sumX + this.sumX2Y*this.count*this.sumX2;
            return (offset + x*scale + x*x*accel)/det;
        }
    };
    
    function squareProject(data)
    {
        var fitter = new SquareFitter();
        for (var i = 0; i < data.length; i++)
        {
            fitter.add(i, data[i]);
        }
        return fitter.project(60);
    }
    

    Example2:

    >>> squareProject([
            21410, 21886, 21837, 21895, 21564, 21714, 21571, 21324, 21310, 21390,
            21764, 21598, 21493, 21352, 21478, 21058, 20942, 20825, 21321, 20950,
            21039, 21117, 20733, 20773, 20929, 20900, 20687, 20999
        ], 60);
    19282.85862700518
    

    I could do this for higher degree polynomials, but the expressions would get even longer. For arbitrary degree, you would have to look at matrices.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I have thousands of HTML files to process using Groovy/Java and I need to
I have some data like this: 1 2 3 4 5 9 2 6
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.