Zero-Shot Time Series Forecasting in AlloyDB with Google’s TimesFM
Time series forecasting — predicting future values based on past data — is crucial for businesses. From inventory management to resource planning, accurate forecasts drive better decisions. However, traditional methods often involve complex model training, fine-tuning, and a deep understanding of statistical models like ARIMA or SARIMA. What if you could get accurate forecasts without the heavy lifting of model training?
Introducing native time series forecasting in AlloyDB, powered by TimesFM, Google’s advanced time series foundation model.
The Challenge with Traditional Forecasting
Classic forecasting models require significant effort:
- Data Pipeline Overhead: To leverage these models on operational data, data engineers need to set up complex pipelines to extract data from the database and feed it into external prediction models.
- Retraining Overhead: Models may need frequent retraining as data distributions shift.
What is TimesFM?
TimesFM is a powerful foundation model developed by Google Research, pre-trained on a massive dataset of 500 million time series. Leveraging a decoder-only transformer architecture, TimesFM excels at zero-shot forecasting. This means it can generate accurate predictions on new datasets without any specific training on that data.
The integration of TimesFM into AlloyDB via the google_ml_integration extension brings this zero-shot forecasting capability directly into your database.
Forecasting with timesfm and Alloydb
You can start generating forecasts within minutes.
- Deploying TimesFM from Vertex AI Model Garden: You’ll need to find TimesFM in the Model Garden and deploy it to a Vertex AI Endpoint. Full instructions are available in the AlloyDB documentation.
Key Tip: In order to leverage this model for prediction, ensure you select Public (Shared endpoint) under Advanced settings. Also, choose the same region as your AlloyDB instance for faster results.
2. Registering the Model in AlloyDB: Once deployed, you’ll get an endpoint URL. Use this to register the model within AlloyDB using google_ml.create_model(). See Register the model.
Step 3: Enable Forecasting & Generate Predictions
Enable the feature for your session or database:
-- For the current session
SET google_ml_integration.enable_forecasting = 'on';
-- Alternatively, for a specific database persistently
-- ALTER DATABASE your_db_name SET google_ml_integration.enable_forecasting = 'on';Now, you can call the ai.forecast function.
Example Dataset used: Forecasting Daily Temperature
I have created weeklytemperate_v2 table using the daily temperature from above dataset.
CREATE TABLE weeklyTemperature_v2 (id SERIAL PRIMARY KEY, week_date timestamp, avg_temp float);
INSERT INTO weeklyTemperature_v2(week_date, avg_temp)
SELECT
date_trunc('week', "timestamp") AS week_date,
AVG(temperature) AS avg_temp
FROM dailytemperature
GROUP BY date_trunc('week', "timestamp")
ORDER BY week_date;
-- Insert valuesSELECT * FROM ai.forecast(
model_id => 'timesfm_v2', -- model registered via google_ml.create_model
source_query => ' select * from weeklytemperature_v2 order by week_date limit 300',
data_col => 'avg_temp',
timestamp_col => 'week_date',
horizon => 100,
conf_level => 0.95
);
forecast_timestamp | forecast_value | confidence_level | prediction_interval_lower_bound | prediction_interval_upper_bound | ai_forecast_status
---------------------+-------------------+------------------+---------------------------------+---------------------------------+--------------------
1986-09-29 00:00:00 | 9.549649238586426 | 0.95 | 7.986692428588867 | 11.08416938781738 | SUCCESS
1986-10-06 00:00:00 | 9.960966110229492 | 0.95 | 8.42795181274414 | 11.48766040802002 | SUCCESS
1986-10-13 00:00:00 | 10.23709678649902 | 0.95 | 8.740107536315918 | 11.75872993469238 | SUCCESS
1986-10-20 00:00:00 | 10.72667694091797 | 0.95 | 9.200634956359863 | 12.26481342315674 | SUCCESS
1986-10-27 00:00:00 | 11.32920169830322 | 0.95 | 9.83945083618164 | 12.88973236083984 | SUCCESS
1986-11-03 00:00:00 | 11.8832368850708 | 0.95 | 10.36215019226074 | 13.44828414916992 | SUCCESS
1986-11-10 00:00:00 | 12.31446552276611 | 0.95 | 10.79358863830566 | 13.89069652557373 | SUCCESS
1986-11-17 00:00:00 | 12.67383003234863 | 0.95 | 11.16984844207764 | 14.25028228759766 | SUCCESS
1986-11-24 00:00:00 | 13.22207736968994 | 0.95 | 11.70285987854004 | 14.79251098632812 | SUCCESS
1986-12-01 00:00:00 | 13.57160568237305 | 0.95 | 11.97588348388672 | 15.17686653137207 | SUCCESS
1986-12-08 00:00:00 | 13.78735828399658 | 0.95 | 12.24295043945312 | 15.38835906982422 | SUCCESS
...The output will include the forecast_timestamp(weekly next values), the forecasted value (prediction), and upper/lower bounds (prediction_upper, prediction_lower) based on the confidence level.
GRAPH VIEW
- Daily Temperature plot over 400 weeks.
2. Same plot as above with prediction with red line based on the context(previous data points) of 300 to predict next 100 points.
3. Focussed view for the 100 predicted points.
Why This Matters
- Simplicity: Generate forecasts with SQL, no complex ML pipelines needed within your application logic.
- Zero-Shot Power: Leverage Google’s pre-trained model without any training on your specific data.
- Speed: Get instant predictions, enabling real-time analytical applications.
- Decoupled Architecture: The ML model is managed in Vertex AI, while your data stays in AlloyDB.
Bonus
While TimesFM provides powerful zero-shot capabilities, you may want to leverage your own models. If your team has already developed its own custom forecasting models (perhaps using libraries like Prophet, ARIMA, or a custom-trained model) and deployed them on a Vertex AI Endpoint, you can register them with AlloyDB in the same way.
The google_ml.create_model() function is flexible. Instead of pointing to the TimesFM endpoint, you simply provide the endpoint URL for your own custom model. This allows you to keep your data in AlloyDB and call your preferred model using a simple SQL interface, giving you the choice between zero-shot speed and custom-tuned precision.
Conclusion
AlloyDB’s integration with TimesFM democratizes time series forecasting. By bringing a powerful, zero-shot foundation model directly to your data, you can unlock valuable insights and make more informed, data-driven business decisions with unprecedented ease and speed.