/* * the word table indicator is increased, but not forced */ void wordtableindicatorprob(int j, int t, double *uone, double *uzero) { int nn = ddS.Nwt[j][t]; int tt = ddS.Twt[j][t]; double e1, e0; /* * fudge to handle multi-threading case where constraints violated */ if ( nn>0 && tt==0 ) tt=1; if ( tt>nn ) tt = nn; if ( nn==0 ) { /* * should only happen during multi-threading */ tt = 0; e1 = 1; e0 = 0; } else { e1 = S_UV(ddC.SY,nn,tt+1); if ( tt==1 ) e0 = nn - ddP.awpar; else e0 = S_U(ddC.SY,nn,tt); } *uone = e1 * (ddP.bwpar+ddP.awpar*ddS.TWt[t]) * betabasewordprob(j) * (tt+1)/(nn+1); *uzero = e0 * (nn-tt+1)/(nn+1); }
/* * probability of topic from doc level PDP (ddP.bdk!=NULL version) * * t - topic * (i,mi) - word index and corresponding multi version * pK - input contribution to posterior from adding word with topic * *dip - set to prob. indicator would be 1, if NULL leave */ double docfact(D_MiSi_t *dD, int t, int i, int mi, double pK, float *dip) { int N = dD->Mi[t], S = dD->Si[t]; int n, s; assert(dip); *dip = 1; if ( ddP.bdk==NULL ) return pK; if ( M_multi(i) ) { int mii; // assert(mi<ddM.dim_multiind || did==ddN.D-1); mii = ddM.multiind[mi]-dD->mi_base; assert(mii>=0); assert(mii<ddM.MI_max); n = dD->Mik[mii][t]; s = dD->Sik[mii][t]; } else { n = s = 0; } if ( s==0 ) { return pK * (ddP.bdk[t]+ddP.ad*S)/(ddP.bdk[t]+N); } else { double one = pK * (ddP.bdk[t]+ddP.ad*S) * (s+1.0)/(n+1.0); double zero = (n-s+1.0)/(n+1.0); one *= S_UV(ddC.SD,n,s+1); if ( s==1 ) zero *= n - ddP.ad; else zero *= S_U(ddC.SD,n,s); *dip = one/(one + zero); return (one + zero) /(ddP.bdk[t]+N); } return 0; }
/* * prob. the doc table indicator is increased, but not forced */ void doctableindicatorprob(int d, int t, int Ttot, double *uone, double *uzero) { int nn = ddS.Ndt[d][t]; int tt = ddS.Tdt[d][t]; double e1, e0; e1 = S_UV(ddC.SX,nn,tt+1); if ( tt==1 ) e0 = nn - ddP.apar; else e0 = S_U(ddC.SX,nn,tt); *uone = e1 * (ddP.bpar+ddP.apar*Ttot) * alphabasetopicprob(t) * (tt+1)/(nn+1); *uzero = e0 * (nn-tt+1)/(nn+1); }
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; }