Skip to content

Commit

Permalink
Merge pull request #2918 from ChadFulton/ss-cykfs
Browse files Browse the repository at this point in the history
ENH:  Statespace: New / improved Kalman filter, smoother, simulation smoother (feature branch)
  • Loading branch information
ChadFulton committed Apr 28, 2016
2 parents bd7a1ac + 5188082 commit 83022fd
Show file tree
Hide file tree
Showing 41 changed files with 9,629 additions and 541 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,21 @@ help
# Project specific
statsmodels/version.py
cythonize.dat
cythonize_exclusions.dat
statsmodels.egg-info/
iterate.dat
hash_dict.pickle
rehab.table
salary.table
.ipynb_checkpoints
statsmodels/tsa/statespace/_statespace.pyx
statsmodels/tsa/statespace/_representation.pyx
statsmodels/tsa/statespace/_kalman_filter.pyx
statsmodels/tsa/statespace/_kalman_smoother.pyx
statsmodels/tsa/statespace/_simulation_smoother.pyx
statsmodels/tsa/statespace/_tools.pyx
statsmodels/tsa/statespace/_filters/_conventional.pyx
statsmodels/tsa/statespace/_filters/_inversions.pyx
statsmodels/tsa/statespace/_filters/_univariate.pyx
statsmodels/tsa/statespace/_smoothers/_conventional.pyx
statsmodels/tsa/statespace/_smoothers/_univariate.pyx
116 changes: 98 additions & 18 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@

curdir = os.path.abspath(os.path.dirname(__file__))
README = open(pjoin(curdir, "README.rst")).read()
CYTHON_EXCLUSION_FILE = 'cythonize_exclusions.dat'

DISTNAME = 'statsmodels'
DESCRIPTION = 'Statistical computations and models for use with SciPy'
Expand Down Expand Up @@ -89,6 +90,16 @@ def generate_cython():
raise RuntimeError("Running cythonize failed!")


def init_cython_exclusion(filename):
with open(filename, 'w') as f:
pass


def append_cython_exclusion(path, filename):
with open(filename, 'a') as f:
f.write(path + "\n")


def strip_rc(version):
return re.sub(r"rc\d+$", "", version)

Expand Down Expand Up @@ -329,25 +340,92 @@ def run(self):

from numpy.distutils.misc_util import get_info

# Reset the cython exclusions file
init_cython_exclusion(CYTHON_EXCLUSION_FILE)

npymath_info = get_info("npymath")
ext_data = dict(
kalman_loglike = {"name" : "statsmodels/tsa/kalmanf/kalman_loglike.c",
"depends" : ["statsmodels/src/capsule.h"],
"include_dirs": ["statsmodels/src"],
"sources" : []},
_statespace = {"name" : "statsmodels/tsa/statespace/_statespace.c",
"depends" : ["statsmodels/src/capsule.h"],
"include_dirs": ["statsmodels/src"] + npymath_info['include_dirs'],
"libraries": npymath_info['libraries'],
"library_dirs": npymath_info['library_dirs'],
"sources" : []},
linbin = {"name" : "statsmodels/nonparametric/linbin.c",
"depends" : [],
"sources" : []},
_smoothers_lowess = {"name" : "statsmodels/nonparametric/_smoothers_lowess.c",
"depends" : [],
"sources" : []}
)
kalman_loglike = {"name" : "statsmodels/tsa/kalmanf/kalman_loglike.c",
"depends" : ["statsmodels/src/capsule.h"],
"include_dirs": ["statsmodels/src"],
"sources" : []},
_statespace = {"name" : "statsmodels/tsa/statespace/_statespace.c",
"depends" : ["statsmodels/src/capsule.h"],
"include_dirs": ["statsmodels/src"] + npymath_info['include_dirs'],
"libraries": npymath_info['libraries'],
"library_dirs": npymath_info['library_dirs'],
"sources" : []},
linbin = {"name" : "statsmodels/nonparametric/linbin.c",
"depends" : [],
"sources" : []},
_smoothers_lowess = {"name" : "statsmodels/nonparametric/_smoothers_lowess.c",
"depends" : [],
"sources" : []}
)

statespace_ext_data = dict(
_representation = {"name" : "statsmodels/tsa/statespace/_representation.c",
"include_dirs": ['statsmodels/src'] + npymath_info['include_dirs'],
"libraries": npymath_info['libraries'],
"library_dirs": npymath_info['library_dirs'],
"sources": []},
_kalman_filter = {"name" : "statsmodels/tsa/statespace/_kalman_filter.c",
"include_dirs": ['statsmodels/src'] + npymath_info['include_dirs'],
"libraries": npymath_info['libraries'],
"library_dirs": npymath_info['library_dirs'],
"sources": []},
_kalman_filter_conventional = {"name" : "statsmodels/tsa/statespace/_filters/_conventional.c",
"filename": "_conventional",
"include_dirs": ['statsmodels/src'] + npymath_info['include_dirs'],
"libraries": npymath_info['libraries'],
"library_dirs": npymath_info['library_dirs'],
"sources": []},
_kalman_filter_inversions = {"name" : "statsmodels/tsa/statespace/_filters/_inversions.c",
"filename": "_inversions",
"include_dirs": ['statsmodels/src'] + npymath_info['include_dirs'],
"libraries": npymath_info['libraries'],
"library_dirs": npymath_info['library_dirs'],
"sources": []},
_kalman_filter_univariate = {"name" : "statsmodels/tsa/statespace/_filters/_univariate.c",
"filename": "_univariate",
"include_dirs": ['statsmodels/src'] + npymath_info['include_dirs'],
"libraries": npymath_info['libraries'],
"library_dirs": npymath_info['library_dirs'],
"sources": []},
_kalman_smoother = {"name" : "statsmodels/tsa/statespace/_kalman_smoother.c",
"include_dirs": ['statsmodels/src'] + npymath_info['include_dirs'],
"libraries": npymath_info['libraries'],
"library_dirs": npymath_info['library_dirs'],
"sources": []},
_kalman_smoother_conventional = {"name" : "statsmodels/tsa/statespace/_smoothers/_conventional.c",
"filename": "_conventional",
"include_dirs": ['statsmodels/src'] + npymath_info['include_dirs'],
"libraries": npymath_info['libraries'],
"library_dirs": npymath_info['library_dirs'],
"sources": []},
_kalman_smoother_univariate = {"name" : "statsmodels/tsa/statespace/_smoothers/_univariate.c",
"filename": "_univariate",
"include_dirs": ['statsmodels/src'] + npymath_info['include_dirs'],
"libraries": npymath_info['libraries'],
"library_dirs": npymath_info['library_dirs'],
"sources": []},
_kalman_simulation_smoother = {"name" : "statsmodels/tsa/statespace/_simulation_smoother.c",
"filename": "_simulation_smoother",
"include_dirs": ['statsmodels/src'] + npymath_info['include_dirs'],
"libraries": npymath_info['libraries'],
"library_dirs": npymath_info['library_dirs'],
"sources": []},
_kalman_tools = {"name" : "statsmodels/tsa/statespace/_tools.c",
"filename": "_tools",
"sources": []},
)
try:
from scipy.linalg import cython_blas
ext_data.update(statespace_ext_data)
except ImportError:
for name, data in statespace_ext_data.items():
path = '.'.join([data["name"].split('.')[0], 'pyx.in'])
append_cython_exclusion(path, CYTHON_EXCLUSION_FILE)

extensions = []
for name, data in ext_data.items():
Expand All @@ -356,7 +434,9 @@ def run(self):
destdir = ".".join(os.path.dirname(data["name"]).split("/"))
data.pop('name')

obj = Extension('%s.%s' % (destdir, name), **data)
filename = data.pop('filename', name)

obj = Extension('%s.%s' % (destdir, filename), **data)

extensions.append(obj)

Expand Down
17 changes: 17 additions & 0 deletions statsmodels/src/math.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# ## Math Functions
# Real and complex log and abs functions
from libc.math cimport log as dlog, abs as dabs
cimport numpy as np

cdef extern from "numpy/npy_math.h":
np.float64_t NPY_PI
np.float64_t npy_cabs(np.npy_cdouble z)
np.npy_cdouble npy_clog(np.npy_cdouble z)

cdef inline np.float64_t zabs(np.complex128_t z):
return npy_cabs((<np.npy_cdouble *> &z)[0])

