Skip to content

Multivariate t distribution #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions TMB/inst/include/tmbutils/density.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ typedef matrix<scalartype> matrixtype; \
typedef array<scalartype> arraytype;

#define VARIANCE_NOT_YET_IMPLEMENTED vectortype variance(){};
#define JACOBIAN_NOT_YET_IMPLEMENTED arraytype jacobian(arraytype x){};

/** \brief Multivariate normal distribution with user supplied covariance matrix

Expand Down Expand Up @@ -201,6 +202,59 @@ class N01{
VARIANCE_NOT_YET_IMPLEMENTED;
};



/** \brief Multivariate t distribution with user supplied scale matrix

Class to evaluate the negative log density of a multivariate t distributed variable with general scale matrix Sigma and location vector 0 and df degrees of freedom.
This class should not be used as input distribution for SEPARABLE_t or PROJ_t.
*/
template <class scalartype_>
class MVT_t: public MVNORM_t<scalartype_>
{
TYPEDEFS(scalartype_);
#include "lgamma.hpp"
scalartype df;

public:
MVT_t()
: MVNORM_t<scalartype>()
{}

MVT_t(scalartype df_)
: MVNORM_t<scalartype>()
{
setdf(df_);
}
MVT_t(matrixtype Sigma_, scalartype df_)
: MVNORM_t<scalartype>(Sigma_)
{
setdf(df_);
}

void setdf(scalartype df_){
df = df_;
}

/** \brief Covariance extractor */
matrixtype cov(){
if(df > 2){
return this->Sigma*df/(df-scalartype(2.0));
}
}

/** \brief Evaluate the negative log density */
scalartype operator()(vectortype x){
scalartype p = x.size();
return -lgamma(scalartype(0.5)*(df+p))+lgamma(scalartype(0.5)*df)+p*scalartype(0.5)*log(df)+p*lgamma(scalartype(0.5))-scalartype(0.5)*this->logdetQ + scalartype(0.5)*(df+p)*log(scalartype(1.0)+this->Quadform(x)/df);

}
JACOBIAN_NOT_YET_IMPLEMENTED;
VARIANCE_NOT_YET_IMPLEMENTED;
};



/** \brief Stationary AR1 process

Class to evaluate the negative log density of a (multivariate) AR1
Expand Down