double MultiDimGauss::getValueForVector(gsl_vector* point) const { assert(covarianceMatrixes.size() == 0 || expectedValues[0]->size == point->size); double result = 0.0; for(unsigned int i = 0; i < covarianceMatrixes.size(); ++i) { result += dmvnorm(point->size, point, expectedValues[i], covarianceMatrixes[i]) * heightFactors[i]; } return result; }
static double prior_u_Gp(int gn, SEXP da){ SEXP V = GET_SLOT(da, install("Sigma")); int *Gp = Gp_SLOT(da), *nc = NCOL_SLOT(da), *nlev = NLEV_SLOT(da) ; double *v = REAL(VECTOR_ELT(V, gn)), *u = U_SLOT(da), ans = 0.0; if (nc[gn] == 1) { /* univariate normal */ for (int j = 0; j < nlev[gn]; j++) ans += -0.5 * u[Gp[gn] + j] * u[Gp[gn] + j] / v[0]; return ans ; } else { /* multivariate normal */ double *xv = Alloca(nc[gn], double), *iv = Alloca(nc[gn] * nc[gn], double); R_CheckStack() ; solve_po(nc[gn], v, iv) ; for (int j = 0; j < nlev[gn]; j++){ for (int i = 0; i < nc[gn]; i++) xv[i] = u[Gp[gn] + i * nlev[gn] + j] ; ans += dmvnorm(nc[gn], xv, (double*) NULL, iv) ; } return ans ; } }
void ba81NormalQuad::setup(double Qwidth, int Qpoints, double *means, Eigen::MatrixXd &priCov, Eigen::VectorXd &sVar) { quadGridSize = Qpoints; numSpecific = sVar.rows() * sVar.cols(); primaryDims = priCov.rows(); maxDims = primaryDims + (numSpecific? 1 : 0); maxAbilities = primaryDims + numSpecific; if (maxAbilities == 0) { setup0(); return; } totalQuadPoints = 1; for (int dx=0; dx < maxDims; dx++) { totalQuadPoints *= quadGridSize; } if (int(Qpoint.size()) != quadGridSize) { Qpoint.clear(); Qpoint.reserve(quadGridSize); double qgs = quadGridSize-1; for (int px=0; px < quadGridSize; ++px) { Qpoint.push_back(px * 2 * Qwidth / qgs - Qwidth); } } std::vector<double> tmpWherePrep(totalQuadPoints * maxDims); Eigen::VectorXi quad(maxDims); for (int qx=0; qx < totalQuadPoints; qx++) { double *wh = tmpWherePrep.data() + qx * maxDims; decodeLocation(qx, maxDims, quad.data()); pointToWhere(quad.data(), wh, maxDims); } totalPrimaryPoints = totalQuadPoints; weightTableSize = totalQuadPoints; if (numSpecific) { totalPrimaryPoints /= quadGridSize; speQarea.resize(quadGridSize * numSpecific); weightTableSize *= numSpecific; } std::vector<double> tmpPriQarea; tmpPriQarea.reserve(totalPrimaryPoints); { Eigen::VectorXd where(primaryDims); for (int qx=0; qx < totalPrimaryPoints; qx++) { decodeLocation(qx, primaryDims, quad.data()); pointToWhere(quad.data(), where.data(), primaryDims); double den = exp(dmvnorm(primaryDims, where.data(), means, priCov.data())); tmpPriQarea.push_back(den); } } std::vector<int> priOrder; priOrder.reserve(totalPrimaryPoints); for (int qx=0; qx < totalPrimaryPoints; qx++) { priOrder.push_back(qx); } if (0) { sortAreaHelper priCmp(tmpPriQarea); std::sort(priOrder.begin(), priOrder.end(), priCmp); } priQarea.clear(); priQarea.reserve(totalPrimaryPoints); double totalArea = 0; for (int qx=0; qx < totalPrimaryPoints; qx++) { double den = tmpPriQarea[priOrder[qx]]; priQarea.push_back(den); //double prevTotalArea = totalArea; totalArea += den; // if (totalArea == prevTotalArea) { // mxLog("%.4g / %.4g = %.4g", den, totalArea, den / totalArea); // } } for (int qx=0; qx < totalPrimaryPoints; qx++) { priQarea[qx] *= One; priQarea[qx] /= totalArea; //mxLog("%.5g,", priQarea[qx]); } for (int sgroup=0; sgroup < numSpecific; sgroup++) { totalArea = 0; double mean = means[primaryDims + sgroup]; double var = sVar(sgroup); for (int qx=0; qx < quadGridSize; qx++) { double den = exp(dmvnorm(1, &Qpoint[qx], &mean, &var)); speQarea[sIndex(sgroup, qx)] = den; totalArea += den; } for (int qx=0; qx < quadGridSize; qx++) { speQarea[sIndex(sgroup, qx)] /= totalArea; } } //pda(speQarea.data(), numSpecific, quadGridSize); for (int sx=0; sx < int(speQarea.size()); ++sx) { speQarea[sx] *= One; } //pda(speQarea.data(), numSpecific, quadGridSize); wherePrep.clear(); wherePrep.reserve(totalQuadPoints * maxDims); if (numSpecific == 0) { for (int qx=0; qx < totalPrimaryPoints; qx++) { int sortq = priOrder[qx] * maxDims; for (int dx=0; dx < maxDims; ++dx) { wherePrep.push_back(tmpWherePrep[sortq + dx]); } } } else { for (int qx=0; qx < totalPrimaryPoints; ++qx) { int sortq = priOrder[qx] * quadGridSize; for (int sx=0; sx < quadGridSize; ++sx) { int base = (sortq + sx) * maxDims; for (int dx=0; dx < maxDims; ++dx) { wherePrep.push_back(tmpWherePrep[base + dx]); } } } } // recompute whereGram because the order might have changed whereGram.resize(triangleLoc1(maxDims), totalQuadPoints); for (int qx=0; qx < totalQuadPoints; qx++) { double *wh = wherePrep.data() + qx * maxDims; gramProduct(wh, maxDims, &whereGram.coeffRef(0, qx)); } }