cdef inline np.complex128_t zlog(np.complex128_t z):
cdef np.npy_cdouble x
x = npy_clog((<np.npy_cdouble*> &z)[0])
return (<np.complex128_t *> &x)[0]
Empty file.
61 changes: 61 additions & 0 deletions statsmodels/tsa/statespace/_filters/_conventional.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#cython: boundscheck=False
#cython: wraparound=False
#cython: cdivision=False
"""
State Space Models - Conventional Kalman Filter declarations
Author: Chad Fulton
License: Simplified-BSD
"""

cimport numpy as np
from statsmodels.tsa.statespace._representation cimport (
sStatespace, dStatespace, cStatespace, zStatespace
)
from statsmodels.tsa.statespace._kalman_filter cimport (
sKalmanFilter, dKalmanFilter, cKalmanFilter, zKalmanFilter
)

# Single precision
cdef int sforecast_missing_conventional(sKalmanFilter kfilter, sStatespace model)
cdef int supdating_missing_conventional(sKalmanFilter kfilter, sStatespace model)
cdef np.float32_t sinverse_missing_conventional(sKalmanFilter kfilter, sStatespace model, np.float32_t determinant) except *
cdef np.float32_t sloglikelihood_missing_conventional(sKalmanFilter kfilter, sStatespace model, np.float32_t determinant)

cdef int sforecast_conventional(sKalmanFilter kfilter, sStatespace model)
cdef int supdating_conventional(sKalmanFilter kfilter, sStatespace model)
cdef int sprediction_conventional(sKalmanFilter kfilter, sStatespace model)
cdef np.float32_t sloglikelihood_conventional(sKalmanFilter kfilter, sStatespace model, np.float32_t determinant)

# Double precision
cdef int dforecast_missing_conventional(dKalmanFilter kfilter, dStatespace model)
cdef int dupdating_missing_conventional(dKalmanFilter kfilter, dStatespace model)
cdef np.float64_t dinverse_missing_conventional(dKalmanFilter kfilter, dStatespace model, np.float64_t determinant) except *
cdef np.float64_t dloglikelihood_missing_conventional(dKalmanFilter kfilter, dStatespace model, np.float64_t determinant)

cdef int dforecast_conventional(dKalmanFilter kfilter, dStatespace model)
cdef int dupdating_conventional(dKalmanFilter kfilter, dStatespace model)
cdef int dprediction_conventional(dKalmanFilter kfilter, dStatespace model)
cdef np.float64_t dloglikelihood_conventional(dKalmanFilter kfilter, dStatespace model, np.float64_t determinant)

# Single precision complex
cdef int cforecast_missing_conventional(cKalmanFilter kfilter, cStatespace model)
cdef int cupdating_missing_conventional(cKalmanFilter kfilter, cStatespace model)
cdef np.complex64_t cinverse_missing_conventional(cKalmanFilter kfilter, cStatespace model, np.complex64_t determinant) except *
cdef np.complex64_t cloglikelihood_missing_conventional(cKalmanFilter kfilter, cStatespace model, np.complex64_t determinant)

cdef int cforecast_conventional(cKalmanFilter kfilter, cStatespace model)
cdef int cupdating_conventional(cKalmanFilter kfilter, cStatespace model)
cdef int cprediction_conventional(cKalmanFilter kfilter, cStatespace model)
cdef np.complex64_t cloglikelihood_conventional(cKalmanFilter kfilter, cStatespace model, np.complex64_t determinant)

# Double precision complex
cdef int zforecast_missing_conventional(zKalmanFilter kfilter, zStatespace model)
cdef int zupdating_missing_conventional(zKalmanFilter kfilter, zStatespace model)
cdef np.complex128_t zinverse_missing_conventional(zKalmanFilter kfilter, zStatespace model, np.complex128_t determinant) except *
cdef np.complex128_t zloglikelihood_missing_conventional(zKalmanFilter kfilter, zStatespace model, np.complex128_t determinant)

cdef int zforecast_conventional(zKalmanFilter kfilter, zStatespace model)
cdef int zupdating_conventional(zKalmanFilter kfilter, zStatespace model)
cdef int zprediction_conventional(zKalmanFilter kfilter, zStatespace model)
cdef np.complex128_t zloglikelihood_conventional(zKalmanFilter kfilter, zStatespace model, np.complex128_t determinant)

0 comments on commit 83022fd

Please sign in to comment.