Follow ______ on twitter.

7.3.3 Advanced forecasting with exogenous variables

Code

Vega-Lite Chart

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "ARIMAX Risk Analysis for Portfolio Returns",
  "hconcat": [
    {
      "title": "Normalized Returns",
      "width": 200,
      "height": 200,
      "mark": {
        "type": "line",
        "point": true
      },
      "encoding": {
        "x": {
          "field": "date",
          "type": "temporal",
          "title": "Date"
        },
        "y": {
          "field": "norm_return",
          "type": "quantitative",
          "title": "Normalized Return"
        },
        "color": {
          "value": "#00497677"
        }
      }
    },
    {
      "title": [
        "Normalized Exogenous Variable",
        " and Forecast (Market Index)"
      ],
      "width": 200,
      "height": 200,
      "layer": [
        {
          "mark": {
            "type": "line",
            "point": true
          },
          "encoding": {
            "x": {
              "field": "date",
              "type": "temporal",
              "title": "Date"
            },
            "y": {
              "field": "norm_market_index",
              "type": "quantitative",
              "title": "Normalized Market Index"
            },
            "color": {
              "value": "green"
            }
          }
        },
        {
          "mark": {
            "type": "line",
            "point": true,
            "strokeDash": [
              4,
              4
            ]
          },
          "encoding": {
            "x": {
              "field": "date",
              "type": "temporal",
              "title": "Date"
            },
            "y": {
              "field": "market_forecast",
              "type": "quantitative"
            },
            "color": {
              "value": "red"
            }
          }
        },
        {
          "mark": {
            "type": "line",
            "point": true,
            "strokeDash": [
              2,
              2
            ]
          },
          "encoding": {
            "x": {
              "field": "date",
              "type": "temporal",
              "title": "Date"
            },
            "y": {
              "field": "market_worst_case",
              "type": "quantitative"
            },
            "color": {
              "value": "orange"
            }
          }
        }
      ],
      "resolve": {
        "scale": {
          "color": "independent"
        }
      },
      "encoding": {
        "color": {
          "field": "type",
          "type": "nominal",
          "scale": {
            "domain": [
              "Historical",
              "Expected Forecast",
              "1/20 Worst Case"
            ],
            "range": [
              "blue",
              "red",
              "orange"
            ]
          },
          "legend": {
            "title": "Market Index"
          }
        }
      }
    },
    {
      "title": "Normalized Return Forecast",
      "width": 200,
      "height": 200,
      "layer": [
        {
          "mark": {
            "type": "line",
            "point": true
          },
          "encoding": {
            "x": {
              "field": "date",
              "type": "temporal",
              "title": "Date"
            },
            "y": {
              "field": "norm_return",
              "type": "quantitative",
              "title": "Normalized Return"
            },
            "color": {
              "value": "#00497677"
            }
          }
        },
        {
          "mark": {
            "type": "line",
            "point": true,
            "strokeDash": [
              4,
              4
            ]
          },
          "encoding": {
            "x": {
              "field": "date",
              "type": "temporal",
              "title": "Date"
            },
            "y": {
              "field": "return_forecast",
              "type": "quantitative"
            },
            "color": {
              "value": "red"
            }
          }
        },
        {
          "mark": {
            "type": "line",
            "point": true,
            "strokeDash": [
              2,
              2
            ]
          },
          "encoding": {
            "x": {
              "field": "date",
              "type": "temporal",
              "title": "Date"
            },
            "y": {
              "field": "return_worst_case",
              "type": "quantitative"
            },
            "color": {
              "value": "orange"
            }
          }
        }
      ],
      "resolve": {
        "scale": {
          "color": "independent"
        }
      },
      "encoding": {
        "color": {
          "field": "type",
          "type": "nominal",
          "scale": {
            "domain": [
              "Historical",
              "Expected Forecast",
              "1/20 Worst Case"
            ],
            "range": [
              "blue",
              "red",
              "orange"
            ]
          },
          "legend": {
            "title": "Normalized Return"
          }
        }
      }
    },
    {
      "title": "Return Forecast (Regular Space)",
      "width": 200,
      "height": 200,
      "layer": [
        {
          "mark": {
            "type": "line",
            "point": true
          },
          "encoding": {
            "x": {
              "field": "date",
              "type": "temporal",
              "title": "Date"
            },
            "y": {
              "field": "return",
              "type": "quantitative",
              "title": "Return (%)"
            },
            "color": {
              "value": "#004976"
            }
          }
        },
        {
          "mark": {
            "type": "line",
            "point": true,
            "strokeDash": [
              4,
              4
            ]
          },
          "encoding": {
            "x": {
              "field": "date",
              "type": "temporal",
              "title": "Date"
            },
            "y": {
              "field": "regular_forecast",
              "type": "quantitative"
            },
            "color": {
              "value": "red"
            }
          }
        },
        {
          "mark": {
            "type": "line",
            "point": true,
            "strokeDash": [
              2,
              2
            ]
          },
          "encoding": {
            "x": {
              "field": "date",
              "type": "temporal",
              "title": "Date"
            },
            "y": {
              "field": "regular_worst_case",
              "type": "quantitative"
            },
            "color": {
              "value": "orange"
            }
          }
        }
      ],
      "resolve": {
        "scale": {
          "color": "independent"
        }
      },
      "encoding": {
        "color": {
          "field": "type",
          "type": "nominal",
          "scale": {
            "domain": [
              "Historical",
              "Expected Forecast",
              "1/20 Worst Case"
            ],
            "range": [
              "blue",
              "red",
              "orange"
            ]
          },
          "legend": {
            "title": "Regular Return"
          }
        }
      }
    }
  ],
  "data": {
    "values": [
      {
        "date": "2023-01-01",
        "return": 2.5,
        "norm_return": 0.8,
        "norm_market_index": 1.2,
        "type": "Historical"
      },
      {
        "date": "2023-02-01",
        "return": 1.8,
        "norm_return": 0.2,
        "norm_market_index": 0.9,
        "type": "Historical"
      },
      {
        "date": "2023-03-01",
        "return": 3.2,
        "norm_return": 1.5,
        "norm_market_index": 1.5,
        "type": "Historical"
      },
      {
        "date": "2023-04-01",
        "return": 2.1,
        "norm_return": 0.5,
        "norm_market_index": 1,
        "type": "Historical"
      },
      {
        "date": "2023-05-01",
        "return": 2.7,
        "norm_return": 1,
        "norm_market_index": 1.3,
        "type": "Historical"
      },
      {
        "date": "2023-06-01",
        "return": 1.9,
        "norm_return": 0.3,
        "norm_market_index": 0.8,
        "type": "Historical"
      },
      {
        "date": "2023-07-01",
        "return": 2.9,
        "norm_return": 1.2,
        "norm_market_index": 1.4,
        "type": "Historical"
      },
      {
        "date": "2023-08-01",
        "return": 2.3,
        "norm_return": 0.7,
        "norm_market_index": 1.1,
        "type": "Historical"
      },
      {
        "date": "2023-09-01",
        "return": 2,
        "norm_return": 0.4,
        "norm_market_index": 0.7,
        "type": "Historical"
      },
      {
        "date": "2023-10-01",
        "return": 3,
        "norm_return": 1.3,
        "norm_market_index": 1.6,
        "type": "Historical"
      },
      {
        "date": "2023-11-01",
        "return": 2.6,
        "norm_return": 0.9,
        "norm_market_index": 1.2,
        "type": "Historical"
      },
      {
        "date": "2023-12-01",
        "return": 2.2,
        "norm_return": 0.6,
        "norm_market_index": 1,
        "market_forecast": 1,
        "market_worst_case": 1,
        "return_forecast": 0.6,
        "return_worst_case": 0.6,
        "regular_forecast": 2.2,
        "regular_worst_case": 2.2,
        "type": "Historical"
      },
      {
        "date": "2024-01-01",
        "return": null,
        "norm_return": null,
        "norm_market_index": null,
        "market_forecast": 1.1,
        "market_worst_case": 0.8,
        "return_forecast": 0.7,
        "return_worst_case": 0.4,
        "regular_forecast": 2.4,
        "regular_worst_case": 1.9,
        "type": "Expected Forecast"
      },
      {
        "date": "2024-02-01",
        "return": null,
        "norm_return": null,
        "norm_market_index": null,
        "market_forecast": 1.2,
        "market_worst_case": 0.7,
        "return_forecast": 0.8,
        "return_worst_case": 0.3,
        "regular_forecast": 2.6,
        "regular_worst_case": 1.7,
        "type": "Expected Forecast"
      },
      {
        "date": "2024-03-01",
        "return": null,
        "norm_return": null,
        "norm_market_index": null,
        "market_forecast": 1.3,
        "market_worst_case": 0.6,
        "return_forecast": 0.9,
        "return_worst_case": 0.2,
        "regular_forecast": 2.8,
        "regular_worst_case": 1.5,
        "type": "Expected Forecast"
      }
    ]
  }
}