Welcome to Kalman Filters’s documentation!

Data Frame

exception timeSeriesFrame.ColumnHeaderException
args
message
exception timeSeriesFrame.DataException(text=None)
args
message
class timeSeriesFrame.Dataframe(data=None, columnList=None, rowList=None)

This is the base frame that holds flat 2 dimensional data

columnHeader(headerList)
Parameter:headerList (list<String>) – List of header for the columns
plot()
Plot the cumulated weight, it was quickly implemented to plot the result. It is unsafe.
rowHeader(headerList)
Parameter:headerList (list<String>) – List of header for the rows
size()
Return shape of the data. :return: the dimensions of the data. :rtype: tuple(int, int)
summary()
Return the summary of the underlying data. Not yet implemented
toCSV(name='default.csv')
Export the data into comma seperated value (CSV) format :param name: The name of the file to be exported. :type name: String
exception timeSeriesFrame.DataframeException

Base class exception for Dataframe

args
message
exception timeSeriesFrame.RowHeaderException
args
message
timeSeriesFrame.StylusReader(writer)

A simple parse function that read the the output of the internal software that I work with.

Parameter:writer (csv.writer) – csv.writer object
Returns:the data wrapped in timeSeriesFrame
Return type:TimeSeriesFrame
class timeSeriesFrame.TimeSeriesFrame(data=None, rowList=None, columnList=None, rown=None, coln=None)
columnHeader(headerList)
Parameter:headerList (list<String>) – List of header for the columns
columnIterator()

This is a generator to iterate across different time series

Return type:TimeSeriesFrame
plot()
Plot the cumulated weight, it was quickly implemented to plot the result. It is unsafe.
rowHeader(headerList)
rowIterator()

This is a generator to iterate all the time series by date

Return type:TimeSeriesFrame
size()
Return shape of the data. :return: the dimensions of the data. :rtype: tuple(int, int)
summary()
Return the summary of the underlying data. Not yet implemented
toCSV(name='default.csv')
Export the data into comma seperated value (CSV) format :param name: The name of the file to be exported. :type name: String
exception timeSeriesFrame.ToBeImplemented
args
message

digraph inheritancee59e38f240 {
rankdir=LR;
size="8.0, 12.0";
  "TimeSeriesFrame.RowHeaderException" [shape=box,style="setlinewidth(0.5)",fontsize=10,fontname=Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans,height=0.25];
  "TimeSeriesFrame.DataframeException" -> "TimeSeriesFrame.RowHeaderException" [arrowsize=0.5,style="setlinewidth(0.5)"];
  "TimeSeriesFrame.ColumnHeaderException" [shape=box,style="setlinewidth(0.5)",fontsize=10,fontname=Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans,height=0.25];
  "TimeSeriesFrame.DataframeException" -> "TimeSeriesFrame.ColumnHeaderException" [arrowsize=0.5,style="setlinewidth(0.5)"];
  "TimeSeriesFrame.DataframeException" [shape=box,style="setlinewidth(0.5)",fontsize=10,fontname=Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans,height=0.25];
  "TimeSeriesFrame.DataException" [shape=box,style="setlinewidth(0.5)",fontsize=10,fontname=Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans,height=0.25];
  "TimeSeriesFrame.DataframeException" -> "TimeSeriesFrame.DataException" [arrowsize=0.5,style="setlinewidth(0.5)"];
  "TimeSeriesFrame.ToBeImplemented" [shape=box,style="setlinewidth(0.5)",fontsize=10,fontname=Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans,height=0.25];
  "TimeSeriesFrame.DataframeException" -> "TimeSeriesFrame.ToBeImplemented" [arrowsize=0.5,style="setlinewidth(0.5)"];
  "TimeSeriesFrame.Dataframe" [shape=box,style="setlinewidth(0.5)",fontsize=10,fontname=Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans,height=0.25];
  "TimeSeriesFrame.TimeSeriesFrame" [shape=box,style="setlinewidth(0.5)",fontsize=10,fontname=Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans,height=0.25];
  "TimeSeriesFrame.Dataframe" -> "TimeSeriesFrame.TimeSeriesFrame" [arrowsize=0.5,style="setlinewidth(0.5)"];
}

Regression

class regression.ECRegression(respond=None, regressors=None, intercept=False, D=None, d=None, **args)
R2()

FIX ME Simple R Squared by the definition on Wikipedia

Returns:R squared statistics
Return type:double
error(time=None)

Compute and return the estimation error

