Follow ______ on twitter.

7.4.2 Identifying Trends in Time Series for Risk Analysis

Code

Vega-Lite Chart

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "Comparison of Moving Average, Exponential Smoothing, and Trend Line Applied to Synthetic Time Series Data",
  "data": {
    "sequence": {
      "start": 1,
      "stop": 365,
      "step": 1,
      "as": "day"
    }
  },
  "transform": [
    {
      "calculate": "0.1 * datum.day + 0.0007 * pow(datum.day, 2)",
      "as": "trend"
    },
    {
      "calculate": "15 * sin(2 * PI * datum.day / (365 / 4))",
      "as": "seasonality"
    },
    {
      "calculate": "20 * sin(2 * PI * datum.day / (365 / 2))",
      "as": "cyclicality"
    },
    {
      "calculate": "sampleNormal(0, 10)",
      "as": "noise"
    },
    {
      "calculate": "120 + datum.trend + datum.seasonality + datum.cyclicality + datum.noise",
      "as": "value"
    },
    {
      "window": [
        {
          "op": "mean",
          "field": "value",
          "as": "moving_average"
        }
      ],
      "frame": [
        -50,
        50
      ]
    },
    {
      "calculate": "datum.value * 0.8 + (1 - 0.8) * (datum.previous_smoothing || datum.value)",
      "as": "exponential_smoothing"
    }
  ],
  "hconcat": [
    {
      "title": "Moving Average (50 day)",
      "layer": [
        {
          "mark": "line",
          "encoding": {
            "x": {
              "field": "day",
              "type": "quantitative",
              "title": "Day"
            },
            "y": {
              "field": "value",
              "type": "quantitative"
            },
            "color": {
              "value": "lightgray"
            }
          }
        },
        {
          "mark": "line",
          "encoding": {
            "x": {
              "field": "day",
              "type": "quantitative",
              "title": "Day"
            },
            "y": {
              "field": "moving_average",
              "type": "quantitative",
              "title": "Value"
            },
            "color": {
              "value": "blue"
            }
          }
        }
      ]
    },
    {
      "title": "Exponential Smoothing",
      "layer": [
        {
          "mark": "line",
          "encoding": {
            "x": {
              "field": "day",
              "type": "quantitative",
              "title": "Day"
            },
            "y": {
              "field": "value",
              "type": "quantitative"
            },
            "color": {
              "value": "lightgray"
            }
          }
        },
        {
          "mark": "line",
          "encoding": {
            "x": {
              "field": "day",
              "type": "quantitative",
              "title": "Day"
            },
            "y": {
              "field": "exponential_smoothing",
              "type": "quantitative",
              "title": "Value"
            },
            "color": {
              "value": "green"
            }
          }
        }
      ]
    },
    {
      "title": "Trend Line",
      "layer": [
        {
          "mark": "line",
          "encoding": {
            "x": {
              "field": "day",
              "type": "quantitative",
              "title": "Day"
            },
            "y": {
              "field": "value",
              "type": "quantitative"
            },
            "color": {
              "value": "lightgray"
            }
          }
        },
        {
          "transform": [
            {
              "regression": "value",
              "on": "day",
              "method": "linear",
              "as": [
                "day",
                "trend_line"
              ]
            }
          ],
          "mark": "line",
          "encoding": {
            "x": {
              "field": "day",
              "type": "quantitative",
              "title": "Day"
            },
            "y": {
              "field": "trend_line",
              "type": "quantitative",
              "title": "Trend Line"
            },
            "color": {
              "value": "red"
            }
          }
        }
      ]
    }
  ]
}