|
| 1 | +# BigQuery AI.Detect_Anomalies |
| 2 | + |
| 3 | +`AI.DETECT_ANOMALIES` uses the pre-trained **TimesFM** model to identify |
| 4 | +deviations in time series data without needing to train a custom model. |
| 5 | + |
| 6 | +## Syntax Reference |
| 7 | + |
| 8 | +This function compares a target dataset against a historical dataset to identify |
| 9 | +anomalies. |
| 10 | + |
| 11 | +```sql |
| 12 | +SELECT * |
| 13 | +FROM AI.DETECT_ANOMALIES( |
| 14 | + { TABLE `project.dataset.history_table` | (SELECT * FROM history_query) }, |
| 15 | + { TABLE `project.dataset.target_table` | (SELECT * FROM target_query) }, |
| 16 | + data_col => 'DATA_COL', |
| 17 | + timestamp_col => 'TIMESTAMP_COL' |
| 18 | + [, model => 'MODEL'] |
| 19 | + [, id_cols => ID_COLS] |
| 20 | + [, anomaly_prob_threshold => ANOMALY_PROB_THRESHOLD] |
| 21 | +) |
| 22 | + |
| 23 | +``` |
| 24 | + |
| 25 | +### Input Arguments |
| 26 | + |
| 27 | +Argument | Requirement | Type | Description |
| 28 | +:--------------------------- | :----------- | :------------ | :---------- |
| 29 | +**`historical_data`** | **Required** | Table/Query | The source table or subquery containing historical data for training context. |
| 30 | +**`target_data`** | **Required** | Table/Query | The source table or subquery containing data to analyze for anomalies. |
| 31 | +**`data_col`** | **Required** | String | The numeric column to analyze. |
| 32 | +**`timestamp_col`** | **Required** | String | The column containing dates/timestamps. |
| 33 | +**`id_cols`** | Optional | Array<String> | Grouping columns for multiple series (e.g., `['store_id']`). |
| 34 | +**`anomaly_prob_threshold`** | Optional | Float64 | Threshold for anomaly detection (0 to 1). Defaults to 0.95. |
| 35 | +**`model`** | Optional | String | Model version. Defaults to `'TimesFM 2.0'`. |
| 36 | + |
| 37 | +### Output Schema |
| 38 | + |
| 39 | +| Column | Type | Description | |
| 40 | +| :------------------------------- | :--------- | :--------------------------- | |
| 41 | +| **`id_cols`** | (As Input) | Original identifiers for the | |
| 42 | +: : : series. : |
| 43 | +| **`time_series_timestamp`** | TIMESTAMP | Timestamp for the analyzed | |
| 44 | +: : : points. : |
| 45 | +| **`time_series_data`** | FLOAT64 | The original data value. | |
| 46 | +| **`is_anomaly`** | BOOL | TRUE if the point is | |
| 47 | +: : : identified as an anomaly. : |
| 48 | +| **`lower_bound`** | FLOAT64 | Lower bound of the expected | |
| 49 | +: : : range. : |
| 50 | +| **`upper_bound`** | FLOAT64 | Upper bound of the expected | |
| 51 | +: : : range. : |
| 52 | +| **`anomaly_probability`** | FLOAT64 | Probability that the point | |
| 53 | +: : : is an anomaly. : |
| 54 | +| **`ai_detect_anomalies_status`** | STRING | Error messages or empty | |
| 55 | +: : : string on success. A minimum : |
| 56 | +: : : of 3 data points is : |
| 57 | +: : : required. : |
| 58 | + |
| 59 | +## Examples |
| 60 | + |
| 61 | +### Basic Anomaly Detection |
| 62 | + |
| 63 | +Detect anomalies in daily bike trips for a specific 2-month window based on |
| 64 | +prior history. |
| 65 | + |
| 66 | +```sql |
| 67 | +WITH bike_trips AS ( |
| 68 | + SELECT EXTRACT(DATE FROM starttime) AS date, COUNT(*) AS num_trips |
| 69 | + FROM `bigquery-public-data.new_york.citibike_trips` |
| 70 | + GROUP BY date |
| 71 | +) |
| 72 | +SELECT * |
| 73 | +FROM AI.DETECT_ANOMALIES( |
| 74 | + -- Historical context (Training data equivalent) |
| 75 | + (SELECT * FROM bike_trips WHERE date <= DATE('2016-06-30')), |
| 76 | + -- Target range (Data to inspect for anomalies) |
| 77 | + (SELECT * FROM bike_trips WHERE date BETWEEN '2016-07-01' AND '2016-09-01'), |
| 78 | + data_col => 'num_trips', |
| 79 | + timestamp_col => 'date' |
| 80 | +); |
| 81 | + |
| 82 | +``` |
| 83 | + |
| 84 | +### Multivariate Detection (Multiple Series) |
| 85 | + |
| 86 | +Use `id_cols` to detect anomalies separately for different user types (e.g., |
| 87 | +Subscriber vs. Customer) in the same query. |
| 88 | + |
| 89 | +```sql |
| 90 | +WITH bike_trips AS ( |
| 91 | + SELECT |
| 92 | + EXTRACT(DATE FROM starttime) AS date, usertype, gender, |
| 93 | + COUNT(*) AS num_trips |
| 94 | + FROM `bigquery-public-data.new_york.citibike_trips` |
| 95 | + GROUP BY date, usertype, gender |
| 96 | + ) |
| 97 | +SELECT * |
| 98 | +FROM |
| 99 | + AI.DETECT_ANOMALIES( |
| 100 | + # Historical data from a query |
| 101 | + (SELECT * FROM bike_trips WHERE date <= DATE('2016-06-30')), |
| 102 | + # Target data from a query |
| 103 | + (SELECT * FROM bike_trips WHERE date BETWEEN '2016-07-01' AND '2016-09-01'), |
| 104 | + data_col => 'num_trips', |
| 105 | + timestamp_col => 'date', |
| 106 | + id_cols => ['usertype', 'gender'], |
| 107 | + model => "TimesFM 2.5", |
| 108 | + anomaly_prob_threshold => 0.8); |
| 109 | + |
| 110 | +``` |
0 commit comments