Parameter:time (datetime.date) – The specific date of the weight
Reutrn:TimeSeriesFrame of the estimation error
Return type:TimeSeriesFrame<double>
getEstimate(date=None)

Get the estimate of the regression

Parameter:date (datetime.date) – return the weight on a specific date
Returns:Weight computed from the regression
Return type:scipy.matrix
isECConstraintable()
isICConstraintable()
Returns:Boolean function to see if inequality and equality contraints can be imposed to the model. Default is True
Return type:boolean
predict(time=None)

This function take the (list of) date and return prediction in a timeseriesframe

Parameter:time (datetime.date) – the specific date of the weight
Returns:TimeSeriesFrame of estimate
Return type:TimeSeriesFram<double>
train()

This fucntion will estimate the weight in the regression.

Returns:reference to the object itself
class regression.ICRegression(respond=None, regressors=None, intercept=False, D=None, d=None, G=None, a=None, b=None, **args)

This is an abstruct class for Regression Type of problem.

R2()

FIX ME Simple R Squared by the definition on Wikipedia

Returns:R squared statistics
Return type:double
error(time=None)

Compute and return the estimation error

Parameter:time (datetime.date) – The specific date of the weight
Reutrn:TimeSeriesFrame of the estimation error
Return type:TimeSeriesFrame<double>
getEstimate(date=None)

Get the estimate of the regression

Parameter:date (datetime.date) – return the weight on a specific date
Returns:Weight computed from the regression
Return type:scipy.matrix
isECConstraintable()
isICConstraintable()
Returns:Boolean function to see if inequality and equality contraints can be imposed to the model. Default is True
Return type:boolean
predict(time=None)

This function take the (list of) date and return prediction in a timeseriesframe

Parameter:time (datetime.date) – the specific date of the weight
Returns:TimeSeriesFrame of estimate
Return type:TimeSeriesFram<double>
train()

This fucntion will estimate the weight in the regression.

FIXME

Returns:reference to the object itself
class regression.Regression(respond=None, regressors=None, intercept=False, **args)

This is an abstruct class for Regression Type of problem.

R2()

FIX ME Simple R Squared by the definition on Wikipedia

Returns:R squared statistics
Return type:double
error(time=None)

Compute and return the estimation error

Parameter:time (datetime.date) – The specific date of the weight
Reutrn:TimeSeriesFrame of the estimation error
Return type:TimeSeriesFrame<double>
getEstimate(date=None)

Get the estimate of the regression

Parameter:date (datetime.date) – return the weight on a specific date
Returns:Weight computed from the regression
Return type:scipy.matrix
isECConstraintable()
Returns:Boolean function to see if equality contraints can be imposed to the model. Default is True
Return type:boolean
isICConstraintable()
Returns:Boolean function to see if inequality and equality contraints can be imposed to the model. Default is True
Return type:boolean
predict(time=None)

This function take the (list of) date and return prediction in a timeseriesframe

Parameter:time (datetime.date) – the specific date of the weight
Returns:TimeSeriesFrame of estimate
Return type:TimeSeriesFram<double>
train()

This fucntion will estimate the weight in the regression.

Returns:reference to the object itself
regression.main()

digraph inheritance34a6724fd0 {
rankdir=LR;
size="8.0, 12.0";
  "regression.ECRegression" [style="setlinewidth(0.5)",URL="#regression.ECRegression",fontname=Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans,height=0.25,shape=box,fontsize=10];
  "regression.Regression" -> "regression.ECRegression" [arrowsize=0.5,style="setlinewidth(0.5)"];
  "regression.Regression" [style="setlinewidth(0.5)",URL="#regression.Regression",fontname=Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans,height=0.25,shape=box,fontsize=10];
  "regression.ICRegression" [style="setlinewidth(0.5)",URL="#regression.ICRegression",fontname=Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans,height=0.25,shape=box,fontsize=10];
  "regression.ECRegression" -> "regression.ICRegression" [arrowsize=0.5,style="setlinewidth(0.5)"];
}

Kalman Filter

This module contains ordinary kalman filter classes

class kalmanFilter.ECKalmanFilter(respond=None, regressors=None, intercept=False, Sigma=None, sigma=None, initBeta=None, initVariance=None, Phi=None, D=None, d=matrix([[, 1.]], ), **args)

This is a KalmanFilter Class subclassed from Regression

R2()

FIX ME Simple R Squared by the definition on Wikipedia

Returns:R squared statistics
Return type:double
error(time=None)

Compute and return the estimation error

