Skip to content

Instantly share code, notes, and snippets.

@vijayvd
vijayvd / EM
Last active August 29, 2015 14:23
Expectation Maximization
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Introduction to Expectation Maximization via Example"
]
},
{
@vijayvd
vijayvd / StateSpaceModel
Created May 15, 2015 01:26
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 \\
@vijayvd
vijayvd / KalmanFilter
Created May 7, 2015 03:20
Problem with pykalman
{"nbformat_minor": 0, "cells": [{"execution_count": 1, "cell_type": "code", "source": "import pandas as pd\nimport numpy as np\nimport pykalman as kf\nimport scipy as sp\n%pylab inline", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Populating the interactive namespace from numpy and matplotlib\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "### Convention", "cell_type": "markdown", "metadata": {}}, {"source": "$$\n\\begin{split}\nx_{t+1} \n&= \nAx_t + b_t + \\epsilon_t,\\ \\epsilon_t\\sim \\mathscr{N}(0, Q_t) \n\\\\\nz_t\n&=\nC_tx_t + d_t + \\delta_t,\\ \\delta_t\\sim\\mathscr{N}(0, R_t)\n\\end{split}\n$$", "cell_type": "markdown", "metadata": {}}, {"execution_count": 2, "cell_type": "code", "source": "n_obs, n_lat = 1, 2\nT = 1000", "outputs": [], "metadata": {"collapsed": true, "trusted": true}}, {"execution_count": 3, "cell_type": "code", "source": "x = np.zeros((T,n_lat))\nz = np.zeros((T,n_obs))", "outputs": [], "metadata": {"collapsed": true, "trusted": true}},
@vijayvd
vijayvd / cvxpy unbounded issue
Created January 8, 2014 16:16
CVXPY is unbounded with constraints but bounded without constraints.
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
@vijayvd
vijayvd / gist:6269621
Created August 19, 2013 14:15
Using KNeighborsClassifier as a base learner for AdaBoostClassifier
Running following code:
clf = AdaBoostClassifier(n_estimators=100, base_estimator=KNeighborsClassifier()); clf.fit(x,y)
I get the following traceback:
TypeError Traceback (most recent call last)
/usr/local/lib/python2.7/dist-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
176 else: