static double aterms_mu(double mya, void *mydata) { int e, t; double val = 0; #ifdef A_DEBUG float save_a = ddC.a_mu->a; double like; #endif S_remake(ddC.a_mu, mya); for (e=0; e<ddN.E; e++) { for (t=0; t<ddN.T; t++) { if ( ddS.cp_et[e][t]==0 ) continue; if (e==ddN.E-1) val += S_S(ddC.a_mu, ddS.C_eDt[e][t], ddS.cp_et[e][t]); else val += S_S(ddC.a_mu, ddS.C_eDt[e][t] + ddS.cp_et[e+1][t], ddS.cp_et[e][t]); } val += poch(ddP.b_mu[e], mya, ddS.Cp_e[e]); } myarms_evals++; #ifdef A_DEBUG yap_message("Eval aterms_mu(%lf) = %lf (S had %f)", mya, val, save_a); ddP.a_mu = mya; cache_update("am"); like = likelihood(); if ( last_val != 0 ) { yap_message(", lp=%lf diffs=%lf vs %lf\n", like, val-last_val, like-last_like); } last_like = like; last_val = val; #endif return val; }
double likelihood_PYalpha() { int i,t; double likelihood = 0; double la = 0; double lb = log(ddP.bpar); if ( ddP.apar>0 ) la = log(ddP.apar); for (i=0; i<ddN.DT; i++) { uint16_t Td_ = 0; for (t=0; t<ddN.T; t++) { if ( ddS.Ndt[i][t]>0 ) { Td_ += ddS.Tdt[i][t]; if ( ddS.Ndt[i][t]>1 ) { likelihood += S_S(ddC.SX,ddS.Ndt[i][t],ddS.Tdt[i][t]); } } } if ( ddP.apar==0 ) { likelihood += Td_*lb; } else { likelihood += Td_*la + gcache_value(&ddC.lgba, (int)Td_); } likelihood -= gcache_value(&ddC.lgb, (int)ddS.NdT[i]); //yap_infinite(likelihood); } return likelihood; }
static double aterms_phi0(double mya, void *mydata) { int v; double val = 0; #ifdef A_DEBUG float save_a = ddC.a_phi0->a; double like; #endif S_remake(ddC.a_phi0, mya); val += poch(ddP.b_phi0, mya, ddS.S_0_nz); for (v=0; v<ddN.W; v++) { if ( ddS.S_0vT[v]>1 ) val += S_S(ddC.a_phi0, ddS.S_0vT[v], 1); } myarms_evals++; #ifdef A_DEBUG yap_message("Eval aterms_phi0(%lf) = %lf (S had %f)", mya, val, save_a); ddP.a_phi0 = mya; cache_update("ap0"); like = likelihood(); if ( last_val != 0 ) { yap_message(", lp=%lf diffs=%lf vs %lf\n", like, val-last_val, like-last_like); } last_like = like; last_val = val; #endif return val; }
static double aterms_theta(double mya, void *mydata) { int i, t; double val = 0; #ifdef A_DEBUG float save_a = ddC.a_theta->a; double like; #endif S_remake(ddC.a_theta, mya); for (i=0; i<ddN.DT; i++) { for (t=0; t<ddN.T; t++) { if ( ddS.n_dt[i][t]>1 ) { val += S_S(ddC.a_theta,ddS.n_dt[i][t],ddS.c_dt[i][t]); } } val += poch(ddP.b_theta, mya, ddS.C_dT[i]); } myarms_evals++; #ifdef A_DEBUG yap_message("Eval aterms_theta(%lf) = %lf (S had %f)", mya, val, save_a); ddP.a_theta = mya; cache_update("at"); like = likelihood(); if ( last_val != 0 ) { yap_message(", lp=%lf diffs=%lf vs %lf\n", like, val-last_val, like-last_like); } last_like = like; last_val = val; #endif return val; }
static double aterms(double mya, void *mydata) { int i, t; double val = 0; double la = log(mya); #ifdef A_DEBUG float save_a = ddC.SX->a; double like; #endif S_remake(ddC.SX, mya); for (i=0; i<ddN.DT; i++) { uint32_t Td_ = 0; for (t=0; t<ddN.T; t++) { Td_ += ddS.Tdt[i][t]; if ( ddS.Ndt[i][t]>1 ) { val += S_S(ddC.SX,ddS.Ndt[i][t],ddS.Tdt[i][t]); } } val += Td_*la + lgamma(ddP.bpar/mya+Td_) - lgamma(ddP.bpar/mya); } myarms_evals++; #ifdef A_DEBUG yap_message("Eval aterms(%lf) = %lf (S had %f)", mya, val, save_a); ddP.apar = mya; cache_update("a"); like = likelihood(); if ( last_val != 0 ) { yap_message(", lp=%lf diffs=%lf vs %lf\n", like, val-last_val, like-last_like); } last_like = like; last_val = val; #endif return val; }
static double awterms(double myaw, void *mydata) { int i, t; double val = 0; double law = log(myaw); #ifdef A_DEBUG float save_a = ddC.SY->a; double like; #endif S_remake(ddC.SY, myaw); for (t=0; t<ddN.T; t++) { uint32_t Tw_ = 0; for (i=0; i<ddN.W; i++) { Tw_ += ddS.Twt[i][t]; if ( ddS.Nwt[i][t]>1 ) { val += S_S(ddC.SY,ddS.Nwt[i][t],ddS.Twt[i][t]); } } val += Tw_*law + lgamma(ddP_bwpar(t)/myaw+Tw_) - lgamma(ddP_bwpar(t)/myaw); } myarms_evals++; #ifdef A_DEBUG yap_message("Eval awterms(%lf) = %lf (S had %f)", myaw, val, save_a); ddP.awpar = myaw; cache_update("aw"); like = likelihood(); if ( last_val != 0 ) { yap_message(", lp=%lf diffs=%lf vs %lf\n", like, val-last_val, like-last_like); } last_like = like; last_val = val; #endif return val; }
static double aterms_phi1(double mya, void *mydata) { int e, t, v; double val = 0; #ifdef A_DEBUG float save_a = ddC.a_phi1->a; double like; #endif S_remake(ddC.a_phi1, mya); for (e=0; e<ddN.E; e++) { for (t=0; t<ddN.T; t++) { if ( ddS.S_Vte[t][e]==0 ) continue; val += poch(ddP.b_phi[e][t], mya, ddS.S_Vte[t][e]); for (v=0; v<ddN.W; v++) { if ( ddS.s_vte[v][t][e]==0 ) continue; if (e<ddN.E-1) { val += S_S(ddC.a_phi1, ddS.m_vte[v][t][e] + ddS.s_vte[v][t][e+1] , ddS.s_vte[v][t][e]); } else { val += S_S(ddC.a_phi1, ddS.m_vte[v][t][e], ddS.s_vte[v][t][e]); } } } } myarms_evals++; #ifdef A_DEBUG yap_message("Eval aterms_phi1(%lf) = %lf (S had %f)", mya, val, save_a); ddP.a_phi1 = mya; cache_update("ap"1); like = likelihood(); if ( last_val != 0 ) { yap_message(", lp=%lf diffs=%lf vs %lf\n", like, val-last_val, like-last_like); } last_like = like; last_val = val; #endif return val; }
static double aterms(double x, void *mydata) { int i, k; ALData *mp = (ALData *)mydata; double val = 0; if ( x<=0 ) { fprintf(stderr,"Illegal discount value in aterms()\n"); exit(1); } if ( mp->verbose>1 ) { fprintf(stderr,"Extending S for M=%d a=%lf\n", mp->maxt,x); } if ( mp->S ) S_remake(mp->S,x); else mp->S = S_make(mp->maxn,mp->maxt,mp->maxn,mp->maxt,x,S_STABLE); if ( !mp->S ) { fprintf(stderr,"Out of memory for S table\n"); exit(1); } for (i=0; i<mp->I; i++) { val += mp->T[i] * log(x) + lgamma(mp->T[i]+mp->bpar[i]/x) - lgamma(mp->bpar[i]/x); if ( mp->val ) { for (k=0; k<mp->K[i]; k++) { scnt_int n; stcnt_int t; mp->val(&n, &t, i, k); if ( n>1 ) val += S_S(mp->S, n, t); } } else { for (k=0; k<mp->K[i]; k++) if ( mp->n[i][k]>1 ) val += S_S(mp->S, mp->n[i][k],mp->t[i][k]); } } return val; }
int main(int argc, char* argv[]) { int c; int n, t; stable_t *S; /* * default values for args */ while ( (c=getopt(argc, argv,"ha:N:T:"))>=0 ) { switch ( c ) { case 'a': if ( !optarg || sscanf(optarg,"%f",&apar)!=1 ) yaps_quit("Need a valid 'a' argument\n"); break; case 'N': if ( !optarg || sscanf(optarg,"%d",&N)!=1 ) yaps_quit("Need a valid 'N' argument\n"); break; case 'T': if ( !optarg || sscanf(optarg,"%d",&T)!=1 ) yaps_quit("Need a valid 'T' argument\n"); break; case 'h': usage(); exit(0); } } if ( T>N ) T = N; if ( !(S=S_make(N+1,T,2*N,2*T,apar,S_UVTABLE|S_STABLE|S_VERBOSE)) ) yaps_quit("S_make failed\n"); S_report(S, stdout); #if 1 /* * list various values */ printf("S(%d,%d) = %10.6lg, V=n/a U=%lg\n", N, 1, S_S(S,N,1), S_U(S,N,1)); for (t=2; t<=T; t++) printf("S(%d,%d) = %10.6lg, V=%lg U=%lg\n", N, t, S_S(S,N,t), S_V(S,N,t), S_U(S,N,t)); printf("\nS(%d,%d) = %10.6lg, V=n/a U=%lg\n", N+10, 1, S_S(S,N+10,1), S_U(S,N+10,1)); for (t=2; t<=2*T+10; t++) printf("S(%d,%d) = %10.6lg, V=%lg U=%lg\n", N+10, t, S_S(S,N+10,t), S_V(S,N+10,t), S_U(S,N+10,t)); for (n=T+1; n<=2*N; n++) printf("S(%d,%d..) = %10.6lg %10.6lg\n", n, T, S_S(S,n,T), S_S(S,n,T+1)); for (n=2; n<8; n++ ) { printf("S(%d,%d) = %10.6lg ", n, 1, S_S(S,n,1)); for (t=2; t<=n; t++) printf(" %10.6lg", S_S(S,n,t)); printf("\n"); } for (n=2; n<8; n++ ) { printf("V(%d,%d) = %10.6lg", n, 2, S_V(S,n,2)); for (t=3; t<=n; t++) printf(" %10.6lg", S_V(S,n,t)); printf("\n"); } for (n=2; n<8; n++ ) { printf("U(%d,%d) = %10.6lg", n, 2, S_U(S,n,2)); for (t=3; t<=n; t++) printf(" %10.6lg", S_U(S,n,t)); printf("\n"); } #else { /* * Sample a partition of size T of N by Chinese Rest. distribution * start by sampling the last entry from (1,2,...,N-T+1); * see p(m | CRD, apr, N, T) on page 4 of "doc/alpha.pdf" */ double *prob = malloc(sizeof(*prob)*N); double probtot = 1.0; prob[1] = 1.0; for (t=2; t<=N-T+1; t++) probtot += prob[t] = (N-t+1)*(t-apar) / S_U(S,N-t,T-1)/(t-1) * prob[t-1]; for (t=1; t<=N-T+1; t++) printf("p(m=%d) = %lg\n", t, prob[t]/probtot ); } #endif S_report(S, stdout); S_free(S); return 0; }
double likelihood_PYbeta() { int i,t; double likelihood = 0; double lbw = log(ddP.bwpar); double law = log(ddP.awpar); likelihood += pctl_gammaprior(ddP.bwpar); /* * term for k-th node */ #ifdef BWPAR0 for (t=1; t<ddN.T; t++) { #else for (t=0; t<ddN.T; t++) { #endif uint32_t Tw_ = 0; for (i=0; i<ddN.W; i++) { int tt = ddS.Twt[i][t]; int nn = ddS.Nwt[i][t]; if ( nn>0 ) { Tw_ += tt; likelihood += S_S(ddC.SY,nn,tt); #if 1 if ( !finite(likelihood) || isinf(likelihood) || isnan(likelihood) ) yap_quit("Like=%lf: Nwt[%d][%d]=%d Twt[i][t]=%d S.M=%d S.N=%d\n", likelihood, i, t, (int)ddS.Nwt[i][t],(int)ddS.Twt[i][t],ddC.SY->usedM, ddC.SY->usedN); #endif } } yap_infinite(likelihood); if ( ddP.awpar==0 ) { likelihood += Tw_*lbw; } else { #ifdef L_CACHE likelihood += Tw_*law + gcache_value(&ddC.lgbaw, (int)Tw_); #else likelihood += Tw_*law + gammadiff((int)Tw_, ddP.bwpar/ddP.awpar, 0.0); #endif } #ifdef L_CACHE likelihood -= gcache_value(&ddC.lgbw, (int)ddS.NWt[t]); #else likelihood -= gammadiff((int)ddS.NWt[t], ddP.bwpar, 0.0); #endif yap_infinite(likelihood); } yap_infinite(likelihood); return likelihood; } double likelihood_PYbeta_PDP() { /* * the constant prior */ int j; double likelihood = 0; for (j=0; j<ddN.W; j++) { if ( ddS.TwT[j]>0 ) { likelihood += ddS.TwT[j]*log(ddP.betapr[j]); } } //yap_infinite(likelihood); return likelihood; }
/* * use prior a is uniform */ double samplea2(double mya, stable_t *S, int I, int *K, scnt_int *T, scnt_int **n, stcnt_int **t, void (*getval)(scnt_int *n, stcnt_int *t, unsigned i, unsigned k), double *bpar, rngp_t rng, int loops, int verbose) { double inita[3] = {A_MIN,1,A_MAX}; int i, k; ALData ald; stcnt_int *mp; int n_m = 0; inita[1] = mya; if ( fabs(inita[1]-A_MAX)/A_MAX<0.00001 ) { inita[1] = A_MAX*0.999 + A_MIN*0.001; } if ( fabs(inita[1]-A_MIN)/A_MIN<0.00001 ) { inita[1] = A_MIN*0.999 + A_MAX*0.001; } #ifdef SQUEEZEA /* * bound current move to be less than SQEEZEA */ if ( inita[1]-SQUEEZEA>A_MIN ) inita[0] = inita[1]-SQUEEZEA; if ( inita[1]+SQUEEZEA<A_MAX ) inita[2] = inita[1]+SQUEEZEA; #endif ald.T = T; ald.n = n; ald.t = t; ald.I = I; ald.K = K; ald.val = getval; ald.bpar = bpar; ald.verbose = verbose; ald.maxt = 1; ald.maxn = 1; ald.S = NULL; n_m = 0; for (i=0; i<I; i++) for (k=0; k<K[i]; k++) { if ( t[i][k]>1 && t[i][k]<n[i][k]) n_m += t[i][k]-1 ; } ald.m = malloc(sizeof(*ald.m)*n_m); if ( !ald.m ) { fprintf(stderr,"Out of memory for samplea()\n"); exit(1); } // fprintf(stderr,"m[%d][%d] tabl space = %d\n", I, K[0], n_m); mp = ald.m; for (i=0; i<I; i++) for (k=0; k<K[i]; k++) { if ( t[i][k]>1 && t[i][k]<n[i][k]) { int N = n[i][k]; double ptot = S_S(S,N,t[i][k]); double rem = ptot + log(rng_unit(rng)); int M; // fprintf(stderr,"Sampling[%d][%d] n=%d, t=%d: ", i,k, N, t[i][k]); for (M=t[i][k]-1; M>=1; M--) { // each round instantiates another count int l; double fact = 0.0; for (l=1; l<=N-M; l++) { double term; if ( l>1 ) fact += log((l-mya)*(N-l+1)/(l-1)); term = fact+S_S(S,N-l,M)-ptot; if ( term>= rem ) break; rem = logminus(rem, term); } if ( l>N-M ) l = N-M; mp[M-1] = l; N -= l; assert(N>=1); // fprintf(stderr," %d", l); } // fprintf(stderr," %d\n", N); mp += t[i][k]-1; } } #ifdef PSAMPLE_ARS arms_simple(3, inita, inita+2, aterms2, NULL, 0, inita+1, &mya); if ( mya<inita[0] || mya>inita[2] ) { fprintf(stderr,"Arms_simple(apar) returned value out of bounds\n"); exit(1); } #else inita[1] = A_MAX; if ( SliceSimple(&mya, aterms2, inita, rng, loops, &ald) ) { fprintf(stderr,"SliceSimple error\n"); exit(1); } #endif if ( ald.S ) S_free(ald.S); return mya; }
/* * compute likelihood ratio difference based on *M */ static double merge_like_Tdt_sum(int k1, int k2, merge_alpha_t *M) { double *val; int d; val = dvec(ddN.DT); for (d=0; d<ddN.DT; d++) { } #endif static double merge_like_Tdt(int k1, int k2, merge_alpha_t *M) { int d; double la = 0; double lb = log(ddP.bpar); int TD_diff = 0; double likelihood = 0; if ( ddP.apar>0 ) la = log(ddP.apar); for (d=0; d<ddN.DT; d++) { int Td_diff; /* total change in T for doc */ if ( M->Ndt[d]>1 ) { likelihood -= S_S(ddC.SX,ddS.Ndt[d][k2],ddS.Tdt[d][k2]); likelihood -= S_S(ddC.SX,ddS.Ndt[d][k1],ddS.Tdt[d][k1]); likelihood += S_S(ddC.SX,M->Ndt[d],M->Tdt[d]); assert(M->Tdt[d]>=1); assert(M->Tdt[d]<=M->Ndt[d]); assert(ddS.Ndt[d][k2]==0 || ddS.Tdt[d][k2]>0); assert(ddS.Ndt[d][k1]==0 || ddS.Tdt[d][k1]>0); } yap_infinite(likelihood); TD_diff += Td_diff = (M->Tdt[d]-ddS.Tdt[d][k2]-ddS.Tdt[d][k1]); if ( Td_diff==0 ) continue; if ( ddP.apar==0 ) { likelihood += Td_diff*lb; } else { likelihood += Td_diff*la; if ( Td_diff<0 ) likelihood -= gammadiff(-Td_diff,M->TdT[d]+ddP.bpar/ddP.apar, 0.0); else likelihood += gammadiff(Td_diff, M->TdT[d]-Td_diff+ddP.bpar/ddP.apar, 0.0); } yap_infinite(likelihood); } if ( ddP.PYalpha==H_PDP ) { likelihood += (M->TDt-ddS.TDt[k1])*log(ddP.alphapr[k1]) - ddS.TDt[k2]*log(ddP.alphapr[k2]); } else if ( ddP.PYalpha==H_HDP ) { likelihood += lgamma(M->TDTm+M->TDt-TD_diff+ddP.b0) - lgamma(M->TDTm+M->TDt+ddP.b0); likelihood -= gammadiff(ddS.TDt[k1], ddP.b0*ddP.alphapr[k1], 0.0); likelihood -= gammadiff(ddS.TDt[k2], ddP.b0*ddP.alphapr[k2], 0.0); likelihood += gammadiff(M->TDt, ddP.b0*ddP.alphapr[k1], 0.0); } else { double lga0 = lgamma(1-ddP.a0); likelihood += lgamma(M->TDTm+M->TDt-TD_diff+ddP.b0) - lgamma(M->TDTm+M->TDt+ddP.b0); /* because k2 gone to zero, so one less topic */ likelihood -= log(ddP.b0+ddP.a0*(ddS.TDTnz-1)); if ( ddS.TDt[k2]>1 ) likelihood -= lgamma(ddS.TDt[k2]-ddP.a0) - lga0; if ( ddS.TDt[k1]>1 ) likelihood -= lgamma(ddS.TDt[k1]-ddP.a0) - lga0; likelihood += lgamma(M->TDt-ddP.a0) - lga0; } yap_infinite(likelihood); return likelihood; }
/* * compute likelihood ratio difference based on *M */ static double merge_like_Twt(int k1, int k2, merge_beta_t *M) { int i, w; double likelihood = 0; #ifndef BWPAR0 double lbw = log(ddP.bwpar); #endif double law = log(ddP.awpar); double TW_diff = 0; #ifdef BWPAR0 yap_quit("BWPAR0 unimpleented in merge\n"); #endif for (i=0; i<ddN.W; i++) { likelihood -= S_S(ddC.SY,ddS.Nwt[i][k1],ddS.Twt[i][k2]); likelihood -= S_S(ddC.SY,ddS.Nwt[i][k1],ddS.Twt[i][k2]); likelihood += S_S(ddC.SY,M->Nwt[i],M->Twt[i]); } if ( ddP.awpar==0 ) { #ifdef BWPAR0 likelihood += M->TWt-ddS.TWt[k1]*log(ddP_bwpar(k1)) -ddS.TWt[k2]*log(ddP_bwpar(k2)); #else likelihood += (M->TWt-ddS.TWt[k1]-ddS.TWt[k2])*lbw; #endif } else { likelihood += (M->TWt-ddS.TWt[k1]-ddS.TWt[k2])*law + gammadiff((int)M->TWt, ddP.bwpar/ddP.awpar, 0.0) - gammadiff((int)ddS.TWt[k1], ddP_bwpar(k1)/ddP.awpar, 0.0) - gammadiff((int)ddS.TWt[k2], ddP_bwpar(k2)/ddP.awpar, 0.0); } likelihood += gammadiff((int)ddS.NWt[k1], ddP_bwpar(k1), 0.0); likelihood += gammadiff((int)ddS.NWt[k2], ddP_bwpar(k2), 0.0); likelihood -= gammadiff((int)M->NWt, ddP.bwpar, 0.0); yap_infinite(likelihood); if ( ddP.PYbeta==H_PDP ) { for (w=0; w<ddN.W; w++) { if ( ddS.TwT[w]>0 ) { // ??????????????? likelihood += ddS.TwT[w]*log(ddP.betapr[w]); } } } else if ( ddP.PYbeta==H_HDP ) { yap_quit("merge with PYbeta unimplemented\n"); likelihood += lgamma(M->TWTm+M->TWt-TW_diff+ddP.bw0) - lgamma(M->TWTm+M->TWt+ddP.bw0); for (w=0; w<ddN.W; w++) { // ??????????? likelihood -= gammadiff(ddS.TWt[k1], ddP.bw0*ddP.betapr[k1], 0.0); likelihood -= gammadiff(ddS.TWt[k2], ddP.bw0*ddP.betapr[k2], 0.0); likelihood += gammadiff(M->TWt, ddP.bw0*ddP.betapr[k1], 0.0); } } else { double lgaw0 = lgamma(1-ddP.aw0); likelihood += lgamma(M->TWTm+M->TWt-TW_diff+ddP.bw0) - lgamma(M->TWTm+M->TWt+ddP.bw0); /* because k2 gone to zero, so one less topic */ likelihood -= log(ddP.bw0+ddP.aw0*(ddS.TWTnz-1)); if ( ddS.TWt[k2]>1 ) likelihood -= lgamma(ddS.TWt[k2]-ddP.aw0) - lgaw0; if ( ddS.TWt[k1]>1 ) likelihood -= lgamma(ddS.TWt[k1]-ddP.aw0) - lgaw0; likelihood += lgamma(M->TWt-ddP.aw0) - lgaw0; } yap_infinite(likelihood); return likelihood; }