Parameter:time (datetime.date) – The specific date of the weight
Reutrn:TimeSeriesFrame of the estimation error
Return type:TimeSeriesFrame<double>
getEstimate(date=None)

Get the estimate of the regression

Parameter:date (datetime.date) – return the weight on a specific date
Returns:Weight computed from the regression
Return type:scipy.matrix
isECConstraintable()
isICConstraintable()
Returns:Boolean function to see if inequality and equality contraints can be imposed to the model. Default is True
Return type:boolean
predict(time=None)

This function take the (list of) date and return prediction in a timeseriesframe

Parameter:time (datetime.date) – the specific date of the weight
Returns:TimeSeriesFrame of estimate
Return type:TimeSeriesFram<double>
train()
This fucntion will start the estimation. This is separated from addData.
class kalmanFilter.ICKalmanFilter(respond=None, regressors=None, intercept=False, Sigma=None, sigma=None, initBeta=None, initVariance=None, Phi=None, D=None, d=matrix([[, 1.]], ), G=None, a=None, b=None, **args)

This is a KalmanFilter Class subclassed from Regression

R2()

FIX ME Simple R Squared by the definition on Wikipedia

Returns:R squared statistics
Return type:double
error(time=None)

Compute and return the estimation error

Parameter:time (datetime.date) – The specific date of the weight
Reutrn:TimeSeriesFrame of the estimation error
Return type:TimeSeriesFrame<double>
getEstimate(date=None)

Get the estimate of the regression

Parameter:date (datetime.date) – return the weight on a specific date
Returns:Weight computed from the regression
Return type:scipy.matrix
isECConstraintable()
isICConstraintable()
Returns:Boolean function to see if inequality and equality contraints can be imposed to the model. Default is True
Return type:boolean
predict(time=None)

This function take the (list of) date and return prediction in a timeseriesframe

Parameter:time (datetime.date) – the specific date of the weight
Returns:TimeSeriesFrame of estimate
Return type:TimeSeriesFram<double>
train()
This fucntion will start the estimation. This is separated from addData.
class kalmanFilter.KalmanFilter(respond=None, regressors=None, intercept=False, Sigma=None, sigma=None, initBeta=None, initVariance=None, Phi=None, **args)

This is a Kalman filter Class subclassed from Regression

R2()

FIX ME Simple R Squared by the definition on Wikipedia

Returns:R squared statistics
Return type:double
error(time=None)

Compute and return the estimation error

Parameter:time (datetime.date) – The specific date of the weight
Reutrn:TimeSeriesFrame of the estimation error
Return type:TimeSeriesFrame<double>
getEstimate(date=None)

Get the estimate of the regression

Parameter:date (datetime.date) – return the weight on a specific date
Returns:Weight computed from the regression
Return type:scipy.matrix
isECConstraintable()
Returns:Boolean function to see if equality contraints can be imposed to the model. Default is True
Return type:boolean
isICConstraintable()
Returns:Boolean function to see if inequality and equality contraints can be imposed to the model. Default is True
Return type:boolean
predict(time=None)

This function take the (list of) date and return prediction in a timeseriesframe

Parameter:time (datetime.date) – the specific date of the weight
Returns:TimeSeriesFrame of estimate
Return type:TimeSeriesFram<double>
train()
This fucntion will start the estimation. This is separated from addData.
class kalmanFilter.KalmanSmoother(respond=None, regressors=None, intercept=False, Sigma=None, sigma=None, initBeta=None, initVariance=None, Phi=None, **args)

This is a Kalman Smoother Class subclassed from Kalman Filter

R2()

FIX ME Simple R Squared by the definition on Wikipedia

Returns:R squared statistics
Return type:double
error(time=None)

Compute and return the estimation error

Parameter:time (datetime.date) – The specific date of the weight
Reutrn:TimeSeriesFrame of the estimation error
Return type:TimeSeriesFrame<double>
getEstimate(date=None)

Get the estimate of the regression

Parameter:date (datetime.date) – return the weight on a specific date
Returns:Weight computed from the regression
Return type:scipy.matrix
isECConstraintable()
Returns:Boolean function to see if equality contraints can be imposed to the model. Default is True
Return type:boolean
isICConstraintable()
Returns:Boolean function to see if inequality and equality contraints can be imposed to the model. Default is True
Return type:boolean
predict(time=None)

This function take the (list of) date and return prediction in a timeseriesframe

Parameter:time (datetime.date) – the specific date of the weight
Returns:TimeSeriesFrame of estimate
Return type:TimeSeriesFram<double>
train()
This fucntion will start the estimation. This is separated from addData.
kalmanFilter.main()

