1 #ifndef __BAYESM_H__
2 #define __BAYESM_H__
3 
4 #include <RcppArmadillo.h>
5 #include <Rcpp.h>
6 #include <stdio.h>
7 #include <time.h>
8 
9 using namespace arma;
10 using namespace Rcpp;
11 
12 //CUSTOM STRUCTS--------------------------------------------------------------------------------------------------
13 //Used in rhierLinearMixture, rhierLinearModel, rhierMnlDP, rhierMnlRwMixture, rhierNegbinRw, and rsurGibbs
14 struct moments{
15   vec y;
16   mat X;
17   mat XpX;
18   vec Xpy;
19   mat hess;
20 };
21 
22 //Used in rhierLinearMixture, rhierLinearModel, rhierMnlRWMixture, and utilityFunctions.cpp
23 struct unireg{
24     vec beta;
25     double sigmasq;
26   };
27 
28 //Used in rhierMnlDP, rhierMnlRwMixture, and utilityFunctions.cpp
29 struct mnlMetropOnceOut{
30   vec betadraw;
31   int stay;
32   double oldll;
33 };
34 
35 //Used in rDPGibbs, rhierMnlDP, rivDP, and utilityFunctions.cpp
36 struct lambda{
37     vec mubar;
38     double Amu;
39     double nu;
40     mat V;
41 };
42 
43 //Used in rDPGibbs, rhierMnlDP, rivDP, and utilityFunctions.cpp
44 struct priorAlpha{
45   double power;
46   double alphamin;
47   double alphamax;
48   int n;
49 };
50 
51 //Used  in rDPGibbs, rhierMnlDP, rivDP, and utilityFunctions.cpp
52 struct murooti{
53   vec mu;
54   mat rooti;
55 };
56 
57 //Used in rDPGibbs, rhierMnlDP, rivDP, and utilityFunctions.cpp
58 struct thetaStarIndex{
59   ivec indic;
60   std::vector<murooti> thetaStar_vector;
61 };
62 
63 //Used in rhierMnlDP, rivDP
64 struct DPOut{
65   ivec indic;
66   std::vector<murooti> thetaStar_vector;
67   std::vector<murooti> thetaNp1_vector;
68   double alpha;
69   int Istar;
70   lambda lambda_struct;
71 };
72 
73 //EXPOSED FUNCTIONS-----------------------------------------------------------------------------------------------
74 List rwishart(double nu, mat const& V);
75 
76 List rmultireg(mat const& Y, mat const& X, mat const& Bbar, mat const& A, double nu, mat const& V);
77 
78 vec rdirichlet(vec const& alpha);
79 
80 double llmnl(vec const& beta, vec const& y, mat const& X);
81 
82 mat lndIChisq(double nu, double ssq, mat const& X);
83 
84 double lndMvst(vec const& x, double nu, vec const& mu, mat const& rooti, bool NORMC);
85 
86 double lndMvn(vec const& x, vec const& mu, mat const& rooti);
87 
88 double lndIWishart(double nu, mat const& V, mat const& IW);
89 
90 vec rmvst(double nu, vec const& mu, mat const& root);
91 
92 vec breg(vec const& y, mat const& X, vec const& betabar, mat const& A);
93 
94 vec cgetC(double e, int k);
95 
96 List rmixGibbs( mat const& y,  mat const& Bbar, mat const& A, double nu, mat const& V,  vec const& a, vec const& p,  vec const& z);
97   //rmixGibbs contains the following support functions, which are called ONLY THROUGH rmixGibbs: drawCompsFromLabels, drawLabelsFromComps, and drawPFromLabels
98 
99 //SUPPORT FUNCTIONS (contained in utilityFunctions.cpp and trunNorm.cpp)-----------------------------------------------------------
100 //Used in rmvpGibbs and rmnpGibbs
101 vec condmom(vec const& x, vec const& mu, mat const& sigmai, int p, int j);
102 
103 //double rtrun1(double mu, double sigma,double trunpt, int above); <--NO LONGER USED
104 
105 double trunNorm(double mu,double sig, double trunpt, int above);
106 
107 //Used in rhierLinearModel, rhierLinearMixture and rhierMnlRWMixture
108 mat drawDelta(mat const& x,mat const& y,vec const& z,List const& comps,vec const& deltabar,mat const& Ad);
109 
110 unireg runiregG(vec const& y, mat const& X, mat const& XpX, vec const& Xpy, double sigmasq, mat const& A, vec const& Abetabar, double nu, double ssq);
111 
112 //Used in rnegbinRW and rhierNegbinRw
113 double llnegbin(vec const& y, vec const& lambda, double alpha, bool constant);
114 
115 double lpostbeta(double alpha, vec const& beta, mat const& X, vec const& y, vec const& betabar, mat const& rootA);
116 
117 double lpostalpha(double alpha, vec const& beta, mat const& X, vec const& y, double a, double b);
118 
119 //Used in rbprobitGibbs (uses breg1 and trunNorm_vec) and rordprobitGibbs (uses breg1 and rtrunVec)
120 vec breg1(mat const& root, mat const& X, vec const& y, vec const& Abetabar);
121 
122 vec rtrunVec(vec const& mu,vec const& sigma, vec const& a, vec const& b);
123 
124 vec trunNorm_vec(vec const& mu, vec const& sig, vec const& trunpt, vec const& above);
125 
126 //Used in rhierMnlDP and rhierMnlRwMixture
127 mnlMetropOnceOut mnlMetropOnce(vec const& y, mat const& X, vec const& oldbeta, double oldll,double s, mat const& incroot, vec const& betabar, mat const& rootpi);
128 
129 //Used in rDPGibbs, rhierMnlDP, rivDP
130 int rmultinomF(vec const& p);
131 
132 mat yden(std::vector<murooti> const& thetaStar, mat const& y);
133 
134 ivec numcomp(ivec const& indic, int k);
135 
136 murooti thetaD(mat const& y, lambda const& lambda_struct);
137 
138 thetaStarIndex thetaStarDraw(ivec indic, std::vector<murooti> thetaStar_vector, mat const& y, mat ydenmat, vec const& q0v, double alpha, lambda const& lambda_struct, int maxuniq);
139 
140 vec q0(mat const& y, lambda const& lambda_struct);
141 
142 vec seq_rcpp(double from, double to, int len); //kept _rcpp due to conflict with base seq function
143 
144 double alphaD(priorAlpha const& priorAlpha_struct, int Istar, int gridsize);
145 
146 murooti GD(lambda const& lambda_struct);
147 
148 lambda lambdaD(lambda const& lambda_struct, std::vector<murooti> const& thetaStar_vector, vec const& alim, vec const& nulim, vec const& vlim, int gridsize);
149 
150 //FUNCTION TIMING (contained in functionTiming.cpp)---------------------------------------------------------------
151 void startMcmcTimer();
152 void infoMcmcTimer(int rep, int R);
153 void endMcmcTimer();
154 
155 #endif
156