Estimation DLL: refactor detrending stuff to avoid allocating the matrix for detrended data at every iteration
parent
e347f2ae7a
commit
e5d093c6ad
|
@ -31,9 +31,9 @@ DetrendData::DetrendData(const bool INlogLinear) //, Vector& INtrendCoeff)
|
||||||
};
|
};
|
||||||
|
|
||||||
void
|
void
|
||||||
DetrendData::detrend(const VectorView &SteadyState, const MatrixConstView &dataView, Matrix &Y)
|
DetrendData::detrend(const VectorView &SteadyState, const MatrixConstView &dataView,
|
||||||
|
MatrixView &detrendedDataView)
|
||||||
{
|
{
|
||||||
// dummy
|
detrendedDataView = dataView;
|
||||||
Y=dataView;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,7 @@ class DetrendData
|
||||||
public:
|
public:
|
||||||
virtual ~DetrendData(){};
|
virtual ~DetrendData(){};
|
||||||
DetrendData(const bool logLinear); // add later Vector& trendCoeff);
|
DetrendData(const bool logLinear); // add later Vector& trendCoeff);
|
||||||
void detrend(const VectorView &SteadyState, const MatrixConstView &dataView, Matrix &Y);
|
void detrend(const VectorView &SteadyState, const MatrixConstView &dataView, MatrixView &detrendedDataView);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const bool logLinear;
|
const bool logLinear;
|
||||||
|
|
|
@ -59,10 +59,11 @@ InitializeKalmanFilter::InitializeKalmanFilter(const std::string &dynamicDllFile
|
||||||
void
|
void
|
||||||
InitializeKalmanFilter::initialize(VectorView &steadyState, const Vector &deepParams, Matrix &R,
|
InitializeKalmanFilter::initialize(VectorView &steadyState, const Vector &deepParams, Matrix &R,
|
||||||
const Matrix &Q, Matrix &RQRt, Matrix &T,
|
const Matrix &Q, Matrix &RQRt, Matrix &T,
|
||||||
double &penalty, const MatrixConstView &dataView, Matrix &Y, int &info)
|
double &penalty, const MatrixConstView &dataView,
|
||||||
|
MatrixView &detrendedDataView, int &info)
|
||||||
{
|
{
|
||||||
modelSolution.compute(steadyState, deepParams, g_x, g_u);
|
modelSolution.compute(steadyState, deepParams, g_x, g_u);
|
||||||
detrendData.detrend(steadyState, dataView, Y);
|
detrendData.detrend(steadyState, dataView, detrendedDataView);
|
||||||
|
|
||||||
setT(T, info);
|
setT(T, info);
|
||||||
setRQR(R, Q, RQRt, info);
|
setRQR(R, Q, RQRt, info);
|
||||||
|
@ -72,9 +73,10 @@ InitializeKalmanFilter::initialize(VectorView &steadyState, const Vector &deepPa
|
||||||
void
|
void
|
||||||
InitializeKalmanFilter::initialize(VectorView &steadyState, const Vector &deepParams, Matrix &R,
|
InitializeKalmanFilter::initialize(VectorView &steadyState, const Vector &deepParams, Matrix &R,
|
||||||
const Matrix &Q, Matrix &RQRt, Matrix &T, Matrix &Pstar, Matrix &Pinf,
|
const Matrix &Q, Matrix &RQRt, Matrix &T, Matrix &Pstar, Matrix &Pinf,
|
||||||
double &penalty, const MatrixConstView &dataView, Matrix &Y, int &info)
|
double &penalty, const MatrixConstView &dataView,
|
||||||
|
MatrixView &detrendedDataView, int &info)
|
||||||
{
|
{
|
||||||
initialize(steadyState, deepParams, R, Q, RQRt, T, penalty, dataView, Y, info);
|
initialize(steadyState, deepParams, R, Q, RQRt, T, penalty, dataView, detrendedDataView, info);
|
||||||
setPstar(Pstar, Pinf, T, RQRt, info);
|
setPstar(Pstar, Pinf, T, RQRt, info);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -50,10 +50,10 @@ public:
|
||||||
virtual ~InitializeKalmanFilter();
|
virtual ~InitializeKalmanFilter();
|
||||||
// initialise all KF matrices
|
// initialise all KF matrices
|
||||||
void initialize(VectorView &steadyState, const Vector &deepParams, Matrix &R, const Matrix &Q, Matrix &RQRt,
|
void initialize(VectorView &steadyState, const Vector &deepParams, Matrix &R, const Matrix &Q, Matrix &RQRt,
|
||||||
Matrix &T, Matrix &Pstar, Matrix &Pinf, double &penalty, const MatrixConstView &dataView, Matrix &Y, int &info);
|
Matrix &T, Matrix &Pstar, Matrix &Pinf, double &penalty, const MatrixConstView &dataView, MatrixView &detrendedDataView, int &info);
|
||||||
// initialise parameter dependent KF matrices only but not Ps
|
// initialise parameter dependent KF matrices only but not Ps
|
||||||
void initialize(VectorView &steadyState, const Vector &deepParams, Matrix &R, const Matrix &Q, Matrix &RQRt,
|
void initialize(VectorView &steadyState, const Vector &deepParams, Matrix &R, const Matrix &Q, Matrix &RQRt,
|
||||||
Matrix &T, double &penalty, const MatrixConstView &dataView, Matrix &Y, int &info);
|
Matrix &T, double &penalty, const MatrixConstView &dataView, MatrixView &detrendedDataView, int &info);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const double lyapunov_tol;
|
const double lyapunov_tol;
|
||||||
|
|
|
@ -73,18 +73,17 @@ KalmanFilter::compute_zeta_varobs_back_mixed(const std::vector<size_t> &zeta_bac
|
||||||
double
|
double
|
||||||
KalmanFilter::compute(const MatrixConstView &dataView, VectorView &steadyState,
|
KalmanFilter::compute(const MatrixConstView &dataView, VectorView &steadyState,
|
||||||
const Matrix &Q, const Matrix &H, const Vector &deepParams,
|
const Matrix &Q, const Matrix &H, const Vector &deepParams,
|
||||||
VectorView &vll, size_t start, size_t period, double &penalty, int &info)
|
VectorView &vll, MatrixView &detrendedDataView,
|
||||||
|
size_t start, size_t period, double &penalty, int &info)
|
||||||
{
|
{
|
||||||
Matrix Y(dataView.getRows(), dataView.getCols()); // data
|
|
||||||
|
|
||||||
if(period==0) // initialise all KF matrices
|
if(period==0) // initialise all KF matrices
|
||||||
initKalmanFilter.initialize(steadyState, deepParams, R, Q, RQRt, T, Pstar, Pinf,
|
initKalmanFilter.initialize(steadyState, deepParams, R, Q, RQRt, T, Pstar, Pinf,
|
||||||
penalty, dataView, Y, info);
|
penalty, dataView, detrendedDataView, info);
|
||||||
else // initialise parameter dependent KF matrices only but not Ps
|
else // initialise parameter dependent KF matrices only but not Ps
|
||||||
initKalmanFilter.initialize(steadyState, deepParams, R, Q, RQRt, T,
|
initKalmanFilter.initialize(steadyState, deepParams, R, Q, RQRt, T,
|
||||||
penalty, dataView, Y, info);
|
penalty, dataView, detrendedDataView, info);
|
||||||
|
|
||||||
return filter(Y, H, vll, start, info);
|
return filter(detrendedDataView, H, vll, start, info);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -92,13 +91,13 @@ KalmanFilter::compute(const MatrixConstView &dataView, VectorView &steadyState,
|
||||||
* 30:*
|
* 30:*
|
||||||
*/
|
*/
|
||||||
double
|
double
|
||||||
KalmanFilter::filter(const Matrix &dataView, const Matrix &H, VectorView &vll, size_t start, int &info)
|
KalmanFilter::filter(const MatrixView &detrendedDataView, const Matrix &H, VectorView &vll, size_t start, int &info)
|
||||||
{
|
{
|
||||||
double loglik=0.0, ll, logFdet, Fdet;
|
double loglik=0.0, ll, logFdet, Fdet;
|
||||||
size_t p = Finv.getRows();
|
size_t p = Finv.getRows();
|
||||||
|
|
||||||
bool nonstationary = true;
|
bool nonstationary = true;
|
||||||
for (size_t t = 0; t < dataView.getCols(); ++t)
|
for (size_t t = 0; t < detrendedDataView.getCols(); ++t)
|
||||||
{
|
{
|
||||||
if (nonstationary)
|
if (nonstationary)
|
||||||
{
|
{
|
||||||
|
@ -138,7 +137,7 @@ KalmanFilter::filter(const Matrix &dataView, const Matrix &H, VectorView &vll,
|
||||||
}
|
}
|
||||||
|
|
||||||
// err= Yt - Za
|
// err= Yt - Za
|
||||||
MatrixConstView yt(dataView, 0, t, dataView.getRows(), 1); // current observation vector
|
MatrixConstView yt(detrendedDataView, 0, t, detrendedDataView.getRows(), 1); // current observation vector
|
||||||
vt = yt;
|
vt = yt;
|
||||||
blas::gemm("N", "N", -1.0, Z, a_init, 1.0, vt);
|
blas::gemm("N", "N", -1.0, Z, a_init, 1.0, vt);
|
||||||
// at+1= T(at+ KFinv *err)
|
// at+1= T(at+ KFinv *err)
|
||||||
|
|
|
@ -55,7 +55,8 @@ public:
|
||||||
|
|
||||||
double compute(const MatrixConstView &dataView, VectorView &steadyState,
|
double compute(const MatrixConstView &dataView, VectorView &steadyState,
|
||||||
const Matrix &Q, const Matrix &H, const Vector &deepParams,
|
const Matrix &Q, const Matrix &H, const Vector &deepParams,
|
||||||
VectorView &vll, size_t start, size_t period, double &penalty, int &info);
|
VectorView &vll, MatrixView &detrendedDataView, size_t start, size_t period,
|
||||||
|
double &penalty, int &info);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const std::vector<size_t> zeta_varobs_back_mixed;
|
const std::vector<size_t> zeta_varobs_back_mixed;
|
||||||
|
@ -77,7 +78,7 @@ private:
|
||||||
InitializeKalmanFilter initKalmanFilter; //Initialise KF matrices
|
InitializeKalmanFilter initKalmanFilter; //Initialise KF matrices
|
||||||
|
|
||||||
// Method
|
// Method
|
||||||
double filter(const Matrix &data, const Matrix &H, VectorView &vll, size_t start, int &info);
|
double filter(const MatrixView &detrendedDataView, const Matrix &H, VectorView &vll, size_t start, int &info);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -34,8 +34,8 @@ LogLikelihoodMain::LogLikelihoodMain( //const Matrix &data_arg, Vector &deepPara
|
||||||
: estSubsamples(estiParDesc.estSubsamples),
|
: estSubsamples(estiParDesc.estSubsamples),
|
||||||
logLikelihoodSubSample(dynamicDllFile, estiParDesc, n_endo, n_exo, zeta_fwrd_arg, zeta_back_arg, zeta_mixed_arg, zeta_static_arg, qz_criterium,
|
logLikelihoodSubSample(dynamicDllFile, estiParDesc, n_endo, n_exo, zeta_fwrd_arg, zeta_back_arg, zeta_mixed_arg, zeta_static_arg, qz_criterium,
|
||||||
varobs, riccati_tol, lyapunov_tol, info_arg),
|
varobs, riccati_tol, lyapunov_tol, info_arg),
|
||||||
vll(estiParDesc.getNumberOfPeriods()) // time dimension size of data
|
vll(estiParDesc.getNumberOfPeriods()), // time dimension size of data
|
||||||
|
detrendedData(varobs.size(), estiParDesc.getNumberOfPeriods())
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -55,9 +55,12 @@ LogLikelihoodMain::compute(Matrix &steadyState, const Vector &estParams, Vector
|
||||||
|
|
||||||
MatrixConstView dataView(data, 0, estSubsamples[i].startPeriod,
|
MatrixConstView dataView(data, 0, estSubsamples[i].startPeriod,
|
||||||
data.getRows(), estSubsamples[i].endPeriod-estSubsamples[i].startPeriod+1);
|
data.getRows(), estSubsamples[i].endPeriod-estSubsamples[i].startPeriod+1);
|
||||||
|
MatrixView detrendedDataView(detrendedData, 0, estSubsamples[i].startPeriod,
|
||||||
|
data.getRows(), estSubsamples[i].endPeriod-estSubsamples[i].startPeriod+1);
|
||||||
|
|
||||||
VectorView vllView(vll, estSubsamples[i].startPeriod, estSubsamples[i].endPeriod-estSubsamples[i].startPeriod+1);
|
VectorView vllView(vll, estSubsamples[i].startPeriod, estSubsamples[i].endPeriod-estSubsamples[i].startPeriod+1);
|
||||||
logLikelihood += logLikelihoodSubSample.compute(vSteadyState, dataView, estParams, deepParams,
|
logLikelihood += logLikelihoodSubSample.compute(vSteadyState, dataView, estParams, deepParams,
|
||||||
Q, H, vllView, info, start, i);
|
Q, H, vllView, detrendedDataView, info, start, i);
|
||||||
}
|
}
|
||||||
return logLikelihood;
|
return logLikelihood;
|
||||||
};
|
};
|
||||||
|
|
|
@ -32,6 +32,7 @@ private:
|
||||||
std::vector<EstimationSubsample> &estSubsamples; // reference to member of EstimatedParametersDescription
|
std::vector<EstimationSubsample> &estSubsamples; // reference to member of EstimatedParametersDescription
|
||||||
LogLikelihoodSubSample logLikelihoodSubSample;
|
LogLikelihoodSubSample logLikelihoodSubSample;
|
||||||
Vector vll; // vector of all KF step likelihoods
|
Vector vll; // vector of all KF step likelihoods
|
||||||
|
Matrix detrendedData;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual ~LogLikelihoodMain();
|
virtual ~LogLikelihoodMain();
|
||||||
|
|
|
@ -44,12 +44,12 @@ LogLikelihoodSubSample::LogLikelihoodSubSample(const std::string &dynamicDllFile
|
||||||
|
|
||||||
double
|
double
|
||||||
LogLikelihoodSubSample::compute(VectorView &steadyState, const MatrixConstView &dataView, const Vector &estParams, Vector &deepParams,
|
LogLikelihoodSubSample::compute(VectorView &steadyState, const MatrixConstView &dataView, const Vector &estParams, Vector &deepParams,
|
||||||
Matrix &Q, Matrix &H, VectorView &vll, int &info, size_t start, size_t period)
|
Matrix &Q, Matrix &H, VectorView &vll, MatrixView &detrendedDataView, int &info, size_t start, size_t period)
|
||||||
{
|
{
|
||||||
|
|
||||||
updateParams(estParams, deepParams, Q, H, period);
|
updateParams(estParams, deepParams, Q, H, period);
|
||||||
if (info == 0)
|
if (info == 0)
|
||||||
logLikelihood = kalmanFilter.compute(dataView, steadyState, Q, H, deepParams, vll, start, period, penalty, info);
|
logLikelihood = kalmanFilter.compute(dataView, steadyState, Q, H, deepParams, vll, detrendedDataView, start, period, penalty, info);
|
||||||
// else
|
// else
|
||||||
// logLikelihood+=penalty;
|
// logLikelihood+=penalty;
|
||||||
|
|
||||||
|
|
|
@ -39,7 +39,7 @@ public:
|
||||||
const std::vector<size_t> &varobs_arg, double riccati_tol_in, double lyapunov_tol, int &info);
|
const std::vector<size_t> &varobs_arg, double riccati_tol_in, double lyapunov_tol, int &info);
|
||||||
|
|
||||||
double compute(VectorView &steadyState, const MatrixConstView &dataView, const Vector &estParams, Vector &deepParams,
|
double compute(VectorView &steadyState, const MatrixConstView &dataView, const Vector &estParams, Vector &deepParams,
|
||||||
Matrix &Q, Matrix &H, VectorView &vll, int &info, size_t start, size_t period);
|
Matrix &Q, Matrix &H, VectorView &vll, MatrixView &detrendedDataView, int &info, size_t start, size_t period);
|
||||||
virtual ~LogLikelihoodSubSample();
|
virtual ~LogLikelihoodSubSample();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
Loading…
Reference in New Issue