Concepts / Time Series Prediction

Time Series Prediction

The central question of time series prediction is a question about direction. You take everything known up to now and ask what you expect to happen next.

  • Programming

A Question About Tomorrow

Imagine you run a bike rental service and want to know how many bikes people will rent tomorrow. You have a record of every rental that has happened so far. The natural approach is to look at that record, notice what has been happening, and use it to estimate what will happen next. This is the core idea behind time series prediction: use the past to forecast the future.

The idea sounds simple, but it hides a constraint that shapes the whole task. A forecaster never knows the future. Any honest way of testing a forecasting model has to reproduce that situation, where the model only has access to what came before the moment it makes a prediction. Keeping that constraint in mind is the subject of this article.

Why the Order of Observations Matters

Time series data is recorded in sequence, so the order of the observations carries meaning. A plot of bike rentals over several days makes a pattern visible almost immediately: the daytime hours and the nighttime hours of each 24-hour cycle look clearly different. Weekdays also seem to follow a different rhythm than weekends.

These regularities are exactly what makes forecasting possible. They suggest that what happened before carries information about what will happen next. If the observations were shuffled into a random order, the daily and weekly rhythms would be scrambled, and the record would no longer tell a story about how the system moves through time.

Key point

The central question of time series prediction is a question about direction. You take everything known up to now and ask what you expect to happen next.

Choosing a Cutoff Date

To test a forecasting model honestly, you divide the dated record into two portions using a cutoff date. The training set consists of all data up to the chosen date, and the test set consists of all data after that date. The model learns only from the training portion and is judged only on the test portion.

The reason for this rule follows directly from the goal of prediction. If the test set contained moments that happened earlier than some training moments, the model would be evaluated on a situation it could never face in practice, because a real forecaster does not know the future. A date-based cutoff keeps the test period entirely in the future relative to everything the model learned from. The cutoff is a deliberate choice that marks where the past ends and the prediction period begins.

Start of recordearliest dateTraining setfirst 184 points (23 days)Cutoff datebetween day 23 and day 24Test setremaining 64 points (8days)End of recordlatest dateall data before cutoffall data after cutoff
Where does the cutoff fall on the dated timeline, and which rows land before it (training) versus after it (test)?

Splitting the Bike Rental Record

The source applies this rule to its bike rental data. It uses the first 184 data points, which correspond to the first 23 days, as the training set. The remaining 64 data points, which correspond to the remaining 8 days, form the test set. The cutoff falls between the 23rd day and the 24th day, so the model learns from the opening stretch of the record and is judged on the stretch that follows.

Worked example

Locating the cutoff in a generated record

A generated daily record covers 12 days, numbered 1 through 12. You want to train on the first 9 days and test on the rest. Which days belong to each set, and where is the cutoff?

  1. 1
    Identify the training days

    The training set holds all data up to the chosen date, so days 1 through 9 are training days.

  2. 2
    Identify the test days

    The test set holds all data after the cutoff, so days 10 through 12 are test days.

  3. 3
    Locate the cutoff

    The cutoff falls between day 9 and day 10. No test day occurs earlier than any training day.

Result

Training uses days 1 to 9, the cutoff sits between day 9 and day 10, and testing uses days 10 to 12.

Defining Input and Output in the Bike Rental Setup

A forecasting task needs a precise statement of what goes in and what comes out. In the bike rental example, the only input feature is the date and time at which a given number of rentals occurred. The source gives the example timestamp 2015-08-01 00:00:00 as the input. Nothing else about the rentals, such as weather or station details, is used in this particular setup.

The output is the number of rentals in the following three hours. Each prediction is therefore about a window of time that starts at the input timestamp, not a single instant. The source notes that this three-hour window is set according to the DataFrame used in its analysis.

Date and timee.g. 2015-08-01 00:00:00Forecasting modellearned from trainingportionRentals in next threehoursthe predicted valueonly featurepredicts
Which column flows into the model as the input feature, and which value must the model predict for a future date?

Ways an Evaluation Becomes Unrealistic

An evaluation becomes unrealistic when the model gains access to information it would not have at prediction time. The clearest case is a test set that contains moments earlier than some training moments. Because the model has already seen later data, its test score no longer reflects how it would perform on genuinely future days.

Rows mixed togetherearly and late daysinterleavedTraining includeslate dayslater moments seen duringtrainingTest includes earlydaysearlier moments judgedafter later onesCutoff datefixed point on the timelineTraining: all databefore cutoffonly the past is learnedTest: all data aftercutoffalways in the future oftraining
How does a randomly shuffled split let the model train on days that occur after the days it is tested on, and how does a time-ordered split prevent that?
  • Common mistake

    Shuffling the rows before splitting

    What it looks like

    Randomly assigning 20 percent of rows of a dated record to the test set.

    Why it fails

    Test rows can fall earlier in time than training rows, so the model is judged on a moment it would never face when forecasting.

    Do this instead

    Split by a cutoff date so that every test observation comes after every training observation.

  • Common mistake

    Including features that are not yet known at prediction time

    What it looks like

    Using the number of rentals in the three hours being predicted as an input feature.

    Why it fails

    The value being predicted is already inside the input, so the model's score measures lookup rather than forecasting.

    Do this instead

    Use only information that would be available at the input timestamp. In the source's setup, that is the date and time alone.

  • Common mistake

    Evaluating against the wrong output window

    What it looks like

    Scoring a prediction against the rentals in the next hour when the target is the next three hours.

    Why it fails

    A model evaluated against the wrong window can look better or worse for reasons unrelated to its quality.

Predict the Split Before Revealing It

What do you think happens?

The source's bike rental record has 31 days in total, and the training set covers the first 23 days. How many days belong to the test set?

Reveal answer
Answer:

8 days

The test set is everything after the cutoff. Since 23 days train the model and 31 days exist in total, the remaining 8 days form the test set. This matches the source's 64 test points corresponding to 8 days.

EASY

A generated record covers 20 days. You want to test on the final 5 days. Where should the cutoff date fall, and what would go wrong if you instead randomly chose 5 days from anywhere in the record for the test set?

Hints
  • The cutoff separates everything before it from everything after it.
  • Ask whether a randomly chosen test day could occur earlier than some training day.

Putting the Pieces Together

Time series prediction asks what will happen next, using what has already happened. The order of observations carries information, which is why an honest evaluation must respect the direction of time. You choose a cutoff date, train on everything before it, and test on everything after it. In the bike rental setup, the input is a date and time and the output is the number of rentals in the following three hours.

Key Takeaways

  • Time series prediction uses the past to forecast the future, so the order of observations must be respected.
  • The training set holds all data up to a cutoff date and the test set holds all data after it.
  • In the source's bike rental example, the first 184 points (23 days) train the model and the remaining 64 points (8 days) test it.
  • The only input feature is the date and time, and the output is the number of rentals in the following three hours.
  • Random shuffling and features that reveal future values make a time series evaluation unrealistic.

Related concepts

More concepts starting with T