digraph inheritance46a7dcc392 {
rankdir=LR;
size="8.0, 12.0";
  "kalmanFilter.KalmanSmoother" [style="setlinewidth(0.5)",URL="#kalmanFilter.KalmanSmoother",fontname=Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans,height=0.25,shape=box,fontsize=10];
  "kalmanFilter.KalmanFilter" -> "kalmanFilter.KalmanSmoother" [arrowsize=0.5,style="setlinewidth(0.5)"];
  "regression.ECRegression" [style="setlinewidth(0.5)",URL="#regression.ECRegression",fontname=Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans,height=0.25,shape=box,fontsize=10];
  "regression.Regression" -> "regression.ECRegression" [arrowsize=0.5,style="setlinewidth(0.5)"];
  "kalmanFilter.ECKalmanFilter" [style="setlinewidth(0.5)",URL="#kalmanFilter.ECKalmanFilter",fontname=Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans,height=0.25,shape=box,fontsize=10];
  "kalmanFilter.KalmanFilter" -> "kalmanFilter.ECKalmanFilter" [arrowsize=0.5,style="setlinewidth(0.5)"];
  "regression.ECRegression" -> "kalmanFilter.ECKalmanFilter" [arrowsize=0.5,style="setlinewidth(0.5)"];
  "regression.ICRegression" [style="setlinewidth(0.5)",URL="#regression.ICRegression",fontname=Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans,height=0.25,shape=box,fontsize=10];
  "regression.ECRegression" -> "regression.ICRegression" [arrowsize=0.5,style="setlinewidth(0.5)"];
  "regression.Regression" [style="setlinewidth(0.5)",URL="#regression.Regression",fontname=Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans,height=0.25,shape=box,fontsize=10];
  "kalmanFilter.ICKalmanFilter" [style="setlinewidth(0.5)",URL="#kalmanFilter.ICKalmanFilter",fontname=Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans,height=0.25,shape=box,fontsize=10];
  "kalmanFilter.ECKalmanFilter" -> "kalmanFilter.ICKalmanFilter" [arrowsize=0.5,style="setlinewidth(0.5)"];
  "regression.ICRegression" -> "kalmanFilter.ICKalmanFilter" [arrowsize=0.5,style="setlinewidth(0.5)"];
  "kalmanFilter.KalmanFilter" [style="setlinewidth(0.5)",URL="#kalmanFilter.KalmanFilter",fontname=Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans,height=0.25,shape=box,fontsize=10];
  "regression.Regression" -> "kalmanFilter.KalmanFilter" [arrowsize=0.5,style="setlinewidth(0.5)"];
}

libregression

This module contains a list of algoritm that is used for estimation. These algorithm are unsafe and all these type and bound check should be done in the wrapper level.

List of algorithm that has been inplemented:

Ordinary Least Squares Ordinary Least Squares with Linear Equality Constraints Ordinary Least Squares with Linear Inequality Constraints

Stepwise Least Squares Stepwise Least Squares with Linear Equality Constraints Stepwise Least Squares with Linear Inequality Constraints

Kalman Filter Kalman Smoother Kalman Filter with Linear Equality Constraints Kalman Filter with Linear Inequality Constraints

Flexible Least Squares with Linear Inequality Constraints

UNTESTED: Kalman Smoother with Linear Equality Constraints Kalman Smoother with Linear Inequality Constraints

A lot of matrix inversion are used in these function and singularity is not checked. Potential optimisation includes more stable inversion, large scale QP solver for Flexible Least Squares

Naming convention:
function with no ec/ic prefix are estimator for unconstrained problem ec* prefix: this is for equality constraints. ic* prefixL this is for inequality constraints.
libregression.constrainedflexibleleastsquare(X, y, lamb, W1, W2, Phi, D, d, smallG, a, b)

\begin{eqnarray*}
\arg_{\beta_t,\forall t}\min\sum_{t = 1}^m (y_t - X^T_t \beta_t)^T W_1 (y_t - X^T_t \beta_t) +& \sum_{t=1}^{m-1}\lambda (\beta_{t+1} - \Phi \beta_t)^T W_2 (\beta_{t+1} - \Phi \beta_t)\\
G \beta_t & \geq g, \quad \forall t\\
D \beta_t & = d, \quad \forall t
\end{eqnarray*}

libregression.ecregression(X, y, W, D, d)
Parameters:
  • X (scipy.matrix<float>) – Independent variable
  • y (scipy.matrix<float>) – Dependent variable
  • W (scipy.matrix<float>) – Weight matrix
  • D (scipy.matrix<float>) – Equality constraint matrix
  • d (scipy.matrix<float>) – Equality constraint vector
