1st cut of KF refactoring and simplification

git-svn-id: https://www.dynare.org/svn/dynare/trunk@2750 ac1d8469-bf42-47a9-8791-bf33cf982152
time-shift
george 2009-06-10 06:40:07 +00:00
parent 36ecf9d6bd
commit 1245d60326
5 changed files with 1836 additions and 1557 deletions

File diff suppressed because it is too large Load Diff

View File

@ -154,6 +154,50 @@ class SmootherResults{
};
class BasicKalmanTask{
// friend class KalmanUniTask;
// SSForm ssf;
const GeneralMatrix &data;
const ConstGeneralMatrix &Zt;
const ConstGeneralMatrix &Ht;
const ConstGeneralMatrix &Tt;
const ConstGeneralMatrix &Rt;
const ConstGeneralMatrix &Qt;
const StateInit&init;
public:
BasicKalmanTask(const GeneralMatrix&d,const GeneralMatrix&ZZ,
const GeneralMatrix&HH,const GeneralMatrix&TT,
const GeneralMatrix&RR,const GeneralMatrix&QQ,
const StateInit&init_state);
// BasicKalmanTask(const GeneralMatrix&d,const TMatrix&Z,
// const TMatrix&H,const TMatrix&T,
// const TMatrix&R,const TMatrix&Q,
// const StateInit&init_state);
BasicKalmanTask(const GeneralMatrix&d,const ConstGeneralMatrix&ZZ,
const ConstGeneralMatrix&HH,const ConstGeneralMatrix&TT,
const ConstGeneralMatrix&RR,const ConstGeneralMatrix&QQ,
const StateInit&init_state);
virtual ~BasicKalmanTask();
// double filter(int&per,int&d)const;
// double filter(int&per,int&d, int start, std::vector<double>* vll)const;
double filter(int&per,int&d,int start, std::vector<double>* vll)const;
// double filter_and_smooth(SmootherResults&sres,int&per,int&d)const;
protected:
double filterNonDiffuse(const Vector&a,const GeneralMatrix&Pstar,
int start, std::vector<double>* vll) const; //int first,FilterResults&fres)const;
// void filterDiffuse(const Vector&a,const GeneralMatrix&Pstar,
// const GeneralMatrix&Pinf,int first,
// DiffuseFilterResults&fres)const;
// void smootherNonDiffuse(const FilterResults&fres,SmootherResults&sres)const;
// void smootherDiffuse(const DiffuseFilterResults&fres,SmootherResults&sres)const;
// void smootherNonDiffuseStep(int t,const FilterResults&fres,
// Vector&rt,GeneralMatrix&Nt,
// Vector&alphat,GeneralMatrix&Vt,
// Vector&etat)const;
static double calcStepLogLik(const PLUFact&Finv,const Vector&v);
};
class KalmanUniTask;
class KalmanTask{
friend class KalmanUniTask;

View File

@ -1,16 +1,16 @@
# $Id: Makefile 531 2005-11-30 13:49:48Z kamenik $
# Copyright 2005, Ondra Kamenik
DEBUG = yes
#DEBUG = yes
#LD_LIBS := -llapack -lcblas -lf77blas -latlas -lg2c
CC_FLAGS := -DMATLAB -DWINDOWS -DNO_BLAS_H -DNO_LAPACK_H \
-Wall -I../../gensylv/cc -I../cc \
CC_FLAGS := -DMATLAB -DWINDOWS -DNO_BLAS_H -DNO_LAPACK_H \
-Wall -I../sylv/cc -I../cc \
-Ic:/"Program Files"/MATLAB_SV71/extern/include
ifeq ($(DEBUG),yes)
# CC_FLAGS := -DDEBUG $(CC_FLAGS) -g
CC_FLAGS := -DTIMING_LOOP -DDEBUG $(CC_FLAGS) -g -pg #-Wl,-pg
CC_FLAGS := -DTIMING_LOOP -DDEBUG $(CC_FLAGS) -g #-pg #-Wl,-pg
KALMANLIB := kalmanlib_dbg.a
else
# CC_FLAGS := $(CC_FLAGS) -O2
@ -34,9 +34,9 @@ endif
# end add
matrix_interface := GeneralMatrix Vector SylvException
matobjs := $(patsubst %, ../../gensylv/cc/%.o, $(matrix_interface))
mathsource := $(patsubst %, ../../gensylv/cc/%.h, $(matrix_interface))
matcppsource := $(patsubst %, ../../gensylv/cc/%.cpp, $(matrix_interface))
matobjs := $(patsubst %, ../sylv/cc/%.o, $(matrix_interface))
mathsource := $(patsubst %, ../sylv/cc/%.h, $(matrix_interface))
matcppsource := $(patsubst %, ../sylv/cc/%.cpp, $(matrix_interface))
# cppsource := $(patsubst %.cweb,%.cpp,$(cwebsource))
kalmancppsource := $(wildcard ../cc/*.cpp)
kalmanhsource := $(wildcard ../cc/*.h)
@ -62,27 +62,27 @@ dummy.ch:
c++ $(CC_FLAGS) -c $*.cpp
kalmanlib.a: $(objects) # $(matobjs) #$(kalmanobjects)
ar cr kalmanlib.a $(kalmanobjects) $(matobjs) # $(objects)
ranlib kalmanlib.a
$(KALMANLIB): $(objects) # $(matobjs) #$(kalmanobjects)
ar cr $(KALMANLIB) $(kalmanobjects) $(matobjs) # $(objects)
ranlib $(KALMANLIB)
kalman_smoother_dll.dll: kalman_smoother.o kalmanlib.a #$(hsource) $(cppsource)
kalman_smoother_dll.dll: kalman_smoother.o $(KALMANLIB) #$(hsource) $(cppsource)
gcc -shared $(CC_FLAGS) -o kalman_smoother_dll.dll kalman_smoother.o \
kalmanlib.a $(LD_LIBS)
minv.dll: minv.o kalmanlib.a # $(hsource) $(cppsource)
minv.dll: minv.o $(KALMANLIB) # $(hsource) $(cppsource)
gcc -shared $(CC_FLAGS) -o minv.dll minv.o \
kalmanlib.a $(LD_LIBS)
kalman_filter_dll.dll: kalman_filters.o kalmanlib.a # $(hsource) $(cppsource)
kalman_filter_dll.dll: kalman_filters.o $(KALMANLIB) # $(hsource) $(cppsource)
gcc -shared $(CC_FLAGS) -o kalman_filter_dll.dll kalman_filters.o \
$(KALMANLIB) $(LD_LIBS)
kalman_filters_testx.exe: kalman_filters_testx.o kalmanlib.a # $(hsource) $(cppsource)
kalman_filters_testx.exe: kalman_filters_testx.o $(KALMANLIB) # $(hsource) $(cppsource)
gcc $(CC_FLAGS) -pg -o kalman_filters_testx.exe kalman_filters_testx.o \
$(KALMANLIB) $(LD_LIBS)
all: $(objects) kalmanlib.a kalman_smoother_dll.dll kalman_filter_dll.dll # $(cppsource) $(hsource) $(kalmanhsource) $(kalmancppsource)
all: $(objects) $(KALMANLIB) kalman_smoother_dll.dll kalman_filter_dll.dll # $(cppsource) $(hsource) $(kalmanhsource) $(kalmancppsource)
kalman_filter_loop.o: kalman_filters.cpp
c++ -DTIMING_LOOP $(CC_FLAGS) -o kalman_filter_loop.o kalman_filters.cpp

View File

@ -115,34 +115,56 @@ extern "C" {
// create state init
StateInit* init = NULL;
std::vector<double>* vll=new std::vector<double> (nper);
if (diffuse) {
GeneralMatrix Pinf(mxGetPr(prhs[9]), mxGetM(prhs[9]), mxGetN(prhs[9]));
init = new StateInit(P, Pinf, a.getData());
}
else
{
init = new StateInit(P, a.getData());
}
// fork, create objects and do filtering
KalmanTask kt(Y, Z, H, T, R, Q, *init);
if (uni)
{
KalmanUniTask kut(kt);
loglik = kut.filter(per, d, (start-1), vll);
per = per / Y.numRows();
d = d / Y.numRows();
}
else
{
if (diffuse||uni)
{
if (diffuse)
{
GeneralMatrix Pinf(mxGetPr(prhs[9]), mxGetM(prhs[9]), mxGetN(prhs[9]));
init = new StateInit(P, Pinf, a.getData());
}
else
{
init = new StateInit(P, a.getData());
}
// fork, create objects and do filtering
KalmanTask kt(Y, Z, H, T, R, Q, *init);
if (uni)
{
KalmanUniTask kut(kt);
loglik = kut.filter(per, d, (start-1), vll);
per = per / Y.numRows();
d = d / Y.numRows();
}
else
{
#ifdef TIMING_LOOP
for (int tt=0;tt<1000;++tt)
{
#endif
loglik = kt.filter(per, d, (start-1), vll);
loglik = kt.filter(per, d, (start-1), vll);
#ifdef TIMING_LOOP
}
//mexPrintf("kalman_filter: finished 10,000 loops");
mexPrintf("kalman_filter: finished 1000 loops");
#endif
}
}
else // basic Kalman
{
init = new StateInit(P, a.getData());
BasicKalmanTask bkt(Y, Z, H, T, R, Q, *init);
#ifdef TIMING_LOOP
for (int tt=0;tt<1000;++tt)
{
#endif
loglik = bkt.filter( per, d, (start-1), vll);
#ifdef DEBUG
mexPrintf("Basickalman_filter: loglik=%d \n", loglik);
#endif
#ifdef TIMING_LOOP
}
mexPrintf("Basickalman_filter: finished 1000 loops");
#endif
}
// destroy init
delete init;

View File

@ -218,38 +218,64 @@ int main(int argc, char* argv[])
int d;
// create state init
StateInit* init = NULL;
std::vector<double>* vll=new std::vector<double> (nper);
if (diffuse) {
GeneralMatrix Pinf(P.numRows(),P.numCols());
Pinf.zeros();
init = new StateInit(P, Pinf, a.getData());
}
else
{
init = new StateInit(P, a.getData());
}
// fork, create objects and do filtering
#ifdef TIMING_LOOP
for (int tt=0;tt<10000;++tt)
{
#endif
KalmanTask kt(Y, Z, H, T, R, Q, *init);
if (uni)
{
KalmanUniTask kut(kt);
loglik = kut.filter(per, d, (start-1), vll);
per = per / Y.numRows();
d = d / Y.numRows();
}
else
{
loglik = kt.filter(per, d, (start-1), vll);
}
#ifdef TIMING_LOOP
// mexPrintf("kalman_filter: finished %d loops", tt);
}
mexPrintf("kalman_filter: finished 10,000 loops");
#endif
std::vector<double>* vll=new std::vector<double> (nper);
if (diffuse||uni)
{
if (diffuse)
{
GeneralMatrix Pinf(P.numRows(),P.numCols());
Pinf.zeros();
init = new StateInit(P, Pinf, a.getData());
}
else
{
init = new StateInit(P, a.getData());
}
// fork, create objects and do filtering
#ifdef TIMING_LOOP
for (int tt=0;tt<10000;++tt)
{
#endif
KalmanTask kt(Y, Z, H, T, R, Q, *init);
if (uni)
{
KalmanUniTask kut(kt);
loglik = kut.filter(per, d, (start-1), vll);
per = per / Y.numRows();
d = d / Y.numRows();
}
else
{
loglik = kt.filter(per, d, (start-1), vll);
}
#ifdef TIMING_LOOP
// mexPrintf("kalman_filter: finished %d loops", tt);
}
mexPrintf("kalman_filter: finished 10,000 loops");
#endif
}
else // basic Kalman
{
init = new StateInit(P, a.getData());
BasicKalmanTask bkt(Y, Z, H, T, R, Q, *init);
#ifdef TIMING_LOOP
for (int tt=0;tt<10000;++tt)
{
#endif
loglik = bkt.filter( per, d, (start-1), vll);
#ifdef DEBUG
mexPrintf("Basickalman_filter: loglik=%d \n", loglik);
#endif
#ifdef TIMING_LOOP
}
mexPrintf("Basickalman_filter: finished 10,000 loops");
#endif
}
// destroy init
delete init;
mexPrintf("logLik = %f \n", loglik);