int mutualproximity::normalize( musly_trackid seed_trackid, musly_trackid* trackids, int length, float* sim) { if (seed_trackid >= (int)norm_facts.size()) { return -1; } float seed_mu = norm_facts[seed_trackid].mu; float seed_std = norm_facts[seed_trackid].std; for (int i = 0; i < length; i++) { int tid = trackids[i]; if (tid >= (int)norm_facts.size()) { return -1; } if (tid == seed_trackid) { sim[i] = 0; continue; } float d = sim[i]; if (isnan(d)) { continue; } double p1 = 1 - normcdf((d - seed_mu)/seed_std); double p2 = 1 - normcdf((d - norm_facts[tid].mu)/norm_facts[tid].std); sim[i] = 1 - p1*p2; } return 0; }
void CGppe::Predict_CGppe_Laplace(double sigma, MatrixXd t, MatrixXd x, VectorXd idx_global, VectorXd ind_t, VectorXd ind_x, MatrixXd tstar, MatrixXd test_pair) { int Kt_ss = 1; double sigma_star, val; MatrixXd Kx_star, Kx_star_star, kstar, Kss, Css; MatrixXd Kt_star = covfunc_t->Compute(t, tstar); Kx_star = GetMatRow(Kx, test_pair.transpose()).transpose(); //maybe need some transpose? Kx_star_star = GetMat(Kx, test_pair.transpose(), test_pair.transpose()); // test to test kstar = Kron(Kt_star, Kx_star); kstar = GetMatRow(kstar, idx_global); Kss = Kt_ss * Kx_star_star; mustar = kstar.transpose() * Kinv * GetVec(f, idx_global); Css = Kss - kstar.transpose() * W * llt.solve(Kinv * kstar); sigma_star = sqrt(Css(0, 0) + Css(1, 1) - 2 * Css(0, 1) + pow(sigma, 2)); val = ( mustar(0) - mustar(1) ) / sigma_star; p = normcdf(val); }
double lnnormcdf(double x, double mean, double std_dev) { if (x > mean) return log(normcdf(x, mean, std_dev)); double a = (x-mean)/std_dev; double pfa = 0.5*erfcx(-a/ntk::math::sqrt2); pfa = log(pfa)-(a*a/2.); return pfa; }
VectorXd CGppe::deriv_log_likelihood_CGppe_fast(double sigma, const TypePair& all_pairs, VectorXd idx_global_1, VectorXd idx_global_2, int M, int N) { VectorXd deriv_loglike, z, cdf_val, pdf_val, val; // test if idx vectors are empty ? M = all_pairs.rows(); int n = M * N; z = (GetVec(f, idx_global_1) - GetVec(f, idx_global_2)) / sigma; cdf_val = normcdf(z); pdf_val = normpdf(z); val = pow(sigma,-1) * (pdf_val.cwiseQuotient(cdf_val)); return Get_Cumulative_Val(idx_global_1, val, n) - Get_Cumulative_Val(idx_global_2, val, n); }
double CGppe::log_likelihood(double sigma, TypePair all_pairs, VectorXd idx_global_1, VectorXd idx_global_2, int M, int N) { M = all_pairs.rows(); VectorXd idx_1, idx_2, z; double loglike = 0; for (int j = 0;j < M;j++) { if (all_pairs(j).rows() == 0) continue; idx_1 = ind2global(all_pairs(j).col(0), j, N); idx_2 = ind2global(all_pairs(j).col(1), j, N); z = (GetVec(f, idx_1) - GetVec(f, idx_2)) / sigma; z = normcdf(z); loglike += log(z.array()).sum(); } return loglike; }
void mexFunction(int nlhs, mxArray* plhs [],int nrhs, const mxArray* prhs []) { const double sq2=sqrt(2); double rho=* (double*)mxGetData(prhs[0]); double norm=* (double*)mxGetData(prhs[1]); double* table= mxGetPr(prhs[2]); size_t n= mxGetNumberOfElements(prhs[2]); double* x= mxGetPr(prhs[3]); size_t nx=mxGetNumberOfElements(prhs[3]); plhs[0]=mxCreateDoubleMatrix(1,nx,mxREAL); double* y=mxGetPr(plhs[0]); /*mexPrintf("nTable=%u nX=%u \n" ,n,nx); */ double y0=0.5; int pos; int i; double t,xv; for (i=0;i<nx;++i) { xv=normcdf(sq2,x[i]); /* mexPrintf("x=%f xv=%f \n",x[i],xv); */ pos= (int) (xv*(n+1)); if(pos<=1) y0=table[0]; else if(pos>=n) y0=table[n-1]; else { t= (n+1)*xv-pos; /*mexPrintf("pos=%d t=%f, %f<y<%f \n",pos,t,table[pos-1],table[pos]); */ y0=(1-t)*table[pos-1]+t*table[pos]; } y[i]=exp(inversion(rho,norm,y0,xv)); /* mexPrintf("y=%f (%f) \n",y[i],y0); */ } }
MatrixXd CGppe::deriv2_log_likelihood_CGppe_fast(double sigma, TypePair all_pairs, VectorXd idx_global_1, VectorXd idx_global_2, int M, int N) { VectorXd deriv_loglike, z, cdf_val, pdf_val, val, ratio, all_diag_idx, ind, ind_trans; M = all_pairs.rows(); int n = M * N; VectorXd consec(n); MatrixXd Deriv2 = MatrixXd::Zero(n, n); for (int i = 0;i < n;i++) { consec(i) = i; } all_diag_idx = sub2ind(n, n, consec, consec); z = (GetVec(f, idx_global_1) - GetVec(f, idx_global_2)) / sigma; cdf_val = normcdf(z); pdf_val = normpdf(z); ratio = pdf_val.array() / cdf_val.array(); val = -(1. / pow(sigma, 2)) * (ratio.array() * (z + ratio).array()); ind = sub2ind(n, n, idx_global_1, idx_global_2); Deriv2 = SetMatGenIdx(Deriv2, ind, -val); ind_trans = sub2ind(n, n, idx_global_2, idx_global_1); Deriv2 = SetMatGenIdx(Deriv2, ind_trans, -val); Deriv2 = SetMatGenIdx(Deriv2, all_diag_idx, GetMatGenIdx(Deriv2, all_diag_idx) + Get_Cumulative_Val(idx_global_1, val, n)); Deriv2 = SetMatGenIdx(Deriv2, all_diag_idx, GetMatGenIdx(Deriv2, all_diag_idx) + Get_Cumulative_Val(idx_global_2, val, n)); return Deriv2; }
// standard normal comulated density function float normcdf(float x) { float result; if (x < -7.) result = ndf(x) / sqrt(1. + x * x); else if (x > 7.) { result = 1. - normcdf(-x); /* x = -x; float tempx = ndf(x) / sqrt(1. + x * x); result = 1. - tempx; */ } else { result = 0.2316419; static float a[5] = { 0.31938153, -0.356563782, 1.781477937, -1.821255978, 1.330274429 }; result = 1. / (1 + result * fabs(x)); result = 1 - ndf(x) * (result * (a[0] + result * (a[1] + result * (a[2] + result * (a[3] + result * a[4]))))); if (x <= 0.) result = 1. - result; } return result; }
double CGppe::maximum_expected_improvement(const VectorXd & theta_t, const VectorXd& theta_x, const double& sigma, const MatrixXd& t, const MatrixXd & x, const VectorXd& idx_global, const VectorXd& ind_t, const VectorXd& ind_x, MatrixXd tstar, int N, double fbest) { VectorXd idx_xstar=Nfirst(N); int Kt_ss = 1; double mei; MatrixXd Kx_star, Kx_star_star, kstar, Kss, Css; MatrixXd Kt_star = covfunc_t->Compute(t, tstar); //dsp(GetKinv(),"Kinv"); Kx_star = GetMatRow(Kx, idx_xstar.transpose()); //maybe need some transpose? Kx_star_star = GetMat(Kx, idx_xstar.transpose(), idx_xstar.transpose()); // test to test kstar = Kron(Kt_star, Kx_star); kstar = GetMatRow(kstar, idx_global); Kss = Kt_ss * Kx_star_star; mustar = kstar.transpose() * Kinv * GetVec(f, idx_global); Css = Kss - kstar.transpose() * W * llt.solve(Kinv * kstar); varstar = Css.diagonal(); VectorXd sigmastar = sqrt(varstar.array()); VectorXd z = (fbest - mustar.array()) / sigmastar.array(); VectorXd pdfval = normpdf(z); VectorXd cdfval = normcdf(z); VectorXd inter = z.array() * (1 - cdfval.array()); VectorXd el = sigmastar.cwiseProduct(inter - pdfval); el=-1*el; mei = el.maxCoeff(); //dsp(mei,"mei"); return mei; }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { size_t NN, X_sum, P, D, Lp1; mwSize K; double *X_inds, *X_vals; double *lPhi, *lTheta, *lPsi, *W; mxArray *Km; double *inds, *vals, *num_nz; double eps; if(nlhs != NOUTPUTS) mexErrMsgTxt("Usage: see \"help sample_Xpn_kernel_meat\""); if(nrhs != NINPUTS) mexErrMsgTxt("Usage: see \"help sample_Xpn_kernel_meat\""); // Grab inputs X_inds = mxGetPr(prhs[0]); X_vals = mxGetPr(prhs[1]); lPhi = mxGetPr(prhs[2]); lTheta = mxGetPr(prhs[3]); lPsi = mxGetPr(prhs[4]); W = mxGetPr(prhs[6]); X_sum = mxGetScalar(prhs[7]); Km = (mxArray*)prhs[5]; // Cell array NN = mxGetM(prhs[0]); P = mxGetM(prhs[2]); K = mxGetN(prhs[2]); D = mxGetN(prhs[4]); Lp1 = mxGetM(prhs[6]); eps = mxGetEps(); // Allocate output plhs[0] = mxCreateDoubleMatrix(X_sum, 3, mxREAL); plhs[1] = mxCreateDoubleMatrix(X_sum, 1, mxREAL); plhs[2] = mxCreateDoubleMatrix(1, 1, mxREAL); inds = mxGetPr(plhs[0]); vals = mxGetPr(plhs[1]); num_nz = mxGetPr(plhs[2]); // Pre-allocate scratch arrays unsigned int topic_sz = K*sizeof(double); double *zeta = (double*)mxMalloc(topic_sz); double *cump_scr = (double*)mxMalloc(topic_sz); double *mnr = (double*)mxMalloc(topic_sz); // log-thinning activation function, not Phi as in the topics // Change this to just a double* //mxArray *lphi = mxCreateDoubleMatrix(D, K, mxREAL); //mxArray *res; double *A, *lphi; char *chn = "N"; // don't transpose for dgemv double onef = 1.0, zerof = 0.0; // alpha, beta in dgemv size_t onei = 1; // incx in dgemv lphi = mxMalloc(D*K*sizeof(double)); //lphi_ptr = mxGetPr(lphi); //printf("Computing lphi...\n"); //printf(" K: %d\n", K); // Compute phi{k} = p(Znk = 1) for all k here (Each one is a matrix) for (int k = 0; k < K; k++) { //printf("k: %d ", k); A = mxGetPr(mxGetCell(Km, k)); //printf("Before dgemv.."); dgemv(chn, &D, &Lp1, &onef, A, &D, &W[k*Lp1], &onei, &zerof, &lphi[k*D], &onei); //dgemv(chn, &D, &Lp1, &onef, A, &D, &W[k*Lp1], &onei, &zerof, &lphi_ptr[k*D], &onei); //printf("after dgemv\n"); } //printf("Sampling...\n"); // Probit each entry and take log normcdf(lphi, D*K); for (int ii = 0; ii < D*K; ii++) lphi[ii] = log(lphi[ii] + eps); // main loop unsigned int cur_idx = 0; unsigned int noff = X_sum; unsigned int koff = 2*X_sum; for(int ii = 0; ii < NN; ii++) { // Get indices and data (convert to 0-based) unsigned int p = X_inds[ii] - 1; unsigned int n = X_inds[ii + NN] - 1; // These arrays have NN rows unsigned int xx = X_vals[ii]; // Construct probability double zeta_max = DBL_MIN; memset(zeta,0,topic_sz); for(int k = 0; k < K; k++) { // When we say N we mean D // lZnk is NxK and lPsi is KxN so we index them backwards // lphi is NxK and contains the log-thinning probabilities //zeta[k] = lPhi[p + k*P] + lTheta[k] + lZnk[n + k*D] + lPsi[k + n*K]; zeta[k] = lPhi[p + k*P] + lTheta[k] + lphi[n + k*D] + lPsi[k + n*K]; if(zeta[k] > zeta_max) zeta_max = zeta[k]; } for(int k = 0; k < K; k++) { zeta[k] -= zeta_max; zeta[k] = exp(zeta[k]); } double zeta_sum = sum(zeta, K); for(int k = 0; k < K; k++) zeta[k] /= zeta_sum; // for(int k = 0; k < K; k++) // printf("%.2f,", zeta[k]); // printf("\n"); // Split up observed counts into topics and add to output if(xx > 1) { multirnd(mnr, xx, zeta, K, cump_scr, eps); for(int k = 0; k < K; k++) { if(mnr[k] > 0) { inds[cur_idx] = p + 1; inds[cur_idx + noff] = n + 1; inds[cur_idx + koff] = k + 1; // +1 b/c k is index vals[cur_idx] = mnr[k]; cur_idx++; } } } else { // Result of below is 1-based, so don't add 1 to kk below double kk = discrnd(zeta, K, cump_scr, eps); inds[cur_idx] = p + 1; inds[cur_idx + noff] = n + 1; inds[cur_idx + koff] = kk; vals[cur_idx] = 1; cur_idx++; } } // Free scratch memory mxFree(zeta); mxFree(cump_scr); mxFree(mnr); mxFree(lphi); *num_nz = cur_idx; // cur_idx has been incr. 1 beyond already }
//static inline float occupancy(const float mean, const float var) //{ // return normcdf((alpha*mean + beta) / sqrt(1 + alpha*alpha*var)); //} //static inline float occupancy(const float mean, const float var) //{ // return normcdf((mean - beta) / powf(sqrt(var), alpha)); //} //static inline float occupancy(const float mean, const float var) //{ // return normcdf(mean / (sqrt(var)*alpha + beta)); //} //static inline float occupancy(const float mean, const float var) //{ // return normcdf((mean - beta) / (sqrt(var)*alpha)); //} static inline float occupancy(const float mu, const float var) { //return 1.f - normcdf((alpha - mu) / sqrt(var + beta)); return normcdf((mu - alpha) / sqrt(var + beta)); }
__device__ void double_precision_math_functions() { int iX; double fX, fY; acos(1.0); acosh(1.0); asin(0.0); asinh(0.0); atan(0.0); atan2(0.0, 1.0); atanh(0.0); cbrt(0.0); ceil(0.0); copysign(1.0, -2.0); cos(0.0); cosh(0.0); cospi(0.0); cyl_bessel_i0(0.0); cyl_bessel_i1(0.0); erf(0.0); erfc(0.0); erfcinv(2.0); erfcx(0.0); erfinv(1.0); exp(0.0); exp10(0.0); exp2(0.0); expm1(0.0); fabs(1.0); fdim(1.0, 0.0); floor(0.0); fma(1.0, 2.0, 3.0); fmax(0.0, 0.0); fmin(0.0, 0.0); fmod(0.0, 1.0); frexp(0.0, &iX); hypot(1.0, 0.0); ilogb(1.0); isfinite(0.0); isinf(0.0); isnan(0.0); j0(0.0); j1(0.0); jn(-1.0, 1.0); ldexp(0.0, 0); lgamma(1.0); llrint(0.0); llround(0.0); log(1.0); log10(1.0); log1p(-1.0); log2(1.0); logb(1.0); lrint(0.0); lround(0.0); modf(0.0, &fX); nan("1"); nearbyint(0.0); nextafter(0.0, 0.0); fX = 1.0; norm(1, &fX); norm3d(1.0, 0.0, 0.0); norm4d(1.0, 0.0, 0.0, 0.0); normcdf(0.0); normcdfinv(1.0); pow(1.0, 0.0); rcbrt(1.0); remainder(2.0, 1.0); remquo(1.0, 2.0, &iX); rhypot(0.0, 1.0); rint(1.0); fX = 1.0; rnorm(1, &fX); rnorm3d(0.0, 0.0, 1.0); rnorm4d(0.0, 0.0, 0.0, 1.0); round(0.0); rsqrt(1.0); scalbln(0.0, 1); scalbn(0.0, 1); signbit(1.0); sin(0.0); sincos(0.0, &fX, &fY); sincospi(0.0, &fX, &fY); sinh(0.0); sinpi(0.0); sqrt(0.0); tan(0.0); tanh(0.0); tgamma(2.0); trunc(0.0); y0(1.0); y1(1.0); yn(1, 1.0); }