| 1 |
/* |
| 2 |
* Copyright (C) 1996-2011 Daniel Waggoner and Tao Zha |
| 3 |
* |
| 4 |
* This free software: you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation, either version 3 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* It is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* If you did not received a copy of the GNU General Public License |
| 15 |
* with this software, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
|
| 18 |
#ifndef __DIRICHLET_RESTRICTIONS__ |
| 19 |
#define __DIRICHLET_RESTRICTIONS__ |
| 20 |
|
| 21 |
#ifdef __cplusplus |
| 22 |
extern "C" { |
| 23 |
#endif |
| 24 |
|
| 25 |
#include "dw_matrix.h" |
| 26 |
#include "dw_switch.h" |
| 27 |
|
| 28 |
|
| 29 |
typedef struct |
| 30 |
{ |
| 31 |
//=== Number of states and quasi-free variables === |
| 32 |
int nstates; // number states |
| 33 |
int total_Dirichlet_parameters; // total_Dirichlet_parameters = sv->nfree + dw_DimA(DirichletDim) |
| 34 |
|
| 35 |
//=== Workspace for Dirichlet parameters === |
| 36 |
PRECISION *B; // Workspace for the dirichlet parameters. Dimension of B is total_dirichlet_parameters. |
| 37 |
PRECISION **b; // Dirichlet variables. Points to the buffer B. |
| 38 |
|
| 39 |
//=== Prior information === |
| 40 |
TMatrix Prior; // Restricted prior on the columns of Q. Must be nstates x nstates with positive elements. |
| 41 |
PRECISION *Prior_B; // Workspace for prior on the Dirichlet parameters |
| 42 |
PRECISION **Prior_b; // Dirichlet prior parameters. Points to the buffer Prior_B. |
| 43 |
|
| 44 |
//=== Restrictions === |
| 45 |
int *DirichletDim; // DirichletDim[k] = number Dirichlet parameters for the kth Dirichlet random variable |
| 46 |
int **NonZeroIndex; // nstates x nstates : Q(i,j) = (NonZeroIndex[i][j] >= 0) ? B[NonZeroIndex[i][j]]*MQ(i,j) : 0.0; |
| 47 |
TMatrix MQ; // nstates x nstates : Q(i,j) = (NonZeroIndex[i][j] >= 0) ? B[NonZeroIndex[i][j]]*MQ(i,j) : 0.0; |
| 48 |
|
| 49 |
//=== Control variables === |
| 50 |
int UseErgodic; |
| 51 |
|
| 52 |
//=== Workspace === |
| 53 |
PRECISION LogPriorConstant; |
| 54 |
PRECISION scale; |
| 55 |
|
| 56 |
} TDirichletRestrictions; |
| 57 |
|
| 58 |
void FreeDirichletRestrictions(TDirichletRestrictions *restrictions); |
| 59 |
TDirichletRestrictions* CreateDirichletRestrictions(int nstates, TMatrix Prior, int* DirichletDim, |
| 60 |
int** NonZeroIndex, TMatrix MQ); |
| 61 |
TDirichletRestrictions* DuplicateDirichletRestrictions(TDirichletRestrictions *restrictions); |
| 62 |
|
| 63 |
TMarkovStateVariable* CreateMarkovStateVariable_Mixture(int nstates, int nlags_encoded, TMatrix Prior); |
| 64 |
TMarkovStateVariable* CreateMarkovStateVariable_NoRestrictions(int nstates, int nlags_encoded, TMatrix Prior); |
| 65 |
TMarkovStateVariable* CreateMarkovStateVariable_Exclusion(int nstates, int nlags_encoded, TMatrix Prior, TMatrix Exclusion); |
| 66 |
TMarkovStateVariable* CreateMarkovStateVariable_SimpleRestrictions(int nstates, int nlags_encoded, TMatrix Prior, TMatrix* Restrictions); |
| 67 |
TMarkovStateVariable* CreateMarkovStateVariable_ConstantState(int nlags_encoded); |
| 68 |
|
| 69 |
|
| 70 |
//====== QRoutines ======// |
| 71 |
QRoutines* dirichlet_restrictions_CreateQRoutines(void); |
| 72 |
|
| 73 |
int dirichlet_restrictions_ComputeTransitionMatrix(int t, TMarkovStateVariable *sv, TStateModel *model); |
| 74 |
PRECISION dirichlet_restrictions_LogPrior_q( TMarkovStateVariable *sv); |
| 75 |
int dirichlet_restrictions_Probability_s0(TVector p, TMarkovStateVariable *sv); |
| 76 |
|
| 77 |
PRECISION dirichlet_restrictions_DrawProposalTransitionMatrixParameters(int *S, TMarkovStateVariable *sv, TStateModel *model); |
| 78 |
int dirichlet_restrictions_DrawTransitionMatrixParameters(int *S, TMarkovStateVariable *sv, TStateModel *model); |
| 79 |
int dirichlet_restrictions_DrawTransitionMatrixParametersFromPrior(TMarkovStateVariable *sv); |
| 80 |
|
| 81 |
int dirichlet_restrictions_DefaultTransitionMatrixParameters(TMarkovStateVariable *sv, TStateModel *model); |
| 82 |
int dirichlet_restrictions_Update_q_from_BaseTransitionMatrix(int t, TMarkovStateVariable *sv, TStateModel *model); |
| 83 |
|
| 84 |
//====== Utility Routines ======// |
| 85 |
TMatrix dirichlet_restrictions_CreatePriorFromDuration(int nbasestates, PRECISION duration); |
| 86 |
void dirichlet_restrictions_SetLogPriorConstant(TDirichletRestrictions *restrictions); |
| 87 |
void dirichlet_restrictions_SetTransitionMatrixParametersToPriorMean(PRECISION *q, TDirichletRestrictions *restrictions); |
| 88 |
int dirichlet_restrictions_Populate_B(PRECISION *q, TDirichletRestrictions *restrictions); |
| 89 |
void dirichlet_restrictions_Populate_q(PRECISION *q, TDirichletRestrictions *restrictions); |
| 90 |
int dirichlet_restrictions_CheckRestrictions(int* DirichletDim, int** NonZeroIndex, TMatrix MQ, TMatrix Prior, int nstates); |
| 91 |
|
| 92 |
//====== Routines to simulate and evalute Dirichlet density ======// |
| 93 |
void DrawDirichlet(PRECISION *b, PRECISION *alpha, int n); |
| 94 |
void DrawDirichlet_free(PRECISION *q, PRECISION *alpha, int n); |
| 95 |
PRECISION DrawDirichlet_kernel(PRECISION *b, PRECISION *alpha, int n); |
| 96 |
PRECISION LogDirichlet_pdf(PRECISION *q, PRECISION *alpha, int n); |
| 97 |
PRECISION LogDirichlet_kernel(PRECISION *q, PRECISION *alpha, int n); |
| 98 |
void DrawIndependentDirichlet_free(PRECISION *q, PRECISION *alpha, int *dirichlet_dims, int m); |
| 99 |
PRECISION LogIndependentDirichlet_pdf(PRECISION *q, PRECISION *alpha, int *dirichlet_dims, int m); |
| 100 |
|
| 101 |
|
| 102 |
#ifdef __cplusplus |
| 103 |
} |
| 104 |
#endif |
| 105 |
#endif |