Mercurial > hg > trimatlab
comparison private/sample_transmat_hdp.m @ 0:be936975f254
Initial check in.
author | samer |
---|---|
date | Wed, 01 Feb 2012 14:06:37 +0000 |
parents | |
children | 0e0f2805ef9c |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:be936975f254 |
---|---|
1 function [T,I]=sample_transmat_hdp(A0,B0,A1,B1,K,L,T0) | |
2 % sample_transmat_hdp - Sample 1st order Markov transition matrix using HDP | |
3 % | |
4 % sample_transmat_hdp :: | |
5 % nonneg ~'Shape parameter for base Dirichlet Gamma prior', | |
6 % nonneg ~'Scale parameter for base Dirichlet Gamma prior', | |
7 % nonneg ~'Shape parameter for sub Dirichlet Gamma prior', | |
8 % nonneg ~'Scale parameter for sub Dirichlet Gamma prior', | |
9 % natural ~'size of alphabet', | |
10 % natural ~'num of transition matrices to produce', | |
11 % -> [[K,K,L]] ~ 'lots of transition matrices', | |
12 % [[L,3]] ~ 'entropy rate, redundancy, and predinfo rate for all'. | |
13 % | |
14 % An extra optional argument of type [[K,K]] can be added, which | |
15 % is used as a sort of bias for the sampled matrices. | |
16 | |
17 if nargin<7, T0=ones(K,K); end | |
18 | |
19 Alpha0=gamrnd(A0,B0,1,1,L); | |
20 Alpha1=gamrnd(A1,B1,1,1,L); | |
21 | |
22 | |
23 % base distributions | |
24 H0=sample_dir(repmat(Alpha0/K,[K,1,1])); | |
25 HT=stoch(repmat(H0,[1,K,1])).*repmat(T0/sum(T0(:)),[1,1,L]); | |
26 T=sample_dir(repmat(Alpha1,[K,K,1]).*HT); | |
27 | |
28 if any(sum(H0)<0.99) | |
29 disp('normalisation problem in H') | |
30 keyboard | |
31 end | |
32 if nargout>=2 | |
33 I=zeros(K,3); | |
34 for i=1:L | |
35 I(i,:)=mc_global_info(T(:,:,i)); | |
36 end | |
37 end | |
38 end | |
39 | |
40 % taken from stats/models/dirichlet.m>stoch_gamma | |
41 function y=sample_dir(beta) | |
42 x=randg(beta); | |
43 z=sum(x,1); | |
44 y=x./repmat(z,size(x,1),1); | |
45 bad=find(z==0); | |
46 for i=1:length(bad) | |
47 beta_i=beta(:,bad(i)); % misbehaving params | |
48 k=(beta_i==max(beta_i)); % entries with max beta | |
49 y(:,bad(i))=mnrnd(1,stoch(k)); | |
50 end | |
51 end | |
52 | |
53 function [Y,Z]=stoch(X), | |
54 Z=sum(X,1); | |
55 Y=X./repmat(Z,size(X,1),1); | |
56 end | |
57 | |
58 |