From 5504811b1a53c358d03368c97a84641de2fee0c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Fri, 24 Sep 2010 13:01:52 +0200 Subject: [PATCH] Removed obsolete Kalman filter code --- mex/sources/kalman/cc/Makefile | 64 - mex/sources/kalman/cc/disclyap_fast.cpp | 102 - mex/sources/kalman/cc/disclyap_fast.h | 34 - mex/sources/kalman/cc/kalman.cpp | 2481 ----------------- mex/sources/kalman/cc/kalman.h | 339 --- mex/sources/kalman/cc/ssf.cpp | 323 --- mex/sources/kalman/cc/ssf.h | 193 -- mex/sources/kalman/cc/ssf_uni.cpp | 166 -- mex/sources/kalman/cc/ssf_uni.h | 101 - mex/sources/kalman/cc/state_init.cpp | 59 - mex/sources/kalman/cc/state_init.h | 60 - mex/sources/kalman/cc/ts_exception.h | 64 - mex/sources/kalman/cc/utils.cpp | 411 --- mex/sources/kalman/cc/utils.h | 121 - mex/sources/kalman/matlab/Makefile | 134 - mex/sources/kalman/matlab/Makefile_mex | 80 - .../kalman/matlab/diffuse_kalman_filter.cpp | 153 - .../kalman/matlab/disclyap_fast_dll.cpp | 89 - mex/sources/kalman/matlab/gendata.m | 30 - mex/sources/kalman/matlab/gmvm.cpp | 122 - mex/sources/kalman/matlab/kalman_filter.m | 37 - mex/sources/kalman/matlab/kalman_filter_Z.cpp | 167 -- mex/sources/kalman/matlab/kalman_filters.cpp | 209 -- mex/sources/kalman/matlab/kalman_filters.m | 61 - .../kalman/matlab/kalman_filters_testx.cpp | 324 --- mex/sources/kalman/matlab/kalman_smoother.cpp | 141 - mex/sources/kalman/matlab/kalman_smoother.m | 41 - mex/sources/kalman/matlab/kalmandll_test.m | 23 - mex/sources/kalman/matlab/kalmanlib.def | 2 - mex/sources/kalman/matlab/mexopts.bat | 58 - mex/sources/kalman/matlab/minv.cpp | 108 - mex/sources/kalman/matlab/qtamvm.cpp | 148 - mex/sources/kalman/matlab/qtmvm.cpp | 155 - .../univariate_diffuse_kalman_filter.cpp | 158 -- .../matlab/univariate_kalman_filter.cpp | 156 -- mex/sources/kalman/qt/FortranSubroutines.zip | Bin 5447 -> 0 bytes mex/sources/kalman/qt/cc/qt.h | 37 - mex/sources/kalman/qt/f90/LdM.f90 | 24 - mex/sources/kalman/qt/f90/LdSLd.f90 | 34 - mex/sources/kalman/qt/f90/LdV.f90 | 17 - mex/sources/kalman/qt/f90/MTt.f90 | 31 - mex/sources/kalman/qt/f90/Makefile | 37 - mex/sources/kalman/qt/f90/QT2Ld.f90 | 26 - mex/sources/kalman/qt/f90/QT2T.f90 | 20 - mex/sources/kalman/qt/f90/QTSQTt.f90 | 50 - mex/sources/kalman/qt/f90/QTV.f90 | 33 - mex/sources/kalman/qt/f90/QTV_1.f90 | 33 - mex/sources/kalman/qt/f90/S2D.f90 | 19 - mex/sources/kalman/qt/f90/S2U.f90 | 23 - mex/sources/kalman/qt/f90/TD.f90 | 22 - mex/sources/kalman/qt/f90/TM.f90 | 29 - mex/sources/kalman/qt/f90/TSTt.f90 | 34 - mex/sources/kalman/qt/f90/TT.f90 | 29 - mex/sources/kalman/qt/f90/TU.f90 | 31 - mex/sources/kalman/qt/f90/TUt.f90 | 29 - mex/sources/kalman/qt/f90/TV.f90 | 27 - mex/sources/kalman/qt/test/Makefile | 85 - mex/sources/kalman/qt/test/QT_tab | 11 - mex/sources/kalman/qt/test/QTmatlabTest.m | 45 - mex/sources/kalman/qt/test/aa_dat | 11 - mex/sources/kalman/qt/test/ascii_array.cpp | 84 - mex/sources/kalman/qt/test/ascii_array.h | 15 - mex/sources/kalman/qt/test/dgemmmtm_exe.cpp | 117 - mex/sources/kalman/qt/test/dgemvm_exe.cpp | 104 - mex/sources/kalman/qt/test/qtamvm_exe.cpp | 115 - mex/sources/kalman/qt/test/qtmmmtm_exe.cpp | 100 - mex/sources/kalman/qt/test/qtv1mvm_exe.cpp | 105 - mex/sources/kalman/qt/test/qtvmvm_exe.cpp | 101 - mex/sources/kalman/sylv/cc/GeneralMatrix.cpp | 825 ------ mex/sources/kalman/sylv/cc/GeneralMatrix.h | 355 --- mex/sources/kalman/sylv/cc/Makefile | 41 - mex/sources/kalman/sylv/cc/SylvException.cpp | 69 - mex/sources/kalman/sylv/cc/SylvException.h | 39 - mex/sources/kalman/sylv/cc/SylvMemory.h | 63 - mex/sources/kalman/sylv/cc/Vector.cpp | 370 --- mex/sources/kalman/sylv/cc/Vector.h | 184 -- mex/sources/kalman/sylv/cc/cppblas.h | 68 - mex/sources/kalman/sylv/cc/cpplapack.h | 82 - mex/sources/kalman/testing/Makefile | 65 - mex/sources/kalman/testing/ascii_matrix.cpp | 56 - mex/sources/kalman/testing/ascii_matrix.h | 24 - mex/sources/kalman/testing/dynare_filter.m | 33 - mex/sources/kalman/testing/dynare_smoother.m | 37 - mex/sources/kalman/testing/kalmandll_test.m | 20 - mex/sources/kalman/testing/test_data.tgz | Bin 29902 -> 0 bytes mex/sources/kalman/testing/tests.asv | 311 --- mex/sources/kalman/testing/tests.cpp | 311 --- 87 files changed, 11345 deletions(-) delete mode 100644 mex/sources/kalman/cc/Makefile delete mode 100644 mex/sources/kalman/cc/disclyap_fast.cpp delete mode 100644 mex/sources/kalman/cc/disclyap_fast.h delete mode 100644 mex/sources/kalman/cc/kalman.cpp delete mode 100644 mex/sources/kalman/cc/kalman.h delete mode 100644 mex/sources/kalman/cc/ssf.cpp delete mode 100644 mex/sources/kalman/cc/ssf.h delete mode 100644 mex/sources/kalman/cc/ssf_uni.cpp delete mode 100644 mex/sources/kalman/cc/ssf_uni.h delete mode 100644 mex/sources/kalman/cc/state_init.cpp delete mode 100644 mex/sources/kalman/cc/state_init.h delete mode 100644 mex/sources/kalman/cc/ts_exception.h delete mode 100644 mex/sources/kalman/cc/utils.cpp delete mode 100644 mex/sources/kalman/cc/utils.h delete mode 100644 mex/sources/kalman/matlab/Makefile delete mode 100644 mex/sources/kalman/matlab/Makefile_mex delete mode 100644 mex/sources/kalman/matlab/diffuse_kalman_filter.cpp delete mode 100644 mex/sources/kalman/matlab/disclyap_fast_dll.cpp delete mode 100644 mex/sources/kalman/matlab/gendata.m delete mode 100644 mex/sources/kalman/matlab/gmvm.cpp delete mode 100644 mex/sources/kalman/matlab/kalman_filter.m delete mode 100644 mex/sources/kalman/matlab/kalman_filter_Z.cpp delete mode 100644 mex/sources/kalman/matlab/kalman_filters.cpp delete mode 100644 mex/sources/kalman/matlab/kalman_filters.m delete mode 100644 mex/sources/kalman/matlab/kalman_filters_testx.cpp delete mode 100644 mex/sources/kalman/matlab/kalman_smoother.cpp delete mode 100644 mex/sources/kalman/matlab/kalman_smoother.m delete mode 100644 mex/sources/kalman/matlab/kalmandll_test.m delete mode 100644 mex/sources/kalman/matlab/kalmanlib.def delete mode 100644 mex/sources/kalman/matlab/mexopts.bat delete mode 100644 mex/sources/kalman/matlab/minv.cpp delete mode 100644 mex/sources/kalman/matlab/qtamvm.cpp delete mode 100644 mex/sources/kalman/matlab/qtmvm.cpp delete mode 100644 mex/sources/kalman/matlab/univariate_diffuse_kalman_filter.cpp delete mode 100644 mex/sources/kalman/matlab/univariate_kalman_filter.cpp delete mode 100644 mex/sources/kalman/qt/FortranSubroutines.zip delete mode 100644 mex/sources/kalman/qt/cc/qt.h delete mode 100644 mex/sources/kalman/qt/f90/LdM.f90 delete mode 100644 mex/sources/kalman/qt/f90/LdSLd.f90 delete mode 100644 mex/sources/kalman/qt/f90/LdV.f90 delete mode 100644 mex/sources/kalman/qt/f90/MTt.f90 delete mode 100644 mex/sources/kalman/qt/f90/Makefile delete mode 100644 mex/sources/kalman/qt/f90/QT2Ld.f90 delete mode 100644 mex/sources/kalman/qt/f90/QT2T.f90 delete mode 100644 mex/sources/kalman/qt/f90/QTSQTt.f90 delete mode 100644 mex/sources/kalman/qt/f90/QTV.f90 delete mode 100644 mex/sources/kalman/qt/f90/QTV_1.f90 delete mode 100644 mex/sources/kalman/qt/f90/S2D.f90 delete mode 100644 mex/sources/kalman/qt/f90/S2U.f90 delete mode 100644 mex/sources/kalman/qt/f90/TD.f90 delete mode 100644 mex/sources/kalman/qt/f90/TM.f90 delete mode 100644 mex/sources/kalman/qt/f90/TSTt.f90 delete mode 100644 mex/sources/kalman/qt/f90/TT.f90 delete mode 100644 mex/sources/kalman/qt/f90/TU.f90 delete mode 100644 mex/sources/kalman/qt/f90/TUt.f90 delete mode 100644 mex/sources/kalman/qt/f90/TV.f90 delete mode 100644 mex/sources/kalman/qt/test/Makefile delete mode 100644 mex/sources/kalman/qt/test/QT_tab delete mode 100644 mex/sources/kalman/qt/test/QTmatlabTest.m delete mode 100644 mex/sources/kalman/qt/test/aa_dat delete mode 100644 mex/sources/kalman/qt/test/ascii_array.cpp delete mode 100644 mex/sources/kalman/qt/test/ascii_array.h delete mode 100644 mex/sources/kalman/qt/test/dgemmmtm_exe.cpp delete mode 100644 mex/sources/kalman/qt/test/dgemvm_exe.cpp delete mode 100644 mex/sources/kalman/qt/test/qtamvm_exe.cpp delete mode 100644 mex/sources/kalman/qt/test/qtmmmtm_exe.cpp delete mode 100644 mex/sources/kalman/qt/test/qtv1mvm_exe.cpp delete mode 100644 mex/sources/kalman/qt/test/qtvmvm_exe.cpp delete mode 100644 mex/sources/kalman/sylv/cc/GeneralMatrix.cpp delete mode 100644 mex/sources/kalman/sylv/cc/GeneralMatrix.h delete mode 100644 mex/sources/kalman/sylv/cc/Makefile delete mode 100644 mex/sources/kalman/sylv/cc/SylvException.cpp delete mode 100644 mex/sources/kalman/sylv/cc/SylvException.h delete mode 100644 mex/sources/kalman/sylv/cc/SylvMemory.h delete mode 100644 mex/sources/kalman/sylv/cc/Vector.cpp delete mode 100644 mex/sources/kalman/sylv/cc/Vector.h delete mode 100644 mex/sources/kalman/sylv/cc/cppblas.h delete mode 100644 mex/sources/kalman/sylv/cc/cpplapack.h delete mode 100644 mex/sources/kalman/testing/Makefile delete mode 100644 mex/sources/kalman/testing/ascii_matrix.cpp delete mode 100644 mex/sources/kalman/testing/ascii_matrix.h delete mode 100644 mex/sources/kalman/testing/dynare_filter.m delete mode 100644 mex/sources/kalman/testing/dynare_smoother.m delete mode 100644 mex/sources/kalman/testing/kalmandll_test.m delete mode 100644 mex/sources/kalman/testing/test_data.tgz delete mode 100644 mex/sources/kalman/testing/tests.asv delete mode 100644 mex/sources/kalman/testing/tests.cpp diff --git a/mex/sources/kalman/cc/Makefile b/mex/sources/kalman/cc/Makefile deleted file mode 100644 index db5fc8dba..000000000 --- a/mex/sources/kalman/cc/Makefile +++ /dev/null @@ -1,64 +0,0 @@ -# $Id: Makefile 531 2005-11-30 13:49:48Z kamenik $ -# Copyright 2005, Ondra Kamenik - -#DEBUG = yes - -LD_LIBS := -llapack -lcblas -lf77blas -latlas -lg2c -CC_FLAGS := -DMATLAB -DWINDOWS -DNO_BLAS_H -DNO_LAPACK_H \ - -fpic -Wall -I../sylv/cc \ - -I$(MATLAB_PATH)/extern/include #-pg - -ifeq ($(DEBUG),yes) - CC_FLAGS := -DDEBUG $(CC_FLAGS) -g -else - CC_FLAGS := $(CC_FLAGS) -O3 -endif - -# Added by GP -# LDFLAGS := -llapack -lcblas -lf77blas -latlas -lg2c -lstdc++ -lmingw32 - LDFLAGS := -Wl,--library-path $(LD_LIBRARY_PATH) \ - -Wl,-L$(MATLAB_PATH)/extern/lib/win32/microsoft/ \ - -Wl,-llibmex -Wl,-llibmx -Wl,-llibmwlapack -Wl,-llibdflapack \ - -lg2c -lmingw32 -lstdc++ $(LDFLAGS) - -# -Wl,-L'f:/CygWin/usr/local/atlas/lib' \ -# -Wl,-L'f:/CygWin/lib' \ -# $(LDFLAGS) - LD_LIBS :=$(LDFLAGS) -# end add - -matrix_interface := GeneralMatrix Vector SylvException -matobjs := $(patsubst %, ../sylv/cc/%.o, $(matrix_interface)) -mathsource := $(patsubst %, ../sylv/cc/%.h, $(matrix_interface)) -matcppsource := $(patsubst %, ../sylv/cc/%.cpp, $(matrix_interface)) -cwebsource := $(wildcard *.cweb) -# cppsource := $(patsubst %.cweb,%.cpp,$(cwebsource)) -cppsource := $(wildcard *.cpp) -hsource := $(wildcard *.h) -objects := $(patsubst %.cpp,%.o,$(cppsource)) -hwebsource := $(wildcard *.hweb) - -dummy.ch: - touch dummy.ch - -# %.cpp: %.cweb dummy.ch -# ctangle -bhp $*.cweb dummy.ch $*.cpp - -# %.h: %.hweb dummy.ch -# ctangle -bhp $*.hweb dummy.ch $*.h - -%.o: %.cpp $(hsource) $(mathsource) - c++ $(CC_FLAGS) -c $*.cpp - -all: $(objects) # $(cppsource) $(hsource) - -doc: main.web $(hwebsource) $(cwebsource) - cweave -bhp main.web - pdftex main - mv main.pdf ts.pdf - -clear: - rm -f *.o - rm -f *.{pdf,dvi,log,scn,idx,toc} -# rm -f *.cpp -# rm -f *.h diff --git a/mex/sources/kalman/cc/disclyap_fast.cpp b/mex/sources/kalman/cc/disclyap_fast.cpp deleted file mode 100644 index 3b66c295c..000000000 --- a/mex/sources/kalman/cc/disclyap_fast.cpp +++ /dev/null @@ -1,102 +0,0 @@ -/* -* Copyright (C) 2008-2009 Dynare Team -* -* This file is part of Dynare. -* -* Dynare is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* Dynare is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with Dynare. If not, see . -*/ -/**************************************************************** -% function X=disclyap_fast(G,V,tol,ch) -% -% Solve the discrete Lyapunov Equation -% X=G*X*G'+V -% Using the Doubling Algorithm -% -% INPUT: -% G and V - square General matrices of same size -% tol - double tollerance level -% flag_ch - integer flag: if 1 check if the result is positive -% definite and generate an error message if it is not -% OUTPUT: -% on completion V - square General matrice contains solution -% -% based on work of Joe Pearlman and Alejandro Justiniano -% 3/5/2005 -% C++ version 28/07/09 by Dynare team -****************************************************************/ -#include "ts_exception.h" -#include "cppblas.h" -#include "GeneralMatrix.h" -//#include "Vector.h" -#include "SylvException.h" -#include "utils.h" -#include "mex.h" - -void disclyap_fast(const GeneralMatrix &G, const GeneralMatrix & V, GeneralMatrix &X, double tol = 1e-16, int flag_ch=0) - { - /** - if nargin == 2 | isempty( ch ) == 1 - flag_ch = 0; - else - flag_ch = 1; - end - **/ - //P0=V; - GeneralMatrix P0(V); - //A0=G; - GeneralMatrix A0(G); - - //n=size(G,1); - int n= A0.numCols(); - const double alpha=1.0; - const double half=0.5; - const double neg_alpha=-1.0; - const double omega=0.0; - - GeneralMatrix A1(n,n); - GeneralMatrix Ptmp(n,n); - GeneralMatrix P1(P0); - GeneralMatrix I(n,n); - I.unit(); - bool matd=true; - while (matd ) // matrix diff > tol - { - //P1=P0+A0*P0*A0'; - // first step Ptmp=P0*A0'; - // DGEMM: C := alpha*op( A )*op( B ) + beta*C, - BLAS_dgemm("N", "T", &n, &n, &n, &alpha, P0.base(), &n, - A0.base(), &n, &omega, Ptmp.base(), &n); - // P1=P0+A0*Ptmp; - BLAS_dgemm("N", "N", &n, &n, &n, &alpha, A0.base(), &n, - Ptmp.base(), &n, &alpha, P1.base(), &n); - // A1=A0*A0; - // A0=A1 (=A0*A0); - // A0.multRight(A0); - BLAS_dgemm("N", "N", &n, &n, &n, &alpha, A0.base(), &n, - A0.base(), &n, &omega, A1.base(), &n); - - // check if max( max( abs( P1 - P0 ) ) )>tol - matd=P0.isDiffSym(P1, tol); - P0=P1; - A0=A1; - }//end while - - // X=P0=(P0+P0')/2; - BLAS_dgemm("T", "N", &n, &n, &n, &half, P1.base(), &n, - I.base(), &n, &half, P0.base(), &n); - X=P0; - // Check that X is positive definite - if (flag_ch==1) - NormCholesky chol(P0); - } diff --git a/mex/sources/kalman/cc/disclyap_fast.h b/mex/sources/kalman/cc/disclyap_fast.h deleted file mode 100644 index abf7bca34..000000000 --- a/mex/sources/kalman/cc/disclyap_fast.h +++ /dev/null @@ -1,34 +0,0 @@ -/* -* Copyright (C) 2008-2009 Dynare Team -* -* This file is part of Dynare. -* -* Dynare is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* Dynare is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with Dynare. If not, see . -*/ -/**************************************************************** -% function X=disclyap_fast(G,V,ch) -% -% Solve the discrete Lyapunov Equation -% X=G*X*G'+V -% Using the Doubling Algorithm -% -% If ch is defined then the code will check if the resulting X -% is positive definite and generate an error message if it is not -% -% based on work of Joe Pearlman and Alejandro Justiniano -% 3/5/2005 -% C++ version 28/07/09 by Dynare team -****************************************************************/ -#include "GeneralMatrix.h" -void disclyap_fast(const GeneralMatrix &G, const GeneralMatrix & V, GeneralMatrix &X, double tol, int ch); diff --git a/mex/sources/kalman/cc/kalman.cpp b/mex/sources/kalman/cc/kalman.cpp deleted file mode 100644 index 1d53280ee..000000000 --- a/mex/sources/kalman/cc/kalman.cpp +++ /dev/null @@ -1,2481 +0,0 @@ -/* -* Copyright (C) 2008-2009 Dynare Team -* -* This file is part of Dynare. -* -* Dynare is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* Dynare is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with Dynare. If not, see . -*/ - -/* derived from c++kalman_filter library by O. Kamenik */ - -/***************** -The file is divided into two parts, routines for multivariate filtering -and smoothing and univariate ones. The only reason for keeping these in one -file, is that the univariate routines use code bits from multivariate -routines. -*****************/ - -#include "kalman.h" -#include "ts_exception.h" - -#include "cppblas.h" -//#include "cpplapack.h" - -#include -#include -#include - -/***************** -We delete everything which is not |NULL|. This is important, since -it may happen that the reults are not filled completely. (For instance -in the beginning or if error eccurred.) -*****************/ - -FilterResults::~FilterResults() - { - for(unsigned int i= 0;i (int)Finv.size()+1, - "Wrong time for FilterResults::set"); - - int tm= t-1; - if(Finv[tm]) - delete Finv[tm]; - if(v[tm]) - delete v[tm]; - if(L[tm]) - delete L[tm]; - if(a[tm]) - delete a[tm]; - if(P[tm]) - delete P[tm]; - - if(t> maxt) - maxt= t; - - Finv[tm]= new PLUFact(FFinv); - v[tm]= new Vector(vv); - L[tm]= new GeneralMatrix(LL); - a[tm]= new Vector(aa); - P[tm]= new GeneralMatrix(PP); - loglik[tm]= ll; - } - -const PLUFact&FilterResults::getFInverse(int t)const - { - TS_RAISE_IF(t<1||t> maxt, - "Wrong time for FilterResults::getFInverse"); - return*(Finv[t-1]); - } - -const Vector&FilterResults::getV(int t)const - { - TS_RAISE_IF(t<1||t> maxt, - "Wrong time for FilterResults::getV"); - return*(v[t-1]); - } - -const GeneralMatrix&FilterResults::getL(int t)const - { - TS_RAISE_IF(t<1||t> maxt, - "Wrong time for FilterResults::getL"); - return*(L[t-1]); - } - -const Vector&FilterResults::getA(int t)const - { - TS_RAISE_IF(t<1||t> maxt, - "Wrong time for FilterResults::getA"); - return*(a[t-1]); - } - -const GeneralMatrix&FilterResults::getP(int t)const - { - TS_RAISE_IF(t<1||t> maxt, - "Wrong time for FilterResults::getP"); - return*(P[t-1]); - } - - /***************** - This adds all the log likelihoods for all periods. If some periods - in the results have not been set, these are initialized to zeros and - thus this method is pretty safe but may not be if the likelihood tends to - be far lower or higher than 0. -*****************/ - -double -FilterResults::getLogLikelihood()const - { - double res= 0.0; - for(unsigned int i= 0;i* vloglik)const - { - double res= 0.0; - for(unsigned int i= 0;i* vloglik)const - { - double res= 0.0; - for(unsigned int i= start;i maxt, - "Wrong time for DiffuseFilterResults::isFinfRegular"); - return Finf_reg[t-1]; - } - - - -bool -DiffuseFilterResults::isPinfZero(int t)const - { - TS_RAISE_IF(t<1||t> maxt, - "Wrong time for DiffuseFilterResults::isPinfZero"); - return Pinf_zero[t-1]; - } - - /***************** - Here we raise an error on attempt to retrieve inverse of $F_\infty$ - for a period in which it was not regular. Caller has to call - |isFinfRegular| first. -*****************/ - -const PLUFact&DiffuseFilterResults::getFinfInverse(int t)const - { - TS_RAISE_IF(t<1||t> maxt, - "Wrong time for DiffuseFilterResults::getFinfInverse"); - TS_RAISE_IF(!isFinfRegular(t), - "Finf not regular in the period in DiffuseFilterResults::getFinfInverse"); - return getFInverse(t); - } - - /***************** - Here we issue an error on attempt to retrieve inverse of $F_*$ for a - period when $F_\infty$ was regular. -*****************/ - -const PLUFact&DiffuseFilterResults::getFstarInverse(int t)const - { - TS_RAISE_IF(t<1||t> maxt, - "Wrong time for DiffuseFilterResults::getFstarInverse"); - TS_RAISE_IF(isFinfRegular(t), - "Finf not zero in the period in DiffuseFilterResults::getFstarInverse"); - return getFInverse(t); - } - - /***************** - This should be called only when $F_\infty$ was regular, we raise an - error otherwise. -*****************/ - -const GeneralMatrix&DiffuseFilterResults::getF2(int t)const - { - TS_RAISE_IF(t<1||t> maxt, - "Wrong time for FilterResults::getF2"); - TS_RAISE_IF(!isFinfRegular(t), - "Finf not regular in the period in DiffuseFilterResults::getF2"); - return*(F_2[t-1]); - } - - /***************** - This should be called only when $F_\infty$ was regular, we raise an - error otherwise. -*****************/ - -const GeneralMatrix&DiffuseFilterResults::getL1(int t)const - { - TS_RAISE_IF(t<1||t> maxt, - "Wrong time for FilterResults::getL1"); - TS_RAISE_IF(!isFinfRegular(t), - "Finf not regular in the period in DiffuseFilterResults::getL1"); - return*(L_1[t-1]); - } - - /***************** - The $P_\infty$ should be retrieved only if it is not zero, (these - are all diffuse periods) -*****************/ - -const GeneralMatrix&DiffuseFilterResults::getPinf(int t)const - { - TS_RAISE_IF(t<1||t> maxt, - "Wrong time for FilterResults::getPinf"); - TS_RAISE_IF(isPinfZero(t), - "Pinf is zero in the period in DiffuseFilterResults::getPinf"); - return*(L_1[t-1]); - } - - /***************** - This sets the diffuse results for diffuse periods when - $F_\infty$ is regular. Note that in this case the inverse - $F_\infty^{-1}$ is stored as $F^{-1}$ of |FilterResults|, and thus is - returned by call |getFinfInverse|. Also, $L^{(0)}$ is stored as $L$ of - |FilterResults|, and retrieved by |getL0()| which is equivalent to - |FilterResults::getL()| (see |@<|DiffuseFilterResults| class - declaration@>|). -*****************/ - -void DiffuseFilterResults::set(int t,const PLUFact&FFinv,const GeneralMatrix&FF_2, - const Vector&vv,const GeneralMatrix&LL_0, - const GeneralMatrix&LL_1,const Vector&aa, - const GeneralMatrix&PPstar,const GeneralMatrix&PPinf, - double ll) - { - FilterResults::set(t,FFinv,vv,LL_0,aa,PPstar,ll); - - int tm= t-1; - if(L_1[tm]) - delete L_1[tm]; - if(Pinf[tm]) - delete Pinf[tm]; - if(F_2[tm]) - delete F_2[tm]; - - L_1[tm]= new GeneralMatrix(LL_1); - Pinf[tm]= new GeneralMatrix(PPinf); - F_2[tm]= new GeneralMatrix(FF_2); - Finf_reg[tm]= true; - Pinf_zero[tm]= false; - } - - /***************** - This sets the diffuse results for diffuse period when the $F_\infty$ - is zero. We do not set $F^{(2)}$, and we set $F_*^{-1}$ instead of - $F_\infty^{-1}$. -*****************/ - -void DiffuseFilterResults::set(int t,const PLUFact&FFstarinv,const Vector&vv, - const GeneralMatrix&LL_0,const Vector&aa, - const GeneralMatrix&PPstar,const GeneralMatrix&PPinf, - double ll) - { - FilterResults::set(t,FFstarinv,vv,LL_0,aa,PPstar,ll); - - int tm= t-1; - if(Pinf[tm]) - delete Pinf[tm]; - Pinf[tm]= new GeneralMatrix(PPinf); - - Finf_reg[tm]= false; - Pinf_zero[tm]= false; - } - - /***************** - This returns number of initial diffuse periods (those having - $P_\infty$ non-zero) -*****************/ - -int DiffuseFilterResults::getDiffusePeriods()const - { - int d= maxt; - while(d> 1&&isPinfZero(d)) - d--; - return d; - } - - -; - -SmootherResults::~SmootherResults() - { - for(unsigned int i= 0;i (int)alpha.size()+1, - "Wrong time for SmootherResults::set"); - if(t 1, - "Results not finished in SmootherResults::exportAlpha"); - TS_RAISE_IF(out.numCols()!=(int)alpha.size(), - "Wrong number of columns in SmootherResults::exportAlpha"); - TS_RAISE_IF(alpha[0]->length()!=out.numRows(), - "Wrong number of rows in SmootherResults::exportAlpha"); - for(unsigned int tm= 0;tm 1, - "Results not finished in SmootherResults::exportEta"); - TS_RAISE_IF(out.numCols()!=(int)eta.size(), - "Wrong number of columns in SmootherResults::exportEta"); - TS_RAISE_IF(eta[0]->length()!=out.numRows(), - "Wrong number of rows in SmootherResults::exportEta"); - for(unsigned int tm= 0;tm 1, - "Results not finished in SmootherResults::exportV"); - int m= V[0]->numRows(); - TS_RAISE_IF(out.numCols()!=(int)V.size()*m, - "Wrong number of columns in SmootherResults::exportV"); - TS_RAISE_IF(m!=out.numRows(), - "Wrong number of rows in SmootherResults::exportV"); - for(unsigned int tm= 0;tm* vll)const - { - if(!init.isDiffuse()) - { - FilterResults fres(data.numCols()); - filterNonDiffuse(init.getA(),init.getPstar(),1,fres); - d= 0; - per= fres.getMaxT(); - if(fres.hasFinished()) - return fres.getLogLikelihood(start, vll); - } - else - { - DiffuseFilterResults fres(data.numCols()); - filterDiffuse(init.getA(),init.getPstar(),init.getPinf(), - 1,fres); - d= fres.getDiffusePeriods(); - per= fres.getMaxT(); - if(fres.hasFinished()) - return fres.getLogLikelihood(start, vll); - } - return 0.0; - } - -double -BasicKalmanTask::filter(int&per,int&d, int start, std::vector* vll)const - { - d= 0; - per= vll->size() ; - return filterNonDiffuse(init.getA(),init.getPstar(), start, vll); - } - -/***************** - This is public interface that runs a filter followed by a smoother. - In addition to things returned by |KalmanTask::filter|, it fills also - |SmootherResults|, which must be initialized to the number of columns - of the |data|. -*****************/ -double -KalmanTask::filter_and_smooth(SmootherResults&sres, - int&per,int&d)const - { - if(!init.isDiffuse()) - { - FilterResults fres(data.numCols()); - filterNonDiffuse(init.getA(),init.getPstar(),1,fres); - d= 0; - per= fres.getMaxT(); - if(fres.hasFinished()) - { - smootherNonDiffuse(fres,sres); - return fres.getLogLikelihood(); - } - } - else - { - DiffuseFilterResults fres(data.numCols()); - filterDiffuse(init.getA(),init.getPstar(),init.getPinf(), - 1,fres); - d= fres.getDiffusePeriods(); - per= fres.getMaxT(); - if(fres.hasFinished()) - { - smootherDiffuse(fres,sres); - return fres.getLogLikelihood(); - } - } - return 0.0; - } - - -/***************** - This runs a Basic non-diffuse filter with the given $t$, $a_t$ and - $P_t$. It fills the |FilterResults|. - - First we check that the passed $P_t$ is positive definite by checking - that it has not negative diagonal and is symmetric diagonally - dominant. This is not equivalent to positive definitness but it excludes - ``much'' of indefinite matrices. This check is important since this - routine is called from a diffuse filter and it is possible due to a - wrong guess/calculation of a number of diffuse periods that numerical - instability and roundoff errors make the matrix $P_*$ broken. - - Then we cycle until the end of period and perform a classical Kalman - filter operations. -*****************/ -double -BasicKalmanTask::filterNonDiffuse(const Vector&a,const GeneralMatrix&P, - int start, std::vector* vll)const - { - double loglik=0; - Vector at(a); - GeneralMatrix Pt(P); -// GeneralMatrix PtZeros(Pt.numRows(), Pt.numCols()); -// PtZeros.zeros(); - if(TSUtils::hasNegativeDiagonal(Pt)||!TSUtils::isSymDiagDominant(Pt)) - return 0.0; - const int m=Pt.numRows(); - const int n=Zt.numRows(); - int inc =1; - const int rcols= Rt.numCols(); - GeneralMatrix Ft (Ht.numRows(), Ht.numCols() ); - PLUFact Ftinv(Ht.numRows(), Ht.numCols()); - GeneralMatrix Lt(Tt); - GeneralMatrix PtLttrans(m,m); - GeneralMatrix PtOld(m,m); - GeneralMatrix Mt(m,n); - GeneralMatrix Kt(m,n); - GeneralMatrix KtTransTmp(n,m); // perm space for temp Kt trans - GeneralMatrix QtRttrans(rcols,Rt.numRows()); - - bool isTunit=0;// Tt->isUnit(); - bool isQzero= Qt.isZero(); - bool isRzero= Rt.isZero(); - const double alpha=1.0; - const double neg_alpha=-1.0; - const double omega=0.0; - Vector vt(n); - Vector Finvv(n); - Vector atsave(m);//((const Vector&)at); - int p; - int t= 1; - double vFinvv,ll; - bool nonSteady=true; - for(;t<=data.numCols()&&nonSteady;++t) - { -// ConstVector yt(data,t-1); - - /***************** - This calculates $$v_t = y_t - Z_t*a_t.$$ - *****************/ -// Vector vt(yt); -// vt=yt; - memcpy(vt.base(), &(data.get(0,t-1)), n*sizeof(double)); -// Zt.multsVec(vt,at); - BLAS_dgemv("N", &n, &m, &neg_alpha, Zt.base(), &n, at.base(), - &inc, &alpha, vt.base(), &inc); - - /***************** - This calculates $$F_t = Z_tP_tZ_t^T+H_t.$$ - *****************/ -// GeneralMatrix Mt(Pt,Zt,"trans"); - BLAS_dgemm("N", "T", &m, &n, &m, &alpha, Pt.base(), &m, - Zt.base(), &n, &omega, Mt.base(), &m); - -// GeneralMatrix Ft(Ht); - Ft=Ht; -// Ft.multAndAdd(Zt,ConstGeneralMatrix(Mt)); - // DGEMM: C := alpha*op( A )*op( B ) + beta*C, - BLAS_dgemm("N", "N", &n, &n, &m, &alpha, Zt.base(), &n, - Mt.base(), &m, &alpha, Ft.base(), &n); - -// PLUFact Ftinv(Ft); -// if(!Ftinv.isRegular()) -// return 0.0; - Ftinv=Ft; - /***************** - This calculates $$K_t = T_tP_tZ_t^TF_t^{-1}.$$ - *****************/ -// GeneralMatrix Kt(Tt,Mt); - BLAS_dgemm("N", "N", &m, &n, &m, &alpha, Tt.base(), &m, - Mt.base(), &m, &omega, Kt.base(), &m); -// Ftinv.multInvRight(Kt); - Ftinv.multInvRight(Kt, KtTransTmp); // using perm space for temp KT trans -// Kt.multInvRight(Ft); - - /***************** - This calculates $$L_t = T_t-K_tZ_t.$$ - *****************/ - //GeneralMatrix Lt(Tt); - Lt=Tt; - //Lt.multAndAdd(ConstGeneralMatrix(Kt),Zt,-1.0); - // DGEMM: C := alpha*op( A )*op( B ) + beta*C, - BLAS_dgemm("N", "N", &m, &m, &n, &neg_alpha, Kt.base(), &m, - Zt.base(), &n, &alpha, Lt.base(), &m); - - - /***************** - Here we calc likelihood and store results. - *****************/ - // double ll= calcStepLogLik(Ftinv,vt); - p= Ftinv.numRows(); - Finvv=vt; - Ftinv.multInvLeft(Finvv); - vFinvv= vt.dot(Finvv); - ll=-0.5*(p*log(2*M_PI)+Ftinv.getLogDeterminant()+vFinvv); - - // fres.set(t,Ftinv,vt,Lt,at,Pt,ll); - (*vll)[t-1]=ll; - if (t>start) loglik+=ll; - - if(tstart) loglik+=ll; - - if(tisUnit(t); - bool isQzero= ssf.Q->isZero(t); - bool isRzero= ssf.R->isZero(t); - - /***************** - This calculates $$v_t = y_t - Z_t*a_t.$$ - *****************/ - Vector vt(yt); - Zt.multsVec(vt,at); - - /***************** - This calculates $$F_t = Z_tP_tZ_t^T+H_t.$$ - *****************/ - GeneralMatrix Mt(Pt,Zt,"trans"); - GeneralMatrix Ft(Ht); - Ft.multAndAdd(Zt,ConstGeneralMatrix(Mt)); - - - PLUFact Ftinv(Ft); - if(!Ftinv.isRegular()) - return; - - /***************** - This calculates $$K_t = T_tP_tZ_t^TF_t^{-1}.$$ - *****************/ - GeneralMatrix Kt(Tt,Mt); - Ftinv.multInvRight(Kt); - - /***************** - This calculates $$L_t = T_t-K_tZ_t.$$ - *****************/ - GeneralMatrix Lt(Tt); - Lt.multAndAdd(ConstGeneralMatrix(Kt),Zt,-1.0); - - - /***************** - Here we calc likelihood and store results. - *****************/ - double ll= calcStepLogLik(Ftinv,vt); - fres.set(t,Ftinv,vt,Lt,at,Pt,ll); - - if(too $, then we switch to |KalmanTask::filterNonDiffuse|. - -The switching has two reasons: -The first is that the non-diffuse filter is computationally more efficient -(since it avoids multiplications of zero matrices). The second reason -is much more important. As $P_\infty$ approaches to zero, then -$F_\infty=Z P_\infty Z^T$ approaches to zero and might contain severe -roundoffs. All the operations employing its inverse, $F_\infty^{-1}$, -will commit very bad roundoff errors, and the results will become -unusable. That is why it is important to not only switch to -non-diffuse filter, but also to switch at the right period. - -In theory, the period $d$ of switching is equal to a number of -(univariate) observations for which $F_\infty$ is regular. This is -because the regular $F_\infty=ZP_\infty Z^T$ conveys some information -to $P=P_*+\kappa P_\infty$. However, it is only a theoretical result; -in real floating point world it is difficult to recognize a regular -matrix in this process. Moreover, the $F_\infty$ might be singular and -still convey some information for the diffuse elements since it might -have non-zero rank. - -In this implementation, we use the above idea with the following test -for regularity of $F_\infty$. $F_\infty$ is considered to be regular, -if its PLU factorization yields a condition number estimate less than -$10^10$. During the process it might happen that $P_\infty$ is -indefinite. In this case we correct it by setting its negative -eigenvalues to zero. So $F_\infty=ZP_\infty Z$ is always positive -semidefinite, so no tests for a sign of its determinant are -necessary. Further, the test for $F_\infty=0$ here is equivalent to an -exact match. This can be done since the roundoff errors are believed -to be eliminated during correcting the $P_\infty$ matrix, where not -only negative eigenvalues but also very small positive eigenvalues are -corrected to zeros. In neither case, this is if $F_\infty$ is regular -and still is non-zero, we raise end the filter. This error can be -recognized by |FilterResults::per| less than a number of periods. - -This is just one of many ways, how to implement this non-continuous -algorithm. It is theoretically continuous (since the non-diffuse -periods having $P_\infty$ zero are covered by the branch where -$F_\infty=0$). However, it is computationally discontinuous, since the -calcs depend on when we switch to non-diffuse filter. Because of the -roundoff errors we are uncertain about the switch. An experience shows -that if we switch late, the results can be very bad due to roundoff -errors implied by late switch, if we switch too early, the results -might be wrong since we neglect some uncertainity. - -Main decision point is |ndiff|. Whenever |ndiff<=0|, we consider -$P_\infty$ as zero and carry on as in non-diffuse filter. -*****************/ - -void -KalmanTask::filterDiffuse(const Vector&a,const GeneralMatrix&Pstar, -const GeneralMatrix&Pinf,int first, -DiffuseFilterResults&fres)const - { - Vector at(a); - GeneralMatrix Ptstar(Pstar); - GeneralMatrix Ptinf(Pinf); - int ndiff= init.getNDiff(); - for(int t= first;t<=data.numCols();t++) - { - - /***************** - If $P_\infty$ is exactly zero, then we run the non-diffuse - filter. The $P_\infty$ might become exactly zero by negative or zero - |ndiff|, or by $P\infty$ definitness correction. - *****************/ - if(TSUtils::isZero(Ptinf)) - { - filterNonDiffuse(at,Ptstar,t,fres); - return; - } - - ConstVector yt(data,t-1); - ConstGeneralMatrix Zt(((const TMatrix&)*ssf.Z)[t]); - ConstGeneralMatrix Ht(((const TMatrix&)*ssf.H)[t]); - ConstGeneralMatrix Tt(((const TMatrix&)*ssf.T)[t]); - ConstGeneralMatrix Qt(((const TMatrix&)*ssf.Q)[t]); - ConstGeneralMatrix Rt(((const TMatrix&)*ssf.R)[t]); - bool isTunit= ssf.T->isUnit(t); - bool isQzero= ssf.Q->isZero(t); - bool isRzero= ssf.R->isZero(t); - - /***************** - This calculates $$v_t = y_t - Z_t*a_t.$$ - *****************/ - Vector vt(yt); - Zt.multsVec(vt,at); - - - /***************** - This calculates $$M_{*,t} = P_{*,t}Z_t^T.$$ - *****************/ - GeneralMatrix Mtstar(Ptstar,Zt,"trans"); - - /***************** - This calculates $$F_{*,t} = Z_tP_{*,t}^T+H_t.$$ - *****************/ - GeneralMatrix Ftstar(Ht); - Ftstar.multAndAdd(Zt,ConstGeneralMatrix(Mtstar)); - - /***************** - This calculates $$M_{\infty,t} = P_{\infty,t}Z_t^T.$$ - *****************/ - GeneralMatrix Mtinf(Ptinf,Zt,"trans"); - - /***************** - This calculates $$F_{\infty,t} = Z_tP_{\infty,t}Z_t^T.$$ - *****************/ - GeneralMatrix Ftinf(Zt,ConstGeneralMatrix(Mtinf)); - - - PLUFact Ftinfinv(Ftinf); - if(Ftinfinv.isRegular()&&Ftinfinv.getRcond()> 1.e-10) - { - ndiff-= ssf.p; - - /***************** - We calculate all other matrices, and if we have not come to the end, - also $a_{t+1}$, $P_{*,t+1}$ and $P_{\infty,t+1}$. If |ndiff<=0|, we - set $P_{\infty,t+1}=0$. The matrix can be set to zero even if it is - not positive semidefinite in the code correcting definitness of - $P_\infty$. - *****************/ - - /***************** - This calculates $$F_t^{(2)} = -F_{\infty,t}^{-1}F_{*,t}F_{\infty,t}^{-1}.$$ - *****************/ - GeneralMatrix Ft_2(Ftstar); - Ftinfinv.multInvRight(Ft_2); - Ftinfinv.multInvLeft(Ft_2); - Ft_2.mult(-1.0); - - /***************** - This calculates $$K_t^{(0)} = T_tM_{\infty,t}F_t^{(1)}.$$ - *****************/ - GeneralMatrix Kt_0(Tt,Mtinf); - Ftinfinv.multInvRight(Kt_0); - - /***************** - This calculates $$K_t^{(1)} = T_t(M_{\infty,t}F_t^{(2)}+M_{*,t}F_t^{(1)}).$$ - *****************/ - GeneralMatrix Kt_1(Mtstar); - Ftinfinv.multInvRight(Kt_1); - Kt_1.multAndAdd(Mtinf,Ft_2); - if(!isTunit) - Kt_1.multLeft(Tt); - - /***************** - This calculates $$L_t^{(0)} = T_t-K_t^{(0)}Z_t.$$ - *****************/ - GeneralMatrix Lt_0(Tt); - Lt_0.multAndAdd(ConstGeneralMatrix(Kt_0),Zt,-1.0); - - /***************** - This calculates $$L_t^{(1)} = -K_t^{(1)}Z_t.$$ - *****************/ - GeneralMatrix Lt_1(Kt_1,Zt); - Lt_1.mult(-1.0); - - /***************** - This calculates log likelihood and store results - *****************/ - double ll= -0.5*(ssf.p*log(2*M_PI)+Ftinfinv.getLogDeterminant()); - fres.set(t,Ftinfinv,Ft_2,vt,Lt_0,Lt_1,at,Pstar,Pinf,ll); - - if(tisZero(t); - bool isRzero= ssf.R->isZero(t); - - - /***************** - Calculate $$\eta_t = Q_tR_t^Tr_t.$$ - *****************/ - etat.zeros(); - if(!isQzero&&!isRzero){ - Rt.multVecTrans(0.0,etat,1.0,rt); - Vector etatsav((const Vector&)etat); - Qt.multVec(0.0,etat,1.0,etatsav); - } - - - - /***************** - This calculates $$r_{t-1} = Z^T_tF_t^{-1}v_t + L^T_tr_t.$$ - *****************/ - Vector rtsav((const Vector&)rt); - Lt.multVecTrans(0.0,rt,1.0,rtsav); - Vector Ftinvvt(vt); - Ftinv.multInvLeft(Ftinvvt); - Zt.multVecTrans(1.0,rt,1.0,Ftinvvt); - - - - /***************** - This calculates $$N_{t-1} = Z^T_tF_t^{-1}Z_t+L^T_tN_tL_t.$$ - *****************/ - GeneralMatrix NtLt(Nt,Lt); - Nt.zeros(); - Nt.multAndAdd(Lt,"trans",NtLt); - GeneralMatrix FtinvZt(Zt); - Ftinv.multInvLeft(FtinvZt); - Nt.multAndAdd(Zt,"trans",ConstGeneralMatrix(FtinvZt)); - - - - /***************** - This calculates $$\alpha_t = a_t + P_tr_{t-1}.$$ - *****************/ - alphat= (const Vector&)at; - Pt.multVec(1.0,alphat,1.0,rt); - - - /***************** - This calculates $$V_t = P_t - P_tN_{t-1}P_t.$$ - *****************/ - Vt= (const GeneralMatrix&)Pt; - GeneralMatrix NtPt(Nt,Pt); - Vt.multAndAdd(Pt,NtPt,-1.0); - - } - - /***************** - The non-diffuse smoother just performs a series of - |KalmanTask::smootherNonDiffuseStep|. - *****************/ - void - KalmanTask::smootherNonDiffuse(const FilterResults&fres, - SmootherResults&sres)const - { - Vector rt(ssf.m); - rt.zeros(); - GeneralMatrix Nt(ssf.m,ssf.m); - Nt.zeros(); - for(int t= data.numCols();t>=1;t--) - { - Vector alphat(ssf.m); - GeneralMatrix Vt(ssf.m,ssf.m); - Vector etat(ssf.r); - smootherNonDiffuseStep(t,fres,rt,Nt,alphat,Vt,etat); - sres.set(t,alphat,etat,Vt); - } - } - - /***************** - Here we cycle from $t=T,\ldots, 1$. Whenever $P_\infty$ is zero, we - perform the non-diffuse step. Otherwise we permorn a common code to - diffuse smoothing and then fork according to regularity of $F_\infty$. - *****************/ - void - KalmanTask::smootherDiffuse(const DiffuseFilterResults&fres, - SmootherResults&sres)const - { - Vector rt_0(ssf.m); - Vector rt_1(ssf.m); - GeneralMatrix Nt_0(ssf.m,ssf.m); - GeneralMatrix Nt_1(ssf.m,ssf.m); - GeneralMatrix Nt_2(ssf.m,ssf.m); - rt_0.zeros(); - rt_1.zeros(); - Nt_0.zeros(); - Nt_1.zeros(); - Nt_2.zeros(); - - for(int t= data.numCols();t>=1;t--) - { - Vector alphat(ssf.m); - GeneralMatrix Vt(ssf.m,ssf.m); - Vector etat(ssf.r); - if(fres.isPinfZero(t)) - { - smootherNonDiffuseStep(t,fres,rt_0,Nt_0,alphat,Vt,etat); - } - else - { - const Vector&vt= fres.getV(t); - const GeneralMatrix&Lt_0= fres.getL0(t); - const Vector&at= fres.getA(t); - const GeneralMatrix&Ptstar= fres.getPstar(t); - const GeneralMatrix&Ptinf= fres.getPinf(t); - ConstGeneralMatrix Zt(((const TMatrix&)*ssf.Z)[t]); - ConstGeneralMatrix Qt(((const TMatrix&)*ssf.Q)[t]); - ConstGeneralMatrix Tt(((const TMatrix&)*ssf.T)[t]); - ConstGeneralMatrix Rt(((const TMatrix&)*ssf.R)[t]); - bool isTunit= ssf.T->isUnit(t); - bool isQzero= ssf.Q->isZero(t); - bool isRzero= ssf.R->isZero(t); - - /***************** - Calculate $$\eta_t =Q_tR_tr_t^{(0)}.$$ - *****************/ - etat.zeros(); - if(!isQzero&&!isRzero) - { - Rt.multVecTrans(0.0,etat,1.0,rt_0); - Vector etatsav((const Vector&)etat); - Qt.multVec(0.0,etat,1.0,etatsav); - } - - if(!fres.isFinfRegular(t)) - { - /***************** - We call here |smootherNonDiffuseStep| and calculate $r_{t-1}^{(1)}$, - $N_{t-1}^{(1)}$, $N_{t-1}^{(2)}$ and correct for $\alpha_t$. - *****************/ - smootherNonDiffuseStep(t,fres,rt_0,Nt_0,alphat,Vt,etat); - - /***************** - This calculates $$r_{t-1}^{(1)} = T^T_tr_t^{(1)}.$$ - *****************/ - if(!isTunit) - { - Vector rt_1sav((const Vector&)rt_1); - rt_1.zeros(); - Tt.multVecTrans(0.0,rt_1,1.0,rt_1sav); - } - - /***************** - This corrects $\alpha_t$ after|KalmanTask::smootherNonDiffuseStep|. - This adds $P_{\infty,t}r_{t-1}^{(1)}$ to $\alpha_t$. - *****************/ - Ptinf.multVec(1.0,alphat,1.0,rt_1); - - /***************** - This calculates $$N_{t-1}^{(1)} = T_t^TN_t^{(1)}L_t^{(0)}.$$ - *****************/ - if(!isTunit) - { - GeneralMatrix Nt_1Lt_0(Nt_1,Lt_0); - Nt_1.zeros(); - Nt_1.multAndAdd(Tt,"trans",ConstGeneralMatrix(Nt_1Lt_0)); - } - else - Nt_1.mult(Nt_1,Lt_0); - - /***************** - This calculates $$N_{t-1}^{(2)} = T_t^TN_t^{(2)}T_t.$$ - *****************/ - if(!isTunit) - { - GeneralMatrix Nt_2Tt(Nt_2,Tt); - Nt_2.zeros(); - Nt_2.multAndAdd(Tt,"trans",ConstGeneralMatrix(Nt_2Tt)); - } - - } - else - { - const GeneralMatrix&Lt_1= fres.getL1(t); - const GeneralMatrix&Ft_2= fres.getF2(t); - const PLUFact&Ftinfinv= fres.getFinfInverse(t); - - /***************** - This calculates $$r_{t-1}^{(1)} = Z^T_tF_{\infty,t}^{-1}v_t^{(0)} + - L_t^{(0)T}r_t^{(1)} + L_t^{(1)T}r_t^{(0)}.$$ - *****************/ - Vector rt_1sav((const Vector&)rt_1); - Lt_0.multVecTrans(0.0,rt_1,1.0,rt_1sav); - Lt_1.multVecTrans(1.0,rt_1,1.0,rt_0); - Vector Ftinfinvvt(vt); - Ftinfinv.multInvLeft(Ftinfinvvt); - Zt.multVecTrans(1.0,rt_1,1.0,Ftinfinvvt); - - /***************** - This calculates $$r_{t-1}^{(0)} = L_t^{(0)}r_t^{(0)}.$$ - *****************/ - Vector rt_0sav((const Vector&)rt_0); - Lt_0.multVecTrans(0.0,rt_0,1.0,rt_0sav); - - /***************** - This calculates - $$N_{t-1}^{(2)} = Z_t^TF_t^{(2)}Z_t + L_t^{(0)T}N_t^{(2)}L_t^{(0)}+ - L_t^{(0)T}N_t^{(1)}L_t^{(1)} + L_t^{(1)T}N_t^{(1)}L_t^{(0)} - + L_t^{(1)T}N_t^{(0)}L_t^{(1)}. - $$ - *****************/ - GeneralMatrix Nt_2sav((const GeneralMatrix&)Nt_2); - Nt_2.zeros(); - GeneralMatrix Ft_2Zt(Ft_2,Zt); - Nt_2.multAndAdd(Zt,"trans",ConstGeneralMatrix(Ft_2Zt)); - GeneralMatrix Nt_2Lt_0(Nt_2sav,Lt_0); - Nt_2.multAndAdd(Lt_0,"trans",Nt_2Lt_0); - GeneralMatrix Nt_1Lt_1(Nt_1,Lt_1); - Nt_2.multAndAdd(Lt_0,"trans",Nt_1Lt_1); - GeneralMatrix Nt_1Lt_0(Nt_1,Lt_0); - Nt_2.multAndAdd(Lt_1,"trans",Nt_1Lt_0); - GeneralMatrix Nt_0Lt_1(Nt_0,Lt_1); - Nt_2.multAndAdd(Lt_1,"trans",Nt_0Lt_1); - - /***************** - This calculates - $$N_{t-1}^{(1)} = Z_t^TF_t^{(1)}Z_t + L_t^{(0)T}N_t^{(1)}L_t^{(0)}+ - L_t^{(1)T}N_t^{(0)}L_t^{(0)}.$$ |Nt_1Lt_0| was set in |@|. - *****************/ - Nt_1.zeros(); - GeneralMatrix FtinfinvZt(Zt); - Ftinfinv.multInvLeft(FtinfinvZt); - Nt_1.multAndAdd(Zt,"trans",ConstGeneralMatrix(FtinfinvZt)); - Nt_1.multAndAdd(Lt_0,"trans",Nt_1Lt_0); - GeneralMatrix Nt_0Lt_0(Nt_0,Lt_0); - Nt_1.multAndAdd(Lt_1,"trans",Nt_0Lt_0); - - /***************** - This calculates $$N_{t-1}^{(0)} = L_t^{(0)T}N_t^{(0)}L_t^{(0)}.$$ - |Nt_0Lt_0| was set in |@|. - *****************/ - Nt_0.zeros(); - Nt_0.multAndAdd(Lt_0,"trans",Nt_0Lt_0); - - /***************** - This calculates $$\alpha_t = a_t^{(0)} + P_{*,t}r_{t-1}^{(0)} + P_{\infty,t}r_{t-1}^{(1)}.$$ - for diffuse smoother and regular $F_{\infty,t} - *****************/ - alphat= (const Vector&)at; - Ptstar.multVec(1.0,alphat,1.0,rt_0); - Ptinf.multVec(1.0,alphat,1.0,rt_1); - } - - /***************** - This calculates $$V_t = P_{*,t} - P_{*,t}N_{t-1}^{(0)}P_{*,t} - - P_{\infty,t}N_{t-1}^{(1)}P_{*,t} -(P_{\infty,t}N_{t-1}^{(1)}P_{*,t})^T - - P_{\infty,t}N_{t-1}^{(2)}P_{\infty,t}.$$ - *****************/ - Vt= (const GeneralMatrix&)Ptstar; - GeneralMatrix Nt_0Ptstar(Nt_0,Ptstar); - Vt.multAndAdd(Ptstar,Nt_0Ptstar,-1.0); - GeneralMatrix Nt_2Ptinf(Nt_2,Ptinf); - Vt.multAndAdd(Ptinf,Nt_2Ptinf,-1.0); - GeneralMatrix Nt_1Ptstar(Nt_1,Ptstar); - GeneralMatrix PtinfNt_1Ptstar(Ptinf,Nt_1Ptstar); - Vt.add(-1.0,PtinfNt_1Ptstar); - Vt.add(-1.0,PtinfNt_1Ptstar,"trans"); - - }// end if/else - sres.set(t,alphat,etat,Vt); - }// end for - } - - - /***************** - This evaluates a step loglikelihood as - \log p(y_t\vert Y_{t-1})=-{1\over 2}\left[p\log(2\pi)+\log\vert F_t\vert+ - v_t^TF_t^{-1}v_t\right]$$ - This is a static method. - *****************/ - - double - KalmanTask::calcStepLogLik(const PLUFact&Finv,const Vector&v) - { - int p= Finv.numRows(); - Vector Finvv(v); - Finv.multInvLeft(Finvv); - double vFinvv= v.dot(Finvv); - return-0.5*(p*log(2*M_PI)+Finv.getLogDeterminant()+vFinvv); - } - - - FilterUniResults::~FilterUniResults() - { - for(unsigned int i= 0;i (int)L.size()+1, - "Wrong time for FilterUniResults::set"); - - int tm= t-1; - if(L[tm]) - delete L[tm]; - if(a[tm]) - delete a[tm]; - if(P[tm]) - delete P[tm]; - - if(t> maxt) - maxt= t; - - F[tm]= FF; - v[tm]= vv; - L[tm]= new GeneralMatrix(LL); - a[tm]= new Vector(aa); - P[tm]= new GeneralMatrix(PP); - loglik[tm]= ll; - } - - ; - - double FilterUniResults::getF(int t)const - { - TS_RAISE_IF(t<1||t> maxt, - "Wrong time for FilterUniResults::getF"); - return F[t-1]; - } - - ; - - double FilterUniResults::getV(int t)const - { - TS_RAISE_IF(t<1||t> maxt, - "Wrong time for FilterUniResults::getV"); - return v[t-1]; - } - - ; - - const GeneralMatrix&FilterUniResults::getL(int t)const - { - TS_RAISE_IF(t<1||t> maxt, - "Wrong time for FilterUniResults::getL"); - return*(L[t-1]); - } - - ; - - const Vector&FilterUniResults::getA(int t)const - { - TS_RAISE_IF(t<1||t> maxt, - "Wrong time for FilterUniResults::getA"); - return*(a[t-1]); - } - - ; - - const GeneralMatrix&FilterUniResults::getP(int t)const - { - TS_RAISE_IF(t<1||t> maxt, - "Wrong time for FilterUniResults::getP"); - return*(P[t-1]); - } - - /***************** - This adds all the log likelihoods for all periods. If some periods - in the results have not been set, these are initialized to zeros and - thus this method is pretty safe but may not be if the likelihood tends to - be far lower or higher than 0. - *****************/ - double - FilterUniResults::getLogLikelihood()const - { - double res= 0.0; - for(unsigned int i= 0;i* vloglik)const - { - double res= 0.0; - for(unsigned int i= 0;i* vloglik)const - { - double res= 0.0; - for(unsigned int i= start;i maxt, - "Wrong time for DiffuseFilterUniResults::isFinfRegular"); - return Finf_reg[t-1]; - } - - ; - - bool - DiffuseFilterUniResults::isPinfZero(int t)const - { - TS_RAISE_IF(t<1||t> maxt, - "Wrong time for DiffuseFilterUniResults::isPinfZero"); - return Pinf_zero[t-1]; - } - - ; - - double - DiffuseFilterUniResults::getFinf(int t)const - { - TS_RAISE_IF(t<1||t> maxt, - "Wrong time for DiffuseFilterUniResults::getFinf"); - TS_RAISE_IF(!isFinfRegular(t), - "Finf not regular in the period in DiffuseFilterUniResults::getFinf"); - return getF(t); - } - - ; - - double - DiffuseFilterUniResults::getFstar(int t)const - { - TS_RAISE_IF(t<1||t> maxt, - "Wrong time for DiffuseFilterUniResults::getFstar"); - TS_RAISE_IF(isFinfRegular(t), - "Finf not zero in the period in DiffuseFilterUniResults::getFstar"); - return getF(t); - } - - - ; - - double - DiffuseFilterUniResults::getF2(int t)const - { - TS_RAISE_IF(t<1||t> maxt, - "Wrong time for DiffuseFilterUniResults::getF2"); - TS_RAISE_IF(!isFinfRegular(t), - "Finf not regular in the period in DiffuseFilterUniResults::getF2"); - return F_2[t-1]; - } - - ; - - const - GeneralMatrix&DiffuseFilterUniResults::getL1(int t)const - { - TS_RAISE_IF(t<1||t> maxt, - "Wrong time for FilterUniResults::getL1"); - TS_RAISE_IF(!isFinfRegular(t), - "Finf not regular in the period in DiffuseFilterUniResults::getL1"); - return*(L_1[t-1]); - } - - ; - - const - GeneralMatrix&DiffuseFilterUniResults::getPinf(int t)const - { - TS_RAISE_IF(t<1||t> maxt, - "Wrong time for FilterUniResults::getPinf"); - TS_RAISE_IF(isPinfZero(t), - "Pinf is zero in the period in DiffuseFilterUniResults::getPinf"); - return*(Pinf[t-1]); - } - - ; - - void - DiffuseFilterUniResults::set(int t,double FF,double FF_2, - double vv,const GeneralMatrix&LL_0, - const GeneralMatrix&LL_1,const Vector&aa, - const GeneralMatrix&PPstar,const GeneralMatrix&PPinf, - double ll) - { - FilterUniResults::set(t,FF,vv,LL_0,aa,PPstar,ll); - - int tm= t-1; - if(L_1[tm]) - delete L_1[tm]; - if(Pinf[tm]) - delete Pinf[tm]; - - L_1[tm]= new GeneralMatrix(LL_1); - Pinf[tm]= new GeneralMatrix(PPinf); - F_2[tm]= FF_2; - Finf_reg[tm]= true; - Pinf_zero[tm]= false; - } - - ; - - void - DiffuseFilterUniResults::set(int t,double FFstar,double vv, - const GeneralMatrix&LL_0,const Vector&aa, - const GeneralMatrix&PPstar,const GeneralMatrix&PPinf, - double ll) - { - FilterUniResults::set(t,FFstar,vv,LL_0,aa,PPstar,ll); - - int tm= t-1; - if(Pinf[tm]) - delete Pinf[tm]; - Pinf[tm]= new GeneralMatrix(PPinf); - - Finf_reg[tm]= false; - Pinf_zero[tm]= false; - } - - ; - - int - DiffuseFilterUniResults::getDiffusePeriods()const - { - int d= maxt; - while(d> 1&&isPinfZero(d)) - d--; - return d; - } - - /***************** - @ This converts a multivariate |KalmanTask| to univariate - |KalmanUniTask|. It unfolds time dimension so that at each time only - one univariate observation comes. The measurment equation is - transformed so that the measurment errors would not be correlated. - *****************/ - KalmanUniTask::KalmanUniTask(const KalmanTask&kt) - :me(kt.data,*(kt.ssf.Z),*(kt.ssf.H)), - ssf(TMatrixCycle(*(me.Z),"rows"),TScalarCycle(*(me.H)), - TMatrixPadUnit(*(kt.ssf.T),kt.data.numRows()), - TMatrixPadZero(*(kt.ssf.R),kt.data.numRows()), - TMatrixPadZero(*(kt.ssf.Q),kt.data.numRows())), - data(me.y.base(),1,me.y.numRows()*me.y.numCols()), - init(kt.init) - { - } - - - double - KalmanUniTask::filter(int&per,int&d)const - { - if(!init.isDiffuse()) - { - FilterUniResults fres(data.numCols()); - filterNonDiffuse(init.getA(),init.getPstar(),1,fres); - d= 0; - per= fres.getMaxT(); - if(fres.hasFinished()) - return fres.getLogLikelihood(); - } - else - { - DiffuseFilterUniResults fres(data.numCols()); - filterDiffuse(init.getA(),init.getPstar(),init.getPinf(), - 1,fres); - d= fres.getDiffusePeriods(); - per= fres.getMaxT(); - if(fres.hasFinished()) - return fres.getLogLikelihood(); - } - return 0.0; - } - - double - KalmanUniTask::filter(int&per,int&d, int start, std::vector* vll)const - { - if(!init.isDiffuse()) - { - FilterUniResults fres(data.numCols()); - filterNonDiffuse(init.getA(),init.getPstar(),1,fres); - d= 0; - per= fres.getMaxT(); - if(fres.hasFinished()) - return fres.getLogLikelihood(start,vll); - } - else - { - DiffuseFilterUniResults fres(data.numCols()); - filterDiffuse(init.getA(),init.getPstar(),init.getPinf(), - 1,fres); - d= fres.getDiffusePeriods(); - per= fres.getMaxT(); - if(fres.hasFinished()) - return fres.getLogLikelihood(start,vll); - } - return 0.0; - } - - double - KalmanUniTask::filter_and_smooth(SmootherResults&sres, - int&per,int&d)const - { - if(!init.isDiffuse()) - { - FilterUniResults fres(data.numCols()); - filterNonDiffuse(init.getA(),init.getPstar(),1,fres); - d= 0; - per= fres.getMaxT(); - if(fres.hasFinished()) - { - smootherNonDiffuse(fres,sres); - return fres.getLogLikelihood(); - } - } - else - { - DiffuseFilterUniResults fres(data.numCols()); - filterDiffuse(init.getA(),init.getPstar(),init.getPinf(), - 1,fres); - d= fres.getDiffusePeriods(); - per= fres.getMaxT(); - if(fres.hasFinished()) - { - smootherDiffuse(fres,sres); - return fres.getLogLikelihood(); - } - } - return 0.0; - } - - /***************** - This filters univariate data starting at given $t$, $a_t$ and - $P_t$. If at some period $F_t\leq 0$, than we end and the filter - results are not finished. - *****************/ - void - KalmanUniTask::filterNonDiffuse(const Vector&a,const GeneralMatrix&P, - int first,FilterUniResults&fres)const - { - Vector at(a); - GeneralMatrix Pt(P); - for(int t= first;t<=data.numCols();t++){ - double yt= data.get(0,t-1); - ConstGeneralMatrix Zt(((const TMatrix&)*ssf.Z)[t]); - double Ht= ((const TScalar&)*ssf.H)[t]; - ConstGeneralMatrix Tt(((const TMatrix&)*ssf.T)[t]); - ConstGeneralMatrix Qt(((const TMatrix&)*ssf.Q)[t]); - ConstGeneralMatrix Rt(((const TMatrix&)*ssf.R)[t]); - bool isTunit= ssf.T->isUnit(t); - bool isQzero= ssf.Q->isZero(t); - bool isRzero= ssf.R->isZero(t); - - - double vt= at.dot(Zt.getData()); - vt= yt-vt; - - Vector Mt(ssf.m); - Mt.zeros(); - Pt.multVec(0.0,Mt,1.0,Zt.getData()); - double Ft= Mt.dot(Zt.getData()); - Ft+= Ht; - - if(Ft<=0.0) - return; - - Vector Kt(ssf.m); - Kt.zeros(); - if(isTunit) - Kt.add(1.0/Ft,Mt); - else - Tt.multVec(0.0,Kt,1.0/Ft,Mt); - - GeneralMatrix Lt(Tt); - Lt.multAndAdd(ConstGeneralMatrix(Kt.base(),ssf.m,1),Zt,-1.0); - - double ll= calcStepLogLik(Ft,vt); - fres.set(t,Ft,vt,Lt,at,Pt,ll); - - if(tisUnit(t); - bool isQzero= ssf.Q->isZero(t); - bool isRzero= ssf.R->isZero(t); - - double vt= at.dot(Zt.getData()); - vt= yt-vt; - - Vector Mtstar(ssf.m); - Mtstar.zeros(); - Ptstar.multVec(0.0,Mtstar,1.0,Zt.getData()); - - double Ftstar= Mtstar.dot(Zt.getData()); - Ftstar+= Ht; - - Vector Mtinf(ssf.m); - Mtinf.zeros(); - Ptinf.multVec(0.0,Mtinf,1.0,Zt.getData()); - - double Ftinf= Mtinf.dot(Zt.getData()); - if(Ftinf<2*DBL_EPSILON) - Ftinf= 0.0; - - if(Ftinf> 0.0) - { - ndiff--; - - double Ft_2= -Ftstar/Ftinf/Ftinf; - - Vector Kt_0(ssf.m); - Kt_0.zeros(); - if(!isTunit) - Tt.multVec(0.0,Kt_0,1.0/Ftinf,Mtinf); - else - Kt_0.add(1.0/Ftinf,Mtinf); - - Vector Kt_1tmp(ssf.m); - Kt_1tmp.zeros(); - Kt_1tmp.add(Ft_2,Mtinf); - Kt_1tmp.add(1.0/Ftinf,Mtstar); - Vector Kt_1(ssf.m); - if(!isTunit) - { - Kt_1.zeros(); - Tt.multVec(0.0,Kt_1,1.0,Kt_1tmp); - } - else - { - Kt_1= (const Vector&)Kt_1tmp; - } - - GeneralMatrix Lt_0(Tt); - Lt_0.multAndAdd(ConstGeneralMatrix(Kt_0.base(),ssf.m,1),Zt,-1.0); - - - GeneralMatrix Lt_1(ConstGeneralMatrix(Kt_1.base(),ssf.m,1),Zt); - Lt_1.mult(-1.0); - - - double ll= -0.5*(log(2*M_PI)+log(Ftinf)); - fres.set(t,Ftinf,Ft_2,vt,Lt_0,Lt_1,at,Pstar,Pinf,ll); - - if(tisZero(t); - bool isRzero= ssf.R->isZero(t); - - - - etat.zeros(); - if(!isQzero&&!isRzero) - { - Rt.multVecTrans(0.0,etat,1.0,rt); - Vector etatsav((const Vector&)etat); - Qt.multVec(0.0,etat,1.0,etatsav); - } - - Vector rtsav((const Vector&)rt); - Lt.multVecTrans(0.0,rt,1.0,rtsav); - rt.add(vt/Ft,Zt.getData()); - - GeneralMatrix NtLt(Nt,Lt); - Nt.zeros(); - Nt.multAndAdd(Lt,"trans",NtLt); - Nt.multAndAdd(Zt,"trans",Zt,1.0/Ft); - - alphat= (const Vector&)at; - Pt.multVec(1.0,alphat,1.0,rt); - - Vt= (const GeneralMatrix&)Pt; - GeneralMatrix NtPt(Nt,Pt); - Vt.multAndAdd(Pt,NtPt,-1.0); - } - - - void - KalmanUniTask::smootherNonDiffuse(const FilterUniResults&fres, - SmootherResults&sres)const - { - Vector rt(ssf.m); - rt.zeros(); - GeneralMatrix Nt(ssf.m,ssf.m); - Nt.zeros(); - for(int t= data.numCols();t>=1;t--){ - Vector alphat(ssf.m); - GeneralMatrix Vt(ssf.m,ssf.m); - Vector etat(ssf.r); - smootherNonDiffuseStep(t,fres,rt,Nt,alphat,Vt,etat); - sres.set(t,alphat,etat,Vt); - } - } - - void - KalmanUniTask::smootherDiffuse(const DiffuseFilterUniResults&fres, - SmootherResults&sres)const - { - - Vector rt_0(ssf.m); - Vector rt_1(ssf.m); - GeneralMatrix Nt_0(ssf.m,ssf.m); - GeneralMatrix Nt_1(ssf.m,ssf.m); - GeneralMatrix Nt_2(ssf.m,ssf.m); - rt_0.zeros(); - rt_1.zeros(); - Nt_0.zeros(); - Nt_1.zeros(); - Nt_2.zeros(); - - - for(int t= data.numCols();t>=1;t--) - { - Vector alphat(ssf.m); - GeneralMatrix Vt(ssf.m,ssf.m); - Vector etat(ssf.r); - if(fres.isPinfZero(t)) - { - smootherNonDiffuseStep(t,fres,rt_0,Nt_0,alphat,Vt,etat); - } - else - { - double vt= fres.getV(t); - const GeneralMatrix&Lt_0= fres.getL0(t); - const Vector&at= fres.getA(t); - const GeneralMatrix&Ptstar= fres.getPstar(t); - const GeneralMatrix&Ptinf= fres.getPinf(t); - ConstGeneralMatrix Zt(((const TMatrix&)*ssf.Z)[t]); - ConstGeneralMatrix Qt(((const TMatrix&)*ssf.Q)[t]); - ConstGeneralMatrix Tt(((const TMatrix&)*ssf.T)[t]); - ConstGeneralMatrix Rt(((const TMatrix&)*ssf.R)[t]); - bool isTunit= ssf.T->isUnit(t); - bool isQzero= ssf.Q->isZero(t); - bool isRzero= ssf.R->isZero(t); - - etat.zeros(); - if(!isQzero&&!isRzero) - { - Rt.multVecTrans(0.0,etat,1.0,rt_0); - Vector etatsav((const Vector&)etat); - Qt.multVec(0.0,etat,1.0,etatsav); - } - - if(!fres.isFinfRegular(t)) - { - smootherNonDiffuseStep(t,fres,rt_0,Nt_0,alphat,Vt,etat); - if(!isTunit) - { - Vector rt_1sav((const Vector&)rt_1); - rt_1.zeros(); - Tt.multVecTrans(0.0,rt_1,1.0,rt_1sav); - } - - Ptinf.multVec(1.0,alphat,1.0,rt_1); - - if(!isTunit) - { - GeneralMatrix Nt_1Lt_0(Nt_1,Lt_0); - Nt_1.zeros(); - Nt_1.multAndAdd(Tt,"trans",ConstGeneralMatrix(Nt_1Lt_0)); - } - else - Nt_1.mult(Nt_1,Lt_0); - - - if(!isTunit) - { - GeneralMatrix Nt_2Tt(Nt_2,Tt); - Nt_2.zeros(); - Nt_2.multAndAdd(Tt,"trans",ConstGeneralMatrix(Nt_2Tt)); - } - - } - else - { - const GeneralMatrix&Lt_1= fres.getL1(t); - double Ft_2= fres.getF2(t); - double Ftinf= fres.getFinf(t); - - - Vector rt_1sav((const Vector&)rt_1); - Lt_0.multVecTrans(0.0,rt_1,1.0,rt_1sav); - Lt_1.multVecTrans(1.0,rt_1,1.0,rt_0); - rt_1.add(vt/Ftinf,Zt.getData()); - - - Vector rt_0sav((const Vector&)rt_0); - Lt_0.multVecTrans(0.0,rt_0,1.0,rt_0sav); - - GeneralMatrix Nt_2sav((const GeneralMatrix&)Nt_2); - Nt_2.zeros(); - Nt_2.multAndAdd(Zt,"trans",Zt,Ft_2); - GeneralMatrix Nt_2Lt_0(Nt_2sav,Lt_0); - Nt_2.multAndAdd(Lt_0,"trans",Nt_2Lt_0); - GeneralMatrix Nt_1Lt_1(Nt_1,Lt_1); - Nt_2.multAndAdd(Lt_0,"trans",Nt_1Lt_1); - GeneralMatrix Nt_1Lt_0(Nt_1,Lt_0); - Nt_2.multAndAdd(Lt_1,"trans",Nt_1Lt_0); - GeneralMatrix Nt_0Lt_1(Nt_0,Lt_1); - Nt_2.multAndAdd(Lt_1,"trans",Nt_0Lt_1); - - - - Nt_1.zeros(); - Nt_1.multAndAdd(Zt,"trans",Zt,1.0/Ftinf); - Nt_1.multAndAdd(Lt_0,"trans",Nt_1Lt_0); - GeneralMatrix Nt_0Lt_0(Nt_0,Lt_0); - Nt_1.multAndAdd(Lt_1,"trans",Nt_0Lt_0); - - Nt_0.zeros(); - Nt_0.multAndAdd(Lt_0,"trans",Nt_0Lt_0); - - alphat= (const Vector&)at; - Ptstar.multVec(1.0,alphat,1.0,rt_0); - Ptinf.multVec(1.0,alphat,1.0,rt_1); - } - - Vt= (const GeneralMatrix&)Ptstar; - GeneralMatrix Nt_0Ptstar(Nt_0,Ptstar); - Vt.multAndAdd(Ptstar,Nt_0Ptstar,-1.0); - GeneralMatrix Nt_2Ptinf(Nt_2,Ptinf); - Vt.multAndAdd(Ptinf,Nt_2Ptinf,-1.0); - GeneralMatrix Nt_1Ptstar(Nt_1,Ptstar); - GeneralMatrix PtinfNt_1Ptstar(Ptinf,Nt_1Ptstar); - Vt.add(-1.0,PtinfNt_1Ptstar); - Vt.add(-1.0,PtinfNt_1Ptstar,"trans"); - - - } - sres.set(t,alphat,etat,Vt); - } - } - - - double - KalmanUniTask::calcStepLogLik(double F,double v) - { - return-0.5*(log(2*M_PI)+log(F)+v*v/F); - } - - - diff --git a/mex/sources/kalman/cc/kalman.h b/mex/sources/kalman/cc/kalman.h deleted file mode 100644 index c9ee00e62..000000000 --- a/mex/sources/kalman/cc/kalman.h +++ /dev/null @@ -1,339 +0,0 @@ -/* -* Copyright (C) 2008-2009 Dynare Team -* -* This file is part of Dynare. -* -* Dynare is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* Dynare is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with Dynare. If not, see . -*/ - -/* derived from c++kalman_filter library by O. Kamenik */ - -/************* -This file defines two most important classes: |KalmanTask| and -|KalmanUniTask|. Both define a linear filtering and smoothing problem; -one for multivariate observations, the other for univariate -observations. The Kalman task consists of three things: the state -space, observed data, and initialization. - -In addition to these two classes, we have also an abstraction for -filter results, for diffuse filter results and smoother results. These -are only containers of series of matrices and vectors. The filter -results are used as an input for smoothing. - -The important convention is that whenever a parameter $t$ stands for a -time period (which is always), it starts from 1 (not zero as in -C/C++). - -In this file, we use the same naming scheme as in Durbin \& Koopman. -*************/ - -#ifndef KALMAN_H -#define KALMAN_H - -#include "state_init.h" -#include "ssf.h" -#include "ssf_uni.h" -#include "utils.h" - -#include - -/************* -This is an output of the filtering and input for the smoothing. This -class is inherited by |DiffuseFilterResults| which enriches the -|FilterResults| with additional information coming from the diffuse -periods. - -The object is initialized for a given number of periods, and maintains -a number of periods for which the filter results were set. If this -number, |maxt|, is less than the number of overall periods, it means -that the filter has not finished. -**************/ -class FilterResults{ - protected: - std::vector Finv; - std::vector v; - std::vector L; - std::vector a; - std::vector P; - std::vector loglik; - int maxt; - public: - FilterResults(int num) - :Finv(num,(PLUFact*)0),v(num,(Vector*)0),L(num,(GeneralMatrix*)0), - a(num,(Vector*)0),P(num,(GeneralMatrix*)0),loglik(num,0.0),maxt(0) - {} - virtual~FilterResults(); - void set(int t,const PLUFact&FFinv,const Vector&vv, - const GeneralMatrix&LL,const Vector&aa, - const GeneralMatrix&P,double ll); - int getMaxT()const - {return maxt;} - bool hasFinished()const - {return maxt==(int)Finv.size();} - const PLUFact&getFInverse(int t)const; - const Vector&getV(int t)const; - const GeneralMatrix&getL(int t)const; - const Vector&getA(int t)const; - const GeneralMatrix&getP(int)const; - double getLogLikelihood()const; - double getLogLikelihood(int start)const; - double getLogLikelihood(std::vector *vloglik)const; - double getLogLikelihood(int start,std::vector *vloglik)const; - }; - - -class DiffuseFilterResults:public FilterResults{ - protected: - std::vector L_1; - std::vector Pinf; - std::vector F_2; - std::vector Finf_reg; - std::vector Pinf_zero; - public: - DiffuseFilterResults(int num) - :FilterResults(num), - L_1(num,(GeneralMatrix*)0), - Pinf(num,(GeneralMatrix*)0), - F_2(num,(GeneralMatrix*)0), - Finf_reg(num,true), - Pinf_zero(num,true) - {} - virtual~DiffuseFilterResults(); - void set(int t,const PLUFact&FFinfinv,const GeneralMatrix&FF_2, - const Vector&vv,const GeneralMatrix&LL_0, - const GeneralMatrix&LL_1,const Vector&aa, - const GeneralMatrix&PPstar,const GeneralMatrix&PPinf, - double ll); - void set(int t,const PLUFact&FFstarinv,const Vector&vv, - const GeneralMatrix&LL_0,const Vector&aa, - const GeneralMatrix&PPstar,const GeneralMatrix&PPinf, - double ll); - int getDiffusePeriods()const; - bool isFinfRegular(int t)const; - bool isPinfZero(int t)const; - const PLUFact&getFinfInverse(int t)const; - const PLUFact&getFstarInverse(int t)const; - const GeneralMatrix&getF2(int t)const; - const GeneralMatrix&getL0(int t)const - {return getL(t);} - const GeneralMatrix&getL1(int t)const; - const GeneralMatrix&getPstar(int t)const - {return getP(t);} - const GeneralMatrix&getPinf(int t)const; - }; - -class SmootherResults{ - protected: - std::vector alpha; - std::vector eta; - std::vector V; - int mint; - public: - SmootherResults(int num) - :alpha(num,(Vector*)0),eta(num,(Vector*)0), - V(num,(GeneralMatrix*)0), - mint(num+1){} - virtual~SmootherResults(); - void set(int t,const Vector&aalpha,const Vector&eeta, - const GeneralMatrix&VV); - void import(const SmootherResults&sres,int period); - void exportAlpha(GeneralMatrix&out)const; - void exportEta(GeneralMatrix&out)const; - void exportV(GeneralMatrix&out)const; - }; - - -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; - const double riccatiTol; - public: - BasicKalmanTask(const GeneralMatrix&d,const GeneralMatrix&ZZ, - const GeneralMatrix&HH,const GeneralMatrix&TT, - const GeneralMatrix&RR,const GeneralMatrix&QQ, - const StateInit&init_state, const double riccatiTol); -// 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, const double riccatiTol); - virtual ~BasicKalmanTask(); -// double filter(int&per,int&d)const; -// double filter(int&per,int&d, int start, std::vector* vll)const; - double filter(int&per,int&d,int start, std::vector* 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* 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; - SSForm ssf; - ConstGeneralMatrix data; - const StateInit&init; - public: - KalmanTask(const GeneralMatrix&d,const GeneralMatrix&Z, - const GeneralMatrix&H,const GeneralMatrix&T, - const GeneralMatrix&R,const GeneralMatrix&Q, - const StateInit&init_state); - KalmanTask(const GeneralMatrix&d,const TMatrix&Z, - const TMatrix&H,const TMatrix&T, - const TMatrix&R,const TMatrix&Q, - const StateInit&init_state); - double filter(int&per,int&d)const; - double filter(int&per,int&d, int start, std::vector* vll)const; - double filter_and_smooth(SmootherResults&sres,int&per,int&d)const; - protected: - void filterNonDiffuse(const Vector&a,const GeneralMatrix&Pstar, - 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 FilterUniResults{ - protected: - std::vector F; - std::vector v; - std::vector L; - std::vector a; - std::vector P; - std::vector loglik; - int maxt; - public: - FilterUniResults(int num) - :F(num,0.0),v(num,0.0),L(num,(GeneralMatrix*)0), - a(num,(Vector*)0),P(num,(GeneralMatrix*)0),loglik(num,0.0),maxt(0) - {} - virtual~FilterUniResults(); - void set(int t,double F,double vv, - const GeneralMatrix&LL,const Vector&aa, - const GeneralMatrix&P,double ll); - int getMaxT()const - {return maxt;} - bool hasFinished()const - {return maxt==(int)F.size();} - double getF(int t)const; - double getV(int t)const; - const GeneralMatrix&getL(int t)const; - const Vector&getA(int t)const; - const GeneralMatrix&getP(int)const; - double getLogLikelihood()const; - double getLogLikelihood(int start)const; - double getLogLikelihood(std::vector* vloglik)const; - double getLogLikelihood(int start,std::vector* vloglik)const; - }; - - -class DiffuseFilterUniResults:public FilterUniResults{ - protected: - std::vector L_1; - std::vector Pinf; - std::vector F_2; - std::vector Finf_reg; - std::vector Pinf_zero; - public: - DiffuseFilterUniResults(int num) - :FilterUniResults(num), - L_1(num,(GeneralMatrix*)0), - Pinf(num,(GeneralMatrix*)0), - F_2(num,0.0), - Finf_reg(num,true), - Pinf_zero(num,true) - {} - virtual~DiffuseFilterUniResults(); - void set(int t,double FFinf,double FF_2, - double vv,const GeneralMatrix&LL_0, - const GeneralMatrix&LL_1,const Vector&aa, - const GeneralMatrix&PPstar,const GeneralMatrix&PPinf, - double ll); - void set(int t,double FFstar,double vv, - const GeneralMatrix&LL_0,const Vector&aa, - const GeneralMatrix&PPstar,const GeneralMatrix&PPinf, - double ll); - int getDiffusePeriods()const; - bool isFinfRegular(int t)const; - bool isPinfZero(int t)const; - double getFinf(int t)const; - double getFstar(int t)const; - double getF2(int t)const; - const GeneralMatrix&getL0(int t)const - {return getL(t);} - const GeneralMatrix&getL1(int t)const; - const GeneralMatrix&getPstar(int t)const - {return getP(t);} - const GeneralMatrix&getPinf(int t)const; - }; - - -class KalmanUniTask{ - private: - MesEquation me; - protected: - SSFormUni ssf; - ConstGeneralMatrix data; - const StateInit&init; - public: - KalmanUniTask(const KalmanTask&kt); - double filter(int&per,int&d)const; - double filter(int&per,int&d, int start, std::vector* vll)const; - double filter_and_smooth(SmootherResults&sres,int&per,int&d)const; - protected: - void filterNonDiffuse(const Vector&a,const GeneralMatrix&Pstar, - int first,FilterUniResults&fres)const; - void filterDiffuse(const Vector&a,const GeneralMatrix&Pstar, - const GeneralMatrix&Pinf,int first, - DiffuseFilterUniResults&fres)const; - void smootherNonDiffuse(const FilterUniResults&fres,SmootherResults&sres)const; - void smootherDiffuse(const DiffuseFilterUniResults&fres,SmootherResults&sres)const; - void smootherNonDiffuseStep(int t,const FilterUniResults&fres, - Vector&rt,GeneralMatrix&Nt, - Vector&alphat,GeneralMatrix&Vt, - Vector&etat)const; - static double calcStepLogLik(double F,double v); - }; - - -#endif - diff --git a/mex/sources/kalman/cc/ssf.cpp b/mex/sources/kalman/cc/ssf.cpp deleted file mode 100644 index 44e69d88a..000000000 --- a/mex/sources/kalman/cc/ssf.cpp +++ /dev/null @@ -1,323 +0,0 @@ -/* -* Copyright (C) 2008-2009 Dynare Team -* -* This file is part of Dynare. -* -* Dynare is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* Dynare is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with Dynare. If not, see . -*/ - -/* derived from c++kalman_filter library by O. Kamenik */ - -#include "ssf.h" -#include "ts_exception.h" -#include "utils.h" - -#include - - -TMatrixCycle::TMatrixCycle(int n,int nr,int nc) -:matrices(new GeneralMatrix*[n]),num(n),nrows(nr),ncols(nc) - { - for(int i= 0;inumRows()!=m||T->numCols()!=m|| - H->numRows()!=p||H->numCols()!=p|| - R->numRows()!=m||R->numCols()!=r|| - Q->numCols()!=r, - "Wrong TMatrix dimension in SSForm constructor"); - } - - -SSForm::SSForm(const GeneralMatrix&zz,const GeneralMatrix&hh, - const GeneralMatrix&tt,const GeneralMatrix&rr, - const GeneralMatrix&qq) - :Z(new TMatrixInvariant(zz)), - H(new TMatrixInvariant(hh)), - T(new TMatrixInvariant(tt)), - R(new TMatrixInvariant(rr)), - Q(new TMatrixInvariant(qq)), - p(zz.numRows()),m(zz.numCols()),r(qq.numRows()) - { - TS_RAISE_IF(T->numRows()!=m||T->numCols()!=m|| - H->numRows()!=p||H->numCols()!=p|| - R->numRows()!=m||R->numCols()!=r|| - Q->numCols()!=r, - "Wrong TMatrix dimension in SSForm constructor"); - } - - -SSForm::SSForm(const SSForm&f) -:Z(f.Z->clone()), -H(f.H->clone()), -T(f.T->clone()), -R(f.R->clone()), -Q(f.Q->clone()), -p(f.p),m(f.m),r(f.r) - {} - - - -SSForm::~SSForm() - { - delete Z; - delete H; - delete T; - delete R; - delete Q; - } - - -MesEquation::MesEquation(const GeneralMatrix&data,const TMatrix&zz, - const TMatrix&hh) - :y(data), - Z((zz.period()*hh.period()==1)?(TMatrix*)new TMatrixInvariant(zz[1]): - (zz.period()*hh.period()==0)?(TMatrix*)new TMatrixCycle(y.numCols(), - zz.numRows(),zz.numCols()) - :(TMatrix*)new TMatrixCycle(zz.period()*hh.period(),zz.numRows(),zz.numCols())), - H((zz.period()*hh.period()==1)?(TMatrix*)new TMatrixInvariant(hh[1]): - (zz.period()*hh.period()==0)?(TMatrix*)new TMatrixCycle(y.numCols(), - hh.numRows(),hh.numCols()) - :(TMatrix*)new TMatrixCycle(zz.period()*hh.period(),hh.numRows(),hh.numCols())) - { - TS_RAISE_IF(y.numRows()!=Z->numRows()||y.numRows()!=H->numRows()|| - y.numRows()!=H->numCols(), - "Incompatible dimension in MesEquation constructor"); - - int mper= zz.period()*hh.period(); - if(mper==1) - { - construct_invariant(); - } - else - { - std::vector chols; - int per= (mper==0)?y.numCols():mper; - for(int t= 1;t<=per;t++) - { - GeneralMatrix ycol(y,0,t-1,y.numRows(),1); - int hi= t; - if(hh.period()> 0) - hi= (t-1)%hh.period()+1; - NormCholesky*ch; - if(hh.period()==0) - { - ch= new NormCholesky(hh[t]); - } - else if(hi-1>=(int)chols.size()) - { - ch= new NormCholesky(hh[t]); - chols.push_back(ch); - } - else - { - ch= chols[hi-1]; - } - ch->getL().multInvLeftUnit(ycol); - if(t-1getL().multInvLeftUnit(Zt); - ((TMatrixCycle*)Z)->set(t,Zt); - GeneralMatrix Ht(hh.numRows(),hh.numRows()); - Ht.zeros(); - for(int i= 0;igetD()[i]; - ((TMatrixCycle*)H)->set(t,Ht); - } - if(hh.period()==0) - delete ch; - } - for(unsigned int i= 0;inumRows()||y.numRows()!=H->numRows()|| - y.numRows()!=H->numCols(), - "Incompatible dimension in MesEquation constructor"); - - construct_invariant(); - } - - -MesEquation::~MesEquation() - { - delete Z; - delete H; - } - -void MesEquation::construct_invariant() - { - if(!TSUtils::isDiagonal((*H)[1])) - { - NormCholesky chol((*H)[1]); - chol.getL().multInvLeftUnit(y); - chol.getL().multInvLeftUnit((*Z)[1]); - (*H)[1].zeros(); - for(int i= 0;inumRows();i++) - (*H)[1].get(i,i)= chol.getD()[i]; - } - } - -; - diff --git a/mex/sources/kalman/cc/ssf.h b/mex/sources/kalman/cc/ssf.h deleted file mode 100644 index 30dfb2d43..000000000 --- a/mex/sources/kalman/cc/ssf.h +++ /dev/null @@ -1,193 +0,0 @@ -/* -* Copyright (C) 2008-2009 Dynare Team -* -* This file is part of Dynare. -* -* Dynare is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* Dynare is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with Dynare. If not, see . -*/ - -/* derived from c++kalman_filter library by O. Kamenik */ - -#ifndef SSF_H -#define SSF_H - -#include "GeneralMatrix.h" - - -class TMatrix{ - public: - virtual const GeneralMatrix&operator[](int t)const= 0; - virtual GeneralMatrix&operator[](int t)= 0; - virtual int numRows()const= 0; - virtual int numCols()const= 0; - virtual int period()const= 0; - virtual bool isZero(int t)const= 0; - virtual bool isUnit(int t)const= 0; - virtual~TMatrix(){} - virtual TMatrix*clone()const= 0; - }; - - -class TMatrixInvariant:public TMatrix,public GeneralMatrix{ - public: - TMatrixInvariant(const GeneralMatrix&m) - :GeneralMatrix(m){} - TMatrixInvariant(const TMatrixInvariant&m) - :GeneralMatrix(m){} - const GeneralMatrix&operator[](int t)const - {return*this;} - GeneralMatrix&operator[](int t) - {return*this;} - int numRows()const - {return GeneralMatrix::numRows();} - int numCols()const - {return GeneralMatrix::numCols();} - int period()const - {return 1;} - bool isZero(int t)const - {return false;} - bool isUnit(int t)const - {return false;} - TMatrix*clone()const - {return new TMatrixInvariant(*this);} - }; - - - -class TMatrixCycle:public TMatrix{ - protected: - GeneralMatrix**const matrices; - int num; - int nrows; - int ncols; - public: - TMatrixCycle(int n,int nr,int nc); - TMatrixCycle(const TMatrixCycle&m); - - TMatrixCycle(const GeneralMatrix&m); - - TMatrixCycle(const TMatrix&m,const char*dummy); - ~TMatrixCycle(); - const GeneralMatrix&operator[](int t)const; - GeneralMatrix&operator[](int t); - int numRows()const - {return nrows;} - int numCols()const - {return ncols;} - int period()const - {return num;} - bool isZero(int t)const - {return false;} - bool isUnit(int t)const - {return false;} - TMatrix*clone()const - {return new TMatrixCycle(*this);} - void set(int t,const GeneralMatrix&m); - }; - - -class TMatrixPadUnit:public TMatrix{ - TMatrix*const tmat; - int skip; - GeneralMatrix unit; - public: - TMatrixPadUnit(const TMatrix&m,int s); - TMatrixPadUnit(const TMatrixPadUnit&m) - :tmat(m.tmat->clone()),skip(m.skip),unit(m.unit){} - ~TMatrixPadUnit() - {delete tmat;} - const GeneralMatrix&operator[](int t)const; - GeneralMatrix&operator[](int t); - int numRows()const - {return tmat->numRows();} - int numCols()const - {return tmat->numCols();} - int period()const - {return skip*tmat->period();} - bool isZero(int t)const - {return false;} - bool isUnit(int t)const - {return(t/skip)*skip!=t;} - TMatrix*clone()const - {return new TMatrixPadUnit(*this);} - }; - - -class TMatrixPadZero:public TMatrix{ - TMatrix*const tmat; - int skip; - GeneralMatrix zero; - public: - TMatrixPadZero(const TMatrix&m,int s); - TMatrixPadZero(const TMatrixPadZero&m) - :tmat(m.tmat->clone()),skip(m.skip),zero(m.zero){} - ~TMatrixPadZero() - {delete tmat;} - const GeneralMatrix&operator[](int t)const; - GeneralMatrix&operator[](int t); - int numRows()const - {return tmat->numRows();} - int numCols()const - {return tmat->numCols();} - int period()const - {return skip*tmat->period();} - bool isUnit(int t)const - {return false;} - bool isZero(int t)const - {return(t/skip)*skip!=t;} - TMatrix*clone()const - {return new TMatrixPadZero(*this);} - }; - - - -struct SSForm{ - TMatrix*const Z; - TMatrix*const H; - TMatrix*const T; - TMatrix*const R; - TMatrix*const Q; - const int p; - const int m; - const int r; - - SSForm(const TMatrix&zz,const TMatrix&hh,const TMatrix&tt, - const TMatrix&rr,const TMatrix&qq); - SSForm(const GeneralMatrix&zz,const GeneralMatrix&hh, - const GeneralMatrix&tt,const GeneralMatrix&rr, - const GeneralMatrix&qq); - SSForm(const SSForm&f); - - ~SSForm(); - }; - - -struct MesEquation{ - GeneralMatrix y; - TMatrix*const Z; - TMatrix*const H; - - MesEquation(const GeneralMatrix&data,const GeneralMatrix&zz, - const GeneralMatrix&hh); - MesEquation(const GeneralMatrix&data,const TMatrix&zz, - const TMatrix&hh); - ~MesEquation(); - protected: - void construct_invariant(); - }; - - -; -#endif - diff --git a/mex/sources/kalman/cc/ssf_uni.cpp b/mex/sources/kalman/cc/ssf_uni.cpp deleted file mode 100644 index 3f39d543d..000000000 --- a/mex/sources/kalman/cc/ssf_uni.cpp +++ /dev/null @@ -1,166 +0,0 @@ -/* -* Copyright (C) 2008-2009 Dynare Team -* -* This file is part of Dynare. -* -* Dynare is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* Dynare is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with Dynare. If not, see . -*/ - -/* derived from c++kalman_filter library by O. Kamenik */ - -#include "ssf_uni.h" -#include "ts_exception.h" - - -TScalarCycle::TScalarCycle(int n) -:ss(new double[n]),flags(new bool[n]),num(n) - { - for(int i= 0;inumRows()!=m||T->numCols()!=m|| - R->numRows()!=m||R->numCols()!=r|| - Q->numCols()!=r, - "Wrong TMatrix dimension in SSFormUni constructor"); - TS_RAISE_IF(Z->numRows()!=1, - "Z is not univariate in SSFormUni constructor"); - } - -; - -SSFormUni::SSFormUni(const GeneralMatrix&zz,double hh, - const GeneralMatrix&tt,const GeneralMatrix&rr, - const GeneralMatrix&qq) - :Z(new TMatrixInvariant(zz)), - H(new TScalarInvariant(hh)), - T(new TMatrixInvariant(tt)), - R(new TMatrixInvariant(rr)), - Q(new TMatrixInvariant(qq)), - m(zz.numCols()),r(qq.numRows()) - { - TS_RAISE_IF(T->numRows()!=m||T->numCols()!=m|| - R->numRows()!=m||R->numCols()!=r|| - Q->numCols()!=r, - "Wrong TMatrix dimension in SSFormUni constructor"); - TS_RAISE_IF(Z->numRows()!=1, - "Z is not univariate in SSFormUni constructor"); - } - -SSFormUni::SSFormUni(const SSFormUni&ssfu) -:Z(ssfu.Z->clone()), -H(ssfu.H->clone()), -T(ssfu.T->clone()), -R(ssfu.R->clone()), -Q(ssfu.Q->clone()), -m(ssfu.m),r(ssfu.r) - {} - - - -SSFormUni::~SSFormUni() - { - delete Z; - delete H; - delete T; - delete R; - delete Q; - } - - -; - diff --git a/mex/sources/kalman/cc/ssf_uni.h b/mex/sources/kalman/cc/ssf_uni.h deleted file mode 100644 index a0d0ba65d..000000000 --- a/mex/sources/kalman/cc/ssf_uni.h +++ /dev/null @@ -1,101 +0,0 @@ -/* -* Copyright (C) 2008-2009 Dynare Team -* -* This file is part of Dynare. -* -* Dynare is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* Dynare is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with Dynare. If not, see . -*/ - -/* derived from c++kalman_filter library by O. Kamenik */ - -#ifndef SSF_UNI_H -#define SSF_UNI_H - -#include "ssf.h" - - -class TScalar{ - public: - virtual const double&operator[](int t)const= 0; - virtual~TScalar(){} - virtual int period()const= 0; - virtual TScalar*clone()const= 0; - }; - -; - -class TScalarInvariant:public TScalar{ - protected: - double s; - public: - TScalarInvariant(double ss) - :s(ss){} - TScalarInvariant(const TScalarInvariant&c) - :s(c.s){} - const double&operator[](int t)const - {return s;} - - int period()const - {return 1;} - TScalar*clone()const - {return new TScalarInvariant(*this);} - }; - -; - -class TScalarCycle:public TScalar{ - protected: - double*const ss; - bool*const flags; - int num; - public: - TScalarCycle(int n); - TScalarCycle(const TScalarCycle&c); - - TScalarCycle(const GeneralMatrix&m); - - TScalarCycle(const TMatrix&m); - ~TScalarCycle(); - const double&operator[](int t)const; - int period()const - {return num;} - TScalar*clone()const - {return new TScalarCycle(*this);} - void set(int t,double s); - }; - -; - -struct SSFormUni{ - TMatrix*const Z; - TScalar*const H; - TMatrix*const T; - TMatrix*const R; - TMatrix*const Q; - const int m; - const int r; - - SSFormUni(const TMatrix&zz,const TScalar&hh,const TMatrix&tt, - const TMatrix&rr,const TMatrix&qq); - SSFormUni(const GeneralMatrix&zz,double hh, - const GeneralMatrix&tt,const GeneralMatrix&rr, - const GeneralMatrix&qq); - SSFormUni(const SSFormUni&ssfu); - ~SSFormUni(); - }; - -; - -#endif - diff --git a/mex/sources/kalman/cc/state_init.cpp b/mex/sources/kalman/cc/state_init.cpp deleted file mode 100644 index d0d5ce79f..000000000 --- a/mex/sources/kalman/cc/state_init.cpp +++ /dev/null @@ -1,59 +0,0 @@ -/* -* Copyright (C) 2008-2009 Dynare Team -* -* This file is part of Dynare. -* -* Dynare is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* Dynare is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with Dynare. If not, see . -*/ - -/* derived from c++kalman_filter library by O. Kamenik */ - -#include "state_init.h" -#include "ts_exception.h" -#include "utils.h" - - -StateInit::StateInit(const GeneralMatrix&PPstar,const Vector&aa) -:m(PPstar.numRows()),ndiffuse(0),Pstar(PPstar), -Pinf(m,m),a(aa) - { - TS_RAISE_IF(Pstar.numRows()!=Pstar.numCols(), - "Pstar not square in StateInit non-diffuse constructor"); - TS_RAISE_IF(m!=a.length(), - "Bad length of initial state vector in StateInit non-diffuse constructor"); - Pinf.zeros(); - } - -; - -StateInit::StateInit(const GeneralMatrix&PPstar,const GeneralMatrix&PPinf, - const Vector&aa) - :m(PPstar.numRows()),ndiffuse(0),Pstar(PPstar), - Pinf(PPinf),a(aa) - { - TS_RAISE_IF(m!=Pstar.numCols()||m!=Pinf.numRows()|| - m!=Pinf.numCols()||m!=a.length(), - "Wrong dimensions for StateInit diffuse constructor"); - TS_RAISE_IF(!TSUtils::isDiagonal(Pinf), - "Pinf is not diagonal in StateInit diffuse constructor"); - - for(int i= 0;i. -*/ - -/* derived from c++kalman_filter library by O. Kamenik */ - -#ifndef STATE_INIT_H -#define STATE_INIT_H - -#include "GeneralMatrix.h" - - -class StateInit{ - const int m; - int ndiffuse; - GeneralMatrix Pstar; - GeneralMatrix Pinf; - Vector a; - public: - StateInit(const GeneralMatrix&PPstar,const Vector&aa); - StateInit(const GeneralMatrix&PPstar,const GeneralMatrix&PPinf, - const Vector&aa); - StateInit(const StateInit&init) - :m(init.m),ndiffuse(init.ndiffuse),Pstar(init.Pstar), - Pinf(init.Pinf),a(init.a){} - virtual~StateInit(){} - int getM()const - {return m;} - bool isDiffuse()const - {return ndiffuse> 0;} - const Vector&getA()const - {return a;} - const GeneralMatrix&getPstar()const - {return Pstar;} - const GeneralMatrix&getPinf()const - {return Pinf;} - int getNDiff()const - {return ndiffuse;} - }; - - -; - -#endif - diff --git a/mex/sources/kalman/cc/ts_exception.h b/mex/sources/kalman/cc/ts_exception.h deleted file mode 100644 index 1079495de..000000000 --- a/mex/sources/kalman/cc/ts_exception.h +++ /dev/null @@ -1,64 +0,0 @@ -/* -* Copyright (C) 2008-2009 Dynare Team -* -* This file is part of Dynare. -* -* Dynare is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* Dynare is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with Dynare. If not, see . -*/ - -/* derived from c++kalman_filter library by O. Kamenik */ - -#ifndef TS_EXCEPTION_H -#define TS_EXCEPTION_H - -#include -#include - -#ifdef MATLAB -#include "mex.h" -#endif - -#define TS_RAISE(mes) \ -throw TSException(__FILE__, __LINE__, mes); - -#define TS_RAISE_IF(expr, mes) \ -if (expr) throw TSException(__FILE__, __LINE__, mes); - - -class TSException{ - char fname[50]; - int lnum; - char message[500]; - public: - TSException(const char*f,int l,const char*mes) - { - strncpy(fname,f,50);fname[49]= '\0'; - strncpy(message,mes,500);message[499]= '\0'; - lnum= l; - } - virtual void print()const - { - printf("At %s:%d:%s\n",fname,lnum,message); -#ifdef MATLAB - mexPrintf("At %s:%d:%s\n",fname,lnum,message); -#endif - } - - virtual const char*getMessage()const - {return message;} - }; - -; -#endif - diff --git a/mex/sources/kalman/cc/utils.cpp b/mex/sources/kalman/cc/utils.cpp deleted file mode 100644 index ec829f098..000000000 --- a/mex/sources/kalman/cc/utils.cpp +++ /dev/null @@ -1,411 +0,0 @@ -/* -* Copyright (C) 2008-2009 Dynare Team -* -* This file is part of Dynare. -* -* Dynare is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* Dynare is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with Dynare. If not, see . -*/ - -/* derived from c++kalman_filter library by O. Kamenik */ - -#include "utils.h" -#include "ts_exception.h" -#include "cppblas.h" -#include "cpplapack.h" - -#include -#include -#include - - -LowerTriangle::LowerTriangle(const GeneralMatrix&m) -:GeneralMatrix(m) - { - TS_RAISE_IF(m.numRows()!=m.numCols(), - "The matrix is not square in LowerTriangle constructor"); - } - - -void LowerTriangle::multInvLeft(GeneralMatrix&m)const - { - TS_RAISE_IF(numCols()!=m.numRows(), - "Wrong dimensions of the matrix for LowerTriangle::multInvLeft"); - int mrows= m.numRows(); - int mcols= m.numCols(); - double alpha= 1.0; - int ld= getLD(); - int ldm= m.getLD(); - BLAS_dtrsm("L","L","N","N",&mrows,&mcols,&alpha,getData().base(), - &ld,m.getData().base(),&ldm); - } - -; - -void LowerTriangle::multInvLeftUnit(GeneralMatrix&m)const - { - TS_RAISE_IF(numCols()!=m.numRows(), - "Wrong dimensions of the matrix for LowerTriangle::multInvLeftUnit"); - int mrows= m.numRows(); - int mcols= m.numCols(); - double alpha= 1.0; - int ld= getLD(); - int ldm= m.getLD(); - BLAS_dtrsm("L","L","N","U",&mrows,&mcols,&alpha,getData().base(), - &ld,m.getData().base(),&ldm); - } - - -; - -NormCholesky::NormCholesky(const GeneralMatrix&a) -:L(a),D(a.numRows()) - { - TS_RAISE_IF(a.numRows()!=a.numCols(), - "The matrix is not square in NormCholesky constructor"); - - int lrows= L.numRows(); - int ldl= L.getLD(); - int info; - LAPACK_dpotrf("L",&lrows,L.getData().base(),&ldl,&info); - TS_RAISE_IF(info> 0, - "The matrix is not positive definite in NormCholesky constructor"); - TS_RAISE_IF(info<0, - "Internal error in NormCholesky constructor"); - - for(int i= 0;i 0){ - int info; - LAPACK_dgetrs(trans,&rows,&bcols,inv.base(),&rows,ipiv,b,&ldb,&info); - TS_RAISE_IF(info<0, - "Internal error in PLUFact::dgetrs"); - } - } - -; - -void PLUFact::multInvLeft(GeneralMatrix&a)const - { - TS_RAISE_IF(rows!=a.numRows(), - "Wrong dimension of the matrix in PLUFact::multInvLeft"); - PL_dgetrs("N",a.getData().base(),a.getLD(),a.numCols()); - } - -; - -void PLUFact::multInvRight(GeneralMatrix&a)const - { - GeneralMatrix atrans(a,"trans"); - TS_RAISE_IF(rows!=atrans.numRows(), - "Wrong dimension of the matrix in PLUFact::multInvRight"); - PL_dgetrs("T",atrans.getData().base(),atrans.getLD(),atrans.numCols()); - for(int i= 0;i. -*/ - -/* derived from c++kalman_filter library by O. Kamenik */ - -#ifndef UTILS_H -#define UTILS_H - -#include "GeneralMatrix.h" - - -class LowerTriangle:public GeneralMatrix{ - public: - LowerTriangle(const GeneralMatrix&m); - LowerTriangle(const LowerTriangle&t) - :GeneralMatrix(t){} - void multInvLeft(GeneralMatrix&m)const; - void multInvLeftUnit(GeneralMatrix&m)const; - }; - -; - -class NormCholesky{ - LowerTriangle L; - Vector D; - public: - NormCholesky(const GeneralMatrix&m); - NormCholesky(const NormCholesky&chol) - :L(chol.L),D(chol.D){} - const LowerTriangle&getL()const - {return L;} - const Vector&getD()const - {return D;} - }; - -; - -class PLUFact{ - Vector inv; - int*ipiv; - int rows; - double rcond; - int detsign; - int info; - public: - PLUFact(const GeneralMatrix&m); - PLUFact(const PLUFact&plu); - PLUFact(const int nc,const int nr ); - virtual~PLUFact() - {delete[]ipiv;} - const PLUFact& operator = (const GeneralMatrix&m); - void multInvLeft(GeneralMatrix&a)const; - void multInvRight(GeneralMatrix&a)const; - // pass temporary GM space for atrans to avoid matrix construction: - void multInvRight(GeneralMatrix&a, GeneralMatrix&atrans)const; - void multInvLeft(Vector&a)const; - void multInvRight(Vector&a)const; - bool isRegular()const - {return info==0;} - double getDeterminant()const; - double getLogDeterminant()const; - int getDetSign()const - {return detsign;} - int numRows()const - {return rows;} - double getRcond()const - {return rcond;} - void print()const; - private: - void PL_dgetrs(const char*trans,double*b,int ldb,int bcols)const; - void calcDetSign(); - }; - -; - -class VDVFact{ - GeneralMatrix V; - Vector D; - bool converged; - public: - VDVFact(const GeneralMatrix&m); - const GeneralMatrix&getV()const - {return V;} - const Vector&getD()const - {return D;} - bool hasConverged()const - {return converged;} - }; - - -; - -struct TSUtils{ - static bool isDiagonal(const ConstGeneralMatrix&m); - static bool isZero(const ConstGeneralMatrix&m); - static bool hasNegativeDiagonal(const ConstGeneralMatrix&m); - static bool isSymDiagDominant(const ConstGeneralMatrix&m); - static double correctDefinitness(GeneralMatrix&m); - static void correctSymmetricity(GeneralMatrix&m); - }; - -; - -#endif - diff --git a/mex/sources/kalman/matlab/Makefile b/mex/sources/kalman/matlab/Makefile deleted file mode 100644 index bfd5947a1..000000000 --- a/mex/sources/kalman/matlab/Makefile +++ /dev/null @@ -1,134 +0,0 @@ -# $Id: Makefile 531 2005-11-30 13:49:48Z kamenik $ -# Copyright 2005, Ondra Kamenik - - -#DEBUG = yes - -#LD_LIBS := -llapack -lcblas -lf77blas -latlas -lg2c - -#CC_FLAGS := -DWINDOWS -mno-cygwin - -CC_FLAGS := -DMATLAB -DNO_BLAS_H -DNO_LAPACK_H \ - -Wall -fpic -I../qt/cc -I../sylv/cc -I../cc \ - -I$(MATLAB_PATH)/extern/include #-pg - -ifeq ($(DEBUG),yes) - CC_FLAGS := -DDEBUG $(CC_FLAGS) -g -# CC_FLAGS := -DTIMING_LOOP -DDEBUG $(CC_FLAGS) -g #-pg #-Wl,-pg - KALMANLIB := kalmanlib_dbg.a -else -# CC_FLAGS := $(CC_FLAGS) -O3 - CC_FLAGS := -DTIMING_LOOP $(CC_FLAGS) -O3 - KALMANLIB := kalmanlib.a -endif - -# Added by GP -# LDFLAGS := -llapack -lcblas -lf77blas -latlas -lg2c -lstdc++ -lmingw32 - #LDFLAG := -Wl,--library-path $(LD_LIBRARY_PATH) - #-Wl,-L'C:/MinGW/lib/gcc-lib/i686-pc-mingw32/4.0.4' - #-Wl,-llibmex -Wl,-llibmx -Wl,-llibmwlapack -Wl,-llibdflapack -lf95 - #-lg2c -lmingw32 kalmanlib.def -Wl,-lmwm_ir - #-Wl,-L'f:/CygWin/lib' - #LD_LIBS := -Wl,--library-path - # -Wl,-L'/usr/lib' - - LD_LIBS := -Wl,-rpath-link,$(MATLAB_PATH)/bin/glnxa64 \ - -Wl,-L$(MATLAB_PATH)/bin/glnxa64 \ - -Wl,-lmex -lmx -lmwlapack -lmwblas -lmat -lm \ - -Wl,-lstdc++ $(LDFLAGS) - - #-Wl,-L'/usr/lib' - -# -Wl,-L'f:/CygWin/usr/local/atlas/lib' -# -Wl,-L'f:/CygWin/lib' -# -Wl,-L'f:/MinGW/lib' -# $(LDFLAGS) -# LD_LIBS :=$(LDFLAGS) -# end add - -matrix_interface := GeneralMatrix Vector SylvException -matobjs := $(patsubst %, ../sylv/cc/%.o, $(matrix_interface)) -mathsource := $(patsubst %, ../sylv/cc/%.h, $(matrix_interface)) -matcppsource := $(patsubst %, ../sylv/cc/%.cpp, $(matrix_interface)) -qtf90source := $(wildcard ../qt/f90/*.f90) -qtobjs := $(patsubst %.f90,%.o,$(qtf90source)) -# cppsource := $(patsubst %.cweb,%.cpp,$(cwebsource)) -kalmancppsource := $(wildcard ../cc/*.cpp) -kalmanhsource := $(wildcard ../cc/*.h) -kalmanobjects := $(patsubst %.cpp,%.o,$(kalmancppsource)) -cppsource := $(wildcard *.cpp) -hsource := $(wildcard *.h) -objects := $(patsubst %.cpp,%.o,$(cppsource)) -hwebsource := $(wildcard *.hweb) -cwebsource := $(wildcard *.cweb) - -dummy.ch: - touch dummy.ch - -# %.cpp: %.cweb dummy.ch -# ctangle -bhp $*.cweb dummy.ch $*.cpp - -# %.h: %.hweb dummy.ch -# ctangle -bhp $*.hweb dummy.ch $*.h - -#%.o: %.cpp $(hsource) $(cppsource) #$(kalmanhsource) $(mathsource) - -%.o: %.cpp $(hsource) $(cppsource) - c++ $(CC_FLAGS) -c $*.cpp - - -$(KALMANLIB): $(objects) # $(matobjs) $(qtobjs) #$(kalmanobjects) - ar cr $(KALMANLIB) $(kalmanobjects) $(matobjs) $(qtobjs) - ranlib $(KALMANLIB) - -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) # $(hsource) $(cppsource) - gcc -shared $(CC_FLAGS) -o minv.dll minv.o \ - kalmanlib.a $(LD_LIBS) - -gmvm.dll: gmvm.o $(KALMANLIB) # $(hsource) $(cppsource) - gcc -shared $(CC_FLAGS) -o gmvm.dll gmvm.o \ - kalmanlib.a $(LD_LIBS) - -qtamvm.dll: qtamvm.o $(KALMANLIB) # $(hsource) $(cppsource) - gcc -shared $(CC_FLAGS) -o qtamvm.dll qtamvm.o \ - kalmanlib.a $(LD_LIBS) - -qtmvm.dll: qtmvm.o $(KALMANLIB) # $(hsource) $(cppsource) - gcc -shared $(CC_FLAGS) -o qtmvm.dll qtmvm.o \ - kalmanlib.a $(LD_LIBS) - -disclyap_fast_dll.dll: disclyap_fast_dll.o $(KALMANLIB) # $(hsource) $(cppsource) - gcc -shared $(CC_FLAGS) -o disclyap_fast_dll.dll disclyap_fast_dll.o \ - $(KALMANLIB) $(LD_LIBS) kalmanlib.def - -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) # $(hsource) $(cppsource) - gcc $(CC_FLAGS) -pg -o kalman_filters_testx.exe kalman_filters_testx.o \ - $(KALMANLIB) $(LD_LIBS) - -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 - -#kalman_filter_loop.dll: kalman_filter_loop.o kalmanlib.a # $(hsource) $(cppsource) -# gcc -shared -DTIMING_LOOP $(CC_FLAGS) -o kalman_filter_loop.dll kalman_filter_loop.o \ -# kalmanlib.a $(LD_LIBS) - -doc: main.web $(hwebsource) $(cwebsource) - cweave -bhp main.web - pdftex main - mv main.pdf ts.pdf - -clear: - rm -f *.o - rm -f *.{pdf,dvi,log,scn,idx,toc} -# rm -f *.cpp -# rm -f *.h diff --git a/mex/sources/kalman/matlab/Makefile_mex b/mex/sources/kalman/matlab/Makefile_mex deleted file mode 100644 index 78f585812..000000000 --- a/mex/sources/kalman/matlab/Makefile_mex +++ /dev/null @@ -1,80 +0,0 @@ -# $Id: Makefile 532 2005-11-30 13:51:33Z kamenik $ - -# Copyright 2005, Ondra Kamenik - -CC = gcc -CC_FLAGS = -Wall -I../../sylv/cc/ -LDFLAGS = -llapack -lcblas -lf77blas -latlas -lg2c -lstdc++ - -ifeq ($(DEBUG),yes) - CC_FLAGS := $(CC_FLAGS) -g -else - CC_FLAGS := $(CC_FLAGS) -O3 -endif - -ifeq ($(OS),Windows_NT) - CC_FLAGS := -mno-cygwin $(CC_FLAGS) - LDFLAGS := -mno-cygwin $(LDFLAGS) - ARCH := w32 - MEX_SUFFIX = dll -else - LDFLAGS := $(LDFLAGS) - ARCH := linux - MEX_SUFFIX = mexglx -endif - -matrix_interface := GeneralMatrix Vector SylvException -matobjs := $(patsubst %, ../../sylv/cc/%.o, $(matrix_interface)) -mathsource := $(patsubst %, ../../sylv/cc/%.h, $(matrix_interface)) -matcppsource := $(patsubst %, ../../sylv/cc/%.cpp, $(matrix_interface)) - -tscwebsource := $(wildcard ../cc/*.cweb) -tscppsource := $(patsubst %.cweb,%.cpp,$(tscwebsource)) -tshwebsource := $(wildcard ../cc/*.hweb) -tshsource := $(patsubst %.hweb,%.h,$(tshwebsource)) -tsobjects := $(patsubst %.cweb,%.o,$(tscwebsource)) - -cppsource := $(wildcard *.cpp) -mexobjects := $(patsubst %.cpp,%_.$(MEX_SUFFIX),$(cppsource)) - -all: $(mexobjects) - -../cc/dummy.ch: - make -C ../cc dummy.ch - -../cc/%.cpp: ../cc/%.cweb ../cc/dummy.ch - make -C ../cc $*.cpp - -../cc/%.h: ../cc/%.hweb ../cc/dummy.ch - make -C ../cc $*.h - -../cc/%.o: ../cc/%.cpp $(tshsource) - make -C ../cc $*.o - -tslib.a: $(tshwebsource) $(tscwebsoure) $(tshsource) $(tscppsource) \ - $(mathsource) $(matcppsource) \ - $(tsobjects) $(matobjs) - ar cr tslib.a $(tsobjects) $(matobjs) - ranlib tslib.a - -# to compile mex objects for Windows do: -# 1. install gnumex -# 2. create mexopts.bat via gnumex in this directory, specify MinGW compilation, and dll output -# 3. change -Ic:/MATLAB7/extern/include according your Matlab setup -# 4. pray it works -%_.$(MEX_SUFFIX): %.cpp $(tshwebsource) $(tscwebsoure) $(tshsource) $(tscppsource) \ - $(mathsource) $(matcppsource) \ - tslib.a -ifeq ($(OS),Windows_NT) - mex.bat -I../../sylv/cc/ -I../cc COMPFLAGS\#"-c -DMATLAB_MEX_FILE" OPTIMFLAGS\#"-O3 -fexceptions -Ic:/MATLAB7/extern/include" GM_ADD_LIBS\#"$(LDFLAGS)" $*.cpp tslib.a -else - mex -cxx -I../../sylv/cc/ -I../cc $*.cpp CFLAGS="$(CC_FLAGS) -fexceptions" tslib.a $(LDFLAGS) -endif - mv $*.$(MEX_SUFFIX) $*_.$(MEX_SUFFIX) - -clear: - rm -f tslib.a - rm -f *.mexglx - rm -f *.dll - make -C ../testing clear - make -C ../cc clear diff --git a/mex/sources/kalman/matlab/diffuse_kalman_filter.cpp b/mex/sources/kalman/matlab/diffuse_kalman_filter.cpp deleted file mode 100644 index fbb6dd6a0..000000000 --- a/mex/sources/kalman/matlab/diffuse_kalman_filter.cpp +++ /dev/null @@ -1,153 +0,0 @@ -// $Id: kalman_filter.cpp 532 2005-11-30 13:51:33Z kamenik $ - -/* -* Copyright (C) 2008-2009 Dynare Team -* -* This file is part of Dynare. -* -* Dynare is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* Dynare is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with Dynare. If not, see . -*/ - -/* derived from c++kalman_filter library by O. Kamenik */ - -// This provides an interface to KalmanTask::filter. - -/****************************************************** -% kalman_filter.cpp : Defines the entry point for -% Computing the likelihood of a stationnary state space model. -% It is called from Dynare DsgeLikelihood.m, -% -% function [LIK, lik] = kalman_filter_dll(T,R,Q,H,P,Y,start,Z/mf[, kalman_tol,riccati_tol]) -% -% INPUTS -% T [double] mm*mm transition matrix of the state equation. -% R [double] mm*rr matrix, mapping structural innovations to state variables. -% Q [double] rr*rr covariance matrix of the structural innovations. -% H [double] pp*pp (or 1*1 =0 if no measurement error) covariance matrix of the measurement errors. -% P [double] mm*mm variance-covariance matrix with stationary variables -% Y [double] pp*smpl matrix of (detrended) data, where pp is the maximum number of observed variables. -% start [integer] scalar, likelihood evaluation starts at 'start'. -% Z or mf: [double] Z: pp*mm matrix mapping state to pp observations -% Alternative parameters -% mf [integer] pp*1 vector of indices - alternative to Z matrix. -% Additional optional parameters -% kalman_tol [double] scalar, tolerance parameter (rcond). -% riccati_tol [double] scalar, tolerance parameter (riccati iteration). -% -% OUTPUTS -% LIK [double] scalar, likelihood -% lik [double] vector, density of observations in each period. -% -% REFERENCES -% See "Filtering and Smoothing of State Vector for Diffuse State Space -% Models", S.J. Koopman and J. Durbin (2003, in Journal of Time Series -% Analysis, vol. 24(1), pp. 85-98). -% -% NOTES -% The vector "lik" is used to evaluate the jacobian of the likelihood. -**********************************************************/ - - - -#include "kalman.h" -#include "ts_exception.h" - -#include "GeneralMatrix.h" -#include "Vector.h" -#include "SylvException.h" - -#include "mex.h" - -extern "C" { - void mexFunction(int nlhs, mxArray* plhs[], - int nrhs, const mxArray* prhs[]) - { - if (nrhs < 8 || nrhs > 10) - mexErrMsgTxt("Must have 8, 9, or 10 input parameters.\n"); - if (nlhs < 1 || nlhs > 3) - mexErrMsgTxt("Must have 1, 2, or 3 output parameters.\n"); - int start = 1; // default start of likelihood calculation - try - { - // make input matrices - GeneralMatrix T(mxGetPr(prhs[0]), mxGetM(prhs[0]), mxGetN(prhs[0])); - GeneralMatrix R(mxGetPr(prhs[1]), mxGetM(prhs[1]), mxGetN(prhs[1])); - GeneralMatrix Q(mxGetPr(prhs[2]), mxGetM(prhs[2]), mxGetN(prhs[2])); - GeneralMatrix H(mxGetPr(prhs[3]), mxGetM(prhs[3]), mxGetN(prhs[3])); - GeneralMatrix Pinf(mxGetPr(prhs[4]), mxGetM(prhs[4]), mxGetN(prhs[4])); - GeneralMatrix P(mxGetPr(prhs[5]), mxGetM(prhs[5]), mxGetN(prhs[5])); - GeneralMatrix Y(mxGetPr(prhs[6]), mxGetM(prhs[6]), mxGetN(prhs[6])); - if (nrhs>6) start = (int)mxGetScalar(prhs[7]); - GeneralMatrix Z(mxGetPr(prhs[8]), mxGetM(prhs[8]), mxGetN(prhs[8])); - int nper = mxGetN(prhs[5]); // no of periods - GeneralMatrix a( mxGetN(prhs[0]), 1);// initiate inital state to 0s - a.zeros(); -#ifdef DEBUG - mexPrintf("kalman_filter: periods = %d ", nper); -#endif - // make storage for output - double loglik; - int per; - int d; - // output for full log-likelihood array - std::vector* vll=new std::vector (nper); - // create state init - StateInit* init = NULL; - init = new StateInit(P, Pinf, a.getData()); - // fork, create objects and do filtering - KalmanTask kt(Y, Z, H, T, R, Q, *init); - // developement of the output. -#ifdef DEBUG - mexPrintf("kalman_filter: running and filling outputs.\n"); -#endif - loglik = kt.filter(per, d, (start-1), vll); - // destroy init - delete init; - - // create output and upload output data - if (nlhs >= 1) - plhs[0] = mxCreateDoubleScalar(loglik); - if (nlhs >= 2) - { - // output full log-likelihood array - /* Set the output pointer to the array of log likelihood. */ - plhs[1] = mxCreateDoubleMatrix(nper,1, mxREAL); - double * mxll= mxGetPr(plhs[1]); - // allocate likelihood array - for (int j=0;j= 3) - { - plhs[1] = mxCreateNumericMatrix(1, 1, mxINT32_CLASS, mxREAL); - (*((int*)mxGetData(plhs[2]))) = per; - } - if (nlhs == 4) - { - plhs[2] = mxCreateNumericMatrix(1, 1, mxINT32_CLASS, mxREAL); - (*((int*)mxGetData(plhs[3]))) = d; - } - } - catch (const TSException& e) - { - mexErrMsgTxt(e.getMessage()); - } - catch (SylvException& e) - { - char mes[300]; - e.printMessage(mes, 299); - mexErrMsgTxt(mes); - } - } - }; diff --git a/mex/sources/kalman/matlab/disclyap_fast_dll.cpp b/mex/sources/kalman/matlab/disclyap_fast_dll.cpp deleted file mode 100644 index 7fe1ac832..000000000 --- a/mex/sources/kalman/matlab/disclyap_fast_dll.cpp +++ /dev/null @@ -1,89 +0,0 @@ -/* -* Copyright (C) 2008-2009 Dynare Team -* -* This file is part of Dynare. -* -* Dynare is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* Dynare is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with Dynare. If not, see . -*/ -/**************************************************************** -% entry Matlab DLL for function X=disclyap_fast(G,V,ch) -% -% which solve the discrete Lyapunov Equation -% X=G*X*G'+V -% Using the Doubling Algorithm -% -% If ch is defined then the code will check if the resulting X -% is positive definite and generate an error message if it is not -% -****************************************************************/ -#include "ts_exception.h" - -#include "GeneralMatrix.h" -#include "SylvException.h" -#include "mex.h" -#include "disclyap_fast.h" - -//void disclyap_fast(GeneralMatrix &G,GeneralMatrix & V, double tol= 1e-16, int ch=0); - - -extern "C" { - void mexFunction(int nlhs, mxArray* plhs[], - int nrhs, const mxArray* prhs[]) - { - - if (nrhs < 2 || nrhs > 4) - mexErrMsgTxt("Must have 2, 3 or 4 input parameters.\n"); - if (nlhs != 1 ) - mexErrMsgTxt("Must have 1 output parameters.\n"); - int cholCheck = 0; - double LyapTol=1e-06; - try - { - // make input matrices - int s = mxGetM(prhs[0]); - GeneralMatrix G(mxGetPr(prhs[0]), mxGetM(prhs[0]), mxGetN(prhs[0])); - GeneralMatrix V(mxGetPr(prhs[1]), mxGetM(prhs[1]), mxGetN(prhs[1])); - - // create output - plhs[0] = mxCreateDoubleMatrix(mxGetM(prhs[0]),mxGetN(prhs[0]), mxREAL); - GeneralMatrix X(mxGetPr(plhs[0]),mxGetM(plhs[0]),mxGetN(plhs[0])); - if (nrhs > 2) - LyapTol = (double)mxGetScalar(prhs[2]); - if (nrhs > 3) - cholCheck = (int)mxGetScalar(prhs[3]); - -#ifdef TIMING_LOOP - for (int tt=0;tt<1000;++tt) - { -#endif - disclyap_fast(G, V, X, LyapTol, cholCheck); -#ifdef TIMING_LOOP - } - mexPrintf("disclyap_fast: finished 1000 loops"); -#endif - - } - catch (const TSException& e) - { - mexErrMsgTxt(e.getMessage()); - } - catch (SylvException& e) - { - char mes[300]; - e.printMessage(mes, 299); - mexErrMsgTxt(mes); - } - - } // mexFunction - }; // extern 'C' diff --git a/mex/sources/kalman/matlab/gendata.m b/mex/sources/kalman/matlab/gendata.m deleted file mode 100644 index b9fabbe7a..000000000 --- a/mex/sources/kalman/matlab/gendata.m +++ /dev/null @@ -1,30 +0,0 @@ -% [y,epsilon,alpha,eta] = gendata(T, ssf, a0) -% -% generates random data of the length T for the given state space form -% and initial state - -% $Id: gendata.m 532 2005-11-30 13:51:33Z kamenik $ -% Copyright 2005, Ondra Kamenik - -function [y,epsilon,alpha,eta] = gendata(T, ssf, a0) - - m = size(ssf.T, 1); - p = size(ssf.Z, 1); - r = size(ssf.R, 2); - - cholH = chol(ssf.H); - cholQ = chol(ssf.Q); - - epsilon = cholH*randn(p,T); - eta = cholQ*randn(r, T); - - y = zeros(p, T); - alpha = zeros(m,T); - alpha(:,1) = a0; - - for t = 1:T - y(:,t) = ssf.Z*alpha(:,t) + epsilon(:,t); - if t ~= T - alpha(:,t+1) = ssf.T*alpha(:,t) + ssf.R*eta(:,t); - end - end diff --git a/mex/sources/kalman/matlab/gmvm.cpp b/mex/sources/kalman/matlab/gmvm.cpp deleted file mode 100644 index 87f9f7825..000000000 --- a/mex/sources/kalman/matlab/gmvm.cpp +++ /dev/null @@ -1,122 +0,0 @@ -/* -* Copyright (C) 2008-2009 Dynare Team -* -* This file is part of Dynare. -* -* Dynare is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* Dynare is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with Dynare. If not, see . -*/ - -/****************************************************** -% -% This provides an interface to QT f90 library by Andrea Pagano -% to multiply Quasi trinagular matrix (T) with a vector a -% -% function [a] = qtmvm(QT,a) -% -% 1. T1 = QT2T(QT;n) and Ld = QT2Ld(QT;n); -% 2. Ta = LdV(Ld;a;n)+TV(T1;a;n). -% -% INPUTS -% T [double] mm*mm transition matrix of the state equation. -% -% OUTPUTS -% Tinverse [double] mm*mm transition matrix of the state equation. -**********************************************************/ - -#include "qt.h" -#include "kalman.h" -#include "ts_exception.h" - -#include "GeneralMatrix.h" -#include "Vector.h" -#include "SylvException.h" - -#include "mex.h" - -extern "C" { - void mexFunction(int nlhs, mxArray* plhs[], - int nrhs, const mxArray* prhs[]) - { - - if (nrhs < 2 ) - mexErrMsgTxt("Must have min 2 input parameters.\n"); - if (nlhs < 1 ) - mexErrMsgTxt("Must have min 1 output parameters.\n") - - ; - //int start = 1; // default start of likelihood calculation - // test for univariate case - try - { - // make input matrices - int n=mxGetM(prhs[0]); - - ConstGeneralMatrix QT(mxGetPr(prhs[0]), n, mxGetN(prhs[0])); - // ConstGeneralMatrix a (mxGetPr(prhs[1]), mxGetM(prhs[1]), mxGetN(prhs[1])); - ConstVector a (mxGetPr(prhs[1]), n); - - // create output and upload output data - plhs[0] = mxCreateDoubleMatrix(mxGetM(prhs[1]),1, mxREAL);// mxGetM(prhs[1]), mxREAL); - // double * mxinv= mxGetPr(plhs[0]); - // GeneralMatrix Ta(mxGetPr(plhs[0]), mxGetM(prhs[1]), mxGetN(prhs[1])); - Vector Ta (mxGetPr(plhs[0]), n); - // Tinv.unit(); - // Ta.zeros(); - - - // make storage for output - -#ifdef TIMING_LOOP - int loops=1;//000; - if (nrhs >2 ) - loops = (int)mxGetScalar(prhs[2]); - for (int tt=0;tt= 1) - { - plhs[0] = mxCreateNumericMatrix(mxGetM(prhs[0]), mxGetM(prhs[0]), mxINT32_CLASS, mxREAL); - double * mxinv= mxGetPr(plhs[0]); - // allocate likelihood array - for (int j=0;j. -*/ - -/* derived from c++kalman_filter library by O. Kamenik */ - -// This provides an interface to KalmanTask::filter. - -/****************************************************** -% kalman_filter.cpp : Defines the entry point for -% Computing the likelihood of a stationnary state space model. -% It is called from Dynare DsgeLikelihood.m, -% -% function [LIK, lik] = kalman_filter_dll(T,R,Q,H,P,Y,start,Z/mf[, kalman_tol,riccati_tol]) -% -% INPUTS -% T [double] mm*mm transition matrix of the state equation. -% R [double] mm*rr matrix, mapping structural innovations to state variables. -% Q [double] rr*rr covariance matrix of the structural innovations. -% H [double] pp*pp (or 1*1 =0 if no measurement error) covariance matrix of the measurement errors. -% P [double] mm*mm variance-covariance matrix with stationary variables -% Y [double] pp*smpl matrix of (detrended) data, where pp is the maximum number of observed variables. -% start [integer] scalar, likelihood evaluation starts at 'start'. -% Z or mf: [double] Z: pp*mm matrix mapping state to pp observations -% Alternative parameters -% mf [integer] pp*1 vector of indices - alternative to Z matrix. -% Additional optional parameters -% kalman_tol [double] scalar, tolerance parameter (rcond). -% riccati_tol [double] scalar, tolerance parameter (riccati iteration). -% -% OUTPUTS -% LIK [double] scalar, likelihood -% lik [double] vector, density of observations in each period. -% -% REFERENCES -% See "Filtering and Smoothing of State Vector for Diffuse State Space -% Models", S.J. Koopman and J. Durbin (2003, in Journal of Time Series -% Analysis, vol. 24(1), pp. 85-98). -% -% NOTES -% The vector "lik" is used to evaluate the jacobian of the likelihood. -**********************************************************/ - - - -#include "kalman.h" -#include "ts_exception.h" - -#include "GeneralMatrix.h" -#include "Vector.h" -#include "SylvException.h" - -#include "mex.h" - -extern "C" { - void mexFunction(int nlhs, mxArray* plhs[], - int nrhs, const mxArray* prhs[]) - { - if (nrhs < 8 || nrhs > 10) - mexErrMsgTxt("Must have 8, 9, or 10 input parameters.\n"); - if (nlhs < 1 || nlhs > 3) - mexErrMsgTxt("Must have 1, 2, or 3 output parameters.\n"); - - // test for univariate case - bool uni = false; - const mxArray* const last = prhs[nrhs-1]; - if (mxIsChar(last) - && ((*mxGetChars(last)) == 'u' || (*mxGetChars(last)) == 'U')) - uni = true; - - // test for diffuse case - bool diffuse = false; - if ((mxIsChar(last) && nrhs == 10) || - (!mxIsChar(last) && nrhs == 9)) - diffuse = true; - - int start = 1; // default start of likelihood calculation - try - { - // make input matrices - GeneralMatrix T(mxGetPr(prhs[0]), mxGetM(prhs[0]), mxGetN(prhs[0])); - GeneralMatrix R(mxGetPr(prhs[1]), mxGetM(prhs[1]), mxGetN(prhs[1])); - GeneralMatrix Q(mxGetPr(prhs[2]), mxGetM(prhs[2]), mxGetN(prhs[2])); - GeneralMatrix H(mxGetPr(prhs[3]), mxGetM(prhs[3]), mxGetN(prhs[3])); - GeneralMatrix P(mxGetPr(prhs[4]), mxGetM(prhs[4]), mxGetN(prhs[4])); - GeneralMatrix Y(mxGetPr(prhs[5]), mxGetM(prhs[5]), mxGetN(prhs[5])); - if (nrhs>6) start = (int)mxGetScalar(prhs[6]); - GeneralMatrix Z(mxGetPr(prhs[7]), mxGetM(prhs[7]), mxGetN(prhs[7])); - int nper = mxGetN(prhs[5]); // no of periods - GeneralMatrix a( mxGetN(prhs[0]), 1);// initiate inital state to 0s - a.zeros(); -#ifdef DEBUG - mexPrintf("kalman_filter: periods = %d ", nper); -#endif - - // make storage for output - double loglik; - int per; - int d; - // output for full log-likelihood array - std::vector* vll=new std::vector (nper); - // create state init - StateInit* init = NULL; - init = new StateInit(P, a.getData()); - // fork, create objects and do filtering - KalmanTask kt(Y, Z, H, T, R, Q, *init); - // developement of the output. -#ifdef DEBUG - mexPrintf("kalman_filter: running and filling outputs.\n"); -#endif - loglik = kt.filter(per, d, (start-1), vll); - // destroy init - delete init; - - // create output and upload output data - if (nlhs >= 1) - plhs[0] = mxCreateDoubleScalar(loglik); - if (nlhs >= 2) - { - // output full log-likelihood array - /* Set the output pointer to the array of log likelihood. */ - plhs[1] = mxCreateDoubleMatrix(nper,1, mxREAL); - double * mxll= mxGetPr(plhs[1]); - // allocate likelihood array - for (int j=0;j= 3) - { - plhs[1] = mxCreateNumericMatrix(1, 1, mxINT32_CLASS, mxREAL); - (*((int*)mxGetData(plhs[2]))) = per; - } - if (nlhs == 4) - { - plhs[2] = mxCreateNumericMatrix(1, 1, mxINT32_CLASS, mxREAL); - (*((int*)mxGetData(plhs[3]))) = d; - } - } - catch (const TSException& e) - { - mexErrMsgTxt(e.getMessage()); - } - catch (SylvException& e) - { - char mes[300]; - e.printMessage(mes, 299); - mexErrMsgTxt(mes); - } - } - }; diff --git a/mex/sources/kalman/matlab/kalman_filters.cpp b/mex/sources/kalman/matlab/kalman_filters.cpp deleted file mode 100644 index f9411f1da..000000000 --- a/mex/sources/kalman/matlab/kalman_filters.cpp +++ /dev/null @@ -1,209 +0,0 @@ -// $Id: kalman_filter.cpp 532 2005-11-30 13:51:33Z kamenik $ - -/* -* Copyright (C) 2008-2009 Dynare Team -* -* This file is part of Dynare. -* -* Dynare is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* Dynare is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with Dynare. If not, see . -*/ - -/* derived from c++kalman_filter library by O. Kamenik */ - -// This provides an interface to KalmanTask::filter. - -/****************************************************** -% kalman_filter.cpp : Defines the entry point for -% Computing the likelihood of a stationnary state space model. -% It is called from Dynare DsgeLikelihood.m, -% -% function [loglik per d vll] = kalman_filter_dll(T,R,Q,H,Y,start,a, Z, P. [Pinf | u/U flag] -% -% INPUTS -% T [double] mm*mm transition matrix of the state equation. -% R [double] mm*rr matrix, mapping structural innovations to state variables. -% Q [double] rr*rr covariance matrix of the structural innovations. -% H [double] pp*pp (or 1*1 =0 if no measurement error) covariance matrix of the measurement errors. -% Y [double] pp*smpl matrix of (detrended) data, where pp is the maximum number of observed variables. -% start [integer] scalar, likelihood evaluation starts at 'start'. -% Z [double] pp*mm matrix mapping state to pp observations -% a [vector] mm vector of initial state, usually of 0s -% P [double] mm*mm variance-covariance matrix with stationary variables -% Pinf [optional] [double] mm*mm variance-covariance matrix with stationary variables -% u/U [optional] [char] u/U univariate -% OUTPUTS -% loglik [double] scalar, total likelihood -% per [int] number of succesfully filtered periods; if no error -% [int] then per equals to the number of columns of Y -% d number of initial periods for which the state is -% still diffuse (d is always 0 for non-diffuse case) -% vll [double] vector, density of observations in each period. -% -% REFERENCES -% See "Filtering and Smoothing of State Vector for Diffuse State Space -% Models", S.J. Koopman and J. Durbin (2003, in Journal of Time Series -% Analysis, vol. 24(1), pp. 85-98). -% -% NOTES -% The vector "vll" is used to evaluate the jacobian of the likelihood. -**********************************************************/ - -#include "kalman.h" -#include "ts_exception.h" - -#include "GeneralMatrix.h" -#include "Vector.h" -#include "SylvException.h" - -#include "mex.h" - -extern "C" { - void mexFunction(int nlhs, mxArray* plhs[], - int nrhs, const mxArray* prhs[]) - { - - if (nrhs < 9 || nrhs > 11) - mexErrMsgTxt("Must have 9, 10 or 11 input parameters.\n"); - if (nlhs < 1 || nlhs > 4) - mexErrMsgTxt("Must have 1, 2, 3 or 4 output parameters.\n"); - //int start = 1; // default start of likelihood calculation - // test for univariate case - bool uni = false; - double riccatiTol=0.000001; - const mxArray* const last = prhs[nrhs-1]; - if (mxIsChar(last) - && ((*mxGetChars(last)) == 'u' || (*mxGetChars(last)) == 'U')) - uni = true; - - // test for diffuse case - bool diffuse = false; - if ((mxIsChar(last) && nrhs == 11) || - (!mxIsChar(last) && nrhs == 10)) - diffuse = true; - - try { - // make input matrices - GeneralMatrix T(mxGetPr(prhs[0]), mxGetM(prhs[0]), mxGetN(prhs[0])); - GeneralMatrix R(mxGetPr(prhs[1]), mxGetM(prhs[1]), mxGetN(prhs[1])); - GeneralMatrix Q(mxGetPr(prhs[2]), mxGetM(prhs[2]), mxGetN(prhs[2])); - GeneralMatrix H(mxGetPr(prhs[3]), mxGetM(prhs[3]), mxGetN(prhs[3])); - GeneralMatrix Y(mxGetPr(prhs[4]), mxGetM(prhs[4]), mxGetN(prhs[4])); - int start = (int)mxGetScalar(prhs[5]); - GeneralMatrix Z(mxGetPr(prhs[6]), mxGetM(prhs[6]), mxGetN(prhs[6])); - GeneralMatrix a(mxGetPr(prhs[7]), mxGetM(prhs[7]), mxGetN(prhs[7])); - GeneralMatrix P(mxGetPr(prhs[8]), mxGetM(prhs[8]), mxGetN(prhs[8])); - - int nper=Y.numCols(); -#ifdef DEBUG - mexPrintf("kalman_filter: periods=%d start=%d, a.length=%d, uni=%d diffuse=%d\n", nper, start,a.numRows(), uni, diffuse); -#endif - - // make storage for output - double loglik; - int per; - int d; - // create state init - StateInit* init = NULL; - std::vector* vll=new std::vector (nper); - 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); -#ifdef TIMING_LOOP - } - 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, riccatiTol); -#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=%f \n", loglik); -#endif -#ifdef TIMING_LOOP - } - mexPrintf("Basickalman_filter: finished 1000 loops"); -#endif - - } - // destroy init - delete init; - - // create output and upload output data - if (nlhs >= 1) - plhs[0] = mxCreateDoubleScalar(loglik); - if (nlhs >= 2) { - plhs[1] = mxCreateNumericMatrix(1, 1, mxINT32_CLASS, mxREAL); - (*((int*)mxGetData(plhs[1]))) = per; - } - if (nlhs >= 3) { - plhs[2] = mxCreateNumericMatrix(1, 1, mxINT32_CLASS, mxREAL); - (*((int*)mxGetData(plhs[2]))) = d; - } - if (nlhs >= 4) - { - // output full log-likelihood array - /* Set the output pointer to the array of log likelihood. */ - plhs[3] = mxCreateDoubleMatrix(nper,1, mxREAL); - double * mxll= mxGetPr(plhs[3]); - // allocate likelihood array - for (int j=0;j 1 - ZM=zeros(size(Y,1), size(T,2)) - for i = 1:size(Y,1) - ZM(i,Z(i))=1 - end -else - ZM=Z; -end -% run basic multivariate kalman filter -[LIK per d lik] = kalman_filter_dll(T,R,Q,H,Y,start,ZM,a, P); - diff --git a/mex/sources/kalman/matlab/kalman_filters_testx.cpp b/mex/sources/kalman/matlab/kalman_filters_testx.cpp deleted file mode 100644 index e76fee083..000000000 --- a/mex/sources/kalman/matlab/kalman_filters_testx.cpp +++ /dev/null @@ -1,324 +0,0 @@ -// $Id: kalman_filter.cpp 532 2005-11-30 13:51:33Z kamenik $ - -/* -* Copyright (C) 2008-2009 Dynare Team -* -* This file is part of Dynare. -* -* Dynare is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* Dynare is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with Dynare. If not, see . -*/ - -/* derived from c++kalman_filter library by O. Kamenik */ - -// This provides an interface to KalmanTask::filter. - -/****************************************************** -% kalman_filter.cpp : Defines the entry point for -% Computing the likelihood of a stationnary state space model. -% It is called from Dynare DsgeLikelihood.m, -% -% function [loglik per d vll] = kalman_filter_dll(T,R,Q,H,Y,start,a, Z, P. [Pinf | u/U flag] -% -% INPUTS -% T [double] mm*mm transition matrix of the state equation. -% R [double] mm*rr matrix, mapping structural innovations to state variables. -% Q [double] rr*rr covariance matrix of the structural innovations. -% H [double] pp*pp (or 1*1 =0 if no measurement error) covariance matrix of the measurement errors. -% Y [double] pp*smpl matrix of (detrended) data, where pp is the maximum number of observed variables. -% start [integer] scalar, likelihood evaluation starts at 'start'. -% Z [double] pp*mm matrix mapping state to pp observations -% a [vector] mm vector of initial state, usually of 0s -% P [double] mm*mm variance-covariance matrix with stationary variables -% Pinf [optional] [double] mm*mm variance-covariance matrix with stationary variables -% u/U [optional] [char] u/U univariate -% OUTPUTS -% loglik [double] scalar, total likelihood -% per [int] number of succesfully filtered periods; if no error -% [int] then per equals to the number of columns of Y -% d number of initial periods for which the state is -% still diffuse (d is always 0 for non-diffuse case) -% vll [double] vector, density of observations in each period. -% -% REFERENCES -% See "Filtering and Smoothing of State Vector for Diffuse State Space -% Models", S.J. Koopman and J. Durbin (2003, in Journal of Time Series -% Analysis, vol. 24(1), pp. 85-98). -% -% NOTES -% The vector "vll" is used to evaluate the jacobian of the likelihood. -**********************************************************/ - -#include -using namespace std; - -#include "kalman.h" -#include "ts_exception.h" - -#include "GeneralMatrix.h" -#include "Vector.h" -#include "SylvException.h" - - -#include "mex.h" - -/************************************* -* This main() is for testing kalman DLL entry point by linking to -* the kalman library statically and passing its hard-coded data: -* parameters, covar, -***************************************/ - - -int main(int argc, char* argv[]) -//main (int nrhs, mxArray* plhs[], - { - int nrhs=9; - int nlhs=4; - if (nrhs < 9 || nrhs > 11) - mexErrMsgTxt("Must have 9, 10 or 11 input parameters.\n"); - if (nlhs < 1 || nlhs > 4) - mexErrMsgTxt("Must have 1, 2, 3 or 4 output parameters.\n"); - //int start = 1; // default start of likelihood calculation - // test for univariate case - bool uni = false; -// const mxArray* const last = prhs[nrhs-1]; -// if (mxIsChar(last) -// && ((*mxGetChars(last)) == 'u' || (*mxGetChars(last)) == 'U')) -// uni = true; - - // test for diffuse case - bool diffuse = false; -// if ((mxIsChar(last) && nrhs == 11) || -// (!mxIsChar(last) && nrhs == 10)) -// diffuse = true; - - double Tmat[]={// need to pass transposed matrices!!?? - - 0, 0, 0, 0, 0, 0, 0, 0, - -0.0013, 0.5000, 0.0000, -0.0000, 0.0188, -0.0013, 0.1182, -0.0017, - 0.2158, 0.0000, 0.9502, -0.0000, 0.0127, 0.2158, 0.0438, -0.0088, - 0.0273, -0.0000, -0.0000, 0.8522, -0.1260, -0.8249, -0.4720, 0.0356, - -0.0716, -0.0000, 0.0000, 0.0000, 0.5491, -0.0716, -0.9573, -0.0935, - -0.0000, -0.0000, 0.0000, -0.0000, -0.0000, -0.0000, 0.0000, -0.0000, - 0, 0, 0, 0, 0, 0, 0, 0, - 0.6464, 0.0000, -0.0000, -0.0000, 0.0573, 0.6464, 0.2126, 0.8441 - - - // 0 ,-0.001294119891461, 0.21578807493606 ,0.027263201686985, -0.071633450625617, -0, 0, 0.646379181371765, - // 0, 0.5, 0, -0, -0, -0, 0, 0, - // 0, 0, 0.9502, -0, 0, 0, 0, -0, - // 0, -0, -0, 0.8522, 0, -0, 0, -0, - // 0, 0.018758765757513, 0.012692095232426, -0.126035674083997, 0.549074256326045, -0, 0, 0.05730910985981, - // 0, -0.001294119891461, 0.21578807493606, -0.824936798313015, -0.071633450625617, -0, 0, 0.646379181371766, - // 0, 0.118192240459753, 0.04380066554165, -0.471963836695487, -0.957255289691476, 0, 0, 0.212592467520726, - // 0, -0.00168993250228, -0.008835241183444, 0.035601779209991, -0.093542875943306, -0, 0, 0.844077271823789 - }; - - double Rmat[]={// need to pass transposed matrices!!?? - 0.2271, 0, 1.0000, 0, 0.0134, 0.2271, 0.0461, -0.0093, - 0.0320, 0, 0, 1.0000, -0.1479, -0.9680, -0.5538, 0.0418, - -0.0026, 1.0000, 0, 0, 0.0375, -0.0026, 0.2364, -0.0034, - -0.0895, 0, 0, 0, 0.6863, -0.0895, -1.1966, -0.1169 - -// 0.2271, 0.0320, -0.0026, -0.0895, -// 0, 0, 1.0000, 0, -// 1.0000, 0, 0, 0, -// 0, 1.0000, 0, 0, -// 0.0134, -0.1479, 0.0375, 0.6863, -// 0.2271, -0.9680, -0.0026, -0.0895, -// 0.0461, -0.5538, 0.2364, -1.1966, -// -0.0093, 0.0418, -0.0034, -0.1169 - }; - double Qmat[]={ - 0.0931, 0, 0, 0, - 0, 0.1849, 0, 0, - 0, 0, 0.0931, 0, - 0, 0, 0, 0.0100 - }; - - double Zmat[]={ // need to pass transposed matrices!!?? - 0, 0, 1, 0, - 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 0, 0 , 1, - 0, 0, 0, 0, - 1, 0, 0, 0, - 0, 1, 0, 0 -// 0, 0, 0, 0, 0, 0, 1, 0, -// 0, 0, 0, 0, 0, 0, 0, 1, -// 1, 0, 0, 0, 0, 0, 0, 0, -// 0, 0, 0, 0, 1, 0, 0, 0 - }; - double Ymat[]={ - -0.4073, 0.2674, 0.2896, 0.0669, 0.1166, -0.1699, -0.2518, -0.0562, -0.3269,-0.0703,-0.1046, -0.4888 ,-0.3524, -0.2485 ,-0.587, -0.4546, -0.397, -0.2353, -0.0352 -0.2171, -0.3754, -0.4322, -0.4572, -0.4903, -0.4518, -0.6435, -0.6304 ,-0.4148, -0.2892, -0.4318, -0.601, -0.4148, -0.4315, -0.3531, -0.8053, -0.468, -0.4263, - 3.1739, 3.738 , 3.8285, 3.3342, 3.7447, 3.783, 3.1039, 2.8413, 3.0338, 0.3669, 0.0847 ,0.0104, 0.2115, -0.6649, -0.9625, -0.733, -0.8664, -1.4441, -1.0179, -1.2729 ,-1.9539, -1.4427, -2.0371, -1.9764, -2.5654, -2.857, -2.5842, -3.0427, -2.8312, -2.332, -2.2768, -2.1816, -2.1043, -1.8969, -2.2388, -2.1679, -2.1172, - 3.2174, 3.1903, 3.3396, 3.1358, 2.8625, 3.3546, 2.4609, 1.9534, 0.9962, -0.7904,-1.1672, -1.2586, -1.3593, -1.3443 ,-0.9413, -0.6023, -0.4516, -0.5129, -0.8741, -1.0784, -1.4091, -1.3627, -1.5731, -1.6037 -1.8814, -2.1482 ,-1.3597, -1.1855, -1.1122, -0.8424, -0.9747, -1.1385, -1.4548, -1.4284, -1.4633, -1.0621, -0.7871, - 0.8635, 0.9058, 0.7656, 0.7936, 0.8631, 0.9074, 0.9547, 1.2045, 1.085, 0.9178, 0.5242, 0.3178 ,0.1472, 0.0227, -0.0799, -0.0611, -0.014, 0.1132, 0.1774, 0.0782, 0.0436, -0.1596, -0.2691, -0.2895, -0.3791, -0.402, -0.4166 ,-0.4037, -0.3636, -0.4075, -0.4311, -0.447, -0.5111, -0.6274, -0.7261, -0.6974, -0.5012 - - }; - - try { - // make input matrices - GeneralMatrix T(Tmat, 8, 8); - GeneralMatrix R(Rmat, 8, 4); - GeneralMatrix Q(Qmat, 4, 4); - GeneralMatrix H(4, 4); - H.zeros(); -/*********use simlated data for time being *********/ - GeneralMatrix Y( 4, 109); - Y.zeros(); - for (int i=0;i<4;++i) - { - for (int j=0;j<109;++j) - { - Y.get(i,j)= ((double) ( rand() % 10 -5.0))/2.0; -#ifdef DEBUG - mexPrintf("Y [%d %d] =%f, \n", i, j,Y.get(i,j)); -#endif - - } - } -/*********** - GeneralMatrix Y(Ymat, 4, 109); - for (int i=0;i<4;++i) - { - for (int j=0;j<109;++j) - { -#ifdef DEBUG - mexPrintf("Y [%d %d] =%f, \n", i, j,Y.get(i,j)); -#endif - } - } -***********/ - double riccatiTol=0.000000000001; - int start = 1; - GeneralMatrix Z(Zmat, 4, 8); - GeneralMatrix a(8, 1); - a.zeros(); - GeneralMatrix P( 8, 8); - P.zeros(); - for (int i=0;i<8;++i) - P.get(i,i)=10.0; - - int nper=Y.numCols(); -#ifdef DEBUG - mexPrintf("kalman_filter: periods=%d start=%d, a.length=%d, uni=%d diffuse=%d\n", nper, start,a.numRows(), uni, diffuse); -#endif - - // make storage for output - double loglik=-1.1111; - int per; - int d; - // create state init - StateInit* init = NULL; - std::vector* vll=new std::vector (nper); - bool basicKF=true;//false; - if (diffuse||uni||basicKF==false) - { - 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, riccatiTol); -#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=%f \n", loglik); -// cout << "loglik " << loglik << "\n"; -#endif -#ifdef TIMING_LOOP - } - mexPrintf("Basickalman_filter: finished 10,000 loops"); -#endif - - } - - - // destroy init - delete init; - mexPrintf("logLik = %f \n", loglik); - delete vll; - // create output and upload output data -/************ - if (nlhs >= 1) - plhs[0] = mxCreateDoubleScalar(loglik); - if (nlhs >= 2) { - plhs[1] = mxCreateNumericMatrix(1, 1, mxINT32_CLASS, mxREAL); - (*((int*)mxGetData(plhs[1]))) = per; - } - if (nlhs >= 3) { - plhs[2] = mxCreateNumericMatrix(1, 1, mxINT32_CLASS, mxREAL); - (*((int*)mxGetData(plhs[2]))) = d; - } - if (nlhs >= 4) - { - // output full log-likelihood array - // Set the output pointer to the array of log likelihood. - plhs[3] = mxCreateDoubleMatrix(nper,1, mxREAL); - double * mxll= mxGetPr(plhs[3]); - // allocate likelihood array - for (int j=0;j. -*/ - -/* derived from c++kalman_filter library by O. Kamenik */ - -// This provides an interface to KalmanTask::filter. - -#include "kalman.h" -#include "ts_exception.h" - -#include "GeneralMatrix.h" -#include "Vector.h" -#include "SylvException.h" - -#include "mex.h" - -extern "C" { - void mexFunction(int nhls, mxArray* plhs[], - int nhrs, const mxArray* prhs[]) - { - if (nhrs < 8 || nhrs > 10) - mexErrMsgTxt("Must have 8, 9, or 10 input parameters.\n"); - if (nhls < 1 || nhls > 6) - mexErrMsgTxt("Must have 1, 2,.. or 6 output parameters.\n"); - - // test for univariate case - bool uni = false; - const mxArray* const last = prhs[nhrs-1]; - if (mxIsChar(last) - && ((*mxGetChars(last)) == 'u' || (*mxGetChars(last)) == 'U')) - uni = true; - - // test for diffuse case - bool diffuse = false; - if ((mxIsChar(last) && nhrs == 10) || - (!mxIsChar(last) && nhrs == 9)) - diffuse = true; - - try { - // make input matrices - GeneralMatrix Z(mxGetPr(prhs[0]), mxGetM(prhs[0]), mxGetN(prhs[0])); - GeneralMatrix H(mxGetPr(prhs[1]), mxGetM(prhs[1]), mxGetN(prhs[1])); - GeneralMatrix T(mxGetPr(prhs[2]), mxGetM(prhs[2]), mxGetN(prhs[2])); - GeneralMatrix R(mxGetPr(prhs[3]), mxGetM(prhs[3]), mxGetN(prhs[3])); - GeneralMatrix Q(mxGetPr(prhs[4]), mxGetM(prhs[4]), mxGetN(prhs[4])); - GeneralMatrix Y(mxGetPr(prhs[5]), mxGetM(prhs[5]), mxGetN(prhs[5])); - GeneralMatrix a(mxGetPr(prhs[6]), mxGetM(prhs[6]), mxGetN(prhs[6])); - GeneralMatrix P(mxGetPr(prhs[7]), mxGetM(prhs[7]), mxGetN(prhs[7])); - - // make storage for output - double loglik; - int per; - int d; - SmootherResults sres(Y.numCols()); - - // create state init - StateInit* init = NULL; - if (diffuse) { - GeneralMatrix Pinf(mxGetPr(prhs[8]), mxGetM(prhs[8]), mxGetN(prhs[8])); - init = new StateInit(P, Pinf, a.getData()); - } else { - init = new StateInit(P, a.getData()); - } - // fork, create objects and do filtering and smoothing - KalmanTask kt(Y, Z, H, T, R, Q, *init); - if (uni) { - KalmanUniTask kut(kt); - SmootherResults sres_uni(Y.numRows()*Y.numCols()); - loglik = kut.filter_and_smooth(sres_uni, per, d); - per = per / Y.numRows(); - d = d / Y.numRows(); - sres.import(sres_uni, Y.numRows()); - } else { - loglik = kt.filter_and_smooth(sres, per, d); - // loglik = kt.filter(per, d); - } - // destroy init - delete init; - - // create output and upload output data - if (nhls >= 1) - plhs[0] = mxCreateDoubleScalar(loglik); - if (nhls >= 2) { - plhs[1] = mxCreateNumericMatrix(1, 1, mxINT32_CLASS, mxREAL); - (*((int*)mxGetData(plhs[1]))) = per; - } - if (nhls >= 3) { - plhs[2] = mxCreateNumericMatrix(1, 1, mxINT32_CLASS, mxREAL); - (*((int*)mxGetData(plhs[2]))) = d; - } - if (nhls >= 4) { - plhs[3] = mxCreateNumericMatrix(T.numRows(), Y.numCols(), mxDOUBLE_CLASS, mxREAL); - if (per == Y.numCols()) { - GeneralMatrix tmp(mxGetPr(plhs[3]), T.numRows(), Y.numCols()); - sres.exportAlpha(tmp); - } - } - if (nhls >= 5) { - plhs[4] = mxCreateNumericMatrix(R.numCols(), Y.numCols(), mxDOUBLE_CLASS, mxREAL); - if (per == Y.numCols()) { - GeneralMatrix tmp(mxGetPr(plhs[4]), R.numCols(), Y.numCols()); - sres.exportEta(tmp); - } - } - if (nhls >= 6) { - int dims[3]; dims[0] = T.numRows(); - dims[1] = T.numRows(); dims[2] = Y.numCols(); - plhs[5] = mxCreateNumericMatrix(3, dims[0], mxDOUBLE_CLASS, mxREAL); - if (per == Y.numCols()) { - GeneralMatrix tmp(mxGetPr(plhs[5]), T.numRows(), - T.numRows()*Y.numCols()); - sres.exportV(tmp); - } - } - } catch (const TSException& e) { - mexErrMsgTxt(e.getMessage()); - } catch (SylvException& e) { - char mes[300]; - e.printMessage(mes, 299); - mexErrMsgTxt(mes); - } - } -}; diff --git a/mex/sources/kalman/matlab/kalman_smoother.m b/mex/sources/kalman/matlab/kalman_smoother.m deleted file mode 100644 index 046813ef6..000000000 --- a/mex/sources/kalman/matlab/kalman_smoother.m +++ /dev/null @@ -1,41 +0,0 @@ -% -% SYNOPSIS -% -% [loglik,per,d,alpha,eta,V] = kalman_smoother(Z,H,T,R,Q,Y,a,P) -% [loglik,per,d,alpha,eta,V] = kalman_smoother(Z,H,T,R,Q,Y,a,P,flag) -% [loglik,per,d,alpha,eta,V] = kalman_smoother(Z,H,T,R,Q,Y,a,Pstar,Pinf) -% [loglik,per,d,alpha,eta,V] = kalman_smoother(Z,H,T,R,Q,Y,a,Pstar,Pinf,flag) -% -% SEMANTICS -% -% The first two commands run a Kalman filter and smoother for non-diffuse initial -% conditions, the other two for diffuse initial conditions. -% -% Input: -% Z,H,T,R,Q gives a state space form -% Y observed data (columns correspond to periods) -% a mean of initial state -% P covariance of initial non-diffuse state -% Pstar finite part of covariance of initial diffuse state -% Pinf infinite part of covariance of initial diffuse state -% flag string starting with 'u', or 'U' runs a univariate -% form of the filter; if omitted, a multivariate version -% is run by default -% -% Output: -% loglik data log likelihood -% per number of succesfully filtered periods; if no error -% then per equals to the number of columns of Y -% d number of initial periods for which the state is -% still diffuse (d is always 0 for non-diffuse case) -% alpha matrix of smoothed states; columns are periods -% eta matrix of smoothed shocks; columns are periods -% V 3D array of smoothed state variances; V(:,:,t) is -% smoothed state variance-covariance at time t -% -% Copyright 2005, Ondra Kamenik -% - -function [loglik,per,d,alpha,eta,V] = kalman_smoother(varargin) - - [loglik,per,d,alpha,eta,V] = kalman_smoother_(varargin{:}); diff --git a/mex/sources/kalman/matlab/kalmandll_test.m b/mex/sources/kalman/matlab/kalmandll_test.m deleted file mode 100644 index d3197b9b8..000000000 --- a/mex/sources/kalman/matlab/kalmandll_test.m +++ /dev/null @@ -1,23 +0,0 @@ -function [LIKDLL loglik]=kalmandll_test(T,mf,R,Q,H,Pstar,Pinf,data,start) - -if isempty(H) - H=zeros(size(data,1), size(data,1)) -elseif H==0 - H=zeros(size(data,1), size(data,1)) -end -Z=zeros(size(data,1), size(T,2)) -for i = 1:size(data,1) -Z(i,mf(i))=1 -end -%LIKDLL= kalman_filter_dll8(T,Z,R,Q,H,Pstar,data,start) -%[loglik,per,d] = kalman_filter_dll(Z,H,T,R,Q,data,a,Pstar) -[LIK2 lik2] = kalman_filter(T,R,Q,H,Pstar,data,start,mf,options_.kalman_tol,options_.riccati_tol) -if isempty(Pinf) - Pinf=zeros(size(T)); -elseif Pinf==0 - Pinf=zeros(size(T)); -end -% test DiffuseLikelihoodH1 -%[loglikd1,per,d] = kalman_filter_dll(Z,H,T,R,Q,data,a,Pstar,Pinf) -[LIKdlikd ] = diffuse_kalman_filter(T,R,Q,H,Pinf,Pstar,data,start,Z,options_.kalman_tol,options_.riccati_tol) -%loglikd2 = dynare_filter(Z,H,T,R,Q,data,Pstar,Pinf) diff --git a/mex/sources/kalman/matlab/kalmanlib.def b/mex/sources/kalman/matlab/kalmanlib.def deleted file mode 100644 index 9d27b788b..000000000 --- a/mex/sources/kalman/matlab/kalmanlib.def +++ /dev/null @@ -1,2 +0,0 @@ -EXPORTS -mexFunction diff --git a/mex/sources/kalman/matlab/mexopts.bat b/mex/sources/kalman/matlab/mexopts.bat deleted file mode 100644 index 53173853d..000000000 --- a/mex/sources/kalman/matlab/mexopts.bat +++ /dev/null @@ -1,58 +0,0 @@ -@echo off -rem C:\ondra\tmp\dynare++\extern\matlab\mexopts.bat -rem Generated by gnumex.m script in c:\fs\gnumex-1.11 -rem gnumex version: 1.11 -rem Compile and link options used for building MEX etc files with -rem the Mingw/Cygwin tools. Options here are: -rem Mingw linking -rem Mex (*.dll) creation -rem Safe linking to temporary libraries -rem Language: C / C++ -rem Compiling for pentium and above -rem Matlab version 7 -rem -set MATLAB=C:\MATLAB7 -set GM_PERLPATH=C:\MATLAB7\sys\perl\win32\bin\perl.exe -set GM_UTIL_PATH=c:\fs\gnumex-1.11 -set PATH=c:\fs\mingw\bin;%PATH% -rem -rem Added libraries for linking -set GM_ADD_LIBS= -rem -rem Type of file to compile (mex or engine) -set GM_MEXTYPE=mex -rem -rem Language for compilation -set GM_MEXLANG=c -rem -rem def files to be converted to libs -set GM_DEFS2LINK=libmx.def;libmex.def;libmat.def; -rem -rem dlltool command line -set GM_DLLTOOL=dlltool -rem -rem compiler options; add compiler flags to compflags as desired -set NAME_OBJECT=-o -set COMPILER=gcc -set COMPFLAGS=-c -DMATLAB_MEX_FILE -set OPTIMFLAGS=-O3 -malign-double -fno-exceptions -mcpu=pentium -set DEBUGFLAGS=-g -set CPPCOMPFLAGS=%COMPFLAGS% -x c++ -set CPPOPTIMFLAGS=%OPTIMFLAGS% -set CPPDEBUGFLAGS=%DEBUGFLAGS% -rem -rem NB Library creation commands occur in linker scripts -rem -rem Linker parameters -set LINKER=%GM_PERLPATH% %GM_UTIL_PATH%\linkmex.pl -set LINKFLAGS= -set CPPLINKFLAGS=GM_ISCPP -set LINKOPTIMFLAGS=-s -set LINKDEBUGFLAGS=-g -Wl,--image-base,0x28000000\n -set LINK_FILE= -set LINK_LIB= -set NAME_OUTPUT=-o %OUTDIR%%MEX_NAME%.dll -rem -rem Resource compiler parameters -set RC_COMPILER=%GM_PERLPATH% %GM_UTIL_PATH%\rccompile.pl -o %OUTDIR%mexversion.res -set RC_LINKER= diff --git a/mex/sources/kalman/matlab/minv.cpp b/mex/sources/kalman/matlab/minv.cpp deleted file mode 100644 index 3bef4319e..000000000 --- a/mex/sources/kalman/matlab/minv.cpp +++ /dev/null @@ -1,108 +0,0 @@ -/* -* Copyright (C) 2008-2009 Dynare Team -* -* This file is part of Dynare. -* -* Dynare is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* Dynare is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with Dynare. If not, see . -*/ - -/* derived from c++kalman_filter library by O. Kamenik */ - -// This provides a matrix inversion - -/****************************************************** -% function [Tinv] = minv(T) -% -% INPUTS -% T [double] mm*mm transition matrix of the state equation. - -% OUTPUTS -% Tinverse [double] mm*mm transition matrix of the state equation. -**********************************************************/ - -#include "kalman.h" -#include "ts_exception.h" - -#include "GeneralMatrix.h" -#include "Vector.h" -#include "SylvException.h" - -#include "mex.h" - -extern "C" { - void mexFunction(int nlhs, mxArray* plhs[], - int nrhs, const mxArray* prhs[]) - { - - if (nrhs < 1 ) - mexErrMsgTxt("Must have min 1 input parameters.\n"); - if (nlhs < 1 ) - mexErrMsgTxt("Must have min 1 output parameters.\n") - - ; - //int start = 1; // default start of likelihood calculation - // test for univariate case - try - { - // make input matrices - ConstGeneralMatrix T(mxGetPr(prhs[0]), mxGetM(prhs[0]), mxGetN(prhs[0])); - // create output and upload output data - plhs[0] = mxCreateDoubleMatrix(mxGetM(prhs[0]), mxGetM(prhs[0]), mxREAL); -// double * mxinv= mxGetPr(plhs[0]); - GeneralMatrix Tinv(mxGetPr(plhs[0]), mxGetM(prhs[0]), mxGetN(prhs[0])); -// Tinv.unit(); -// Tinv.zeros(); - - - // make storage for output - -#ifdef TIMING_LOOP - int loops=1000; - if (nrhs >1 ) - loops = (int)mxGetScalar(prhs[1]); - for (int tt=0;tt= 1) - { - plhs[0] = mxCreateNumericMatrix(mxGetM(prhs[0]), mxGetM(prhs[0]), mxINT32_CLASS, mxREAL); - double * mxinv= mxGetPr(plhs[0]); - // allocate likelihood array - for (int j=0;j. -*/ - -/****************************************************** -% -% This provides an interface to QT f90 library by Andrea Pagano -% to multiply Quasi trinagular matrix (T) with a vector a -% -% function [a] = qtmvm(QT,a) -% -% 1. T1 = QT2T(QT;n) and Ld = QT2Ld(QT;n); -% 2. Ta = LdV(Ld;a;n)+TV(T1;a;n). -% -% INPUTS -% T [double] mm*mm transition matrix of the state equation. -% a [double] mm state vector. -% -% OUTPUTS -% Tinverse [double] mm*mm transition matrix of the state equation. -**********************************************************/ - -#include "qt.h" -#include "kalman.h" -#include "ts_exception.h" - -#include "GeneralMatrix.h" -#include "Vector.h" -#include "SylvException.h" - -#include "mex.h" - -extern "C" { - void mexFunction(int nlhs, mxArray* plhs[], - int nrhs, const mxArray* prhs[]) - { - - if (nrhs < 2 ) - mexErrMsgTxt("Must have min 2 input parameters.\n"); - if (nlhs < 1 ) - mexErrMsgTxt("Must have min 1 output parameters.\n") - - ; - //int start = 1; // default start of likelihood calculation - // test for univariate case - try - { - // make input matrices - int n=mxGetM(prhs[0]); - - double *T1, *Ld, *TV ; - double * QT=mxGetPr(prhs[0]); - // ConstGeneralMatrix a (mxGetPr(prhs[1]), mxGetM(prhs[1]), mxGetN(prhs[1])); - double *a =(mxGetPr(prhs[1])); - - // create output and upload output data - plhs[0] = mxCreateDoubleMatrix(mxGetM(prhs[1]),1, mxREAL);// mxGetM(prhs[1]), mxREAL); - // double * mxinv= mxGetPr(plhs[0]); - // GeneralMatrix Ta(mxGetPr(plhs[0]), mxGetM(prhs[1]), mxGetN(prhs[1])); - double * Ta =mxGetPr(plhs[0]); - // Tinv.unit(); - // Ta.zeros(); - - T1=(double *)mxCalloc(n*n, sizeof(double)); - Ld=(double *)mxCalloc(n*n,sizeof(double)); - TV=(double *)mxCalloc(n, sizeof(double)); - - -#ifdef TIMING_LOOP - int loops=1;//000; - if (nrhs >2 ) - loops = (int)mxGetScalar(prhs[2]); - for (int tt=0;tt= 1) - { - plhs[0] = mxCreateNumericMatrix(mxGetM(prhs[0]), mxGetM(prhs[0]), mxINT32_CLASS, mxREAL); - double * mxinv= mxGetPr(plhs[0]); - // allocate likelihood array - for (int j=0;j. -*/ - -/****************************************************** -% -% This provides an interface to QT f90 library by Andrea Pagano -% to multiply Quasi trinagular matrix (T) with a vector a -% -% function [a] = qtmvm(QT,a) -% -% 1. T1 = QT2T(QT;n) and Ld = QT2Ld(QT;n); -% 2. Ta = LdV(Ld;a;n)+TV(T1;a;n). -% -% INPUTS -% T [double] mm*mm transition matrix of the state equation. -% a [double] mm state vector. -% -% OUTPUTS -% Tinverse [double] mm*mm transition matrix of the state equation. -**********************************************************/ - -#include "qt.h" -#include "kalman.h" -#include "ts_exception.h" - -#include "GeneralMatrix.h" -#include "Vector.h" -#include "SylvException.h" - -#include "mex.h" - -extern "C" { - void mexFunction(int nlhs, mxArray* plhs[], - int nrhs, const mxArray* prhs[]) - { - - if (nrhs < 2 ) - mexErrMsgTxt("Must have min 2 input parameters.\n"); - if (nlhs < 1 ) - mexErrMsgTxt("Must have min 1 output parameters.\n") - - ; - //int start = 1; // default start of likelihood calculation - // test for univariate case - try - { - // make input matrices - int n=mxGetM(prhs[0]); - - ConstGeneralMatrix QT(mxGetPr(prhs[0]), n, mxGetN(prhs[0])); - // ConstGeneralMatrix a (mxGetPr(prhs[1]), mxGetM(prhs[1]), mxGetN(prhs[1])); - Vector a (mxGetPr(prhs[1]), n); - - // create output and upload output data - plhs[0] = mxCreateDoubleMatrix(mxGetM(prhs[1]),1, mxREAL);// mxGetM(prhs[1]), mxREAL); - // double * mxinv= mxGetPr(plhs[0]); - // GeneralMatrix Ta(mxGetPr(plhs[0]), mxGetM(prhs[1]), mxGetN(prhs[1])); - Vector Ta (mxGetPr(plhs[0]), n); - // Tinv.unit(); - // Ta.zeros(); - - GeneralMatrix T1gm(n,n); - GeneralMatrix Ld(n,n); - Vector TV( n); - - // make storage for output - -#ifdef TIMING_LOOP - int loops=1;//000; - if (nrhs >2 ) - loops = (int)mxGetScalar(prhs[2]); - for (int tt=0;tt= 1) - { - plhs[0] = mxCreateNumericMatrix(mxGetM(prhs[0]), mxGetM(prhs[0]), mxINT32_CLASS, mxREAL); - double * mxinv= mxGetPr(plhs[0]); - // allocate likelihood array - for (int j=0;j. -*/ - -/* derived from c++kalman_filter library by O. Kamenik */ - -// This provides an interface to KalmanTask::filter. - -/****************************************************** -% kalman_filter.cpp : Defines the entry point for -% Computing the likelihood of a stationnary state space model. -% It is called from Dynare DsgeLikelihood.m, -% -% function [LIK, lik] = kalman_filter_dll(T,R,Q,H,P,Y,start,Z/mf[, kalman_tol,riccati_tol]) -% -% INPUTS -% T [double] mm*mm transition matrix of the state equation. -% R [double] mm*rr matrix, mapping structural innovations to state variables. -% Q [double] rr*rr covariance matrix of the structural innovations. -% H [double] pp*pp (or 1*1 =0 if no measurement error) covariance matrix of the measurement errors. -% P [double] mm*mm variance-covariance matrix with stationary variables -% Y [double] pp*smpl matrix of (detrended) data, where pp is the maximum number of observed variables. -% start [integer] scalar, likelihood evaluation starts at 'start'. -% Z or mf: [double] Z: pp*mm matrix mapping state to pp observations -% Alternative parameters -% mf [integer] pp*1 vector of indices - alternative to Z matrix. -% Additional optional parameters -% kalman_tol [double] scalar, tolerance parameter (rcond). -% riccati_tol [double] scalar, tolerance parameter (riccati iteration). -% -% OUTPUTS -% LIK [double] scalar, likelihood -% lik [double] vector, density of observations in each period. -% -% REFERENCES -% See "Filtering and Smoothing of State Vector for Diffuse State Space -% Models", S.J. Koopman and J. Durbin (2003, in Journal of Time Series -% Analysis, vol. 24(1), pp. 85-98). -% -% NOTES -% The vector "lik" is used to evaluate the jacobian of the likelihood. -**********************************************************/ - - - -#include "kalman.h" -#include "ts_exception.h" - -#include "GeneralMatrix.h" -#include "Vector.h" -#include "SylvException.h" - -#include "mex.h" - -extern "C" { - void mexFunction(int nlhs, mxArray* plhs[], - int nrhs, const mxArray* prhs[]) - { - if (nrhs < 8 || nrhs > 10) - mexErrMsgTxt("Must have 8, 9, or 10 input parameters.\n"); - if (nlhs < 1 || nlhs > 3) - mexErrMsgTxt("Must have 1, 2, or 3 output parameters.\n"); - - int start = 1; // default start of likelihood calculation - try - { - // make input matrices - GeneralMatrix T(mxGetPr(prhs[0]), mxGetM(prhs[0]), mxGetN(prhs[0])); - GeneralMatrix R(mxGetPr(prhs[1]), mxGetM(prhs[1]), mxGetN(prhs[1])); - GeneralMatrix Q(mxGetPr(prhs[2]), mxGetM(prhs[2]), mxGetN(prhs[2])); - GeneralMatrix H(mxGetPr(prhs[3]), mxGetM(prhs[3]), mxGetN(prhs[3])); - GeneralMatrix Pinf(mxGetPr(prhs[4]), mxGetM(prhs[4]), mxGetN(prhs[4])); - GeneralMatrix P(mxGetPr(prhs[5]), mxGetM(prhs[5]), mxGetN(prhs[5])); - GeneralMatrix Y(mxGetPr(prhs[6]), mxGetM(prhs[6]), mxGetN(prhs[6])); - if (nrhs>6) start = (int)mxGetScalar(prhs[7]); - GeneralMatrix Z(mxGetPr(prhs[8]), mxGetM(prhs[8]), mxGetN(prhs[8])); - int nper = mxGetN(prhs[5]); // no of periods - GeneralMatrix a( mxGetN(prhs[0]), 1);// initiate inital state to 0s - a.zeros(); -#ifdef DEBUG - mexPrintf("kalman_filter: periods = %d ", nper); -#endif - - // make storage for output - double loglik; - int per; - int d; - // output for full log-likelihood array - std::vector* vll=new std::vector (nper); - // create state init - StateInit* init = NULL; - init = new StateInit(P, Pinf, a.getData()); - // fork, create objects and do filtering - KalmanTask kt(Y, Z, H, T, R, Q, *init); - // developement of the output. -#ifdef DEBUG - mexPrintf("kalman_filter: running and filling outputs.\n"); -#endif - KalmanUniTask kut(kt); - loglik = kut.filter(per, d, (start-1), vll); - per = per / Y.numRows(); - d = d / Y.numRows(); - // destroy init - delete init; - - // create output and upload output data - if (nlhs >= 1) - plhs[0] = mxCreateDoubleScalar(loglik); - if (nlhs >= 2) - { - // output full log-likelihood array - /* Set the output pointer to the array of log likelihood. */ - plhs[1] = mxCreateDoubleMatrix(nper,1, mxREAL); - double * mxll= mxGetPr(plhs[1]); - // allocate likelihood array - for (int j=0;j= 3) - { - plhs[1] = mxCreateNumericMatrix(1, 1, mxINT32_CLASS, mxREAL); - (*((int*)mxGetData(plhs[2]))) = per; - } - if (nlhs == 4) - { - plhs[2] = mxCreateNumericMatrix(1, 1, mxINT32_CLASS, mxREAL); - (*((int*)mxGetData(plhs[3]))) = d; - } - } - catch (const TSException& e) - { - mexErrMsgTxt(e.getMessage()); - } - catch (SylvException& e) - { - char mes[300]; - e.printMessage(mes, 299); - mexErrMsgTxt(mes); - } - } - }; diff --git a/mex/sources/kalman/matlab/univariate_kalman_filter.cpp b/mex/sources/kalman/matlab/univariate_kalman_filter.cpp deleted file mode 100644 index ecd5caa0f..000000000 --- a/mex/sources/kalman/matlab/univariate_kalman_filter.cpp +++ /dev/null @@ -1,156 +0,0 @@ -// $Id: kalman_filter.cpp 532 2005-11-30 13:51:33Z kamenik $ - -/* -* Copyright (C) 2008-2009 Dynare Team -* -* This file is part of Dynare. -* -* Dynare is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* Dynare is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with Dynare. If not, see . -*/ - -/* derived from c++kalman_filter library by O. Kamenik */ - -// This provides an interface to KalmanTask::filter. - -/****************************************************** -% kalman_filter.cpp : Defines the entry point for -% Computing the likelihood of a stationnary state space model. -% It is called from Dynare DsgeLikelihood.m, -% -% function [LIK, lik] = kalman_filter_dll(T,R,Q,H,P,Y,start,Z/mf[, kalman_tol,riccati_tol]) -% -% INPUTS -% T [double] mm*mm transition matrix of the state equation. -% R [double] mm*rr matrix, mapping structural innovations to state variables. -% Q [double] rr*rr covariance matrix of the structural innovations. -% H [double] pp*pp (or 1*1 =0 if no measurement error) covariance matrix of the measurement errors. -% P [double] mm*mm variance-covariance matrix with stationary variables -% Y [double] pp*smpl matrix of (detrended) data, where pp is the maximum number of observed variables. -% start [integer] scalar, likelihood evaluation starts at 'start'. -% Z or mf: [double] Z: pp*mm matrix mapping state to pp observations -% Alternative parameters -% mf [integer] pp*1 vector of indices - alternative to Z matrix. -% Additional optional parameters -% kalman_tol [double] scalar, tolerance parameter (rcond). -% riccati_tol [double] scalar, tolerance parameter (riccati iteration). -% -% OUTPUTS -% LIK [double] scalar, likelihood -% lik [double] vector, density of observations in each period. -% -% REFERENCES -% See "Filtering and Smoothing of State Vector for Diffuse State Space -% Models", S.J. Koopman and J. Durbin (2003, in Journal of Time Series -% Analysis, vol. 24(1), pp. 85-98). -% -% NOTES -% The vector "lik" is used to evaluate the jacobian of the likelihood. -**********************************************************/ - - - -#include "kalman.h" -#include "ts_exception.h" - -#include "GeneralMatrix.h" -#include "Vector.h" -#include "SylvException.h" - -#include "mex.h" - -extern "C" { - void mexFunction(int nlhs, mxArray* plhs[], - int nrhs, const mxArray* prhs[]) - { - if (nrhs < 8 || nrhs > 10) - mexErrMsgTxt("Must have 8, 9, or 10 input parameters.\n"); - if (nlhs < 1 || nlhs > 3) - mexErrMsgTxt("Must have 1, 2, or 3 output parameters.\n"); - int start = 1; // default start of likelihood calculation - try - { - // make input matrices - GeneralMatrix T(mxGetPr(prhs[0]), mxGetM(prhs[0]), mxGetN(prhs[0])); - GeneralMatrix R(mxGetPr(prhs[1]), mxGetM(prhs[1]), mxGetN(prhs[1])); - GeneralMatrix Q(mxGetPr(prhs[2]), mxGetM(prhs[2]), mxGetN(prhs[2])); - GeneralMatrix H(mxGetPr(prhs[3]), mxGetM(prhs[3]), mxGetN(prhs[3])); - GeneralMatrix P(mxGetPr(prhs[4]), mxGetM(prhs[4]), mxGetN(prhs[4])); - GeneralMatrix Y(mxGetPr(prhs[5]), mxGetM(prhs[5]), mxGetN(prhs[5])); - if (nrhs>6) start = (int)mxGetScalar(prhs[6]); - GeneralMatrix Z(mxGetPr(prhs[7]), mxGetM(prhs[7]), mxGetN(prhs[7])); - int nper = mxGetN(prhs[5]); // no of periods - GeneralMatrix a( mxGetN(prhs[0]), 1);// initiate inital state to 0s - a.zeros(); -#ifdef DEBUG - mexPrintf("kalman_filter: periods = %d ", nper); -#endif - - // make storage for output - double loglik; - int per; - int d; - // output for full log-likelihood array - std::vector* vll=new std::vector (nper); - // create state init - StateInit* init = NULL; - init = new StateInit(P, a.getData()); - // fork, create objects and do filtering - KalmanTask kt(Y, Z, H, T, R, Q, *init); - // developement of the output. -#ifdef DEBUG - mexPrintf("kalman_filter: running and filling outputs.\n"); -#endif - KalmanUniTask kut(kt); - loglik = kut.filter(per, d, (start-1), vll); - per = per / Y.numRows(); - d = d / Y.numRows(); - // destroy init - delete init; - - // create output and upload output data - if (nlhs >= 1) - plhs[0] = mxCreateDoubleScalar(loglik); - if (nlhs >= 2) - { - // output full log-likelihood array - /* Set the output pointer to the array of log likelihood. */ - plhs[1] = mxCreateDoubleMatrix(nper,1, mxREAL); - double * mxll= mxGetPr(plhs[1]); - // allocate likelihood array - for (int j=0;j= 3) - { - plhs[1] = mxCreateNumericMatrix(1, 1, mxINT32_CLASS, mxREAL); - (*((int*)mxGetData(plhs[2]))) = per; - } - if (nlhs == 4) - { - plhs[2] = mxCreateNumericMatrix(1, 1, mxINT32_CLASS, mxREAL); - (*((int*)mxGetData(plhs[3]))) = d; - } - } - catch (const TSException& e) - { - mexErrMsgTxt(e.getMessage()); - } - catch (SylvException& e) - { - char mes[300]; - e.printMessage(mes, 299); - mexErrMsgTxt(mes); - } - } - }; diff --git a/mex/sources/kalman/qt/FortranSubroutines.zip b/mex/sources/kalman/qt/FortranSubroutines.zip deleted file mode 100644 index 5a69ed6b9b3152ef05a1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EiJ}!oJ zfT?RTm!ef!4h5AI@9H-nfGQ9Qi1F+Ke2~o6f8T>#<{*1FaQ5#%90*_@Dd+E-g3Bai zYwraq8E9EZN#_snG6&hxdBHJ6L(2KPf?nnz+r}<9vFJ!S|J}_llaOsL7o;+bt4QY^ zd_Y=FDKbmAb# zoR4#tF~}yI3(P7ma?Ia(=Q0Rchra;b1tSOj&(lKI^e!+~_{cH;BN 1 .AND. (QT(i,i-1)/= 0.0)) then -h=i-1 -else -h=i -end if -if (j > 1 .AND. (QT(j,j-1)/= 0.0)) then -k=j-1 -k0=k -else -k=j -k0=k -end if -stemp=0 -do while (h <= n) -do while (k <= n) -stemp=stemp+QT(i,h)*S(h,k)*QT(j,k) -k=k+1 -end do -k=k0 -h=h+1 -end do -X(i,j)=stemp -if (i /= j) then -X(j,i)=stemp -end if -end do -end do - - - - -return -end subroutine QTSQTt diff --git a/mex/sources/kalman/qt/f90/QTV.f90 b/mex/sources/kalman/qt/f90/QTV.f90 deleted file mode 100644 index fbe2cc5e4..000000000 --- a/mex/sources/kalman/qt/f90/QTV.f90 +++ /dev/null @@ -1,33 +0,0 @@ -subroutine QTV(X,QT,V,n) -! COMPUTATIONAL SUBROUTINE: X=QT*V QT upper quasi-triangular; V vector - -implicit none - -integer(4), intent(in) :: n -real(8), intent(in) :: QT(n,n) -real(8), intent(in) :: V(n,1) - -real(8), intent(out) :: X(n,1) - -integer(4) :: i,k - -real(8) :: stemp - - - -do i=1,n -stemp = 0.0 -if (i > 1 .AND. (QT(i,i-1)/= 0.0)) then -stemp = QT(i,i-1)*v(i-1,1) -end if - -do k = 0,n-i -stemp = stemp + QT(i,k+i) * V(k+i,1) -end do -X(i,1)=stemp -end do - -return -end subroutine QTV - - diff --git a/mex/sources/kalman/qt/f90/QTV_1.f90 b/mex/sources/kalman/qt/f90/QTV_1.f90 deleted file mode 100644 index 598cf04d0..000000000 --- a/mex/sources/kalman/qt/f90/QTV_1.f90 +++ /dev/null @@ -1,33 +0,0 @@ -subroutine QTV_1(X,QT,V,n) -! COMPUTATIONAL SUBROUTINE: X=QT*V QT upper quasi-triangular; V vector - -implicit none - -integer(4), intent(in) :: n -real(8), intent(in) :: QT(n,n) -real(8), intent(in) :: V(n,1) - -real(8), intent(out) :: X(n,1) - -integer(4) :: i,k - -real(8) :: stemp - -X=0*V - -do i=1,n -stemp = 0.0 -if (i > 1 .AND. (QT(i,i-1)/= 0.0)) then -stemp = QT(i,i-1)*v(i-1,1) -end if - -do k = 0,n-i -stemp = stemp + QT(i,k+i) * V(k+i,1) -end do -X(i,1)=stemp -end do - -return -end subroutine QTV_1 - - diff --git a/mex/sources/kalman/qt/f90/S2D.f90 b/mex/sources/kalman/qt/f90/S2D.f90 deleted file mode 100644 index 432a73016..000000000 --- a/mex/sources/kalman/qt/f90/S2D.f90 +++ /dev/null @@ -1,19 +0,0 @@ -subroutine S2D(X,S,n) -! COMPUTATIONAL SUBROUTINE TUt=T*Ut T upper triangular; U striclty upper triangular - -implicit none - -integer(4), intent(in) :: n -real(8), intent(in) :: S(n,n) -real(8), intent(out) :: X(n,n) - -integer(4) :: i - - - -X=0.0*S -do i=1,n -X(i,i)=sqrt(S(i,i)) -end do -return -end subroutine S2D diff --git a/mex/sources/kalman/qt/f90/S2U.f90 b/mex/sources/kalman/qt/f90/S2U.f90 deleted file mode 100644 index d7ce4dece..000000000 --- a/mex/sources/kalman/qt/f90/S2U.f90 +++ /dev/null @@ -1,23 +0,0 @@ -subroutine S2U(X,S,n) -! COMPUTATIONAL SUBROUTINE: S2U extracts striclty upper triangular from symmetric - -implicit none - -integer(4), intent(in) :: n -real(8), intent(in) :: S(n,n) -real(8), intent(out) :: X(n,n) -integer(4) :: i,j - - - - - -X=0.0*S -do i=1,n -do j=i+1,n -X(i,j)=S(i,j) -end do -end do - -return -end subroutine S2U diff --git a/mex/sources/kalman/qt/f90/TD.f90 b/mex/sources/kalman/qt/f90/TD.f90 deleted file mode 100644 index 59dd31f31..000000000 --- a/mex/sources/kalman/qt/f90/TD.f90 +++ /dev/null @@ -1,22 +0,0 @@ -subroutine TD(X,T,D,n) -! COMPUTATIONAL SUBROUTINE TUt=T*D T upper triangular; D diagonal - -implicit none - -integer(4), intent(in) :: n -real(8), intent(in) :: T(n,n) -real(8), intent(in) :: D(n,n) - -real(8), intent(out) :: X(n,n) -integer(4) :: i,j - - -X=0.0*T -do i=1,n -do j=i,n -X(i,j)=T(i,j)*D(j,j) -end do -end do - -return -end subroutine TD diff --git a/mex/sources/kalman/qt/f90/TM.f90 b/mex/sources/kalman/qt/f90/TM.f90 deleted file mode 100644 index 4e0f2f1fb..000000000 --- a/mex/sources/kalman/qt/f90/TM.f90 +++ /dev/null @@ -1,29 +0,0 @@ -subroutine TM(X,T,M,n) -! COMPUTATIONAL SUBROUTINE: TM=T*M T upper triangular; M arbitrary - - -implicit none - -integer(4), intent(in) :: n -real(8), intent(in) :: T(n,n) -real(8), intent(in) :: M(n,n) - -real(8), intent(out) :: X(n,n) - -integer(4) :: i,j,k - -real(8) :: stemp - - -do i=1,n -do j=1,n -stemp = 0.0 -do k = 0,n-i -stemp = stemp + T(i,k+i) * M(k+i,j) -end do -X(i,j)=stemp -end do -end do - -return -end subroutine TM diff --git a/mex/sources/kalman/qt/f90/TSTt.f90 b/mex/sources/kalman/qt/f90/TSTt.f90 deleted file mode 100644 index d44ffd76d..000000000 --- a/mex/sources/kalman/qt/f90/TSTt.f90 +++ /dev/null @@ -1,34 +0,0 @@ -subroutine TSTt(X,T,S,n) -! COMPUTATIONAL SUBROUTINE TUt=T*Ut T upper triangular; U striclty lower triangular - -implicit none - -integer(4), intent(in) :: n -real(8), intent(in) :: T(n,n) -real(8), intent(in) :: S(n,n) - -real(8), intent(out) :: X(n,n) -integer(4) :: i,j,k,h - -real(8) :: stemp - - -X=0.0*S - -do i=1,n -do j=1,n -stemp = 0.0 -do h = i,n -do k = j,n -stemp = stemp + T(i,h) * S(h,k) * T(j,k) -end do -end do -X(i,j)=stemp -X(j,i)=stemp - -end do - -end do - -return -end subroutine TSTt diff --git a/mex/sources/kalman/qt/f90/TT.f90 b/mex/sources/kalman/qt/f90/TT.f90 deleted file mode 100644 index 5024942e0..000000000 --- a/mex/sources/kalman/qt/f90/TT.f90 +++ /dev/null @@ -1,29 +0,0 @@ -subroutine TT(X, T1,T2,n) -! COMPUTATIONAL SUBROUTINE TUt=T*Ut T upper triangular; U striclty lower triangular - -implicit none - -integer(4), intent(in) :: n -real(8), intent(in) :: T1(n,n) -real(8), intent(in) :: T2(n,n) - -real(8), intent(out) :: X(n,n) -integer(4) :: i,j,k,h - -real(8) :: stemp - -X=0.0*T1 - -do i=1,n -do j=1,n -stemp = 0.0 -do k = 0,n-i -stemp = stemp + T1(i,k+i) * T2(k+i,j) -end do -X(i,j)=stemp -end do - -end do - -return -end subroutine TT \ No newline at end of file diff --git a/mex/sources/kalman/qt/f90/TU.f90 b/mex/sources/kalman/qt/f90/TU.f90 deleted file mode 100644 index 1bcdd253a..000000000 --- a/mex/sources/kalman/qt/f90/TU.f90 +++ /dev/null @@ -1,31 +0,0 @@ -subroutine TU(X,T,U,n) -! COMPUTATIONAL SUBROUTINE: TU=T*U; T upper triangular matrix; U strictly upper triangular - - -implicit none - -integer(4), intent(in) :: n -real(8), intent(in) :: T(n,n) -real(8), intent(in) :: U(n,n) - -real(8), intent(out) :: X(n,n) -integer(4) :: i,j,k - -real(8) :: stemp - - -X=0.0*T - - -do i=1,n-1 -do j=i+1,n -stemp = 0.0 -do k = i,j-1 -stemp = stemp + T(i,k) * U(k,j) -end do -X(i,j)=stemp -end do -end do - -return -end subroutine TU diff --git a/mex/sources/kalman/qt/f90/TUt.f90 b/mex/sources/kalman/qt/f90/TUt.f90 deleted file mode 100644 index 050787044..000000000 --- a/mex/sources/kalman/qt/f90/TUt.f90 +++ /dev/null @@ -1,29 +0,0 @@ -subroutine TUt(X,T,Ut,n) -! COMPUTATIONAL SUBROUTINE TUt=T*Ut T upper triangular; U striclty lower triangular - -implicit none - -integer(4), intent(in) :: n -real(8), intent(in) :: T(n,n) -real(8), intent(in) :: Ut(n,n) - -real(8), intent(out) :: X(n,n) -integer(4) :: i,j,k,h - -real(8) :: stemp - - -X=0.0*T -do i=1,n -do j=1,n-1 -h=max(i,j) -stemp = 0.0 -do k = 0,n-h -stemp = stemp + T(i,k+h) * Ut(k+h,j) -end do -X(i,j)=stemp -end do -end do - -return -end subroutine TUt \ No newline at end of file diff --git a/mex/sources/kalman/qt/f90/TV.f90 b/mex/sources/kalman/qt/f90/TV.f90 deleted file mode 100644 index bdc651f7e..000000000 --- a/mex/sources/kalman/qt/f90/TV.f90 +++ /dev/null @@ -1,27 +0,0 @@ -subroutine TV(X,T,V,n) -! COMPUTATIONAL SUBROUTINE: TV=T*V T upper triangular; V vector - -implicit none - -integer(4), intent(in) :: n -real(8), intent(in) :: T(n,n) -real(8), intent(in) :: V(n,1) - -real(8), intent(out) :: X(n,1) - -integer(4) :: i,k - -real(8) :: stemp - - - -do i=1,n -stemp = 0.0 -do k = 0,n-i -stemp = stemp + T(i,k+i) * V(k+i,1) -end do -X(i,1)=stemp -end do - -return -end subroutine TV diff --git a/mex/sources/kalman/qt/test/Makefile b/mex/sources/kalman/qt/test/Makefile deleted file mode 100644 index 566cd7dfb..000000000 --- a/mex/sources/kalman/qt/test/Makefile +++ /dev/null @@ -1,85 +0,0 @@ -# $Id: Makefile 531 2005-11-30 13:49:48Z kamenik $ -# Copyright 2005, Ondra Kamenik - -#DEBUG = yes - -#LD_LIBS := -llapack -lcblas -lf77blas -latlas -lg2c -#-mno-cygwin -DWINDOWS -CC_FLAGS := -DMATLAB -DNO_BLAS_H -DNO_LAPACK_H \ - -Wall -I../cc -I../../sylv/cc -I../cc \ - -I$(MATLAB_PATH)/extern/include #-pg - -ifeq ($(DEBUG),yes) - CC_FLAGS := -DDEBUG $(CC_FLAGS) -g -pg -# CC_FLAGS := -DTIMING_LOOP -DDEBUG $(CC_FLAGS) -g - KALMANLIB := kalmanlib_dbg.a -else -# CC_FLAGS := $(CC_FLAGS) -O3 - CC_FLAGS := -DTIMING_LOOP $(CC_FLAGS) -O3 - KALMANLIB := kalmanlib.a -endif - -# Added by GP -# LDFLAGS := -llapack -lcblas -lf77blas -latlas -lg2c -lstdc++ -lmingw32 - #LDFLAGS := -Wl,--library-path $(LD_LIBRARY_PATH) - # -Wl,-L'f:/MinGW/lib' - #-Wl,-L'C:/MinGW/lib/gcc-lib/i686-pc-mingw32/4.0.4' -Wl,-L'C:/MinGW/lib' - - LD_LIBS := -Wl,-rpath-link,$(MATLAB_PATH)/bin/glnxa64 \ - -Wl,-L$(MATLAB_PATH)/bin/glnxa64 \ - -Wl,-lmex -lmx -lmwlapack -lmwblas -lmat -lm \ - -Wl,-lstdc++ -lgfortran $(LDFLAGS) - -# -Wl,-L'f:/CygWin/usr/local/atlas/lib' -# -Wl,-L'f:/CygWin/lib' -# $(LDFLAGS) -# LD_LIBS :=$(LDFLAGS) -# end add - -#matrix_interface := GeneralMatrix Vector SylvException -#matobjs := $(patsubst %, ../sylv/cc/%.o, $(matrix_interface)) -#mathsource := $(patsubst %, ../sylv/cc/%.h, $(matrix_interface)) -#matcppsource := $(patsubst %, ../sylv/cc/%.cpp, $(matrix_interface)) -qtf90source := $(wildcard ../f90/*.f90) -qtobjs := $(patsubst %.f90,%.o,$(qtf90source)) -cppsource := $(wildcard *.cpp) -hsource := $(wildcard *.h) -objects := $(patsubst %.cpp,%.o,$(cppsource)) - -dummy.ch: - touch dummy.ch - - -%.o: %.cpp $(hsource) $(cppsource) - c++ $(CC_FLAGS) -c $*.cpp - -dgemmmtm_exe.exe: dgemmmtm_exe.o $(objects) $(qtobjs) $(hsource) $(cppsource) - gcc $(CC_FLAGS) -o dgemmmtm_exe.exe dgemmmtm_exe.o ascii_array.o \ - $(qtobjs) $(LD_LIBS) - -qtmmmtm_exe.exe: qtmmmtm_exe.o $(objects) $(qtobjs) $(hsource) $(cppsource) - gcc $(CC_FLAGS) -o qtmmmtm_exe.exe qtmmmtm_exe.o ascii_array.o \ - $(qtobjs) $(LD_LIBS) - -dgemvm_exe.exe: dgemvm_exe.o $(qtobjs) $(hsource) $(cppsource) - gcc $(CC_FLAGS) -o dgemvm_exe.exe dgemvm_exe.o ascii_array.o \ - $(qtobjs) $(LD_LIBS) - -qtamvm_exe.exe: qtamvm_exe.o $(qtobjs) $(hsource) $(cppsource) - gcc $(CC_FLAGS) -o qtamvm_exe.exe qtamvm_exe.o ascii_array.o \ - $(qtobjs) $(LD_LIBS) - -qtvmvm_exe.exe: qtvmvm_exe.o $(qtobjs) $(hsource) $(cppsource) - gcc $(CC_FLAGS) -o qtvmvm_exe.exe qtvmvm_exe.o ascii_array.o \ - $(qtobjs) $(LD_LIBS) - -qtv1mvm_exe.exe: qtv1mvm_exe.o $(qtobjs) $(hsource) $(cppsource) - gcc $(CC_FLAGS) -o qtv1mvm_exe.exe qtv1mvm_exe.o ascii_array.o \ - $(qtobjs) $(LD_LIBS) - -all: $(objects) dgemmmtm_exe.exe qtmmmtm_exe.exe qtv1mvm_exe.exe dgemvm_exe.exe qtvmvm_exe.exe qtamvm_exe.exe # $(cppsource) $(hsource) $(kalmanhsource) $(kalmancppsource) - -clear: - rm -f *.o - rm -f *.a - rm -f *.{exe,dll} diff --git a/mex/sources/kalman/qt/test/QT_tab b/mex/sources/kalman/qt/test/QT_tab deleted file mode 100644 index d5eeaa432..000000000 --- a/mex/sources/kalman/qt/test/QT_tab +++ /dev/null @@ -1,11 +0,0 @@ -0.153060262389052 0.001380442914015 0.001695719778633 -0.096495788866461 -0.006326378534057 0.000968937494692 0.00940481674898 -0.003274924112445 0.001481132666149 0.004112932319104 -0.012701307804641 -0 0.073952133671219 -0.001504207761519 0.057433281320695 0.009662045343528 0.006617685912409 0.013462091408161 0.007891283166287 0.00060603779672 0.003369481465008 0.018748481144843 -0 0 0.016722949277241 -0.043158612323585 -0.004579527748968 0.014353708194285 -0.002590188137874 0.015728887447029 -0.000497609190313 -0.000142760660804 -0.019083813953199 -0 0 0 0.265453840405543 0.00050417813553 0.002313090008356 0.027009998927894 -0.00302237841155 -0.006605759131604 -0.009638158849163 0.01228621141328 -0 0 0 0 0.040159616212026 -0.035581727103737 -0.016954216473738 -0.007949691886576 0.003628861926114 -0.001291923621455 0.039842615500298 -0 0 0 0 0 0.10084154296869 -0.001042311199417 -0.005382521361517 0.001696402254541 -0.000540820717391 0.023206920949216 -0 0 0 0 0 0 0.054131830294643 -0.020987700402038 0.000402689298563 -0.000018817482395 0.001917827090534 -0 0 0 0 0 0 0 0.14427448853052 -0.000904268210001 -0.004155316544672 0.018404810011954 -0 0 0 0 0 0 0 0 0.009309814517978 0.000447330038827 0.00529992488884 -0 0 0 0 0 0 0 0 0 0.005213746475416 0.012395564932675 -0 0 0 0 0 0 0 0 0 0 0.026158448255444 \ No newline at end of file diff --git a/mex/sources/kalman/qt/test/QTmatlabTest.m b/mex/sources/kalman/qt/test/QTmatlabTest.m deleted file mode 100644 index 3ae646523..000000000 --- a/mex/sources/kalman/qt/test/QTmatlabTest.m +++ /dev/null @@ -1,45 +0,0 @@ -size=100; -AA=rand(size); -BB=rand(size); -[QT1 QT2 U1 U2]=qz(AA,BB); - -a=rand(size,1); - -Loops=10000 - -% Calling QT without use of Sylv Vector and General Matrix -t = clock; -[AAAA]=qtamvm(QT1,a,Loops); -QTcpp_noSylv_TaInnerLoop_time=etime(clock, t) - -% Calling QT using of Sylv Vector and General Matrix -t = clock; -[AAAA]=qtmvm(QT1,a,Loops); -QTcppTaInnerLoop_time=etime(clock, t) - -t = clock; -[AAAA]=qtmvm_sub(QT1,a,Loops); -QTcppTaInnerLoop_time=etime(clock, t) - -t = clock; -for tt=1:Loops%0 -[AAAA]=qtmvm_sub(QT1,a,1); -end -QTcppTaOuterLoop_time=etime(clock, t) - -t = clock; -[AAAA]=gmvm(QT1,a,Loops); -GMcppTaInnrLoop_time=etime(clock, t) - -t = clock; -for tt=1:Loops%0 -[AAAA]=gmvm(QT1,a,1); -end -GMcppTaOuterLoop_time=etime(clock, t) - -t = clock; -for tt=1:Loops%0 -MTA=QT1*a; -end -matlabTa_time=etime(clock, t) - diff --git a/mex/sources/kalman/qt/test/aa_dat b/mex/sources/kalman/qt/test/aa_dat deleted file mode 100644 index cdf5c02e4..000000000 --- a/mex/sources/kalman/qt/test/aa_dat +++ /dev/null @@ -1,11 +0,0 @@ -0.130845306698951 -0.784832609671869 -0.213653301284573 -0.81536432324564 -0.021265975953734 -0.226338606586172 -0.447782600002914 -0.43948408700417 -0.687965758422965 -0.34628969510696 -0.996770191062048 \ No newline at end of file diff --git a/mex/sources/kalman/qt/test/ascii_array.cpp b/mex/sources/kalman/qt/test/ascii_array.cpp deleted file mode 100644 index 958b6fdb8..000000000 --- a/mex/sources/kalman/qt/test/ascii_array.cpp +++ /dev/null @@ -1,84 +0,0 @@ -// ascii_matrix.cpp -// Based on work of 2005, Ondra Kamenik - -#include "ascii_array.h" -#include -#include -#include -#include - -#include -#include - -// if the file doesn't exist, the number array is empty -void -AsciiNumberArray::GetMX(const char* fname, int INrows, int INcols) - { - rows = 0; - cols = INcols; - - std::ifstream file(fname); - std::string line; - - data=(double*)calloc(INcols*INrows,sizeof(double)); - while (getline(file, line)) - { - rows++; - int icols = 0; - const char delims[] = " \r\n\t"; - char* lineptr = strdup(line.c_str()); - char* tok = strtok(lineptr, delims); - while (tok) - { - icols++; - double item; - if (1 != sscanf(tok, "%lf", &item)) - { - fprintf(stderr, "Couldn't parse a token %s as double.\n", tok); - exit(1); - } - data[(rows-1)*INcols+icols-1]=item; - tok = strtok(NULL, delims); - } - free(lineptr); - if (cols) - { - if (cols != icols) - { - fprintf(stderr, "Asserted a different number of columns.\n"); - exit(1); - } - } - else - { - cols = icols; - } - } - } - - -void -AsciiNumberArray::WriteMX() - { - std::ofstream outFile(strcat(fname, "_out")); - for (int i = 0; i < rows; i++) - { - for (int j = 0; j < cols; j++) - { - outFile << data[i*cols+j] << "\t"; - } - outFile << std::endl; - } - outFile.close(); - } - - -void WriteMX(char* fname, double* data, int rows, int cols) - { - AsciiNumberArray OutArray; - OutArray.fname=fname; - OutArray.rows=rows; - OutArray.cols=cols; - OutArray.data=data; - OutArray.WriteMX(); - } diff --git a/mex/sources/kalman/qt/test/ascii_array.h b/mex/sources/kalman/qt/test/ascii_array.h deleted file mode 100644 index aba3ab6f1..000000000 --- a/mex/sources/kalman/qt/test/ascii_array.h +++ /dev/null @@ -1,15 +0,0 @@ -// ascii_matrix.h -// Based on work of Ondra Kamenik - - -struct AsciiNumberArray { - int rows; - int cols; - double * data; - char* fname; - void GetMX(const char* fname, int rows, int cols); - void WriteMX(); -}; - -void WriteMX(char* fname, double* data, int rows, int cols); - diff --git a/mex/sources/kalman/qt/test/dgemmmtm_exe.cpp b/mex/sources/kalman/qt/test/dgemmmtm_exe.cpp deleted file mode 100644 index 51a3ede7c..000000000 --- a/mex/sources/kalman/qt/test/dgemmmtm_exe.cpp +++ /dev/null @@ -1,117 +0,0 @@ -/* -* Copyright (C) 2008-2009 Dynare Team -* -* This file is part of Dynare. -* -* Dynare is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* Dynare is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with Dynare. If not, see . -*/ - -/****************************************************** -% -% This provides an interface to BLAS dgemv f90 library function -% to multiply Quasi trinagular matrix (T) with a vector a -% -% -% use: -% dgemmmtm_exe QTt_file SS_file size [loops - if enabled] -% -% NOTE: due to fortran matrix orientation, input matrices need to be passed -% as transposed so QTt instead QT -% -% -% INPUTS -% QT [double] mm*mm transition matrix of the state equation. -% SS [double] mm*mm state cov matrix. -% -% OUTPUTS -% TSTt update [double] mm*mm state cov matrix updated. -% as file: a_file_out -**********************************************************/ - -#include "cppblas.h" -#include -#include -#include -#include "ascii_array.h" -#include -#include -#include - -int main(int argc, char* argv[]) - { - - if (argc < 3 ) - { - printf("Must have min 2 input parameters.\n"); - exit(1); - } - - try - { - // make input matrices - int n=atoi(argv[3]); - double *TSTt, *TS ; - AsciiNumberArray QT, SS; - QT.GetMX(argv[1],n,n); - SS.GetMX(argv[2],n,n); - const double alpha=1.0; - const double beta=0.0; - // create output and upload output data - TS=(double *)calloc(n*n, sizeof(double)); - TSTt=(double *)calloc(n*n, sizeof(double)); - - -#ifdef TIMING_LOOP - int loops=1;//000; - if (argc >3 ) - loops = atoi(argv[4]); - for (int tt=0;tt. -*/ - -/****************************************************** -% -% This provides an interface to BLAS dgemv f90 library function -% to multiply Quasi trinagular matrix (T) with a vector a -% -% function [a] = dgevm(QT,a) -% -% use: -% dgemvm_exe QTt_file a_file size [loops - if enabled] -% -% NOTE: due to fortran matrix orientation, input matrices need to be passed -% as transposed so QTt instead QT -% -% 1. Ta = dgemv(Ld;a;n)+TV(T1;a;n). -% -% INPUTS -% T [double] mm*mm transition matrix of the state equation. -% a [double] mm state vector. -% -% OUTPUTS -% a update [double] mm vector of the state equation. -% as file: a_file_out -**********************************************************/ - -#include "cppblas.h" -#include -#include -#include -#include "ascii_array.h" -#include -#include -#include - -int main(int argc, char* argv[]) - { - - if (argc < 3 ) - { - printf("Must have min 2 input parameters.\n"); - exit(1); - } - - try - { - // make input matrices - int n=atoi(argv[3]); - double *Ta ; - AsciiNumberArray QT, a; - QT.GetMX(argv[1],n,n); - a.GetMX(argv[2],n,1); - const double alpha=1.0; - const double beta=0.0; - int inc =1; - // create output and upload output data - Ta=(double *)calloc(n, sizeof(double)); - - -#ifdef TIMING_LOOP - int loops=1;//000; - if (argc >3 ) - loops = atoi(argv[4]); - for (int tt=0;tt. -*/ - -/****************************************************** -% -% This provides an interface to QT f90 library by Andrea Pagano -% to multiply Quasi trinagular matrix (T) with a vector a -% -% function [a] = qtamvm(QT,a) -% -% use: -% qtamvm_exe QTt_file a_file size [loops - if enabled] -% -% NOTE: due to fortran matrix orientation, input matrices need to be passed -% as transposed so QTt instead QT -% -% 1. T1 = QT2T(QT;n) and Ld = QT2Ld(QT;n); -% 2. Ta = LdV(Ld;a;n)+TV(T1;a;n). -% -% INPUTS -% T [double] mm*mm transition matrix of the state equation. -% a [double] mm state vector. -% -% OUTPUTS -% a update [double] mm vector of the state equation. -% as file: a_file_out -**********************************************************/ - -#include "qt.h" -#include -#include -#include -#include "ascii_array.h" -#include -#include -#include - -int main(int argc, char* argv[]) - { - - if (argc < 3 ) - { - printf("Must have min 2 input parameters.\n"); - exit(1); - } - - try - { - // make input matrices - int n=atoi(argv[3]); - double *T1, *Ld, *TV, * Ta ; - AsciiNumberArray QT, a; - QT.GetMX(argv[1],n,n); - a.GetMX(argv[2],n,1); - T1=(double *)calloc(n*n, sizeof(double)); - Ld=(double *)calloc(n*n,sizeof(double)); - TV=(double *)calloc(n, sizeof(double)); - // create output and upload output data - Ta=(double *)calloc(n, sizeof(double)); - - -#ifdef TIMING_LOOP - int loops=1;//000; - if (argc >3 ) - loops = atoi(argv[4]); - for (int tt=0;tt. -*/ - -/****************************************************** -% -% This provides an interface to BLAS dgemm library function -% to multiply Quasi trinagular matrix (T) with another matrix -% -% use: -% dgemmmtm_exe QTt_file SS_file size [loops - if enabled] -% -% NOTE: due to fortran matrix orientation, input matrices need to be passed -% as transposed so QTt instead QT -% -% -% INPUTS -% QT [double] mm*mm transition matrix of the state equation. -% SS [double] mm*mm state cov matrix. -% -% OUTPUTS -% TSTt update [double] mm*mm state cov matrix updated. -% as file: a_file_out -**********************************************************/ - -#include "qt.h" -#include -#include -#include -#include "ascii_array.h" -#include -#include -#include - -int main(int argc, char* argv[]) - { - - if (argc < 3 ) - { - printf("Must have min 2 input parameters.\n"); - exit(1); - } - - try - { - // make input matrices - int n=atoi(argv[3]); - double *TSTt ; - AsciiNumberArray QT, SS; - QT.GetMX(argv[1],n,n); - SS.GetMX(argv[2],n,n); - // create output and upload output data - TSTt=(double *)calloc(n*n, sizeof(double)); - - -#ifdef TIMING_LOOP - int loops=1;//000; - if (argc >3 ) - loops = atoi(argv[4]); - for (int tt=0;tt. -*/ - -/****************************************************** -% -% This provides an interface to QT f90 library by Andrea Pagano -% to multiply Quasi trinagular matrix (T) with a vector a -% -% function [a] = qtvmvm(QT,a) -% -% use: -% qtvmvm_exe QTt_file a_file size [loops - if enabled] -% -% NOTE: due to fortran matrix orientation, input matrices need to be passed -% as transposed so QTt instead QT -% -% 2. Ta = QTV(T1;a;n). -% -% INPUTS -% T [double] mm*mm transition matrix of the state equation. -% a [double] mm state vector. -% -% OUTPUTS -% a update [double] mm vector of the state equation. -% as file: a_file_out -**********************************************************/ - -#include "qt.h" -#include -#include -#include -#include "ascii_array.h" -#include -#include -#include - -int main(int argc, char* argv[]) - { - - if (argc < 3 ) - { - printf("Must have min 2 input parameters.\n"); - exit(1); - } - - try - { - // make input matrices - int n=atoi(argv[3]); - double *Ta ; - AsciiNumberArray QT, a; - QT.GetMX(argv[1],n,n); - a.GetMX(argv[2],n,1); - // create output and upload output data - Ta=(double *)calloc(n, sizeof(double)); - - -#ifdef TIMING_LOOP - int loops=1;//000; - if (argc >3 ) - loops = atoi(argv[4]); - for (int tt=0;tt. -*/ - -/****************************************************** -% -% This provides an interface to QT f90 library by Andrea Pagano -% to multiply Quasi trinagular matrix (T) with a vector a -% -% function [a] = qtvmvm(QT,a) -% -% use: -% qtvmvm_exe QTt_file a_file size [loops - if enabled] -% -% NOTE: due to fortran matrix orientation, input matrices need to be passed -% as transposed so QTt instead QT -% -% 2. Ta = QTV(T1;a;n). -% -% INPUTS -% T [double] mm*mm transition matrix of the state equation. -% a [double] mm state vector. -% -% OUTPUTS -% a update [double] mm vector of the state equation. -% as file: a_file_out -**********************************************************/ - -#include "qt.h" -#include -#include -#include -#include "ascii_array.h" -#include -#include -#include - -int main(int argc, char* argv[]) - { - - if (argc < 3 ) - { - printf("Must have min 2 input parameters.\n"); - exit(1); - } - - try - { - // make input matrices - int n=atoi(argv[3]); - double *Ta ; - AsciiNumberArray QT, a; - QT.GetMX(argv[1],n,n); - a.GetMX(argv[2],n,1); - // create output and upload output data - Ta=(double *)calloc(n, sizeof(double)); - - -#ifdef TIMING_LOOP - int loops=1;//000; - if (argc >3 ) - loops = atoi(argv[4]); - for (int tt=0;tt -#include -#include -#include -#include - -//vectornullVec(0); -int GeneralMatrix::md_length = 32; - -GeneralMatrix::GeneralMatrix(const GeneralMatrix& m) -: data(m.rows*m.cols), rows(m.rows), cols(m.cols), ld(m.rows) - { - copy(m); - } - -GeneralMatrix::GeneralMatrix(const ConstGeneralMatrix& m) -: data(m.rows*m.cols), rows(m.rows), cols(m.cols), ld(m.rows) - { - copy(m); - } - -GeneralMatrix::GeneralMatrix(const GeneralMatrix& m, const char* dummy) -: data(m.rows*m.cols), rows(m.cols), cols(m.rows), ld(m.cols) - { - for (int i = 0; i < m.rows; i++) - for (int j = 0; j < m.cols; j++) - get(j,i) = m.get(i,j); - } - -GeneralMatrix::GeneralMatrix(const ConstGeneralMatrix& m, const char* dummy) -: data(m.rows*m.cols), rows(m.cols), cols(m.rows), ld(m.cols) - { - for (int i = 0; i < m.rows; i++) - for (int j = 0; j < m.cols; j++) - get(j,i) = m.get(i,j); - } - - -GeneralMatrix::GeneralMatrix(const GeneralMatrix& m, int i, int j, int nrows, int ncols) -: data(nrows*ncols), rows(nrows), cols(ncols), ld(nrows) - { - copy(m, i, j); - } - -GeneralMatrix::GeneralMatrix(GeneralMatrix& m, int i, int j, int nrows, int ncols) -: data(m.base()+m.ld*j+i, m.ld*(ncols-1)+nrows), rows(nrows), cols(ncols), ld(m.ld) - {} - -GeneralMatrix::GeneralMatrix(const GeneralMatrix& a, const GeneralMatrix& b) -: data(a.rows*b.cols), rows(a.rows), cols(b.cols), ld(a.rows) - { - gemm("N", a, "N", b, 1.0, 0.0); - } - -GeneralMatrix::GeneralMatrix(const GeneralMatrix& a, const GeneralMatrix& b, const char* dum) -: data(a.rows*b.rows), rows(a.rows), cols(b.rows), ld(a.rows) - { - gemm("N", a, "T", b, 1.0, 0.0); - } - -GeneralMatrix::GeneralMatrix(const GeneralMatrix& a, const char* dum, const GeneralMatrix& b) -: data(a.cols*b.cols), rows(a.cols), cols(b.cols), ld(a.cols) - { - gemm("T", a, "N", b, 1.0, 0.0); - } - -GeneralMatrix::GeneralMatrix(const GeneralMatrix& a, const char* dum1, - const GeneralMatrix& b, const char* dum2) - : data(a.cols*b.rows), rows(a.cols), cols(b.rows), ld(a.cols) - { - gemm("T", a, "T", b, 1.0, 0.0); - } - -/* generate new matrix b as subset or whole of matrix a but reordered by -vrows and vcols as Matlab b=a(vrows,vcols) where vectors vrows and vcols start from 1. -It ignores non-positive elements passing zero length vector is equivalent -to Matlab operator ":" = all elements of that dimension in its order */ -GeneralMatrix::GeneralMatrix(const GeneralMatrix& a, const vector&vrows, const vector&vcols) - { - int nrows=0, ncols=0; - if (vrows.size()==0 && vcols.size()==0) - { - *this=a; - return; - } - else - { - if (vrows.size()==0) - nrows=a.numRows(); - else - { - for (int i=0;i0) - nrows++; - else - throw SYLV_MES_EXCEPTION("Non-positive indices in construction by vector."); - } - } - if (nrows>a.numRows()) - throw SYLV_MES_EXCEPTION("Wrong dimensions for construction by vector."); - - if (vcols.size()==0) - ncols=a.numCols(); - else - { - for (int i=0;i0) - ncols++; - else - throw SYLV_MES_EXCEPTION("Non-positive indices in construction by vector."); - } - } - if (ncols>a.numCols()) - throw SYLV_MES_EXCEPTION("Wrong dimensions for construction by vector."); - - data= *(new Vector(nrows*ncols)); - rows=nrows; - cols=ncols; - if(nrows*ncols==0) return; - - for (int i=0;i0 && vcols[j] >0) - get(i,j)=a.get(vrows[i]-1, vcols[j]-1); - } - else if (vrows.size()!=0 && vcols.size()==0) - { - if (vrows[i]>0 ) - get(i,j)=a.get(vrows[i]-1, j); - } - else if (vrows.size()==0 && vcols.size()!=0) - { - if (vcols[j] >0) - get(i,j)=a.get(i, vcols[j]-1); - } - } - } - } - - - -GeneralMatrix::~GeneralMatrix() - { - } - - -/* Matlab element product: this = this .*m */ -void -GeneralMatrix::multElements(const GeneralMatrix& m) - { - if(cols!=m.numCols() || rows!=m.numRows()) - throw SYLV_MES_EXCEPTION("multiply Element porduct: matrices must be same dimension."); - for (int i=0;i numRows() || - j + m.numCols() > numCols()) - throw SYLV_MES_EXCEPTION("Bad submatrix placement, matrix dimensions exceeded."); - - GeneralMatrix tmpsub(*this, i, j, m.numRows(), m.numCols()); - tmpsub.copy(m); - } - -/* this = a*b */ -void GeneralMatrix::mult(const ConstGeneralMatrix& a, const ConstGeneralMatrix& b) - { - gemm("N", a, "N", b, 1.0, 0.0); - } - -/* this = this + scalar*a*b */ -void GeneralMatrix::multAndAdd(const ConstGeneralMatrix& a, const ConstGeneralMatrix& b, - double mult) - { - gemm("N", a, "N", b, mult, 1.0); - } - - -/* this = this + scalar*a*b' */ -void GeneralMatrix::multAndAdd(const ConstGeneralMatrix& a, const ConstGeneralMatrix& b, - const char* dum, double mult) - { - gemm("N", a, "T", b, mult, 1.0); - } - -/* this = this + scalar*a'*b */ -void GeneralMatrix::multAndAdd(const ConstGeneralMatrix& a, const char* dum, - const ConstGeneralMatrix& b, double mult) - { - gemm("T", a, "N", b, mult, 1.0); - } - -/* this = this + scalar*a'*b' */ -void GeneralMatrix::multAndAdd(const ConstGeneralMatrix& a, const char* dum1, - const ConstGeneralMatrix& b, const char* dum2, double mult) - { - gemm("T", a, "T", b, mult, 1.0); - } - -void GeneralMatrix::addOuter(const ConstVector& a, double mult) - { - if (numRows() != numCols()) - throw SYLV_MES_EXCEPTION("Matrix is not square in GeneralMatrix::addOuter."); - if (numRows() != a.length()) - throw SYLV_MES_EXCEPTION("Wrong length of a vector in GeneralMatrix::addOuter."); - - // since BLAS dsyr (symmetric rank 1 update) assumes symmetricity, we do this manually - for (int i = 0; i < numRows(); i++) - for (int j = i; j < numRows(); j++) { - double s = mult*a[i]*a[j]; - get(i,j) = get(i,j) + s; - if (i != j) - get(j,i) = get(j,i) + s; - } - } - - -void GeneralMatrix::multRight(const ConstGeneralMatrix& m) - { - gemm_partial_right("N", m, 1.0, 0.0); - } - -void GeneralMatrix::multLeft(const ConstGeneralMatrix& m) - { - gemm_partial_left("N", m, 1.0, 0.0); - } - -void GeneralMatrix::multRightTrans(const ConstGeneralMatrix& m) - { - gemm_partial_right("T", m, 1.0, 0.0); - } - -void GeneralMatrix::multLeftTrans(const ConstGeneralMatrix& m) - { - gemm_partial_left("T", m, 1.0, 0.0); - } - - -/* this = this * A^(-1) */ -void -GeneralMatrix::multInvRight( GeneralMatrix&A) - { - // check or allocate tmp space for Transpose *this - /** - if (tmpGMp) - { - if (tmpGMp->numCols()!=rows || tmpGMp->numRows()!=cols) - delete (tmpGMp); - } - if (!tmpGMp) - ********/ - tmpGMp= new GeneralMatrix(cols,rows); // allocate space only once if and when needed! - - // tmpGMp=(*this)' i.e. Transpose (*this) - for (int i = 0; i < rows; i++) - for (int j = 0; j < cols; j++) - tmpGMp->get(j,i) = get(i,j); - // check A and this suiability - const int mcols=A.numCols(); - if (A.numRows() != mcols) { - throw SYLV_MES_EXCEPTION("The matrix is not square for inversion."); - } - if (cols != A.numRows()) { - throw SYLV_MES_EXCEPTION("Wrong dimensions for matrix inverse mutliply."); - } - - if (rows > 0) - { - /* out =tmpGMp out = inv(A')*(*this)' = inv(A')*(*tmpGMp) */ - int* ipiv = new int[cols]; - int info; - const int mld=A.getLD(); - LAPACK_dgetrf(&cols, &cols, A.base(), &cols, ipiv, &info); - LAPACK_dgetrs("T", &cols, &mcols, A.base(), &cols, ipiv, tmpGMp->base(), - &mld, &info); - delete [] ipiv; - // *this= Transpose(tmpGMp out) - for (int i = 0; i < rows; i++) - for (int j = 0; j < cols; j++) - get(i,j) = tmpGMp->get(j,i); - } - delete tmpGMp; - } - - -// here we must be careful for ld -void GeneralMatrix::zeros() - { - if (ld == rows) - data.zeros(); - else { - for (int i = 0; i < rows; i++) - for (int j = 0; j < cols; j++) - get(i,j) = 0; - } - } - -void GeneralMatrix::unit() - { - for (int i = 0; i < rows; i++) - for (int j = 0; j < cols; j++) - if (i == j) - get(i,j) = 1.0; - else - get(i,j) = 0.0; - } - -void GeneralMatrix::nans() - { - for (int i = 0; i < rows; i++) - for (int j = 0; j < cols; j++) - get(i,j) = std::numeric_limits::quiet_NaN(); - } - -void GeneralMatrix::infs() - { - for (int i = 0; i < rows; i++) - for (int j = 0; j < cols; j++) - get(i,j) = std::numeric_limits::infinity(); - } - - -// here we must be careful for ld -void GeneralMatrix::mult(double a) - { - if (ld == rows) - data.mult(a); - else { - for (int i = 0; i < rows; i++) - for (int j = 0; j < cols; j++) - get(i,j) *= a; - } - } - -// here we must be careful for ld -void GeneralMatrix::add(double a, const ConstGeneralMatrix& m) - { - if (m.numRows() != rows || m.numCols() != cols) - throw SYLV_MES_EXCEPTION("Matrix has different size in GeneralMatrix::add."); - - if (ld == rows && m.ld == m.rows) - data.add(a, m.data); - else { - for (int i = 0; i < rows; i++) - for (int j = 0; j < cols; j++) - get(i,j) += a*m.get(i,j); - } - } - -void GeneralMatrix::add(double a, const ConstGeneralMatrix& m, const char* dum) - { - if (m.numRows() != cols || m.numCols() != rows) - throw SYLV_MES_EXCEPTION("Matrix has different size in GeneralMatrix::add."); - - for (int i = 0; i < rows; i++) - for (int j = 0; j < cols; j++) - get(i,j) += a*m.get(j,i); - } - - -bool GeneralMatrix::isDiff(const GeneralMatrix& m, const double tol=0.0)const - { - if (m.numRows() != rows || m.numCols() != cols) - throw SYLV_MES_EXCEPTION("Matrix has different size in GeneralMatrix::isDiff."); - for (int i = 0; i < rows; i++) - for (int j = 0; j < cols; j++) - if (fabs(get(i,j) - m.get(i,j))>tol) - return true; - return false; - } - -bool GeneralMatrix::isDiffSym(const GeneralMatrix& m, const double tol=0.0)const - { - if (m.numRows() != rows || m.numCols() != cols || m.numRows() != cols || m.numCols() != rows) - throw SYLV_MES_EXCEPTION("Matrix has different size or not square in GeneralMatrix::isDiffSym."); - for (int i = 0; i < cols; i++) - for (int j = 0; i+j < cols ; j++) // traverse the upper triangle only - if (fabs(get(j,j+i) - m.get(j,j+i))>tol) // along diagonals where higher changes occur - return true; - return false; - } - - -/* x = scalar(a)*x + scalar(b)*this*d */ -void GeneralMatrix::multVec(double a, Vector& x, double b, const ConstVector& d) const - { - if (x.length() != rows || cols != d.length()) { - throw SYLV_MES_EXCEPTION("Wrong dimensions for vector multiply."); - } - if (rows > 0) { - int mm = rows; - int nn = cols; - double alpha = b; - int lda = ld; - int incx = d.skip(); - double beta = a; - int incy = x.skip(); - BLAS_dgemv("N", &mm, &nn, &alpha, data.base(), &lda, d.base(), &incx, - &beta, x.base(), &incy); - } - - } - -void GeneralMatrix::copy(const ConstGeneralMatrix& m, int ioff, int joff) - { - for (int i = 0; i < rows; i++) - for (int j = 0; j < cols; j++) - get(i,j) = m.get(i+ioff,j+joff); - } - - -void -GeneralMatrix::copy(const ConstGeneralMatrix& m) - { - memcpy(data.base() ,m.getData().base() ,m.numCols()*m.numRows()*sizeof(double)); - }; - -void -GeneralMatrix::copy(const GeneralMatrix& m) - { - memcpy(data.base(),m.getData().base(),m.numCols()*m.numRows()*sizeof(double)); - }; - -void -GeneralMatrix::copyColumns(const GeneralMatrix& m, int istart, int iend, int ito) - { - if ((rows!=m.numRows())|| istart m.numCols()-1 || iend> m.numCols()-1 - || ito> cols-1 ) - throw SYLV_MES_EXCEPTION("Wrong dimensions for copying matrix columns."); - - memcpy(data.base()+ito*rows*sizeof(double) - ,m.getData().base()+istart*rows*sizeof(double) - ,(iend-istart+1)*rows*sizeof(double)); - }; - - - -/* emulates Matlab command A(a,b)=B(c,d) where a,b,c,d are vectors or ":")*/ -void -GeneralMatrix::AssignByVectors(GeneralMatrix& a, const vector& vToRows, const vector& vToCols - , const GeneralMatrix& b, const vector& vrows, const vector& vcols) - { - int nrows=0, ncols=0, tonrows=0, toncols=0; - const vector *vpToCols=0, *vpToRows=0, *vpRows=0, *vpCols=0; - vector *tmpvpToCols=0, *tmpvpToRows=0, *tmpvpRows=0, *tmpvpCols=0; - - if (vToRows.size()==0 && vToCols.size()==0 &&vrows.size()==0 && vcols.size()==0) - a=b; - else - { - if (vToRows.size()==0) - { - tonrows=a.numRows(); -// vpToRows=new vector(tonrows); - tmpvpToRows=new vector(tonrows); - for (int i=0;i*)tmpvpToRows; - } - else - { - for (int i=0;i0) - tonrows++; - else - throw SYLV_MES_EXCEPTION("Non-positive indices in assignment by vector."); - } - vpToRows=&vToRows; - } - if (tonrows>a.numRows()) - throw SYLV_MES_EXCEPTION("Wrong dimensions for assignment by vector."); - - if (vToCols.size()==0) - { - toncols=a.numCols(); - tmpvpToCols=new vector(toncols); - for (int i=0;i*)tmpvpToCols; - } - else - { - for (int i=0;i0) - toncols++; - else - throw SYLV_MES_EXCEPTION("Non-positive indices in assignment by vector."); - } - vpToCols=&vToCols; - } - if (toncols>a.numCols()) - throw SYLV_MES_EXCEPTION("Wrong dimensions for assignment by vector."); - - if (vrows.size()==0) - { - nrows=b.numRows(); - tmpvpRows=new vector(nrows); - for (int i=0;i*)tmpvpRows; - } - else - { - for (int i=0;i0) - nrows++; - else - throw SYLV_MES_EXCEPTION("Non-positive indices in assignment by vector."); - } - vpRows=&vrows; - } - if (nrows>b.numRows()) - throw SYLV_MES_EXCEPTION("Wrong dimensions for assignment by vector."); - - if (vcols.size()==0) - { - ncols=b.numCols(); - tmpvpCols=new vector(ncols); - for (int i=0;i*)tmpvpCols; - } - else - { - for (int i=0;i0) - ncols++; - else - throw SYLV_MES_EXCEPTION("Non-positive indices in assignment by vector."); - } - vpCols=&vcols; - } - if (ncols>b.numCols()) - throw SYLV_MES_EXCEPTION("Wrong dimensions for assignment by vector."); - - if (tonrows!=nrows || toncols!=ncols) - throw SYLV_MES_EXCEPTION("Wrong indices dimensions for assignment by vector."); - - if(!(nrows*ncols==0 || tonrows*toncols==0)) - { - for (int i=0;i 0 && ldb > 0 && ldc > 0) { - BLAS_dgemm(transa, transb, &m, &n, &k, &alpha, a.data.base(), &lda, - b.data.base(), &ldb, &beta, data.base(), &ldc); - } else if (numRows()*numCols() > 0) { - if (beta == 0.0) - zeros(); - else - mult(beta); - } - } - -void GeneralMatrix::gemm_partial_left(const char* trans, const ConstGeneralMatrix& m, - double alpha, double beta) - { - int icol; - for (icol = 0; icol + md_length < cols; icol += md_length) { - GeneralMatrix incopy((const GeneralMatrix&)*this, 0, icol, rows, md_length); - GeneralMatrix inplace((GeneralMatrix&)*this, 0, icol, rows, md_length); - inplace.gemm(trans, m, "N", ConstGeneralMatrix(incopy), alpha, beta); - } - if (cols > icol) { - GeneralMatrix incopy((const GeneralMatrix&)*this, 0, icol, rows, cols - icol); - GeneralMatrix inplace((GeneralMatrix&)*this, 0, icol, rows, cols - icol); - inplace.gemm(trans, m, "N", ConstGeneralMatrix(incopy), alpha, beta); - } - } - -void GeneralMatrix::gemm_partial_right(const char* trans, const ConstGeneralMatrix& m, - double alpha, double beta) - { - int irow; - for (irow = 0; irow + md_length < rows; irow += md_length) { - GeneralMatrix incopy((const GeneralMatrix&)*this, irow, 0, md_length, cols); - GeneralMatrix inplace((GeneralMatrix&)*this, irow, 0, md_length, cols); - inplace.gemm("N", ConstGeneralMatrix(incopy), trans, m, alpha, beta); - } - if (rows > irow) { - GeneralMatrix incopy((const GeneralMatrix&)*this, irow, 0, rows - irow, cols); - GeneralMatrix inplace((GeneralMatrix&)*this, irow, 0, rows - irow, cols); - inplace.gemm("N", ConstGeneralMatrix(incopy), trans, m, alpha, beta); - } - } - -ConstGeneralMatrix::ConstGeneralMatrix(const GeneralMatrix& m, int i, int j, int nrows, int ncols) -: data(m.getData(), j*m.getLD()+i, (ncols-1)*m.getLD()+nrows), rows(nrows), cols(ncols), ld(m.getLD()) - { - // can check that the submatirx is fully in the matrix - } - -ConstGeneralMatrix::ConstGeneralMatrix(const ConstGeneralMatrix& m, int i, int j, int nrows, int ncols) -: data(m.getData(), j*m.getLD()+i, (ncols-1)*m.getLD()+nrows), rows(nrows), cols(ncols), ld(m.getLD()) - { - // can check that the submatirx is fully in the matrix - } - - -ConstGeneralMatrix::ConstGeneralMatrix(const GeneralMatrix& m) -: data(m.data), rows(m.rows), cols(m.cols), ld(m.ld) {} - - -double ConstGeneralMatrix::getNormInf() const - { - double norm = 0.0; - for (int i = 0; i < numRows(); i++) { - ConstVector rowi(data.base()+i, ld, cols); - double normi = rowi.getNorm1(); - if (norm < normi) - norm = normi; - } - return norm; - } - -double ConstGeneralMatrix::getNorm1() const - { - double norm = 0.0; - for (int j = 0; j < numCols(); j++) { - ConstVector colj(data.base()+ld*j, 1, rows); - double normj = colj.getNorm1(); - if (norm < normj) - norm = normj; - } - return norm; - } - -/* x = scalar(a)*x + scalar(b)*this*d */ -void ConstGeneralMatrix::multVec(double a, Vector& x, double b, const ConstVector& d) const - { - if (x.length() != rows || cols != d.length()) { - throw SYLV_MES_EXCEPTION("Wrong dimensions for vector multiply."); - } - if (rows > 0) { - int mm = rows; - int nn = cols; - double alpha = b; - int lda = ld; - int incx = d.skip(); - double beta = a; - int incy = x.skip(); - BLAS_dgemv("N", &mm, &nn, &alpha, data.base(), &lda, d.base(), &incx, - &beta, x.base(), &incy); - } - - } - -void ConstGeneralMatrix::multVecTrans(double a, Vector& x, double b, - const ConstVector& d) const - { - if (x.length() != cols || rows != d.length()) { - throw SYLV_MES_EXCEPTION("Wrong dimensions for vector multiply."); - } - if (rows > 0) { - int mm = rows; - int nn = cols; - double alpha = b; - int lda = rows; - int incx = d.skip(); - double beta = a; - int incy = x.skip(); - BLAS_dgemv("T", &mm, &nn, &alpha, data.base(), &lda, d.base(), &incx, - &beta, x.base(), &incy); - } - } - -/* m = inv(this)*m */ -void ConstGeneralMatrix::multInvLeft(const char* trans, int mrows, int mcols, int mld, double* d) const - { - if (rows != cols) { - throw SYLV_MES_EXCEPTION("The matrix is not square for inversion."); - } - if (cols != mrows) { - throw SYLV_MES_EXCEPTION("Wrong dimensions for matrix inverse mutliply."); - } - - if (rows > 0) { - GeneralMatrix inv(*this); - int* ipiv = new int[rows]; - int info; - LAPACK_dgetrf(&rows, &rows, inv.getData().base(), &rows, ipiv, &info); - LAPACK_dgetrs(trans, &rows, &mcols, inv.base(), &rows, ipiv, d, - &mld, &info); - delete [] ipiv; - } - } - -/* m = inv(this)*m */ -void ConstGeneralMatrix::multInvLeft(GeneralMatrix& m) const - { - multInvLeft("N", m.numRows(), m.numCols(), m.getLD(), m.getData().base()); - } - -/* m = inv(this')*m */ -void ConstGeneralMatrix::multInvLeftTrans(GeneralMatrix& m) const - { - multInvLeft("T", m.numRows(), m.numCols(), m.getLD(), m.getData().base()); - } - -/* d = inv(this)*d */ -void ConstGeneralMatrix::multInvLeft(Vector& d) const - { - if (d.skip() != 1) { - throw SYLV_MES_EXCEPTION("Skip!=1 not implemented in ConstGeneralMatrix::multInvLeft(Vector&)"); - } - - multInvLeft("N", d.length(), 1, d.length(), d.base()); - } - -/* d = inv(this')*d */ -void ConstGeneralMatrix::multInvLeftTrans(Vector& d) const - { - if (d.skip() != 1) { - throw SYLV_MES_EXCEPTION("Skip!=1 not implemented in ConstGeneralMatrix::multInvLeft(Vector&)"); - } - - multInvLeft("T", d.length(), 1, d.length(), d.base()); - } - - -bool ConstGeneralMatrix::isFinite() const - { - for (int i = 0; i < numRows(); i++) - for (int j = 0; j < numCols(); j++) - if (! std::isfinite(get(i,j))) - return false; - return true; - } - -bool ConstGeneralMatrix::isZero() const - { - for (int i = 0; i < numRows(); i++) - for (int j = 0; j < numCols(); j++) - if (get(i,j) != 0.0) - return false; - return true; - } - -void ConstGeneralMatrix::print() const - { - printf("rows=%d, cols=%d\n",rows, cols); - for (int i = 0; i < rows; i++) { - printf("row %d:\n",i); - for (int j = 0; j < cols; j++) { - printf("%6.3g ",get(i,j)); - } - printf("\n"); - } - } diff --git a/mex/sources/kalman/sylv/cc/GeneralMatrix.h b/mex/sources/kalman/sylv/cc/GeneralMatrix.h deleted file mode 100644 index 4afd0723d..000000000 --- a/mex/sources/kalman/sylv/cc/GeneralMatrix.h +++ /dev/null @@ -1,355 +0,0 @@ -/* $Header: /var/lib/cvs/dynare_cpp/sylv/cc/GeneralMatrix.h,v 1.3 2004/11/24 20:41:59 kamenik Exp $ */ - -/* Tag $Name: $ */ - -#ifndef GENERAL_MATRIX_H -#define GENERAL_MATRIX_H - -#include "Vector.h" -#include -using namespace std; -//#define nullVec (const vector(0)) -const vectornullVec(0); - -class GeneralMatrix; - -class ConstGeneralMatrix { - friend class GeneralMatrix; - protected: - ConstVector data; - int rows; - int cols; - int ld; - public: - ConstGeneralMatrix(const double* d, int m, int n) - : data(d, m*n), rows(m), cols(n), ld(m) {} - ConstGeneralMatrix(const GeneralMatrix& m); - ConstGeneralMatrix(const GeneralMatrix& m, int i, int j, int nrows, int ncols); - ConstGeneralMatrix(const ConstGeneralMatrix& m, int i, int j, int nrows, int ncols); - virtual ~ConstGeneralMatrix() {} - - const double& get(int i, int j) const - {return data[j*ld+i];} - int numRows() const {return rows;} - int numCols() const {return cols;} - int getLD() const {return ld;} - const double* base() const {return data.base();} - const ConstVector& getData() const {return data;} - - double getNormInf() const; - double getNorm1() const; - /* x = scalar(a)*x + scalar(b)*this*d */ - void multVec(double a, Vector& x, double b, const ConstVector& d) const; - /* x = scalar(a)*x + scalar(b)*this'*d */ - void multVecTrans(double a, Vector& x, double b, const ConstVector& d) const; - /* x = x + this*d */ - void multaVec(Vector& x, const ConstVector& d) const - {multVec(1.0, x, 1.0, d);} - /* x = x + this'*d */ - void multaVecTrans(Vector& x, const ConstVector& d) const - {multVecTrans(1.0, x, 1.0, d);} - /* x = x - this*d */ - void multsVec(Vector& x, const ConstVector& d) const - {multVec(1.0, x, -1.0, d);} - /* x = x - this'*d */ - void multsVecTrans(Vector& x, const ConstVector& d) const - {multVecTrans(1.0, x, -1.0, d);} - /* m = inv(this)*m */ - void multInvLeft(GeneralMatrix& m) const; - /* m = inv(this')*m */ - void multInvLeftTrans(GeneralMatrix& m) const; - /* d = inv(this)*d */ - void multInvLeft(Vector& d) const; - /* d = inv(this')*d */ - void multInvLeftTrans(Vector& d) const; - - bool isFinite() const; - /** Returns true of the matrix is exactly zero. */ - bool isZero() const; - virtual void print() const; - protected: - void multInvLeft(const char* trans, int mrows, int mcols, int mld, double* d) const; - }; - - -class GeneralMatrix { - friend class ConstGeneralMatrix; - protected: - Vector data; - int rows; - int cols; - int ld; - GeneralMatrix * tmpGMp; -public: - GeneralMatrix(int m, int n) - : data(m*n), rows(m), cols(n), ld(m) {} - GeneralMatrix(const double* d, int m, int n) - : data(d, m*n), rows(m), cols(n), ld(m) {} - GeneralMatrix(double* d, int m, int n) - : data(d, m*n), rows(m), cols(n), ld(m) {} - GeneralMatrix(const GeneralMatrix& m); - GeneralMatrix(const ConstGeneralMatrix& m); - GeneralMatrix(const GeneralMatrix&m, const char* dummy); // transpose - GeneralMatrix(const ConstGeneralMatrix&m, const char* dummy); // transpose - GeneralMatrix(const GeneralMatrix& m, int i, int j, int nrows, int ncols); - GeneralMatrix(GeneralMatrix& m, int i, int j, int nrows, int ncols); - /* this = a*b */ - GeneralMatrix(const GeneralMatrix& a, const GeneralMatrix& b); - /* this = a*b' */ - GeneralMatrix(const GeneralMatrix& a, const GeneralMatrix& b, const char* dum); - /* this = a'*b */ - GeneralMatrix(const GeneralMatrix& a, const char* dum, const GeneralMatrix& b); - /* this = a'*b */ - GeneralMatrix(const GeneralMatrix& a, const char* dum1, - const GeneralMatrix& b, const char* dum2); - - /* generate new matrix b as subset or whole of matrix a but - reordered by vrows and vcols as Matlab b=a(vrows,vcols) - ignores non-positive elements and passing zero length vector is equivalent to - Matlab operator ":" = all elements of that dimension in its order */ - - GeneralMatrix(const GeneralMatrix& a, const vector& vrows, const vector& vcols); - GeneralMatrix(const ConstGeneralMatrix& a, const vector &vrows, const vector &vcols) - {GeneralMatrix(GeneralMatrix(a), vrows, vcols);}; - - virtual ~GeneralMatrix(); - const GeneralMatrix& operator=(const GeneralMatrix& m) - { - if (this!=&m) - { - if (rows==m.rows && cols==m.cols && ld==m.ld && data.base()!=m.data.base() ) - copy(m); - else - { - data=m.data; rows=m.rows; cols=m.cols; ld=m.ld; - } - } - return *this; - } - - /* emulate Matlab repmat: new matrix = multv*multh*this */ - GeneralMatrix& repmat(int multv, int multh); - - const double& get(int i, int j) const - {return data[j*ld+i];} - double& get(int i, int j) - {return data[j*ld+i];} - int numRows() const {return rows;} - int numCols() const {return cols;} - int getLD() const {return ld;} - double* base() {return data.base();} - const double* base() const {return data.base();} - Vector& getData() {return data;} - const Vector& getData() const {return data;} - - double getNormInf() const - {return ConstGeneralMatrix(*this).getNormInf();} - double getNorm1() const - {return ConstGeneralMatrix(*this).getNorm1();} - - /* place matrix m to the position (i,j) */ - void place(const ConstGeneralMatrix& m, int i, int j); - void place(const GeneralMatrix& m, int i, int j) - {place(ConstGeneralMatrix(m), i, j);} - - /* this = a*b */ - void mult(const ConstGeneralMatrix& a, const ConstGeneralMatrix& b); - void mult(const GeneralMatrix& a, const GeneralMatrix& b) - {mult(ConstGeneralMatrix(a), ConstGeneralMatrix(b));} - - /* this = this + scalar*a*b */ - void multAndAdd(const ConstGeneralMatrix& a, const ConstGeneralMatrix& b, - double mult=1.0); - void multAndAdd(const GeneralMatrix& a, const GeneralMatrix& b, - double mult=1.0) - {multAndAdd(ConstGeneralMatrix(a), ConstGeneralMatrix(b), mult);} - - /* this = this + scalar*a*b' */ - void multAndAdd(const ConstGeneralMatrix& a, const ConstGeneralMatrix& b, - const char* dum, double mult=1.0); - void multAndAdd(const GeneralMatrix& a, const GeneralMatrix& b, - const char* dum, double mult=1.0) - {multAndAdd(ConstGeneralMatrix(a), ConstGeneralMatrix(b), dum, mult);} - - /* this = this + scalar*a'*b */ - void multAndAdd(const ConstGeneralMatrix& a, const char* dum, const ConstGeneralMatrix& b, - double mult=1.0); - void multAndAdd(const GeneralMatrix& a, const char* dum, const GeneralMatrix& b, - double mult=1.0) - {multAndAdd(ConstGeneralMatrix(a), dum, ConstGeneralMatrix(b), mult);} - - /* this = this + scalar*a'*b' */ - void multAndAdd(const ConstGeneralMatrix& a, const char* dum1, - const ConstGeneralMatrix& b, const char* dum2, double mult=1.0); - void multAndAdd(const GeneralMatrix& a, const char* dum1, - const GeneralMatrix& b, const char* dum2, double mult=1.0) - {multAndAdd(ConstGeneralMatrix(a), dum1, ConstGeneralMatrix(b),dum2, mult);} - - /* this = this + scalar*a*a' */ - void addOuter(const ConstVector& a, double mult=1.0); - void addOuter(const Vector& a, double mult=1.0) - {addOuter(ConstVector(a), mult);} - - /* this = this * m */ - void multRight(const ConstGeneralMatrix& m); - void multRight(const GeneralMatrix& m) - {multRight(ConstGeneralMatrix(m));} - - /* this = m * this */ - void multLeft(const ConstGeneralMatrix& m); - void multLeft(const GeneralMatrix& m) - {multLeft(ConstGeneralMatrix(m));} - - /* this = this * m' */ - void multRightTrans(const ConstGeneralMatrix& m); - void multRightTrans(const GeneralMatrix& m) - {multRightTrans(ConstGeneralMatrix(m));} - - /* this = m' * this */ - void multLeftTrans(const ConstGeneralMatrix& m); - void multLeftTrans(const GeneralMatrix& m) - {multLeftTrans(ConstGeneralMatrix(m));} - - /* Matlab element product: this = this .*m */ - void multElements(const GeneralMatrix& m); - void multElements(const ConstGeneralMatrix& m) - {multElements(GeneralMatrix(m));} - - - /* this = this * m^(-1) */ - void multInvRight(GeneralMatrix&m); - - /* x = scalar(a)*x + scalar(b)*this*d */ - void multVec(double a, Vector& x, double b, const ConstVector& d) const; -// {ConstGeneralMatrix(*this).multVec(a, x, b, d);} - - /* x = scalar(a)*x + scalar(b)*this'*d */ - void multVecTrans(double a, Vector& x, double b, const ConstVector& d) const - {ConstGeneralMatrix(*this).multVecTrans(a, x, b, d);} - - /* x = x + this*d */ - void multaVec(Vector& x, const ConstVector& d) const - {ConstGeneralMatrix(*this).multaVec(x, d);} - - /* x = x + this'*d */ - void multaVecTrans(Vector& x, const ConstVector& d) const - {ConstGeneralMatrix(*this).multaVecTrans(x, d);} - - /* x = x - this*d */ - void multsVec(Vector& x, const ConstVector& d) const - {ConstGeneralMatrix(*this).multsVec(x, d);} - - /* x = x - this'*d */ - void multsVecTrans(Vector& x, const ConstVector& d) const - {ConstGeneralMatrix(*this).multsVecTrans(x, d);} - - /* this = zero */ - void zeros(); - - /** this = unit (on main diagonal) */ - void unit(); - - /* this = NaN */ - void nans(); - - /* this = Inf */ - void infs(); - - /* this = scalar*this */ - void mult(double a); - - /* this = this + scalar*m */ - void add(double a, const ConstGeneralMatrix& m); - void add(double a, const GeneralMatrix& m) - {add(a, ConstGeneralMatrix(m));} - - /* this = this + scalar*m' */ - void add(double a, const ConstGeneralMatrix& m, const char* dum); - void add(double a, const GeneralMatrix& m, const char* dum) - {add(a, ConstGeneralMatrix(m), dum);} - - /* Returns true if this and m matrices are different for more than tolerance tol */ - bool isDiff(const GeneralMatrix& m, const double tol)const; - bool isDiffSym(const GeneralMatrix& m, const double tol)const; - bool isDiffUpprTriang(const GeneralMatrix& m, const double tol=0.0)const - {return isDiffSym(m, tol);} - - bool isFinite() const - {return (ConstGeneralMatrix(*this)).isFinite();} - - bool isZero() const - {return (ConstGeneralMatrix(*this)).isZero();} - - virtual void print() const - {ConstGeneralMatrix(*this).print();} - - void copyColumns(const GeneralMatrix& m, int istart, int iend, int ito); - void copyColumns(const ConstGeneralMatrix& m, int istart, int iend, int ito) - {copyColumns(GeneralMatrix( m), istart, iend, ito);}; - - - /* emulates Matlab command A(a,b)=B(c,d) where a,b,c,d are Matlab index vectors starting from 1 or ":") */ - static void AssignByVectors(GeneralMatrix& a, const vector& vToRows, const vector& vToCols - , const GeneralMatrix& b, const vector& vrows, const vector& vcols); - static void AssignByVectors(GeneralMatrix& a, const vector& vToRows, const vector& vToCols - , const ConstGeneralMatrix& b, const vector &vrows, const vector &vcols) - {AssignByVectors(a, vToRows, vToCols, GeneralMatrix(b), vrows, vcols);}; - void AssignByVectors(const vector& vToRows, const vector& vToCols - , const GeneralMatrix& b, const vector& vrows, const vector& vcols) - {AssignByVectors( *this, vToRows, vToCols, b, vrows, vcols);}; - void AssignByVectors( const vector& vToRows, const vector& vToCols - , const ConstGeneralMatrix& b, const vector &vrows, const vector &vcols) - {AssignByVectors(*this, vToRows, vToCols, GeneralMatrix(b), vrows, vcols);}; - void AssignByVectors( const GeneralMatrix& b, const vector& vrows, const vector& vcols) - {AssignByVectors( *this, nullVec, nullVec, b, vrows, vcols);}; - void AssignByVectors( const ConstGeneralMatrix& b, const vector &vrows, const vector &vcols) - {AssignByVectors(*this, nullVec, nullVec, GeneralMatrix(b), vrows, vcols);}; - void AssignByVectors(const vector& vToRows, const vector& vToCols, const GeneralMatrix& b) - {AssignByVectors( *this, vToRows, vToCols, b, nullVec, nullVec);}; - void AssignByVectors( const vector& vToRows, const vector& vToCols, const ConstGeneralMatrix& b) - {AssignByVectors(*this, vToRows, vToCols, GeneralMatrix(b), nullVec, nullVec);}; - - -private: - void copy(const ConstGeneralMatrix& m, int ioff , int joff ); - void copy(const GeneralMatrix& m, int ioff , int joff ) - {copy(ConstGeneralMatrix(m), ioff, joff);} - void copy(const ConstGeneralMatrix& m); - void copy(const GeneralMatrix& m); - void gemm(const char* transa, const ConstGeneralMatrix& a, - const char* transb, const ConstGeneralMatrix& b, - double alpha, double beta); - void gemm(const char* transa, const GeneralMatrix& a, - const char* transb, const GeneralMatrix& b, - double alpha, double beta) - {gemm(transa, ConstGeneralMatrix(a), transb, ConstGeneralMatrix(b), - alpha, beta);} - - /* this = this * op(m) (without whole copy of this) */ - void gemm_partial_right(const char* trans, const ConstGeneralMatrix& m, - double alpha, double beta); - void gemm_partial_right(const char* trans, const GeneralMatrix& m, - double alpha, double beta) - {gemm_partial_right(trans, ConstGeneralMatrix(m), alpha, beta);} - - /* this = op(m) *this (without whole copy of this) */ - void gemm_partial_left(const char* trans, const ConstGeneralMatrix& m, - double alpha, double beta); - void gemm_partial_left(const char* trans, const GeneralMatrix& m, - double alpha, double beta) - {gemm_partial_left(trans, ConstGeneralMatrix(m), alpha, beta);} - - /* number of rows/columns for copy used in gemm_partial_* */ - static int md_length; -}; - - - - - -#endif /* GENERAL_MATRIX_H */ - - -// Local Variables: -// mode:C++ -// End: diff --git a/mex/sources/kalman/sylv/cc/Makefile b/mex/sources/kalman/sylv/cc/Makefile deleted file mode 100644 index 97edb6a6b..000000000 --- a/mex/sources/kalman/sylv/cc/Makefile +++ /dev/null @@ -1,41 +0,0 @@ -# $Header: /var/lib/cvs/dynare_cpp/sylv/cc/Makefile,v 1.4 2005/01/18 21:28:26 kamenik Exp $ - -# Tag $Name: $ - -CC_FLAGS := -Wall -Winline -fpic -I../testing -I../cc -DMATLAB \ - $(CC_INCLUDE_PATH) -I$(MATLAB)/extern/include #-pg - -LDFLAGS = -Wl,-L$(MATLAB_PATH)/extern/lib/win32/microsoft/ \ - -Wl,-llibmex -Wl,-llibmx -Wl,-llibmwlapack -Wl,-llibdflapack \ - -lg2c -lmingw32 -lstdc++ - -LD_LIBS=$(LDFLAGS) - -ifeq ($(DEBUG),yes) -# CC_FLAGS := $(CC_FLAGS) -g -DTL_DEBUG=2 - CC_FLAGS := $(CC_FLAGS) -g -DPOSIX_THREADS -else - CC_FLAGS := $(CC_FLAGS) -O3 -endif - -ifeq ($(OS),Windows_NT) - CC_FLAGS := -mno-cygwin -mthreads $(CC_FLAGS) -endif - - -objects := $(patsubst %.cpp,%.o,$(wildcard *.cpp)) -headers := $(wildcard *.h) - -all: $(objects) - -sylvester.a: $(objects) - ar cr sylvester.a $(objects) - ranlib sylvester.a - -clear: - rm -f *.o - rm -f sylvester.a - -%.o : %.cpp $(headers) - $(CC) $(CC_FLAGS) $(EXTERN_DEFS) -c $*.cpp - diff --git a/mex/sources/kalman/sylv/cc/SylvException.cpp b/mex/sources/kalman/sylv/cc/SylvException.cpp deleted file mode 100644 index 5d5826f88..000000000 --- a/mex/sources/kalman/sylv/cc/SylvException.cpp +++ /dev/null @@ -1,69 +0,0 @@ -/* $Header: /var/lib/cvs/dynare_cpp/sylv/cc/SylvException.cpp,v 1.2 2004/10/01 10:30:40 kamenik Exp $ */ - -/* Tag $Name: $ */ - -#include "SylvException.h" - -#include -#include - -SylvException::SylvException(const char* f, int l, const SylvException* s) -{ - strcpy(file,f); - line = l; - source = s; -} - -SylvException::~SylvException() -{ - if (source != NULL) { - delete source; - } -} - -void SylvException::printMessage() const -{ - char mes[1500]; - mes[0] = '\0'; - printMessage(mes, 1499); - printf(mes); -} - -int SylvException::printMessage(char* str, int maxlen) const -{ - int remain = maxlen; - if (source != NULL) { - remain = source->printMessage(str, maxlen); - } - char aux[100]; - sprintf(aux, "From %s:%d\n", file, line); - int newremain = remain - strlen(aux); - if (newremain < 0) { - aux[remain] = '\0'; - newremain = 0; - } - strcat(str, aux); - return newremain; -} - -SylvExceptionMessage::SylvExceptionMessage(const char* f, int i, - const char* mes) - : SylvException(f,i,NULL) -{ - strcpy(message,mes); -} - -int SylvExceptionMessage::printMessage(char* str, int maxlen) const -{ - char aux[600]; - sprintf(aux, "At %s:%d:%s\n", file, line, message); - int newremain = maxlen - strlen(aux); - if (newremain < 0) { - aux[maxlen] = '\0'; - newremain = 0; - } - strcat(str, aux); - return newremain; -} - - diff --git a/mex/sources/kalman/sylv/cc/SylvException.h b/mex/sources/kalman/sylv/cc/SylvException.h deleted file mode 100644 index f3c22338a..000000000 --- a/mex/sources/kalman/sylv/cc/SylvException.h +++ /dev/null @@ -1,39 +0,0 @@ -/* $Header: /var/lib/cvs/dynare_cpp/sylv/cc/SylvException.h,v 1.1.1.1 2004/06/04 13:00:44 kamenik Exp $ */ - -/* Tag $Name: $ */ - -#ifndef SYLV_EXCEPTION_H -#define SYLV_EXCEPTION_H - -#include "SylvMemory.h" - - -class SylvException : public MallocAllocator { -protected: - char file[50]; - int line; - const SylvException* source; -public: - SylvException(const char* f, int l, const SylvException* s); - virtual ~SylvException(); - virtual int printMessage(char* str, int maxlen) const; - void printMessage() const; -}; - -class SylvExceptionMessage : public SylvException { - char message[500]; -public: - SylvExceptionMessage(const char* f, int l, const char* mes); - virtual int printMessage(char* str, int maxlen) const; -}; - -// define macros: -#define SYLV_EXCEPTION(exc) (SylvException(__FILE__, __LINE__, exc)) -#define SYLV_MES_EXCEPTION(mes) (SylvExceptionMessage(__FILE__, __LINE__, mes)) - -#endif /* SYLV_EXCEPTION_H */ - - -// Local Variables: -// mode:C++ -// End: diff --git a/mex/sources/kalman/sylv/cc/SylvMemory.h b/mex/sources/kalman/sylv/cc/SylvMemory.h deleted file mode 100644 index 9f89c06cd..000000000 --- a/mex/sources/kalman/sylv/cc/SylvMemory.h +++ /dev/null @@ -1,63 +0,0 @@ -/* $Header: /var/lib/cvs/dynare_cpp/sylv/cc/SylvMemory.h,v 1.1.1.1 2004/06/04 13:00:49 kamenik Exp $ */ - -/* Tag $Name: $ */ - -#ifndef SYLV_MEMORY_H -#define SYLV_MEMORY_H - -//#include "SylvParams.h" - -#include - -class MallocAllocator { -#ifdef USE_MEMORY_POOL -public: - void* operator new(size_t size); - void* operator new[](size_t size); - void operator delete(void* p); - void operator delete[](void* p); -#endif -}; -/* -#ifdef USE_MEMORY_POOL -void* operator new(size_t size); -void* operator new[](size_t size); -void operator delete(void* p); -void operator delete[](void* p); -#endif - -class SylvMemoryPool { - char* base; - size_t length; - size_t allocated; - bool stack_mode; - SylvMemoryPool(const SylvMemoryPool&); - const SylvMemoryPool& operator=(const SylvMemoryPool&); -public: - SylvMemoryPool(); - ~SylvMemoryPool(); - void init(size_t size); - void* allocate(size_t size); - void free(void* p); - void reset(); - void setStackMode(bool); -}; - -class SylvMemoryDriver { - SylvMemoryDriver(const SylvMemoryDriver&); - const SylvMemoryDriver& operator=(const SylvMemoryDriver&); -public: - SylvMemoryDriver(int num_d, int m, int n, int order); - SylvMemoryDriver(const SylvParams& pars, int num_d, int m, int n, int order); - static void setStackMode(bool); - ~SylvMemoryDriver(); -protected: - void allocate(int num_d, int m, int n, int order); -}; -*/ -#endif /* SYLV_MEMORY_H */ - - -// Local Variables: -// mode:C++ -// End: diff --git a/mex/sources/kalman/sylv/cc/Vector.cpp b/mex/sources/kalman/sylv/cc/Vector.cpp deleted file mode 100644 index adb6bdbe4..000000000 --- a/mex/sources/kalman/sylv/cc/Vector.cpp +++ /dev/null @@ -1,370 +0,0 @@ -/* $Header: /var/lib/cvs/dynare_cpp/sylv/cc/Vector.cpp,v 1.1.1.1 2004/06/04 13:01:13 kamenik Exp $ */ - -/* Tag $Name: $ */ - - -#include "Vector.h" -#include "GeneralMatrix.h" -#include "SylvException.h" -#include "cppblas.h" - -#include -#include -#include -#include -#include -#include - -using namespace std; - -ZeroPad zero_pad; - -Vector::Vector(const ConstVector& v) -: len(v.length()), s(1), data(new double[len]), destroy(true) - { - copy(v.base(), v.skip()); - } - -const Vector& Vector::operator=(const Vector& v) - { - if (this == &v) - return *this; - - if (v.length() != length()) { - throw SYLV_MES_EXCEPTION("Attempt to assign vectors with different lengths."); - } - if (s == v.s && - (data <= v.data && v.data < data+len*s || - v.data <= data && data < v.data+v.len*v.s) && - (data-v.data) % s == 0) { - printf("this destroy=%d, v destroy=%d, data-v.data=%d, len=%d\n", destroy, v.destroy, data-v.data, len); - throw SYLV_MES_EXCEPTION("Attempt to assign overlapping vectors."); - } - copy(v.base(), v.skip()); - return *this; - } - -const Vector& Vector::operator=(const ConstVector& v) - { - if (v.length() != length()) { - throw SYLV_MES_EXCEPTION("Attempt to assign vectors with different lengths."); - } - if (v.skip() == 1 && skip() == 1 && ( - (base() < v.base() + v.length() && base() >= v.base()) || - (base() + length() < v.base() + v.length() && - base() + length() > v.base()))) { - throw SYLV_MES_EXCEPTION("Attempt to assign overlapping vectors."); - } - copy(v.base(), v.skip()); - return *this; - } - -void Vector::copy(const double* d, int inc) - { - int n = length(); - int incy = skip(); - BLAS_dcopy(&n, d, &inc, base(), &incy); - } - -Vector::Vector(Vector& v, int off, int l) -: len(l), s(v.skip()), data(v.base()+off*v.skip()), destroy(false) - { - if (off < 0 || off + length() > v.length()) - throw SYLV_MES_EXCEPTION("Subvector not contained in supvector."); - } - -Vector::Vector(const Vector& v, int off, int l) -: len(l), s(1), data(new double[len]), destroy(true) - { - if (off < 0 || off + length() > v.length()) - throw SYLV_MES_EXCEPTION("Subvector not contained in supvector."); - copy(v.base()+off*v.skip(), v.skip()); - } - -Vector::Vector(GeneralMatrix& m, int col) -: len(m.numRows()), s(1), data(&(m.get(0, col))), destroy(false) - { - } - -Vector::Vector(int row, GeneralMatrix& m) -: len(m.numCols()), s(m.getLD()), data(&(m.get(row, 0))), destroy(false) - { - } - -bool Vector::operator==(const Vector& y) const - { - return ConstVector(*this) == y; - } - -bool Vector::operator!=(const Vector& y) const - { - return ConstVector(*this) != y; - } - -bool Vector::operator<(const Vector& y) const - { - return ConstVector(*this) < y; - } - -bool Vector::operator<=(const Vector& y) const - { - return ConstVector(*this) <= y; - } - -bool Vector::operator>(const Vector& y) const - { - return ConstVector(*this) > y; - } - -bool Vector::operator>=(const Vector& y) const - { - return ConstVector(*this) >= y; - } - -void Vector::zeros() - { - if (skip() == 1) { - double* p = base(); - for (int i = 0; i < length()/ZeroPad::length; - i++, p += ZeroPad::length) - memcpy(p, zero_pad.getBase(), sizeof(double)*ZeroPad::length); - for ( ; p < base()+length(); p++) - *p = 0.0; - } else { - for (int i = 0; i < length(); i++) - operator[](i) = 0.0; - } - } - -void Vector::nans() - { - for (int i = 0; i < length(); i++) - operator[](i) = std::numeric_limits::quiet_NaN(); - } - -void Vector::infs() - { - for (int i = 0; i < length(); i++) - operator[](i) = std::numeric_limits::infinity(); - } - -Vector::~Vector() - { - if (destroy) { - delete [] data; - } - } - -void Vector::rotatePair(double alpha, double beta1, double beta2, int i) - { - double tmp = alpha*operator[](i) - beta1*operator[](i+1); - operator[](i+1) = alpha*operator[](i+1) - beta2*operator[](i); - operator[](i) = tmp; - } - -void Vector::add(double r, const Vector& v) - { - add(r, ConstVector(v)); - } - -void Vector::add(double r, const ConstVector& v) - { - int n = length(); - int incx = v.skip(); - int incy = skip(); - BLAS_daxpy(&n, &r, v.base(), &incx, base(), &incy); - } - -void Vector::add(const double* z, const Vector& v) - { - add(z, ConstVector(v)); - } - -void Vector::add(const double* z, const ConstVector& v) - { - int n = length()/2; - int incx = v.skip(); - int incy = skip(); - BLAS_zaxpy(&n, z, v.base(), &incx, base(), &incy); - } - -void Vector::mult(double r) - { - int n = length(); - int incx = skip(); - BLAS_dscal(&n, &r, base(), &incx); - } - -void Vector::mult2(double alpha, double beta1, double beta2, - Vector& x1, Vector& x2, - const Vector& b1, const Vector& b2) - { - x1.zeros(); - x2.zeros(); - mult2a(alpha, beta1, beta2, x1, x2, b1, b2); - } - -void Vector::mult2a(double alpha, double beta1, double beta2, - Vector& x1, Vector& x2, - const Vector& b1, const Vector& b2) - { - x1.add(alpha, b1); - x1.add(-beta1, b2); - x2.add(alpha, b2); - x2.add(-beta2, b1); - } - -double Vector::getNorm() const - { - ConstVector v(*this); - return v.getNorm(); - } - -double Vector::getMax() const - { - ConstVector v(*this); - return v.getMax(); - } - -double Vector::getNorm1() const - { - ConstVector v(*this); - return v.getNorm1(); - } - -double Vector::dot(const Vector& y) const - { - return ConstVector(*this).dot(ConstVector(y)); - } - -bool Vector::isFinite() const - { - return (ConstVector(*this)).isFinite(); - } - -void Vector::print() const - { - for (int i = 0; i < length(); i++) { - printf("%d\t%8.4g\n", i, operator[](i)); -#ifdef MATLAB -//#include "mex.h" - mexPrintf("%d\t%8.4g\n", i, operator[](i)); -#endif - } - } - - -ConstVector::ConstVector(const Vector& v, int off, int l) -: BaseConstVector(l, v.skip(), v.base() + v.skip()*off) - { - if (off < 0 || off + length() > v.length()) { - throw SYLV_MES_EXCEPTION("Subvector not contained in supvector."); - } - } - -ConstVector::ConstVector(const ConstVector& v, int off, int l) -: BaseConstVector(l, v.skip(), v.base() + v.skip()*off) - { - if (off < 0 || off + length() > v.length()) { - throw SYLV_MES_EXCEPTION("Subvector not contained in supvector."); - } - } - -ConstVector::ConstVector(const double* d, int skip, int l) -: BaseConstVector(l, skip, d) - { - } - -ConstVector::ConstVector(const ConstGeneralMatrix& m, int col) -: BaseConstVector(m.numRows(), 1, &(m.get(0, col))) - { - } - -ConstVector::ConstVector(int row, const ConstGeneralMatrix& m) -: BaseConstVector(m.numCols(), m.getLD(), &(m.get(row, 0))) - { - } - -bool ConstVector::operator==(const ConstVector& y) const - { - if (length() != y.length()) - return false; - if (length() == 0) - return true; - int i = 0; - while (i < length() && operator[](i) == y[i]) - i++; - return i == length(); - } - -bool ConstVector::operator<(const ConstVector& y) const - { - int i = std::min(length(), y.length()); - int ii = 0; - while (ii < i && operator[](ii) == y[ii]) - ii++; - if (ii < i) - return operator[](ii) < y[ii]; - else - return length() < y.length(); - } - -double ConstVector::getNorm() const - { - double s = 0; - for (int i = 0; i < length(); i++) { - s+=operator[](i)*operator[](i); - } - return sqrt(s); - } - -double ConstVector::getMax() const - { - double r = 0; - for (int i = 0; i < length(); i++) { - if (abs(operator[](i))>r) - r = abs(operator[](i)); - } - return r; - } - -double ConstVector::getNorm1() const - { - double norm = 0.0; - for (int i = 0; i < length(); i++) { - norm += abs(operator[](i)); - } - return norm; - } - -double ConstVector::dot(const ConstVector& y) const - { - if (length() != y.length()) - throw SYLV_MES_EXCEPTION("Vector has different length in ConstVector::dot."); - int n = length(); - int incx = skip(); - int incy = y.skip(); - return BLAS_ddot(&n, base(), &incx, y.base(), &incy); - } - -bool ConstVector::isFinite() const - { - int i = 0; - while (i < length() && isfinite(operator[](i))) - i++; - return i == length(); - } - -void ConstVector::print() const - { - for (int i = 0; i < length(); i++) { - printf("%d\t%8.4g\n", i, operator[](i)); - } - } - - -ZeroPad::ZeroPad() - { - for (int i = 0; i < length; i++) - pad[i] = 0.0; - } diff --git a/mex/sources/kalman/sylv/cc/Vector.h b/mex/sources/kalman/sylv/cc/Vector.h deleted file mode 100644 index cbe961300..000000000 --- a/mex/sources/kalman/sylv/cc/Vector.h +++ /dev/null @@ -1,184 +0,0 @@ -/* $Header: /var/lib/cvs/dynare_cpp/sylv/cc/Vector.h,v 1.1.1.1 2004/06/04 13:01:13 kamenik Exp $ */ - -/* Tag $Name: $ */ - -#ifndef VECTOR_H -#define VECTOR_H - -/* NOTE! Vector and ConstVector have not common super class in order - * to avoid running virtual method invokation mechanism. Some - * members, and methods are thus duplicated */ - - -#ifdef MATLAB -#include "mex.h" -#define printf mexPrintf -#endif - -#include - -class GeneralMatrix; -class ConstVector; - -class Vector { -protected: - int len; - int s; - double* data; - bool destroy; -public: - Vector() : len(0), s(1), data(0), destroy(false) {} - Vector(int l) : len(l), s(1), data(new double[l]), destroy(true) {} - Vector(Vector& v) : len(v.length()), s(v.skip()), data(v.base()), destroy(false) {} - Vector(const Vector& v) - : len(v.length()), s(1), data(new double[len]), destroy(true) - {copy(v.base(), v.skip());} - Vector(const ConstVector& v); - Vector(const double* d, int l) - : len(l), s(1), data(new double[len]), destroy(true) - {copy(d, 1);} - Vector(double* d, int l) - : len(l), s(1), data(d), destroy(false) {} - Vector(double* d, int skip, int l) - : len(l), s(skip), data(d), destroy(false) {} - Vector(Vector& v, int off, int l); - Vector(const Vector& v, int off, int l); - Vector(GeneralMatrix& m, int col); - Vector(int row, GeneralMatrix& m); - const Vector& operator=(const Vector& v); - const Vector& operator=(const ConstVector& v); - double& operator[](int i) - {return data[s*i];} - const double& operator[](int i) const - {return data[s*i];} - const double* base() const - {return data;} - double* base() - {return data;} - int length() const - {return len;} - int skip() const - {return s;} - - /** Exact equality. */ - bool operator==(const Vector& y) const; - bool operator!=(const Vector& y) const; - /** Lexicographic ordering. */ - bool operator<(const Vector& y) const; - bool operator<=(const Vector& y) const; - bool operator>(const Vector& y) const; - bool operator>=(const Vector& y) const; - - virtual ~Vector(); - void zeros(); - void nans(); - void infs(); - bool toBeDestroyed() const {return destroy;} - void rotatePair(double alpha, double beta1, double beta2, int i); - void add(double r, const Vector& v); - void add(double r, const ConstVector& v); - void add(const double* z, const Vector& v); - void add(const double* z, const ConstVector& v); - void mult(double r); - double getNorm() const; - double getMax() const; - double getNorm1() const; - double dot(const Vector& y) const; - bool isFinite() const; - void print() const; - - /* multiplies | alpha -beta1| |b1| |x1| - | |\otimes I .| | = | | - | -beta2 alpha| |b2| |x2| - */ - static void mult2(double alpha, double beta1, double beta2, - Vector& x1, Vector& x2, - const Vector& b1, const Vector& b2); - /* same, but adds instead of set */ - static void mult2a(double alpha, double beta1, double beta2, - Vector& x1, Vector& x2, - const Vector& b1, const Vector& b2); - /* same, but subtracts instead of add */ - static void mult2s(double alpha, double beta1, double beta2, - Vector& x1, Vector& x2, - const Vector& b1, const Vector& b2) - {mult2a(-alpha, -beta1, -beta2, x1, x2, b1, b2);} -private: - void copy(const double* d, int inc); - const Vector& operator=(int); // must not be used (not implemented) - const Vector& operator=(double); // must not be used (not implemented) -}; - - -class BaseConstVector { -protected: - int len; - int s; - const double* data; -public: - BaseConstVector(int l, int si, const double* d) : len(l), s(si), data(d) {} - BaseConstVector(const BaseConstVector& v) : len(v.len), s(v.s), data(v.data) {} - const BaseConstVector& operator=(const BaseConstVector& v) - {len = v.len; s = v.s; data = v.data; return *this;} - const double& operator[](int i) const - {return data[s*i];} - const double* base() const - {return data;} - int length() const - {return len;} - int skip() const - {return s;} -}; - -class ConstGeneralMatrix; - -class ConstVector : public BaseConstVector { -public: - ConstVector(const Vector& v) : BaseConstVector(v.length(), v.skip(), v.base()) {} - ConstVector(const ConstVector& v) : BaseConstVector(v) {} - ConstVector(const double* d, int l) : BaseConstVector(l, 1, d) {} - ConstVector(const Vector& v, int off, int l); - ConstVector(const ConstVector& v, int off, int l); - ConstVector(const double* d, int skip, int l); - ConstVector(const ConstGeneralMatrix& m, int col); - ConstVector(int row, const ConstGeneralMatrix& m); - - virtual ~ConstVector() {} - /** Exact equality. */ - bool operator==(const ConstVector& y) const; - bool operator!=(const ConstVector& y) const - {return ! operator==(y);} - /** Lexicographic ordering. */ - bool operator<(const ConstVector& y) const; - bool operator<=(const ConstVector& y) const - {return operator<(y) || operator==(y);} - bool operator>(const ConstVector& y) const - {return ! operator<=(y);} - bool operator>=(const ConstVector& y) const - {return ! operator<(y);} - - double getNorm() const; - double getMax() const; - double getNorm1() const; - double dot(const ConstVector& y) const; - bool isFinite() const; - void print() const; -}; - -class ZeroPad { -public: - //static const int length = 16; - enum { length = 16}; -private: - double pad[16]; -public: - ZeroPad(); - const double* getBase() const {return pad;} -}; - -#endif /* VECTOR_H */ - - -// Local Variables: -// mode:C++ -// End: diff --git a/mex/sources/kalman/sylv/cc/cppblas.h b/mex/sources/kalman/sylv/cc/cppblas.h deleted file mode 100644 index 65df7aeb4..000000000 --- a/mex/sources/kalman/sylv/cc/cppblas.h +++ /dev/null @@ -1,68 +0,0 @@ -/* $Header: /var/lib/cvs/dynare_cpp/sylv/cc/cppblas.h,v 1.2 2004/11/24 20:42:52 kamenik Exp $ */ - -/* Tag $Name: $ */ - -#ifndef CPPBLAS_H -#define CPPBLAS_H - -#if defined(MATLAB) && !defined(__linux__) -#define BLAS_dgemm dgemm -#define BLAS_dgemv dgemv -#define BLAS_dtrsv dtrsv -#define BLAS_dtrmv dtrmv -#define BLAS_daxpy daxpy -#define BLAS_dcopy dcopy -#define BLAS_zaxpy zaxpy -#define BLAS_dscal dscal -#define BLAS_dtrsm dtrsm -#define BLAS_ddot ddot -#else /* defined(MATLAB) && !defined(__linux__) */ -#define BLAS_dgemm dgemm_ -#define BLAS_dgemv dgemv_ -#define BLAS_dtrsv dtrsv_ -#define BLAS_dtrmv dtrmv_ -#define BLAS_daxpy daxpy_ -#define BLAS_dcopy dcopy_ -#define BLAS_zaxpy zaxpy_ -#define BLAS_dscal dscal_ -#define BLAS_dtrsm dtrsm_ -#define BLAS_ddot ddot_ -#endif /* defined(MATLAB) && !defined(__linux__) */ - -#define BLCHAR const char* -#define CONST_BLINT const int* -#define CONST_BLDOU const double* -#define BLDOU double* - -extern "C" { - void BLAS_dgemm(BLCHAR transa, BLCHAR transb, CONST_BLINT m, CONST_BLINT n, - CONST_BLINT k, CONST_BLDOU alpha, CONST_BLDOU a, CONST_BLINT lda, - CONST_BLDOU b, CONST_BLINT ldb, CONST_BLDOU beta, - BLDOU c, CONST_BLINT ldc); - void BLAS_dgemv(BLCHAR trans, CONST_BLINT m, CONST_BLINT n, CONST_BLDOU alpha, - CONST_BLDOU a, CONST_BLINT lda, CONST_BLDOU x, CONST_BLINT incx, - CONST_BLDOU beta, BLDOU y, CONST_BLINT incy); - void BLAS_dtrsv(BLCHAR uplo, BLCHAR trans, BLCHAR diag, CONST_BLINT n, - CONST_BLDOU a, CONST_BLINT lda, BLDOU x, CONST_BLINT incx); - void BLAS_dtrmv(BLCHAR uplo, BLCHAR trans, BLCHAR diag, CONST_BLINT n, - CONST_BLDOU a, CONST_BLINT lda, BLDOU x, CONST_BLINT incx); - void BLAS_daxpy(CONST_BLINT n, CONST_BLDOU a, CONST_BLDOU x, CONST_BLINT incx, - BLDOU y, CONST_BLINT incy); - void BLAS_dcopy(CONST_BLINT n, CONST_BLDOU x, CONST_BLINT incx, - BLDOU y, CONST_BLINT incy); - void BLAS_zaxpy(CONST_BLINT n, CONST_BLDOU a, CONST_BLDOU x, CONST_BLINT incx, - BLDOU y, CONST_BLINT incy); - void BLAS_dscal(CONST_BLINT n, CONST_BLDOU a, BLDOU x, CONST_BLINT incx); - void BLAS_dtrsm(BLCHAR side, BLCHAR uplo, BLCHAR transa, BLCHAR diag, CONST_BLINT m, - CONST_BLINT n, CONST_BLDOU alpha, CONST_BLDOU a, CONST_BLINT lda, - BLDOU b, CONST_BLINT ldb); - double BLAS_ddot(CONST_BLINT n, CONST_BLDOU x, CONST_BLINT incx, CONST_BLDOU y, - CONST_BLINT incy); -}; - - -#endif /* CPPBLAS_H */ - -// Local Variables: -// mode:C++ -// End: diff --git a/mex/sources/kalman/sylv/cc/cpplapack.h b/mex/sources/kalman/sylv/cc/cpplapack.h deleted file mode 100644 index bee6a4394..000000000 --- a/mex/sources/kalman/sylv/cc/cpplapack.h +++ /dev/null @@ -1,82 +0,0 @@ -/* $Header: /var/lib/cvs/dynare_cpp/sylv/cc/cpplapack.h,v 1.3 2004/11/24 20:43:10 kamenik Exp $ */ - -/* Tag $Name: $ */ - -#ifndef CPPLAPACK_H -#define CPPLAPACK_H - -#if defined(MATLAB) && !defined(__linux__) -#define LAPACK_dgetrs dgetrs -#define LAPACK_dgetrf dgetrf -#define LAPACK_dgees dgees -#define LAPACK_dgecon dgecon -#define LAPACK_dtrexc dtrexc -#define LAPACK_dtrsyl dtrsyl -#define LAPACK_dpotrf dpotrf -#define LAPACK_dgges dgges -#define LAPACK_dsyev dsyev -#define LAPACK_dsyevr dsyevr -#else /* MATLAB */ -#define LAPACK_dgetrs dgetrs_ -#define LAPACK_dgetrf dgetrf_ -#define LAPACK_dgees dgees_ -#define LAPACK_dgecon dgecon_ -#define LAPACK_dtrexc dtrexc_ -#define LAPACK_dtrsyl dtrsyl_ -#define LAPACK_dpotrf dpotrf_ -#define LAPACK_dgges dgges_ -#define LAPACK_dsyev dsyev_ -#define LAPACK_dsyevr dsyevr_ -#endif /* MATLAB */ - -#define LACHAR const char* -#define CONST_LAINT const int* -#define LAINT int* -#define CONST_LADOU const double* -#define LADOU double* -typedef int (*DGGESCRIT)(const double*, const double*, const double*); - -extern "C" { - void LAPACK_dgetrs(LACHAR trans, CONST_LAINT n, CONST_LAINT nrhs, - CONST_LADOU a, CONST_LAINT lda, CONST_LAINT ipiv, - LADOU b, CONST_LAINT ldb, LAINT info); - void LAPACK_dgetrf(CONST_LAINT m, CONST_LAINT n, LADOU a, - CONST_LAINT lda, LAINT ipiv, LAINT info); - void LAPACK_dgees(LACHAR jobvs, LACHAR sort, const void* select, - CONST_LAINT n, LADOU a, CONST_LAINT lda, LAINT sdim, - LADOU wr, LADOU wi, LADOU vs, CONST_LAINT ldvs, - LADOU work, CONST_LAINT lwork, const void* bwork, LAINT info); - void LAPACK_dgecon(LACHAR norm, CONST_LAINT n, CONST_LADOU a, CONST_LAINT lda, - CONST_LADOU anorm, LADOU rnorm, LADOU work, LAINT iwork, - LAINT info); - void LAPACK_dtrexc(LACHAR compq, CONST_LAINT n, LADOU t, CONST_LAINT ldt, - LADOU q, CONST_LAINT ldq, LAINT ifst, LAINT ilst, LADOU work, - LAINT info); - void LAPACK_dtrsyl(LACHAR trana, LACHAR tranb, CONST_LAINT isgn, CONST_LAINT m, - CONST_LAINT n, CONST_LADOU a, CONST_LAINT lda, CONST_LADOU b, - CONST_LAINT ldb, LADOU c, CONST_LAINT ldc, LADOU scale, - LAINT info); - void LAPACK_dpotrf(LACHAR uplo, CONST_LAINT n, LADOU a, CONST_LAINT lda, - LAINT info); - void LAPACK_dgges(LACHAR jobvsl, LACHAR jobvsr, LACHAR sort, DGGESCRIT delztg, - CONST_LAINT n, LADOU a, CONST_LAINT lda, LADOU b, CONST_LAINT ldb, - LAINT sdim, LADOU alphar, LADOU alphai, LADOU beta, - LADOU vsl, CONST_LAINT ldvsl, LADOU vsr, CONST_LAINT ldvsr, - LADOU work, CONST_LAINT lwork, LAINT bwork, LAINT info); - void LAPACK_dsyev(LACHAR jobz, LACHAR uplo, CONST_LAINT n, LADOU a, CONST_LAINT lda, - LADOU w, LADOU work, CONST_LAINT lwork, LAINT info); - void LAPACK_dsyevr(LACHAR jobz, LACHAR range, LACHAR uplo, CONST_LAINT n, LADOU a, - CONST_LAINT lda, LADOU lv, LADOU vu, CONST_LAINT il, CONST_LAINT iu, - CONST_LADOU abstol, LAINT m, LADOU w, LADOU z, CONST_LAINT ldz, - LAINT isuppz, LADOU work, CONST_LAINT lwork, LAINT iwork, CONST_LAINT liwork, - LAINT info); -}; - - -#endif /* CPPLAPACK_H */ - - -// Local Variables: -// mode:C++ -// End: - diff --git a/mex/sources/kalman/testing/Makefile b/mex/sources/kalman/testing/Makefile deleted file mode 100644 index 60c4fc16b..000000000 --- a/mex/sources/kalman/testing/Makefile +++ /dev/null @@ -1,65 +0,0 @@ -# $Id: Makefile 534 2005-11-30 13:58:11Z kamenik $ -# Copyright 2005, Ondra Kamenik - -#LD_LIBS := -llapack -lcblas -lf77blas -latlas -lg2c -#CC_FLAGS := -Wall -I../cc -I../../sylv/cc - -CC_FLAGS := -Wall -I../cc -I../sylv/cc - - -DEBUG = yes -MATLAB = 1 - -# Added by GP -# LDFLAGS := -llapack -lcblas -lf77blas -latlas -lg2c -lstdc++ -lmingw32 - LDFLAGS := -Wl,--library-path $(LD_LIBRARY_PATH) \ - -Wl,-L'f:/MinGW/lib' \ - -Wl,-L"c:/Program Files"/MATLAB_SV71/extern/lib/win32/microsoft/ \ - -Wl,-llibmex -Wl,-llibmx -Wl,-llibmwlapack -Wl,-llibdflapack \ - -lg2c -lmingw32 -lstdc++ $(LDFLAGS) - -# -Wl,-L'f:/CygWin/usr/local/atlas/lib' \ -# -Wl,-L'f:/CygWin/lib' \ -# $(LDFLAGS) - - LD_LIBS :=$(LDFLAGS) -# end add - -ifeq ($(DEBUG),yes) - CC_FLAGS := $(CC_FLAGS) -g -else - CC_FLAGS := $(CC_FLAGS) -O2 -endif - -matrix_interface := GeneralMatrix Vector SylvException -matobjs := $(patsubst %, ../sylv/cc/%.o, $(matrix_interface)) -mathsource := $(patsubst %, ../sylv/cc/%.h, $(matrix_interface)) -#cwebsource := $(wildcard ../cc/*.cweb) -cppsource := $(wildcard ../cc/*.cpp) -#cppsource := $(patsubst %.cweb,%.cpp,$(cwebsource)) -mainobjects := $(patsubst %.cpp,%.o,$(cppsource)) -#objects := $(patsubst %.cpp,%.o, *.cpp) -objects := $(wildcard *.o) -#hwebsource := $(wildcard ../cc/*.hweb) -hsource := $(wildcard ../cc/*.h) -#hsource := $(patsubst %.hweb,%.h,$(hwebsource)) - -../cc/dummy.ch: - make -C ../cc dummy.ch - -../cc/%.o: ../cc/%.cpp $(hsource) $(mathsource) - make -C ../cc $*.o - -%.o: %.cpp $(hsource) ascii_matrix.h - c++ $(CC_FLAGS) -c $*.cpp - -#tests: $(hmainsource) $(cppmainsource) $(hsource) $(cppsource) objects) \ - -tests: $(hsource) $(cppsource) $(mainobjects) \ - tests.o ascii_matrix.o - c++ $(CC_FLAGS) $(mainobjects) $(matobjs) ascii_matrix.o tests.o -o tests $(LD_LIBS) - -clear: - make -C ../cc clear - rm -f *.o - rm -f tests diff --git a/mex/sources/kalman/testing/ascii_matrix.cpp b/mex/sources/kalman/testing/ascii_matrix.cpp deleted file mode 100644 index 97749de7b..000000000 --- a/mex/sources/kalman/testing/ascii_matrix.cpp +++ /dev/null @@ -1,56 +0,0 @@ -// $Id: ascii_matrix.cpp 534 2005-11-30 13:58:11Z kamenik $ -// Copyright 2005, Ondra Kamenik - -#include "ascii_matrix.h" - -#include -#include - -#include -#include - -// if the file doesn't exist, the number array is empty -void AsciiNumberArray::parse(const char* fname) -{ - rows = 0; - cols = 0; - - std::ifstream file(fname); - std::string line; - - while (getline(file, line)) { - rows++; - int icols = 0; - const char delims[] = " \r\n\t"; - char* lineptr = strdup(line.c_str()); - char* tok = strtok(lineptr, delims); - while (tok) { - icols++; - double item; - if (1 != sscanf(tok, "%lf", &item)) { - fprintf(stderr, "Couldn't parse a token %s as double.\n", tok); - exit(1); - } - data.push_back(item); - tok = strtok(NULL, delims); - } - free(lineptr); - if (cols) { - if (cols != icols) { - fprintf(stderr, "Asserted a different number of columns.\n"); - exit(1); - } - } else { - cols = icols; - } - } -} - - -AsciiMatrix::AsciiMatrix(const AsciiNumberArray& na) - : GeneralMatrix(na.rows, na.cols) -{ - for (int i = 0; i < numRows(); i++) - for (int j = 0; j < numCols(); j++) - get(i, j) = na.data[i*numCols()+j]; -} diff --git a/mex/sources/kalman/testing/ascii_matrix.h b/mex/sources/kalman/testing/ascii_matrix.h deleted file mode 100644 index b654b9655..000000000 --- a/mex/sources/kalman/testing/ascii_matrix.h +++ /dev/null @@ -1,24 +0,0 @@ -// $Id: ascii_matrix.h 534 2005-11-30 13:58:11Z kamenik $ -// Copyright 2005, Ondra Kamenik - -#include "GeneralMatrix.h" - -#include -#include - -struct AsciiNumberArray { - int rows; - int cols; - std::vector data; - AsciiNumberArray(const char* fname) - {parse(fname);} - AsciiNumberArray(std::string fname) - {parse(fname.c_str());} -protected: - void parse(const char* fname); -}; - -class AsciiMatrix : public GeneralMatrix { -public: - AsciiMatrix(const AsciiNumberArray& na); -}; diff --git a/mex/sources/kalman/testing/dynare_filter.m b/mex/sources/kalman/testing/dynare_filter.m deleted file mode 100644 index 4fa46853c..000000000 --- a/mex/sources/kalman/testing/dynare_filter.m +++ /dev/null @@ -1,33 +0,0 @@ -% -% loglik = dynare_filter(Z,H,T,R,Q,Y,Pstar,Pinf) -% -% This is just an interface to DiffuseLikelihoodH1 of Dynare. It -% takes state space in the form -% y_t = Z*alpha_t + epsilon_t -% alpha_{t+1} = T*alpha_t + R*eta_t -% where epsilon covariance is H, eta covariance is Q -% -% It returns log likelihood. -% -% Copyright 2005, Ondra Kamenik - -% $Id: dynare_filter.m 534 2005-11-30 13:58:11Z kamenik $ - -function lik = dynare_filter(Z,H,T,R,Q,Y,Pstar,Pinf) - global bayestopt_ options_ - - pp = size(Z,1); - mm = size(T,1); - rr = size(R,2); - dT = [zeros(pp,pp) Z*T; zeros(mm,pp) T]; - dR = [eye(pp) Z*R; zeros(mm,pp) R]; - dQ = [zeros(pp,pp) zeros(pp,rr); zeros(rr,pp) Q]; - dPinf = [zeros(pp,pp) zeros(pp,mm); zeros(mm,pp) Pinf]; - dPstar = [Z*Pstar*Z' Z*Pstar; Pstar*Z' Pstar]; - - bayestopt_.mf = [1:pp]; - options_.kalman_tol = 1e-10; - - lik = DiffuseLikelihoodH1(dT,dR,dQ,H,dPinf,dPstar,Y,zeros(pp,size(Y,2)),1); - - diff --git a/mex/sources/kalman/testing/dynare_smoother.m b/mex/sources/kalman/testing/dynare_smoother.m deleted file mode 100644 index 29c801c62..000000000 --- a/mex/sources/kalman/testing/dynare_smoother.m +++ /dev/null @@ -1,37 +0,0 @@ -% -% [alpha,epsilon,eta] = dynare_smoother(Z,H,T,R,Q,Y,Pstar,Pinf) -% -% This is just an interface to DiffuseKalmanSmootherH1 of Dynare. It -% takes state space in the form -% y_t = Z*alpha_t + epsilon_t -% alpha_{t+1} = T*alpha_t + R*eta_t -% where epsilon covariance is H, eta covariance is Q -% -% It returns smoothed alpha, epsilon and eta. -% -% Copyright 2005, Ondra Kamenik - -% $Id: dynare_smoother.m 534 2005-11-30 13:58:11Z kamenik $ - -function [alpha,epsilon,eta] = dynare_smoother(Z,H,T,R,Q,Y,Pstar,Pinf) - global options_ - - pp = size(Z,1); - mm = size(T,1); - rr = size(R,2); - dT = [zeros(pp,pp) Z*T; zeros(mm,pp) T]; - dR = [eye(pp) Z*R; zeros(mm,pp) R]; - dQ = [zeros(pp,pp) zeros(pp,rr); zeros(rr,pp) Q]; - dPinf = [Z*Pinf*Z' Z*Pinf; Pinf*Z' Pinf]; - dPstar = [Z*Pstar*Z' Z*Pstar; Pstar*Z' Pstar]; - - mf = [1:pp]; - options_.kalman_tol = 1e-10; - options_.nk = 0; -% if you want DiffuseKalmanSmootherH3, uncomment the following and set -% diffuse_d (possibly empty []) -% options_.diffuse_d = [7]; - - [alpha,epsilon,eta] = DiffuseKalmanSmootherH1(dT,dR,dQ,H,dPinf,dPstar,Y,zeros(pp,size(Y,2)),pp,mm+pp,size(Y,2),mf); - alpha = alpha(pp+1:end,:); - eta = eta(pp+1:end,:); diff --git a/mex/sources/kalman/testing/kalmandll_test.m b/mex/sources/kalman/testing/kalmandll_test.m deleted file mode 100644 index 87ad5aa04..000000000 --- a/mex/sources/kalman/testing/kalmandll_test.m +++ /dev/null @@ -1,20 +0,0 @@ -function [LIKDLL loglik]=kalmandll_test(T,mf,R,Q,H,Pstar,Pinf,data,start) - -if isempty(H) - H=zeros(size(data,1), size(data,1)) -elseif H==0 - H=zeros(size(data,1), size(data,1)) -end -Z=zeros(size(data,1), size(T,2)) -for i = 1:size(data,1) -Z(i,mf(i))=1 -end -LIKDLL= kalman_filter_dll4(T,Z,R,Q,H,Pstar,data,start) -%Y=data; -if isempty(Pinf) - Pinf=zeros(size(T)); -elseif Pinf==0 - Pinf=zeros(size(T)); -end -% test DiffuseLikelihoodH1 -loglik = dynare_filter(Z,H,T,R,Q,data,Pstar,Pinf) \ No newline at end of file diff --git a/mex/sources/kalman/testing/test_data.tgz b/mex/sources/kalman/testing/test_data.tgz deleted file mode 100644 index 6decfec0b491985196e184b8dd855fd661c753bf..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 29902 zcmV(}K+wM*iwFQ8e?~(91MQv5jwD%fo_ouBirxzqIQPpNXekNWNE>1Rjs$_EB#<0n z9$)sEXS)3!8J;Z>?CPFT1~@2od4-4jId<&&+iyAjIrl&3^0)u~@ppgtm!E!>Ps%Bc z(SAy)_K}kPSy~&}{`5~zqx_UpEjhPVll{9~X6`@zoPM3hxqkli&;Rm=fBO07pZ??z zcmMDH@$dipKmD&i{qOJp@cr+8#AE%a&(A;qoFA#@KKf4YpFe(2f+ z-XDDo`I$7|zVmZ^RC~?dKK_m^&t7xObH86{kI~9#@tqw@n%E(ExOnQ8S}k$2elEi8 zuHL_@Jwo4e%=1!~y~~4VJHOQA;d<>N2|HfZzB1Q5zfZr!pc zSn<$l^KAWiwpGfCNAvIaB77>qH@kZOs`l7*q`cdCyZm;z>~7iiSiW=q&Ul#mtEHZ= znq$A~6bU(Ggctf=AB87qAC||SAlFpNM@jf@cWVWe`~K;E<=dxc^;UrIclG{Ne;$uk zrm3H&M|fM(Hs=0#B|rL_Wq*_rTZC%IzKxGpOh#JMDJgjD4O?@>iD-q_VojLp3UzE?}PXZ2oy z?|1c+SG6TITesqNBeuc*GX8$OJ@vPH7vkQo-W!{H4ScVb?$7GI0N?NG{j0iawx`~G zJUzj;-`ZpS`utmd@_YREAO6Sx{BOTf2>5yYm#aAqZ}H#Ay;uA9b}Hsq{P!b1cF@x! zJ$?T8?^61kuPZ<9%D3PC5x%bc>$f5QUSH@h^UA;dY53Zp-^Dh3UHOaMhX3zR(C=d# z{=dKSwGF?ED}RmK@E54!{u;O8FYwCOHvBHGd~L(;LdAV;!|!5)zP90aaph|peitX` zt8Df96ZIeJAiqul=>NX{(^@Nq`p+oor?2|Y5BT_r$#a%gqSE`v?1*&O3=B_=EANd4$bhBV1 zuefvyL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B|bLFm%{_O}luOH!-_(S z&wauE_usqr_4)dIeZD?lpRdo?=j-$J`TBf)zCK@{ug}-#>+|*b`h0!9K3|`&&)4Vc R^Y!`o`9Ju-O0odB0RU^Oa{vGU diff --git a/mex/sources/kalman/testing/tests.asv b/mex/sources/kalman/testing/tests.asv deleted file mode 100644 index ebcc6f71b..000000000 --- a/mex/sources/kalman/testing/tests.asv +++ /dev/null @@ -1,311 +0,0 @@ -// $Id: tests.cpp 534 2005-11-30 13:58:11Z kamenik $ -// Copyright 2005, Ondra Kamenik - -#include "../cc/kalman.h" -#include "../cc/ts_exception.h" -#include "ascii_matrix.h" - -#include "GeneralMatrix.h" -#include "Vector.h" -#include "SylvException.h" - -#include -#include - - -// gettimeofday for MinGW -#ifdef __MINGW32__ -#define _W32_FT_OFFSET (116444736000000000LL) - -typedef struct _filetime { - unsigned long dwLowDateTime; - unsigned long dwHighDateTime; -} filetime; - -extern "C" { - void __stdcall GetSystemTimeAsFileTime(filetime*); -}; - -typedef union { - long long ns100; // time since 1 Jan 1601 in 100ns units - filetime ft; -} w32_ftv; - -void D_gettimeofday(struct timeval* p, struct timezone* tz) -{ - w32_ftv _now; - GetSystemTimeAsFileTime( &(_now.ft) ); - p->tv_usec=(long)((_now.ns100 / 10LL) % 1000000LL ); - p->tv_sec= (long)((_now.ns100-_W32_FT_OFFSET)/10000000LL); - return; -} - -#else -#define D_gettimeofday gettimeofday -#endif // gettimeofday for MinGW - - -struct AsciiKalmanTask { - AsciiMatrix Z; - AsciiMatrix H; - AsciiMatrix T; - AsciiMatrix R; - AsciiMatrix Q; - AsciiMatrix Pstar; - AsciiMatrix Pinf; - AsciiMatrix a; - AsciiMatrix Y; - AsciiKalmanTask(const char* prefix) - : Z(std::string(prefix) + "_Z.dat"), - H(std::string(prefix) + "_H.dat"), - T(std::string(prefix) + "_T.dat"), - R(std::string(prefix) + "_R.dat"), - Q(std::string(prefix) + "_Q.dat"), - Pstar(std::string(prefix) + "_Pstar.dat"), - Pinf(std::string(prefix) + "_Pinf.dat"), - a(std::string(prefix) + "_a.dat"), - Y(std::string(prefix) + "_Y.dat") - {} -}; - -// WallTimer class. Constructor saves the wall time, destructor -// cancels the current time from the saved, and prints the message -// with time information -class WallTimer { - char mes[100]; - struct timeval start; - bool new_line; -public: - WallTimer(const char* m, bool nl = true) - {strcpy(mes, m);new_line = nl; D_gettimeofday(&start, NULL);} - ~WallTimer() - { - struct timeval end; - D_gettimeofday(&end, NULL); - printf("%s%8.4g", mes, - end.tv_sec-start.tv_sec + (end.tv_usec-start.tv_usec)*1.0e-6); - if (new_line) - printf("\n"); - } -}; - -/****************************************************/ -/* declaration of TestRunnable class */ -/****************************************************/ -class TestRunnable { - char name[100]; -public: - TestRunnable(const char* n) - {strncpy(name, n, 100);} - bool test() const; - virtual bool run() const =0; - const char* getName() const - {return name;} -protected: - static bool filter_and_smoother(const char* prefix, bool diffuse_flag); -}; - -bool TestRunnable::test() const -{ - printf("Running test <%s>\n",name); - bool passed; - { - WallTimer tim("Wall clock time ", false); - passed = run(); - } - if (passed) { - printf("............................ passed\n\n"); - return passed; - } else { - printf("............................ FAILED\n\n"); - return passed; - } -} - -/****************************************************/ -/* definition of TestRunnable static methods */ -/****************************************************/ -bool TestRunnable::filter_and_smoother(const char* prefix, bool diffuse_flag) -{ - AsciiKalmanTask akt(prefix); - StateInit* init; - if (diffuse_flag) - init = new StateInit(akt.Pstar, akt.Pinf, akt.a.getData()); - else - init = new StateInit(akt.Pstar, akt.a.getData()); - - KalmanTask kt(akt.Y, akt.Z, akt.H, akt.T, akt.R, akt.Q, *init); - - // multivariate - int per; - int d; - double ll; - GeneralMatrix alpha(akt.T.numRows(), akt.Y.numCols()); - GeneralMatrix eta(akt.R.numCols(), akt.Y.numCols()); - GeneralMatrix V(akt.T.numRows(), akt.T.numRows()*akt.Y.numCols()); - SmootherResults sres(akt.Y.numCols()); - { - WallTimer tim("\tMultivariate time ", true); - ll = kt.filter_and_smooth(sres, per, d); - printf("\t\tll=%f per=%d d=%d\n", ll, per, d); - if (per == akt.Y.numCols()) { - sres.exportAlpha(alpha); - sres.exportEta(eta); - sres.exportV(V); - } else { - printf("\t\tNot finished.\n"); - } - } - - // univariate - KalmanUniTask kut(kt); - int per1; - int d1; - double ll1; - GeneralMatrix alpha1(akt.T.numRows(), akt.Y.numCols()); - GeneralMatrix eta1(akt.R.numCols(), akt.Y.numCols()); - GeneralMatrix V1(akt.T.numRows(), akt.T.numRows()*akt.Y.numCols()); - SmootherResults sres1(akt.Y.numCols()*akt.Y.numRows()); - { - WallTimer tim("\tUnivariate time ", true); - int dd; - ll1 = kut.filter_and_smooth(sres1, per1, dd); - per1 /= akt.Y.numRows(); - d1 = dd/akt.Y.numRows(); - printf("\t\tll=%f per=%d d=%d(%d)\n", ll1, per1, d1, dd); - if (per1 == akt.Y.numCols()) { - SmootherResults sres_uni(akt.Y.numCols()); - sres_uni.import(sres1, akt.Y.numRows()); - sres_uni.exportAlpha(alpha1); - sres_uni.exportEta(eta1); - sres_uni.exportV(V1); - } else { - printf("\t\tNot finished.\n"); - } - } - - // compare - if (per == per1 && per == akt.Y.numCols()) { - WallTimer tim("\tComparison time ", true); - alpha.add(-1.0, alpha1); - eta.add(-1.0, eta1); - V.add(-1.0, V1); - int maxd = std::max(d,d1); - for (int t = 1; t <= maxd; t++) { - Vector alphat(alpha, t-1); - printf("\t\tt=%d alpha error %10.6g\n",t,alphat.getMax()); - Vector etat(eta, t-1); - printf("\t\tt=%d eta error %10.6g\n",t,etat.getMax()); - GeneralMatrix Vt(V, 0, (t-1)*akt.T.numRows(), akt.T.numRows(), akt.T.numRows()); - printf("\t\tt=%d V error %10.6g\n",t,V.getData().getMax()); - } - GeneralMatrix alpha_rest(alpha, 0, maxd, akt.T.numRows(), alpha.numCols()-maxd); - printf("\t\tt=%d.. alpha error %10.6g\n",maxd+1,alpha_rest.getData().getMax()); - GeneralMatrix eta_rest(eta, 0, maxd, akt.R.numCols(), eta.numCols()-maxd); - printf("\t\tt=%d.. eta error %10.6g\n",maxd+1,eta_rest.getData().getMax()); - GeneralMatrix V_rest(V, 0, maxd*akt.T.numRows(), akt.T.numRows(), - V.numCols()-maxd*akt.T.numRows()); - printf("\t\tt=%d.. V error %10.6g\n",maxd+1,V_rest.getData().getMax()); - } - - delete init; - - return true; -} - - -/****************************************************/ -/* definition of TestRunnable subclasses */ -/****************************************************/ -class SmallNonDiffuse : public TestRunnable { -public: - SmallNonDiffuse() - : TestRunnable("Non-diffuse small (p=2,m=3,r=4)") {} - - bool run() const - { - filter_and_smoother("small2x3x4", false); - return true; - } -}; - -class SmallDiffuse : public TestRunnable { -public: - SmallDiffuse() - : TestRunnable("Diffuse small (p=2,m=3,r=4)") {} - - bool run() const - { - return filter_and_smoother("small2x3x4", true); - } -}; - -class MiddleNonDiffuse : public TestRunnable { -public: - MiddleNonDiffuse() - : TestRunnable("Non-diffuse middle (p=10,m=15,r=12)") {} - - bool run() const - { - return filter_and_smoother("10x15x12", false); - } -}; - -class MiddleDiffuse : public TestRunnable { -public: - MiddleDiffuse() - : TestRunnable("Diffuse middle (p=10,m=15,r=12)") {} - - bool run() const - { - return filter_and_smoother("10x15x12", true); - } -}; - -class SOEDiffuse : public TestRunnable { -public: - SOEDiffuse() - : TestRunnable("Diffuse soe (p=8,m=25,r=15)") {} - - bool run() const - { - return filter_and_smoother("soe8x25x15", true); - } -}; - -int main() -{ - TestRunnable* all_tests[50]; - // fill in vector of all tests - int num_tests = 0; - all_tests[num_tests++] = new SmallNonDiffuse(); - all_tests[num_tests++] = new SmallDiffuse(); - all_tests[num_tests++] = new MiddleNonDiffuse(); - all_tests[num_tests++] = new MiddleDiffuse(); - all_tests[num_tests++] = new SOEDiffuse(); - - // launch the tests - int success = 0; - for (int i = 0; i < num_tests; i++) { - try { - if (all_tests[i]->test()) - success++; - } catch (const TSException& e) { - printf("Caugth TS exception in <%s>:\n", all_tests[i]->getName()); - e.print(); - } catch (SylvException& e) { - printf("Caught Sylv exception in <%s>:\n", all_tests[i]->getName()); - e.printMessage(); - } - } - - printf("There were %d tests that failed out of %d tests run.\n", - num_tests - success, num_tests); - - // destroy - for (int i = 0; i < num_tests; i++) { - delete all_tests[i]; - } - - return 0; -} diff --git a/mex/sources/kalman/testing/tests.cpp b/mex/sources/kalman/testing/tests.cpp deleted file mode 100644 index c73f0c4ec..000000000 --- a/mex/sources/kalman/testing/tests.cpp +++ /dev/null @@ -1,311 +0,0 @@ -// $Id: tests.cpp 534 2005-11-30 13:58:11Z kamenik $ -// Copyright 2005, Ondra Kamenik - -#include "../cc/kalman.h" -#include "../cc/ts_exception.h" -#include "ascii_matrix.h" - -#include "GeneralMatrix.h" -#include "Vector.h" -#include "SylvException.h" - -#include -#include - - -// gettimeofday for MinGW -#ifdef __MINGW32__ -#define _W32_FT_OFFSET (116444736000000000LL) - -typedef struct _filetime { - unsigned long dwLowDateTime; - unsigned long dwHighDateTime; -} filetime; - -extern "C" { - void __stdcall GetSystemTimeAsFileTime(filetime*); -}; - -typedef union { - long long ns100; // time since 1 Jan 1601 in 100ns units - filetime ft; -} w32_ftv; - -void D_gettimeofday(struct timeval* p, struct timezone* tz) -{ - w32_ftv _now; - GetSystemTimeAsFileTime( &(_now.ft) ); - p->tv_usec=(long)((_now.ns100 / 10LL) % 1000000LL ); - p->tv_sec= (long)((_now.ns100-_W32_FT_OFFSET)/10000000LL); - return; -} - -#else -#define D_gettimeofday gettimeofday -#endif // gettimeofday for MinGW - - -struct AsciiKalmanTask { - AsciiMatrix Z; - AsciiMatrix H; - AsciiMatrix T; - AsciiMatrix R; - AsciiMatrix Q; - AsciiMatrix Pstar; - AsciiMatrix Pinf; - AsciiMatrix a; - AsciiMatrix Y; - AsciiKalmanTask(const char* prefix) - : Z(std::string(prefix) + "_Z.dat"), - H(std::string(prefix) + "_H.dat"), - T(std::string(prefix) + "_T.dat"), - R(std::string(prefix) + "_R.dat"), - Q(std::string(prefix) + "_Q.dat"), - Pstar(std::string(prefix) + "_Pstar.dat"), - Pinf(std::string(prefix) + "_Pinf.dat"), - a(std::string(prefix) + "_a.dat"), - Y(std::string(prefix) + "_Y.dat") - {} -}; - -// WallTimer class. Constructor saves the wall time, destructor -// cancels the current time from the saved, and prints the message -// with time information -class WallTimer { - char mes[100]; - struct timeval start; - bool new_line; -public: - WallTimer(const char* m, bool nl = true) - {strcpy(mes, m);new_line = nl; D_gettimeofday(&start, NULL);} - ~WallTimer() - { - struct timeval end; - D_gettimeofday(&end, NULL); - printf("%s%8.4g", mes, - end.tv_sec-start.tv_sec + (end.tv_usec-start.tv_usec)*1.0e-6); - if (new_line) - printf("\n"); - } -}; - -/****************************************************/ -/* declaration of TestRunnable class */ -/****************************************************/ -class TestRunnable { - char name[100]; -public: - TestRunnable(const char* n) - {strncpy(name, n, 100);} - bool test() const; - virtual bool run() const =0; - const char* getName() const - {return name;} -protected: - static bool filter_and_smoother(const char* prefix, bool diffuse_flag); -}; - -bool TestRunnable::test() const -{ - printf("Running test <%s>\n",name); - bool passed; - { - WallTimer tim("Wall clock time ", false); - passed = run(); - } - if (passed) { - printf("............................ passed\n\n"); - return passed; - } else { - printf("............................ FAILED\n\n"); - return passed; - } -} - -/****************************************************/ -/* definition of TestRunnable static methods */ -/****************************************************/ -bool TestRunnable::filter_and_smoother(const char* prefix, bool diffuse_flag) -{ - AsciiKalmanTask akt(prefix); - StateInit* init; - if (diffuse_flag) - init = new StateInit(akt.Pstar, akt.Pinf, akt.a.getData()); - else - init = new StateInit(akt.Pstar, akt.a.getData()); - - KalmanTask kt(akt.Y, akt.Z, akt.H, akt.T, akt.R, akt.Q, *init); - - // multivariate - int per; - int d; - double ll; - GeneralMatrix alpha(akt.T.numRows(), akt.Y.numCols()); - GeneralMatrix eta(akt.R.numCols(), akt.Y.numCols()); - GeneralMatrix V(akt.T.numRows(), akt.T.numRows()*akt.Y.numCols()); - SmootherResults sres(akt.Y.numCols()); - { - WallTimer tim("\tMultivariate time ", true); - ll = kt.filter_and_smooth(sres, per, d); - printf("\t\tll=%f per=%d d=%d\n", ll, per, d); - if (per == akt.Y.numCols()) { - sres.exportAlpha(alpha); - sres.exportEta(eta); - sres.exportV(V); - } else { - printf("\t\tNot finished.\n"); - } - } - - // univariate - KalmanUniTask kut(kt); - int per1; - int d1; - double ll1; - GeneralMatrix alpha1(akt.T.numRows(), akt.Y.numCols()); - GeneralMatrix eta1(akt.R.numCols(), akt.Y.numCols()); - GeneralMatrix V1(akt.T.numRows(), akt.T.numRows()*akt.Y.numCols()); - SmootherResults sres1(akt.Y.numCols()*akt.Y.numRows()); - { - WallTimer tim("\tUnivariate time ", true); - int dd; - ll1 = kut.filter_and_smooth(sres1, per1, dd); - per1 /= akt.Y.numRows(); - d1 = dd/akt.Y.numRows(); - printf("\t\tll=%f per=%d d=%d(%d)\n", ll1, per1, d1, dd); - if (per1 == akt.Y.numCols()) { - SmootherResults sres_uni(akt.Y.numCols()); - sres_uni.import(sres1, akt.Y.numRows()); - sres_uni.exportAlpha(alpha1); - sres_uni.exportEta(eta1); - sres_uni.exportV(V1); - } else { - printf("\t\tNot finished.\n"); - } - } - - // compare - if (per == per1 && per == akt.Y.numCols()) { - WallTimer tim("\tComparison time ", true); - alpha.add(-1.0, alpha1); - eta.add(-1.0, eta1); - V.add(-1.0, V1); - int maxd = std::max(d,d1); - for (int t = 1; t <= maxd; t++) { - Vector alphat(alpha, t-1); - printf("\t\tt=%d alpha error %10.6g\n",t,alphat.getMax()); - Vector etat(eta, t-1); - printf("\t\tt=%d eta error %10.6g\n",t,etat.getMax()); - GeneralMatrix Vt(V, 0, (t-1)*akt.T.numRows(), akt.T.numRows(), akt.T.numRows()); - printf("\t\tt=%d V error %10.6g\n",t,V.getData().getMax()); - } - GeneralMatrix alpha_rest(alpha, 0, maxd, akt.T.numRows(), alpha.numCols()-maxd); - printf("\t\tt=%d.. alpha error %10.6g\n",maxd+1,alpha_rest.getData().getMax()); - GeneralMatrix eta_rest(eta, 0, maxd, akt.R.numCols(), eta.numCols()-maxd); - printf("\t\tt=%d.. eta error %10.6g\n",maxd+1,eta_rest.getData().getMax()); - GeneralMatrix V_rest(V, 0, maxd*akt.T.numRows(), akt.T.numRows(), - V.numCols()-maxd*akt.T.numRows()); - printf("\t\tt=%d.. V error %10.6g\n",maxd+1,V_rest.getData().getMax()); - } - - delete init; - - return true; -} - - -/****************************************************/ -/* definition of TestRunnable subclasses */ -/****************************************************/ -class SmallNonDiffuse : public TestRunnable { -public: - SmallNonDiffuse() - : TestRunnable("Non-diffuse small (p=2,m=3,r=4)") {} - - bool run() const - { - filter_and_smoother("small2x3x4", false); - return true; - } -}; - -class SmallDiffuse : public TestRunnable { -public: - SmallDiffuse() - : TestRunnable("Diffuse small (p=2,m=3,r=4)") {} - - bool run() const - { - return filter_and_smoother("small2x3x4", true); - } -}; - -class MiddleNonDiffuse : public TestRunnable { -public: - MiddleNonDiffuse() - : TestRunnable("Non-diffuse middle (p=10,m=15,r=12)") {} - - bool run() const - { - return filter_and_smoother("10x15x12", false); - } -}; - -class MiddleDiffuse : public TestRunnable { -public: - MiddleDiffuse() - : TestRunnable("Diffuse middle (p=10,m=15,r=12)") {} - - bool run() const - { - return filter_and_smoother("10x15x12", true); - } -}; - -class SOEDiffuse : public TestRunnable { -public: - SOEDiffuse() - : TestRunnable("Diffuse soe (p=8,m=25,r=15)") {} - - bool run() const - { - return filter_and_smoother("soe8x25x15", true); - } -}; - -int main() -{ - TestRunnable* all_tests[50]; - // fill in vector of all tests - int num_tests = 0; - all_tests[num_tests++] = new SmallNonDiffuse(); - all_tests[num_tests++] = new SmallDiffuse(); - all_tests[num_tests++] = new MiddleNonDiffuse(); - all_tests[num_tests++] = new MiddleDiffuse(); - all_tests[num_tests++] = new SOEDiffuse(); - - // launch the tests - int success = 0; - for (int i = 0; i < num_tests; i++) { - try { - if (all_tests[i]->test()) - success++; - } catch (const TSException& e) { - printf("Caugth TS exception in <%s>:\n", all_tests[i]->getName()); - e.print(); - } catch (SylvException& e) { - printf("Caught Sylv exception in <%s>:\n", all_tests[i]->getName()); - e.printMessage(); - } - } - - printf("There were %d tests that failed out of %d tests run.\n", - num_tests - success, num_tests); - - // destroy - for (int i = 0; i < num_tests; i++) { - delete all_tests[i]; - } - - return 0; -}