@@ 156-169 (lines=14) @@ | ||
153 | return dot(dot(dot(ch, z), z.T), ch.T) |
|
154 | ||
155 | ||
156 | def sample_wishart_v2(nu, Lambda): |
|
157 | """ |
|
158 | From Sawyer, et. al. 'Wishart Distributions and Inverse-Wishart Sampling' |
|
159 | Runs in constant time |
|
160 | Untested |
|
161 | """ |
|
162 | d = Lambda.shape[0] |
|
163 | ch = cholesky(Lambda) |
|
164 | T = numpy.zeros((d, d)) |
|
165 | for i in xrange(d): |
|
166 | if i != 0: |
|
167 | T[i, :i] = numpy.random.normal(size=(i,)) |
|
168 | T[i, i] = sqrt(chi2.rvs(nu - i + 1)) |
|
169 | return dot(dot(dot(ch, T), T.T), ch.T) |
|
170 | ||
171 | ||
172 | def sample_inverse_wishart(nu, S): |
|
@@ 145-153 (lines=9) @@ | ||
142 | return S |
|
143 | ||
144 | ||
145 | def sample_wishart(nu, Lambda): |
|
146 | ch = cholesky(Lambda) |
|
147 | d = Lambda.shape[0] |
|
148 | z = numpy.zeros((d, d)) |
|
149 | for i in xrange(d): |
|
150 | if i != 0: |
|
151 | z[i, :i] = numpy.random.normal(size=(i,)) |
|
152 | z[i, i] = sqrt(numpy.random.gamma(0.5 * nu - d + 1, 2.0)) |
|
153 | return dot(dot(dot(ch, z), z.T), ch.T) |
|
154 | ||
155 | ||
156 | def sample_wishart_v2(nu, Lambda): |