Skip to content

Instantly share code, notes, and snippets.

@vijayvd
Created May 15, 2015 01:26
Show Gist options
  • Save vijayvd/2c60bef2f5c0b39b7f68 to your computer and use it in GitHub Desktop.
Save vijayvd/2c60bef2f5c0b39b7f68 to your computer and use it in GitHub Desktop.
Fitting State Space Model
{"nbformat_minor": 0, "cells": [{"source": "The following problem arises in the context of private equity, which typically \nreport 'smoothed' returns (think of it as a moving average). As you can imagine, \n'smoothed' returns would have a much lower volatility compared to the volatility \nof 'unsmoothed' returns. For risk calculation we are interested in volatility of \n'unsmoothed' returns.\n\nMathematically, suppose I observe a process $\\bar{r}_t$ which is a moving \naverage of process $r_t$, i.e., $\\bar{r}_t = \\sum_{k=0}^p w_k r_{t-k}$. I also \nknow that $r_t = \\alpha + \\beta r_{I, t} + \\epsilon_t$, where $r_{I, t}$ are \nreturns of a public index and $\\epsilon_t = N(0, \\sigma^2)$. I would like to \nestimate `unsmoothed' returns $r_t, t = 0, \\ldots, T$ from the data: \n$\\bar{r}_t, r_{I,t}, t=0, 1, \\ldots, T$.\n\nWe will view it as a state-space model:\n$$\n\\begin{split}\nz_t\n&=\nA_t z_{t-1} + B_t u_t + \\epsilon_t,\n\\\\\ny_t \n&=\nC_t z_t + D_t u_t + \\delta_t,\n\\\\\n\\epsilon_t &\\sim \\mathcal{N}(0, Q_t),\\ \\delta_t \\sim \\mathcal{N}(0, R_t),\n\\end{split}\n$$\nwhere $z_t$ is the latent variable, $y_t$ is the observation, $u_t$ is an\noptional input or control signal, $\\epsilon_t$ is the system noise and $\\delta_t$\nis the observation noise.\n\nMapping our problem to state-space form, we get \n\n\n$$\nz_t \n= \n\\begin{bmatrix}\nr_t\\\\ r_{t-1} \\\\ \\vdots \\\\ r_{t-p} \n\\end{bmatrix}_{(p+1)\\times 1},\nA_t\n=\n\\begin{bmatrix}\n0, 0, \\ldots, 0 \\\\\n1, 0, \\ldots, 0 \\\\\n\\vdots \\ldots \\vdots \\\\\n0, \\ldots, 1, 0\n\\end{bmatrix}_{(p+1)\\times(p+1)},\nB_t \n=\n\\begin{bmatrix}\n\\alpha,\\ \\beta \\\\\n0,\\ 0 \\\\\n\\vdots\\ \\vdots \\\\\n0,\\ 0\n\\end{bmatrix},\nu_t\n=\n\\begin{bmatrix}\n1\\\\\nr_{I, t}\n\\end{bmatrix},\\\nQ_t\n=\n\\begin{bmatrix}\n\\sigma^2\\quad &\\mathbf 0^\\top_{1\\times p} \\\\\n\\mathbf{0}_{p \\times 1}\\ &\\mathbf{0}_{p\\times p}\n\\end{bmatrix}\n$$\n\n\n$$\ny_t \n= \n\\bar{r}_t,\\quad\n\\\\\nC_t \n=\n\\begin{bmatrix}\nw_0\\ w_1 \\ldots w_p\n\\end{bmatrix},\\\nD_t \n=\n\\mathbf{0}_{1\\times 2},\\\nR_t\n=\n\\mathbf{0}_{1\\times 1}.\n$$\n\n\nWe also assume $y_t \\sim N(0, \\sigma_0^2)$, where $\\sigma_0^2$ is large enough so that it will be \na diffuse prior. Our model has the parameter $\\theta = (B_t, C_t, \\sigma^2, \\sigma_0^2)$.\nWe are mainly interested in $p(z_t|y_{0\\colon t}, u_{0\\colon t}, \\theta)$. \n\nI am very new to using statsmodels. Can someone please provide suggestions, pointers, or sample code for fitting a model?\n", "cell_type": "markdown", "metadata": {"collapsed": true}}, {"execution_count": null, "cell_type": "code", "source": "", "outputs": [], "metadata": {"collapsed": true, "trusted": false}}], "nbformat": 4, "metadata": {"kernelspec": {"display_name": "Python 2", "name": "python2", "language": "python"}, "language_info": {"mimetype": "text/x-python", "nbconvert_exporter": "python", "version": "2.7.9", "name": "python", "file_extension": ".py", "pygments_lexer": "ipython2", "codemirror_mode": {"version": 2, "name": "ipython"}}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment