1st cut of KF refactoring and simplification
git-svn-id: https://www.dynare.org/svn/dynare/trunk@2750 ac1d8469-bf42-47a9-8791-bf33cf982152time-shift
parent
36ecf9d6bd
commit
1245d60326
File diff suppressed because it is too large
Load Diff
|
@ -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 KalmanUniTask;
|
||||||
class KalmanTask{
|
class KalmanTask{
|
||||||
friend class KalmanUniTask;
|
friend class KalmanUniTask;
|
||||||
|
|
|
@ -1,16 +1,16 @@
|
||||||
# $Id: Makefile 531 2005-11-30 13:49:48Z kamenik $
|
# $Id: Makefile 531 2005-11-30 13:49:48Z kamenik $
|
||||||
# Copyright 2005, Ondra Kamenik
|
# Copyright 2005, Ondra Kamenik
|
||||||
|
|
||||||
DEBUG = yes
|
#DEBUG = yes
|
||||||
|
|
||||||
#LD_LIBS := -llapack -lcblas -lf77blas -latlas -lg2c
|
#LD_LIBS := -llapack -lcblas -lf77blas -latlas -lg2c
|
||||||
CC_FLAGS := -DMATLAB -DWINDOWS -DNO_BLAS_H -DNO_LAPACK_H \
|
CC_FLAGS := -DMATLAB -DWINDOWS -DNO_BLAS_H -DNO_LAPACK_H \
|
||||||
-Wall -I../../gensylv/cc -I../cc \
|
-Wall -I../sylv/cc -I../cc \
|
||||||
-Ic:/"Program Files"/MATLAB_SV71/extern/include
|
-Ic:/"Program Files"/MATLAB_SV71/extern/include
|
||||||
|
|
||||||
ifeq ($(DEBUG),yes)
|
ifeq ($(DEBUG),yes)
|
||||||
# CC_FLAGS := -DDEBUG $(CC_FLAGS) -g
|
# 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
|
KALMANLIB := kalmanlib_dbg.a
|
||||||
else
|
else
|
||||||
# CC_FLAGS := $(CC_FLAGS) -O2
|
# CC_FLAGS := $(CC_FLAGS) -O2
|
||||||
|
@ -34,9 +34,9 @@ endif
|
||||||
# end add
|
# end add
|
||||||
|
|
||||||
matrix_interface := GeneralMatrix Vector SylvException
|
matrix_interface := GeneralMatrix Vector SylvException
|
||||||
matobjs := $(patsubst %, ../../gensylv/cc/%.o, $(matrix_interface))
|
matobjs := $(patsubst %, ../sylv/cc/%.o, $(matrix_interface))
|
||||||
mathsource := $(patsubst %, ../../gensylv/cc/%.h, $(matrix_interface))
|
mathsource := $(patsubst %, ../sylv/cc/%.h, $(matrix_interface))
|
||||||
matcppsource := $(patsubst %, ../../gensylv/cc/%.cpp, $(matrix_interface))
|
matcppsource := $(patsubst %, ../sylv/cc/%.cpp, $(matrix_interface))
|
||||||
# cppsource := $(patsubst %.cweb,%.cpp,$(cwebsource))
|
# cppsource := $(patsubst %.cweb,%.cpp,$(cwebsource))
|
||||||
kalmancppsource := $(wildcard ../cc/*.cpp)
|
kalmancppsource := $(wildcard ../cc/*.cpp)
|
||||||
kalmanhsource := $(wildcard ../cc/*.h)
|
kalmanhsource := $(wildcard ../cc/*.h)
|
||||||
|
@ -62,27 +62,27 @@ dummy.ch:
|
||||||
c++ $(CC_FLAGS) -c $*.cpp
|
c++ $(CC_FLAGS) -c $*.cpp
|
||||||
|
|
||||||
|
|
||||||
kalmanlib.a: $(objects) # $(matobjs) #$(kalmanobjects)
|
$(KALMANLIB): $(objects) # $(matobjs) #$(kalmanobjects)
|
||||||
ar cr kalmanlib.a $(kalmanobjects) $(matobjs) # $(objects)
|
ar cr $(KALMANLIB) $(kalmanobjects) $(matobjs) # $(objects)
|
||||||
ranlib kalmanlib.a
|
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 \
|
gcc -shared $(CC_FLAGS) -o kalman_smoother_dll.dll kalman_smoother.o \
|
||||||
kalmanlib.a $(LD_LIBS)
|
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 \
|
gcc -shared $(CC_FLAGS) -o minv.dll minv.o \
|
||||||
kalmanlib.a $(LD_LIBS)
|
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 \
|
gcc -shared $(CC_FLAGS) -o kalman_filter_dll.dll kalman_filters.o \
|
||||||
$(KALMANLIB) $(LD_LIBS)
|
$(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 \
|
gcc $(CC_FLAGS) -pg -o kalman_filters_testx.exe kalman_filters_testx.o \
|
||||||
$(KALMANLIB) $(LD_LIBS)
|
$(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
|
kalman_filter_loop.o: kalman_filters.cpp
|
||||||
c++ -DTIMING_LOOP $(CC_FLAGS) -o kalman_filter_loop.o kalman_filters.cpp
|
c++ -DTIMING_LOOP $(CC_FLAGS) -o kalman_filter_loop.o kalman_filters.cpp
|
||||||
|
|
|
@ -115,34 +115,56 @@ extern "C" {
|
||||||
// create state init
|
// create state init
|
||||||
StateInit* init = NULL;
|
StateInit* init = NULL;
|
||||||
std::vector<double>* vll=new std::vector<double> (nper);
|
std::vector<double>* vll=new std::vector<double> (nper);
|
||||||
if (diffuse) {
|
if (diffuse||uni)
|
||||||
GeneralMatrix Pinf(mxGetPr(prhs[9]), mxGetM(prhs[9]), mxGetN(prhs[9]));
|
{
|
||||||
init = new StateInit(P, Pinf, a.getData());
|
if (diffuse)
|
||||||
}
|
{
|
||||||
else
|
GeneralMatrix Pinf(mxGetPr(prhs[9]), mxGetM(prhs[9]), mxGetN(prhs[9]));
|
||||||
{
|
init = new StateInit(P, Pinf, a.getData());
|
||||||
init = new StateInit(P, a.getData());
|
}
|
||||||
}
|
else
|
||||||
// fork, create objects and do filtering
|
{
|
||||||
KalmanTask kt(Y, Z, H, T, R, Q, *init);
|
init = new StateInit(P, a.getData());
|
||||||
if (uni)
|
}
|
||||||
{
|
// fork, create objects and do filtering
|
||||||
KalmanUniTask kut(kt);
|
KalmanTask kt(Y, Z, H, T, R, Q, *init);
|
||||||
loglik = kut.filter(per, d, (start-1), vll);
|
if (uni)
|
||||||
per = per / Y.numRows();
|
{
|
||||||
d = d / Y.numRows();
|
KalmanUniTask kut(kt);
|
||||||
}
|
loglik = kut.filter(per, d, (start-1), vll);
|
||||||
else
|
per = per / Y.numRows();
|
||||||
{
|
d = d / Y.numRows();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
#ifdef TIMING_LOOP
|
#ifdef TIMING_LOOP
|
||||||
for (int tt=0;tt<1000;++tt)
|
for (int tt=0;tt<1000;++tt)
|
||||||
{
|
{
|
||||||
#endif
|
#endif
|
||||||
loglik = kt.filter(per, d, (start-1), vll);
|
loglik = kt.filter(per, d, (start-1), vll);
|
||||||
#ifdef TIMING_LOOP
|
#ifdef TIMING_LOOP
|
||||||
}
|
}
|
||||||
//mexPrintf("kalman_filter: finished 10,000 loops");
|
mexPrintf("kalman_filter: finished 1000 loops");
|
||||||
#endif
|
#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
|
// destroy init
|
||||||
delete init;
|
delete init;
|
||||||
|
|
|
@ -218,38 +218,64 @@ int main(int argc, char* argv[])
|
||||||
int d;
|
int d;
|
||||||
// create state init
|
// create state init
|
||||||
StateInit* init = NULL;
|
StateInit* init = NULL;
|
||||||
std::vector<double>* vll=new std::vector<double> (nper);
|
std::vector<double>* vll=new std::vector<double> (nper);
|
||||||
if (diffuse) {
|
|
||||||
GeneralMatrix Pinf(P.numRows(),P.numCols());
|
if (diffuse||uni)
|
||||||
Pinf.zeros();
|
{
|
||||||
init = new StateInit(P, Pinf, a.getData());
|
if (diffuse)
|
||||||
}
|
{
|
||||||
else
|
GeneralMatrix Pinf(P.numRows(),P.numCols());
|
||||||
{
|
Pinf.zeros();
|
||||||
init = new StateInit(P, a.getData());
|
init = new StateInit(P, Pinf, a.getData());
|
||||||
}
|
}
|
||||||
// fork, create objects and do filtering
|
else
|
||||||
#ifdef TIMING_LOOP
|
{
|
||||||
for (int tt=0;tt<10000;++tt)
|
init = new StateInit(P, a.getData());
|
||||||
{
|
}
|
||||||
#endif
|
// fork, create objects and do filtering
|
||||||
KalmanTask kt(Y, Z, H, T, R, Q, *init);
|
#ifdef TIMING_LOOP
|
||||||
if (uni)
|
for (int tt=0;tt<10000;++tt)
|
||||||
{
|
{
|
||||||
KalmanUniTask kut(kt);
|
#endif
|
||||||
loglik = kut.filter(per, d, (start-1), vll);
|
KalmanTask kt(Y, Z, H, T, R, Q, *init);
|
||||||
per = per / Y.numRows();
|
if (uni)
|
||||||
d = d / Y.numRows();
|
{
|
||||||
}
|
KalmanUniTask kut(kt);
|
||||||
else
|
loglik = kut.filter(per, d, (start-1), vll);
|
||||||
{
|
per = per / Y.numRows();
|
||||||
loglik = kt.filter(per, d, (start-1), vll);
|
d = d / Y.numRows();
|
||||||
}
|
}
|
||||||
#ifdef TIMING_LOOP
|
else
|
||||||
// mexPrintf("kalman_filter: finished %d loops", tt);
|
{
|
||||||
}
|
loglik = kt.filter(per, d, (start-1), vll);
|
||||||
mexPrintf("kalman_filter: finished 10,000 loops");
|
}
|
||||||
#endif
|
#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
|
// destroy init
|
||||||
delete init;
|
delete init;
|
||||||
mexPrintf("logLik = %f \n", loglik);
|
mexPrintf("logLik = %f \n", loglik);
|
||||||
|
|
Loading…
Reference in New Issue