JNIEXPORT jint JNICALL Java_edu_berkeley_bid_CUMACH_multinomial (JNIEnv *env, jobject obj, jint nrows, jint ncols, jobject jA, jobject jB, jobject jNorm, jint nvals) { float *A = (float*)getPointer(env, jA); int *B = (int*)getPointer(env, jB); float *Norm = (float*)getPointer(env, jNorm); return multinomial(nrows, ncols, A, B, Norm, nvals); }
void number_partition::multinomial_ordered(base &res, INT f_v) { number_partition q; base a; type(q); multinomial(res, f_v); q.multinomial(a, f_v); res *= a; if (f_v) { cout << "multinomial_ordered(" << *this << ") = " << res << endl; } }
/* # code for computing a hierarchical model, with normally distributed # level 1 errors (variance known) and level 2 follows a DP # y[i]: observed datum for obs i # theta[i]: level 1 mean for obs i # phi: vector of unique values of theta (i.e., clusters) # config[i]: cluster label / configuration indicator #################################################################### */ HHRESULT CGaussianMDP::sample_config ( int *&config, int obs, double *sigma2, int n, double *y, double *phi, double alpha ) { /* # config: vector of configuration indicators # obs: index of observation under study # sigma2: (known) level 1 variances( # n: sample size */ int i,j,nclus,oldconfig,ind; int sumconfig = 0; double sumprob; double tempphi = 0.0; HHRESULT hr = HH_OK; /* get number of configurations/clusters also set up other things to check */ sumconfig = 0; nclus = 0; /* number of configurations */ for(i=0; i<n; i++) { if(config[i]==config[obs]) sumconfig++; nclus = imax2(config[i], nclus); } /* ## STEP 1: nothing changes if obs under study (obs) has its own cluster w/prob */ if( (sumconfig == 1) && (runif(0.0,1.0) < (nclus-1.0)/nclus)) { goto Cleanup; } // nconfig counts obs in clusters, current obs not included for(i=0; i<nclus; i++) { nconfig[i] = 0; } for(j=0; j<n; j++) { nconfig[config[j]-1]++; } nconfig[config[obs]-1]--; /* #nclus-star */ /* STEP 2: if there are more than 1 obs in case i's cluster, then: */ if(sumconfig > 1) { sumprob = 0; for(j=0; j<nclus; j++) { prob[j] = nconfig[j] * dnorm(y[obs], phi[j], sqrt(sigma2[obs]), 0); sumprob += prob[j]; } prob[nclus] = (alpha/(nclus+1)) * dnorm(y[obs], phi[nclus], sqrt(sigma2[obs]), 0); sumprob+=prob[nclus]; if(sumprob==0) { for(j=0; j<=nclus; j++) prob[j]=1.0; } /* need to add in a sample-type function */ config[obs] = multinomial(nclus+1,prob); goto Cleanup; } /* STEP 3: if there is just one obs in cluster but need to sample new clustr:*/ /* else s(i)=1 and need to sample new cluster */ if(sumconfig==1) /* # s/b unnec line */ { oldconfig=config[obs]; for(i=0; i<n; i++) { if(config[i] > oldconfig) config[i]--; } config[i]=nclus; for(i=1; i<nclus; i++) { if(i>=oldconfig) { nconfig[i-1]=nconfig[i];/* last elt of nconfig now useless */ } } // shifting the phis down by one, move phi[oldconfig-1] to the end if((oldconfig < nclus) && (nclus>1)) { tempphi = phi[oldconfig-1]; for(i=oldconfig; i<nclus; i++) { phi[i-1] = phi[i]; } phi[nclus-1] = tempphi; } nclus--; sumprob = 0.0; for(j=0; j<nclus; j++) { prob[j] = nconfig[j] * dnorm(y[obs], phi[j], sqrt(sigma2[obs]), 0); sumprob += prob[j]; } prob[nclus] = (alpha/(nclus+1)) * dnorm(y[obs], phi[nclus], sqrt(sigma2[obs]), 0); sumprob += prob[nclus]; if(sumprob == 0) { for(i=0; i<=nclus; i++) prob[i] = 1.0; } config[obs] = multinomial(nclus+1,prob); } Cleanup: return hr; Error: goto Cleanup; }