Returns:

\hat{\beta}

Return type:

scipy.matrix<float>

This return the estimated weight on the following regression problem

y = X \beta

constained to

D \beta = d

The problem is solved using Lagrangian Multiplier and

\begin{eqnarray*}
   E &=& (X^T W X)^{-1}\\
   \lambda &=& (D E D^T)^{-1} (D E X^T W y - d)\\
   \hat{\beta} &=& E (X^T W y - D^T \lambda)
\end{eqnarray*}

libregression.icregression(X, y, W, D, d, G, a, b, n)

This return the estimated weight on the following regression problem

y = X \beta

constained to

\begin{eqnarray*}
D \beta &=& d\\
a \leq G &\beta & \leq b
\end{eqnarray*}

This problem is translated nicely to quadratic programming problem. CVXOPT package is used to as the quadratic solver engine.

Parameters:
  • X (scipy.matrix<float>) – Independent variable
  • y (scipy.matrix<float>) – Dependent variable
  • W (scipy.matrix<float>) – Weight matrix
  • D (scipy.matrix<float>) – Equality constraint matrix
  • d (scipy.matrix<float>) – Equality constraint vector
  • G (scipy.matrix<float>) – Inequality constraint matrix
  • a b (scipy.matrix<float>) – Lower and upper bound of the inequality constraints
Returns:

\hat{\beta}

Return type:

scipy.matrix<float>

libregression.kalman_filter(b, V, Phi, y, X, sigma, Sigma, switch=0, D=None, d=None, G=None, a=None, c=None)

\begin{eqnarray*}
\beta_{t|t-1} = \Phi \: \beta_{t-1|t-1}\\
V_{t|t-1} = \Phi  V_{t-1|t-1} \Phi ^T + \Sigma \\
e_t = y_t -  X_t \beta_{t|t-1}\\
K_t =  V_{t|t-1} X_t^T (\sigma + X_t V_{t|t-1} X_t )^{-1}\\
\beta_{t|t} = \beta_{t|t-1} + K_t e_t\\
V_{t|t} = (I - K_t X_t^T) V_{t|t-1}\\
\end{eqnarray*}

libregression.kalman_predict(b, V, Phi, S)

This fucntion is the predicted step of Kalman Filter

\begin{eqnarray*}
\beta_{t+1|t} &=& \Phi  \beta_{t|t}\\
V_{t+1|t} &=& \Phi  V_{t|t} \Phi^T + S
\end{eqnarray*}

Parameters:
  • b (scipy.matrix<float>) – \beta_{t|t}
  • V (scipy.matrix<float>) – V_{t|t}
  • Phi (scipy.matrix<float>) – \Phi
  • S (scipy.matrix<float>) – S
Returns:

(\beta_{t+1|t}, V_{t+1|t})

Return type:

Tuple of scipy.matrix<float>

libregression.kalman_smoother(b, V, Phi, y, X, sigma, Sigma, switch=0, D=None, d=None, G=None, a=None, c=None)

\begin{eqnarray*}
K_t &= V_{t|t} \Phi ({V_{t+1|t} })^{-1}\\
\beta_{t|T} &= \beta_{t|t} + K_t [ \beta_{t+1|T} - \Phi \beta_{t+1|t}]
\end{eqnarray*}

libregression.kalman_upd(beta, V, y, X, s, S, switch=0, D=None, d=None, G=None, a=None, b=None)

This is the update step of kalman filter.

\begin{eqnarray*}
e_t &=& y_t -  X_t \beta_{t|t-1} \\
K_t &=&  V_{t|t-1} X_t^T (\sigma + X_t V_{t|t-1} X_t )^{-1}\\
\beta_{t|t} &=& \beta_{t|t-1} + K_t e_t\\
V_{t|t} &=& (I - K_t X_t^T) V_{t|t-1}\\
\end{eqnarray*}

libregression.regression(X, y, W)

Return the estimated weight based on ordinary regression model. The algorithm used to solve for the weight is done by matrix inversion.

Parameters:
  • X (scipy.matrix<float>) – Independent variable
  • y (scipy.matrix<float>) – Dependent variable
  • W (scipy.matrix<float>) – Weight matrix
Returns:

\hat{\beta}

Return type:

scipy.matrix<float>

\hat{\beta} = (X^T\; W \; X )^{-1} X^T W  y

Indices and tables

Table Of Contents

This Page