The AI.EVALUATE function
This document describes the AI.EVALUATE function, which lets you
evaluate TimesFM forecasted data against a
reference time series based on historical data, or
TabFM predicted data against
ground truth data.
For example, suppose you have tables that contain data about car rentals each day. The following query returns statistics about how well the model predicts car rentals in the current year based on the previous year:
SELECT *
FROM
AI.EVALUATE(
TABLE rentals.last_year,
TABLE rentals.this_year,
data_col => 'num_rentals',
timestamp_col => 'date');
Syntax
Choose one of the following syntaxes based on whether you're evaluating TimesFM or TabFM predictions:
TimesFM
SELECT
*
FROM
AI.EVALUATE(
{ TABLE HISTORY_TIME_SERIES_TABLE | (HISTORY_TIME_SERIES_QUERY_STATEMENT) },
{ TABLE ACTUAL_TIME_SERIES_TABLE | (ACTUAL_TIME_SERIES_QUERY_STATEMENT) },
data_col => 'DATA_COL',
timestamp_col => 'TIMESTAMP_COL',
[, model => 'MODEL']
[, id_cols => ID_COLS]
[, horizon => HORIZON]
[, context_window => CONTEXT_WINDOW]
)
Arguments
AI.EVALUATE takes the following arguments:
HISTORY_TIME_SERIES_TABLE: the name of the table that contains historical time series data, which is used to generate a forecast. For example,`mydataset.mytable`. The forecasted values are then evaluated against the data in theACTUAL_TIME_SERIES_TABLEorACTUAL_TIME_SERIES_QUERY_STATEMENTargument.If the table is in a different project, then you must prepend the project ID to the table name in the following format, including backticks:
`[PROJECT_ID].[DATASET].[TABLE]`For example,
`myproject.mydataset.mytable`.HISTORY_TIME_SERIES_QUERY_STATEMENT: the GoogleSQL query that generates historical time series data which is used to generate a forecast. The forecasted values are then evaluated against the data in theACTUAL_TIME_SERIES_TABLEorACTUAL_TIME_SERIES_QUERY_STATEMENTargument. See the GoogleSQL query syntax page for the supported SQL syntax of theHISTORY_TIME_SERIES_QUERY_STATEMENTclause.ACTUAL_TIME_SERIES_TABLE: the name of the table that contains the actual time series data. For example,`mydataset.mytable`. The data in this table is evaluated against the forecasted values for the historical time series provided by theHISTORY_TIME_SERIES_TABLEorHISTORY_TIME_SERIES_QUERY_STATEMENTargument.If the table is in a different project, then you must prepend the project ID to the table name in the following format, including backticks:
`[PROJECT_ID].[DATASET].[TABLE]`For example,
`myproject.mydataset.mytable`.ACTUAL_TIME_SERIES_QUERY_STATEMENT: the GoogleSQL query that generates the actual time series data. The data from this query is evaluated against the forecasted values for the historical time series provided by theHISTORY_TIME_SERIES_TABLEorHISTORY_TIME_SERIES_QUERY_STATEMENTargument. See the GoogleSQL query syntax page for the supported SQL syntax of theACTUAL_TIME_SERIES_QUERY_STATEMENTclause.DATA_COL: aSTRINGvalue that specifies the name of the time series data column. The data column must use one of the following data types:INT64NUMERICBIGNUMERICFLOAT64
TIMESTAMP_COL: aSTRINGvalue that specifies the name of the timestamp column. The timestamp column must use one of the following data types:TIMESTAMPDATEDATETIME
MODEL: aSTRINGvalue that specifies the name of the model to use. Supported models includeTimesFM 2.5andTimesFM 3.0(Preview). The default value isTimesFM 2.5.ID_COLS: anARRAY<STRING>value that specifies the names of one or more ID columns. Each unique combination of IDs identifies a unique time series to evaluate. Specify one or more values for this argument in order to evaluate multiple time series using a single query. The columns that you specify must use one of the following data types:STRINGINT64
HORIZON: anINT64value that specifies the number of forecasted time points to evaluate. The default value is1024. The valid input range is[1, 10,000].CONTEXT_WINDOW: anINT64value that specifies the context window length used by BigQuery ML's built-in TimesFM model. The context window length determines how many of the most recent data points from the input time series are used by the model. For example, if your time series date range is March 1 to April 15, data points are selected starting at April 15 and working backwards. Valid values for models are as follows:Model name Supported context window length TimesFM 2.564, 128, 256, 512, 1024, 2048, 4096, 8192, 15360 TimesFM 3.0n * 32wherenis an integer in the range[2, 64]If you don't specify a
CONTEXT_WINDOWvalue when you use theTimesFM 2.5model, theAI.FORECASTfunction automatically chooses the smallest possible context window length to use that is still large enough to cover the number of time series data points in your input data. If you don't specify aCONTEXT_WINDOWvalue when you use theTimesFM 3.0model, theAI.FORECASTfunction defaults to the maximum supported size,2048. The following table shows the relationships between the number of time series data points in the input data, the selected context window length, and the corresponding supported TimesFM model name:Number of time series data points Context window length Supported model names ( 1,64]64 TimesFM 2.5,TimesFM 3.0( 65,128]128 TimesFM 2.5,TimesFM 3.0( 129,256]256 TimesFM 2.5,TimesFM 3.0( 257,512]512 TimesFM 2.5,TimesFM 3.0( 513,1024]1,024 TimesFM 2.5,TimesFM 3.0( 1025,2048]2,048 TimesFM 2.5,TimesFM 3.0( 2049,4096]4,096 TimesFM 2.5( 4097,8192]8,192 TimesFM 2.5( 8193,15360]15,360 TimesFM 2.51536015,360 TimesFM 2.5For the
TimesFM 3.0model, 2,048 is the maximum number of time series data points that are passed to the model. For theTimesFM 2.5model, 15,360 is the maximum number of time series data points that are passed to the model. Any additional time series data points in the input data are ignored.
TabFM
SELECT
*
FROM
AI.EVALUATE(
{ TABLE TRAINING_TABLE | (TRAINING_QUERY) },
{ TABLE PREDICTION_TABLE | (PREDICTION_QUERY) },
label_col => 'LABEL_COL'
[, n_ensembles => NUM_ENSEMBLES]
)
Arguments
AI.EVALUATE takes the following arguments:
TRAINING_TABLE | TRAINING_QUERY: the table or query that contains the training data. All feature columns inTRAINING_TABLE(exceptlabel_col) must be present inPREDICTION_TABLEwith the exact same data type. The table or query result must contain a column namedlabelor the column that you specify in theLABEL_COLargument. Every other column is considered a feature column. The feature and label columns must be one of the following types:STRING,BOOL,INT64,FLOAT64,NUMERICorBIGNUMERIC.PREDICTION_TABLE | PREDICTION_QUERY: the table or query that contains the data to evaluate. The table or query result must contain all of the feature columns in the training data and can optionally contain additional columns.LABEL_COL: aSTRINGvalue that specifies the name of the label column in the training data. You must provide this argument for TabFM evaluation. If the column is of typeSTRINGorBOOL, then classification is evaluated. If the column is of typeINT64,FLOAT64,NUMERICorBIGNUMERIC, then regression is evaluated.NUM_ENSEMBLES: anINT64value between1and32that specifies the number of ensemble members to use for evaluation. The default value is4.
Output
AI.EVALUATE returns the following columns based on whether you're evaluating
TimesFM or TabFM predictions:
TimesFM
The following columns are returned for TimesFM forecasted data:
id_cols: one or more values that contain the identifiers of a time series.id_colscan be anINT64,STRING,ARRAY<INT64>orARRAY<STRING>value. The column names and types are inherited from theID_COLSargument value specified in the function input.mean_absolute_error: aFLOAT64value that contains the mean absolute error for the time series.mean_squared_error: aFLOAT64value that contains the mean squared error for the time series.root_mean_squared_error: aFLOAT64value that contains the root mean squared error for the time series.mean_absolute_percentage_error: aFLOAT64value that contains the mean absolute percentage error for the time series.symmetric_mean_absolute_percentage_error: aFLOAT64value that contains the symmetric mean absolute percentage error for the time series.mean_absolute_scaled_error: aFLOAT64value that contains the mean absolute scaled error for the time series.ai_evaluate_status: aSTRINGvalue that contains the evaluation status. The value is empty if the operation was successful. If the operation wasn't successful, the value is the error string. A common error isThe time series data is too short.This error indicates that there wasn't enough historical data in the time series to generate forecasted data to evaluate. A minimum of 3 data points is required.
TabFM
The following columns are returned for TabFM regression predictions:
mean_absolute_error: aFLOAT64value that contains the mean absolute error for the data.mean_squared_error: aFLOAT64value that contains the mean squared error for the data.mean_squared_log_error: aFLOAT64value that contains the mean squared logarithmic error for the data.median_absolute_error: aFLOAT64value that contains the median absolute error for the data.r2_score: aFLOAT64value that contains the coefficient of determination for the data.explained_variance: aFLOAT64value that contains the explained variance for the data.
The following columns are returned for TabFM classification predictions:
precision: aFLOAT64value that contains the macro-average precision across all classes.recall: aFLOAT64value that contains the macro-average recall across all classes.accuracy: aFLOAT64value that contains the accuracy of the prediction.f1_score: aFLOAT64value that contains the macro-averageF1score across all classes.
Examples
The following examples show how to use the AI.EVALUATE function.
Evaluate a single time series
The following example evaluates historical bike trips against actual bike trips for a single time series:
WITH
citibike_trips AS (
SELECT EXTRACT(DATE FROM starttime) AS date, COUNT(*) AS num_trips
FROM `bigquery-public-data.new_york.citibike_trips`
GROUP BY date
)
SELECT *
FROM
AI.EVALUATE(
(SELECT * FROM citibike_trips WHERE date < '2016-07-01'),
(SELECT * FROM citibike_trips WHERE date >= '2016-07-01'),
data_col => 'num_trips',
timestamp_col => 'date');
The result is similar to the following:
+---------------------+--------------------+-------------------------+--------------------------------+------------------------------------------+-----------------------------+--------------------+
| mean_absolute_error | mean_squared_error | root_mean_squared_error | mean_absolute_percentage_error | symmetric_mean_absolute_percentage_error | mean_absolute_scaled_error | ai_evaluate_status |
+---------------------+--------------------+-------------------------+--------------------------------+------------------------------------------+-----------------------------+--------------------+
| 7512.2744140624982 | 88702684.834815472 | 9418.210277691589 | 16.068001108491149 | 15.740030591250889 | 0.981234567 | null |
+---------------------+--------------------+-------------------------+--------------------------------+------------------------------------------+-----------------------------+--------------------+
Evaluate multiple time series
The following example evaluates historical bike trips against actual bike trips for multiple time series:
WITH
citibike_trips AS (
SELECT EXTRACT(DATE FROM starttime) AS date, usertype, COUNT(*) AS num_trips
FROM `bigquery-public-data.new_york.citibike_trips`
GROUP BY date, usertype
)
SELECT *
FROM
AI.EVALUATE(
(SELECT * FROM citibike_trips WHERE date < '2016-07-01'),
(SELECT * FROM citibike_trips WHERE date >= '2016-07-01'),
data_col => 'num_trips',
timestamp_col => 'date',
id_cols => ['usertype']);
Evaluate regression prediction
The following query evaluates the performance of TabFM regression to predict a penguin's mass:
WITH prepared_data AS (
SELECT *, RAND() <= 0.8 AS training
FROM `bigquery-public-data.ml_datasets.penguins`
WHERE body_mass_g > 0
)
SELECT
*
FROM
AI.EVALUATE(
# Training data
(SELECT * EXCEPT(training) FROM prepared_data WHERE training),
# Prediction data
(SELECT * EXCEPT(training) FROM prepared_data WHERE NOT training),
label_col => 'body_mass_g');
The result is similar to the following:
+---------------------+--------------------+------------------------+-----------------------+---------------------+---------------------+
| mean_absolute_error | mean_squared_error | mean_squared_log_error | median_absolute_error | r2_score | explained_variance |
+---------------------+--------------------+------------------------+-----------------------+---------------------+---------------------+
| 187.47826086956516 | 57233.014492753638 | 0.0036635839277726163 | 160.0 | 0.91107011835200613 | 0.91265547176600315 |
+---------------------+--------------------+------------------------+-----------------------+---------------------+---------------------+
The following query evaluates the performance of TabFM regression to predict
a penguin's mass with the number of ensembles set to 4:
WITH prepared_data AS (
SELECT *, RAND() <= 0.8 AS training
FROM `bigquery-public-data.ml_datasets.penguins`
WHERE body_mass_g > 0
)
SELECT
*
FROM
AI.EVALUATE(
# Training data
(SELECT * EXCEPT(training) FROM prepared_data WHERE training),
# Prediction data
(SELECT * EXCEPT(training) FROM prepared_data WHERE NOT training),
label_col => 'body_mass_g',
n_ensembles => 4);
The result is similar to the following:
+---------------------+--------------------+------------------------+-----------------------+---------------------+---------------------+
| mean_absolute_error | mean_squared_error | mean_squared_log_error | median_absolute_error | r2_score | explained_variance |
+---------------------+--------------------+------------------------+-----------------------+---------------------+---------------------+
| 186.40924242424236 | 54079.01102575756 | 0.0035122601481680986 | 186.75 | 0.9126692412103394 | 0.9127772522404515 |
+---------------------+--------------------+------------------------+-----------------------+---------------------+---------------------+
Evaluate classification prediction
The following query evaluates the performance of TabFM classification to predict a penguin's sex:
WITH prepared_data AS (
SELECT *, RAND() <= 0.8 AS training
FROM `bigquery-public-data.ml_datasets.penguins`
WHERE sex IS NOT NULL and sex != "."
)
SELECT
*
FROM
AI.EVALUATE(
# Training data
(SELECT * EXCEPT(training) FROM prepared_data WHERE training),
# Prediction data
(SELECT * EXCEPT(training) FROM prepared_data WHERE NOT training),
label_col => 'sex');
The result is similar to the following:
+---------------------+---------------------+----------+-------------------+
| precision | recall | accuracy | f1_score |
+---------------------+---------------------+----------+-------------------+
| 0.96511627906976738 | 0.95714285714285707 | 0.96 | 0.959539651141881 |
+---------------------+---------------------+----------+-------------------+
Locations
AI.EVALUATE and the TimesFM and TabFM models are available in all
supported BigQuery ML locations.
Data processing and residency
Data processing and residency for AI.EVALUATE depend on your query location:
- Queries run in the
USmulti-region or any US single region are processed in the US. - Queries run in the
EUmulti-region or any EU single region other thaneurope-west2(London) andeurope-west6(Switzerland) are processed in the EU. - All other locations, including
europe-west2(London) andeurope-west6(Switzerland), have no data residency commitment, and data might be processed outside your query region.
Pricing
AI.EVALUATE usage is billed at the evaluation, inspection, and prediction
rate documented in the BigQuery ML on-demand pricing section
of the BigQuery ML pricing
page.
TimesFM pricing
During Preview, usage of TimesFM 3.0 in BigQuery is billed in
the following ways:
- If you use Enterprise or Enterprise Plus edition, then your usage is billed in slots.
- If you use on-demand pricing, then your usage is billed based on the number of bytes processed.
TimesFM 3.0 will use a token based pricing from 12/01/2026
onwards. At that time, you will be charged for tokens consumed by the
model in your query and BigQuery slots used or bytes processed
for the rest of the query.
TabFM pricing
BigQuery TabFM uses token-based pricing in addition to standard BigQuery compute pricing. You are charged for two components:
- TabFM tokens: tokens consumed by the TabFM model in your query. For rates, see BigQuery ML pricing.
- BigQuery compute: slots used under Enterprise or Enterprise Plus edition, or bytes processed under on-demand pricing, for the rest of the query.
Limitations
The following limitations apply when using AI.EVALUATE with the TabFM model:
- Your training data can include up to 80,000 sampled training rows.
- Your data can include up to 128 feature columns. If you need to use more than 128 feature columns, contact bqml-feedback@google.com.
- You can classify data into at most 10 different categories.
AI.EVALUATEwith the TabFM model can't be used in Assured Workloads projects.
What's next
- Try using a TimesFM model with the
AI.FORECASTfunction. - Try performing regression or classification with the
AI.PREDICTfunction. - For information about forecasting in BigQuery ML, see Forecasting overview.
- For more information about supported SQL statements and functions for time series forecasting models, see End-to-end user journeys for time series forecasting models.