| 1 |
import numpy |
| 2 |
import csb.test as test |
| 3 |
import csb.statistics.pdf |
| 4 |
|
| 5 |
from csb.bio.io.wwpdb import LegacyStructureParser |
| 6 |
from csb.bio.utils import probabilistic_fit, average_structure |
| 7 |
from csb.statistics.scalemixture import ScaleMixture, GammaPrior, InvGammaPrior |
| 8 |
from csb.statistics.scalemixture import GammaPosteriorMAP, InvGammaPosteriorMAP |
| 9 |
|
| 10 |
|
| 11 |
@test.functional |
| 12 |
class TestScaleMixture(test.Case): |
| 13 |
|
| 14 |
def testRandom(self): |
| 15 |
|
| 16 |
mixture = ScaleMixture(scales=[1., 1., 1., 1.], |
| 17 |
prior=GammaPrior(), d=3) |
| 18 |
|
| 19 |
mixture.random() |
| 20 |
samples = mixture.random(10000) |
| 21 |
|
| 22 |
mu = numpy.mean(samples) |
| 23 |
var = numpy.var(samples) |
| 24 |
|
| 25 |
self.assertAlmostEqual(0.0, mu, delta=1e-1) |
| 26 |
self.assertAlmostEqual(1., var, delta=1e-1) |
| 27 |
|
| 28 |
def testLogProb(self): |
| 29 |
|
| 30 |
x = numpy.linspace(-5, 5, 1000) |
| 31 |
|
| 32 |
normal = csb.statistics.pdf.Normal() |
| 33 |
|
| 34 |
mixture = ScaleMixture(scales=[1., 1., 1., 1.], |
| 35 |
prior=None, d=1) |
| 36 |
|
| 37 |
px = mixture(x) |
| 38 |
gx = normal(x) |
| 39 |
|
| 40 |
for i in range(len(px)): |
| 41 |
self.assertAlmostEqual(px[i], 4 * gx[i], delta=1e-1) |
| 42 |
|
| 43 |
def testGamma(self): |
| 44 |
""" |
| 45 |
The posterior of a gaussian scale mixture with gamma prior |
| 46 |
is a Student's t distribution, with parameters alpha and beta. |
| 47 |
|
| 48 |
Give enough samples, we shoud be able to estimate these parameters |
| 49 |
""" |
| 50 |
pdbfile = self.config.getTestFile('ake-xray-ensemble-ca.pdb') |
| 51 |
ensemble = LegacyStructureParser(pdbfile).parse_models() |
| 52 |
X = numpy.array(ensemble[0].get_coordinates(['CA'], True)) |
| 53 |
Y = numpy.array(ensemble[13].get_coordinates(['CA'], True)) |
| 54 |
|
| 55 |
|
| 56 |
mixture = ScaleMixture(scales=X.shape[0], |
| 57 |
prior=GammaPrior(), d=3) |
| 58 |
|
| 59 |
from csb.bio.utils import fit |
| 60 |
|
| 61 |
R, t = fit(X, Y) |
| 62 |
#numpy.random.seed(100) |
| 63 |
# gibbs sampling cycle |
| 64 |
for i in range(200): |
| 65 |
# apply rotation |
| 66 |
data = numpy.sum((X - numpy.dot(Y, numpy.transpose(R)) - t) ** 2, axis= -1) ** (1. / 2) |
| 67 |
# sample scales |
| 68 |
mixture.estimate(data) |
| 69 |
# sample rotations |
| 70 |
R, t = probabilistic_fit(X, Y, mixture.scales) |
| 71 |
|
| 72 |
self.assertEqual(mixture.scales.shape, (211,)) |
| 73 |
|
| 74 |
R_opt = numpy.eye(3) |
| 75 |
t_opt = numpy.zeros((3,)) |
| 76 |
|
| 77 |
for i in range(3): |
| 78 |
self.assertAlmostEqual(t[i], t_opt[i], delta=2.) |
| 79 |
for j in range(3): |
| 80 |
self.assertAlmostEqual(R_opt[i, j], R[i, j], delta=1e-1) |
| 81 |
|
| 82 |
|
| 83 |
def testGammaMAP(self): |
| 84 |
""" |
| 85 |
The posterior of a gaussian scale mixture with gamma prior |
| 86 |
is a Student's t distribution, with parameters alpha and beta. |
| 87 |
|
| 88 |
Give enough samples, we shoud be able to estimate these parameters |
| 89 |
""" |
| 90 |
pdbfile = self.config.getTestFile('ake-xray-ensemble-ca.pdb') |
| 91 |
ensemble = LegacyStructureParser(pdbfile).parse_models() |
| 92 |
X = numpy.array(ensemble[0].get_coordinates(['CA'], True)) |
| 93 |
Y = numpy.array(ensemble[13].get_coordinates(['CA'], True)) |
| 94 |
|
| 95 |
prior = GammaPrior() |
| 96 |
prior.estimator = GammaPosteriorMAP() |
| 97 |
mixture = ScaleMixture(scales=X.shape[0], |
| 98 |
prior=prior, d=3) |
| 99 |
|
| 100 |
from csb.bio.utils import fit, wfit |
| 101 |
R, t = fit(X, Y) |
| 102 |
#numpy.random.seed(100) |
| 103 |
# gibbs sampling cycle |
| 104 |
for i in range(200): |
| 105 |
# apply rotation |
| 106 |
data = numpy.sum((X - numpy.dot(Y, numpy.transpose(R)) - t) ** 2, axis= -1) ** (1. / 2) |
| 107 |
# sample scales |
| 108 |
mixture.estimate(data) |
| 109 |
# sample rotations |
| 110 |
R, t = wfit(X, Y, mixture.scales) |
| 111 |
|
| 112 |
self.assertEqual(mixture.scales.shape, (211,)) |
| 113 |
|
| 114 |
R_opt = numpy.eye(3) |
| 115 |
t_opt = numpy.zeros((3,)) |
| 116 |
|
| 117 |
for i in range(3): |
| 118 |
self.assertAlmostEqual(t[i], t_opt[i], delta=2.) |
| 119 |
for j in range(3): |
| 120 |
self.assertAlmostEqual(R_opt[i, j], R[i, j], delta=1e-1) |
| 121 |
|
| 122 |
|
| 123 |
def testInvGamma(self): |
| 124 |
""" |
| 125 |
The posterior of a gaussian scale mixture with gamma prior |
| 126 |
is a Student's t distribution, with parameters alpha and beta. |
| 127 |
|
| 128 |
Give enough samples, we shoud be able to estimate these parameters |
| 129 |
""" |
| 130 |
pdbfile = self.config.getTestFile('ake-xray-ensemble-ca.pdb') |
| 131 |
ensemble = LegacyStructureParser(pdbfile).parse_models() |
| 132 |
X = numpy.array(ensemble[0].get_coordinates(['CA'], True)) |
| 133 |
Y = numpy.array(ensemble[13].get_coordinates(['CA'], True)) |
| 134 |
|
| 135 |
|
| 136 |
mixture = ScaleMixture(scales=X.shape[0], |
| 137 |
prior=InvGammaPrior(), d=3) |
| 138 |
|
| 139 |
from csb.bio.utils import fit |
| 140 |
|
| 141 |
R, t = fit(X, Y) |
| 142 |
#numpy.random.seed(100) |
| 143 |
# gibbs sampling cycle |
| 144 |
for i in range(200): |
| 145 |
# apply rotation |
| 146 |
data = numpy.sum((X - numpy.dot(Y, numpy.transpose(R)) - t) ** 2, axis= -1) ** (1. / 2) |
| 147 |
# sample scales |
| 148 |
mixture.estimate(data) |
| 149 |
# sample rotations |
| 150 |
R, t = probabilistic_fit(X, Y, mixture.scales) |
| 151 |
|
| 152 |
self.assertEqual(mixture.scales.shape, (211,)) |
| 153 |
|
| 154 |
R_opt = numpy.eye(3) |
| 155 |
t_opt = numpy.zeros((3,)) |
| 156 |
|
| 157 |
for i in range(3): |
| 158 |
self.assertAlmostEqual(t[i], t_opt[i], delta=2.) |
| 159 |
for j in range(3): |
| 160 |
self.assertAlmostEqual(R_opt[i, j], R[i, j], delta=1e-1) |
| 161 |
|
| 162 |
|
| 163 |
def testInvGammaMAP(self): |
| 164 |
""" |
| 165 |
The posterior of a gaussian scale mixture with gamma prior |
| 166 |
is a Student's t distribution, with parameters alpha and beta. |
| 167 |
|
| 168 |
Give enough samples, we shoud be able to estimate these parameters |
| 169 |
""" |
| 170 |
pdbfile = self.config.getTestFile('ake-xray-ensemble-ca.pdb') |
| 171 |
ensemble = LegacyStructureParser(pdbfile).parse_models() |
| 172 |
X = numpy.array(ensemble[0].get_coordinates(['CA'], True)) |
| 173 |
Y = numpy.array(ensemble[13].get_coordinates(['CA'], True)) |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
prior = InvGammaPrior() |
| 178 |
prior.estimator = InvGammaPosteriorMAP() |
| 179 |
mixture = ScaleMixture(scales=X.shape[0], |
| 180 |
prior=prior, d=3) |
| 181 |
|
| 182 |
from csb.bio.utils import fit, wfit |
| 183 |
|
| 184 |
R, t = fit(X, Y) |
| 185 |
#numpy.random.seed(100) |
| 186 |
# gibbs sampling cycle |
| 187 |
for i in range(200): |
| 188 |
# apply rotation |
| 189 |
data = numpy.sum((X - numpy.dot(Y, numpy.transpose(R)) - t) ** 2, axis= -1) ** (1. / 2) |
| 190 |
# sample scales |
| 191 |
mixture.estimate(data) |
| 192 |
# sample rotations |
| 193 |
R, t = wfit(X, Y, mixture.scales) |
| 194 |
|
| 195 |
self.assertEqual(mixture.scales.shape, (211,)) |
| 196 |
|
| 197 |
R_opt = numpy.eye(3) |
| 198 |
t_opt = numpy.zeros((3,)) |
| 199 |
|
| 200 |
for i in range(3): |
| 201 |
self.assertAlmostEqual(t[i], t_opt[i], delta=2.) |
| 202 |
for j in range(3): |
| 203 |
self.assertAlmostEqual(R_opt[i, j], R[i, j], delta=1e-1) |
| 204 |
|
| 205 |
|
| 206 |
def testEnsemble(self): |
| 207 |
""" |
| 208 |
The posterior of a gaussian scale mixture with gamma prior |
| 209 |
is a Student's t distribution, with parameters alpha and beta. |
| 210 |
|
| 211 |
Give enough samples, we shoud be able to estimate these parameters |
| 212 |
""" |
| 213 |
pdbfile = self.config.getTestFile('ake-xray-ensemble-ca.pdb') |
| 214 |
ensemble = LegacyStructureParser(pdbfile).parse_models() |
| 215 |
|
| 216 |
X = numpy.array([model.get_coordinates(['CA'], True) for model in ensemble]) |
| 217 |
x_mu = average_structure(X) |
| 218 |
n =X.shape[1] |
| 219 |
m =X.shape[0] |
| 220 |
R = numpy.zeros((m,3,3)) |
| 221 |
t = numpy.ones((m,3)) |
| 222 |
|
| 223 |
|
| 224 |
prior = GammaPrior() |
| 225 |
|
| 226 |
mixture = ScaleMixture(scales=n, prior = prior, d=3) |
| 227 |
|
| 228 |
|
| 229 |
from csb.bio.utils import fit, wfit |
| 230 |
|
| 231 |
for i in range(m): |
| 232 |
R[i,:,:], t[i,:] = fit(x_mu, X[i]) |
| 233 |
|
| 234 |
# gibbs sampling cycle |
| 235 |
for j in range(200): |
| 236 |
# apply rotation |
| 237 |
data = numpy.array([numpy.sum((x_mu - numpy.dot(X[i], numpy.transpose(R[i])) - t[i]) **2, -1)**0.5 |
| 238 |
for i in range(m)]).T |
| 239 |
# sample scales |
| 240 |
mixture.estimate(data) |
| 241 |
# sample rotations |
| 242 |
for i in range(m): |
| 243 |
R[i,:,:], t[i,:] = wfit(x_mu, X[i], mixture.scales) |
| 244 |
|
| 245 |
|
| 246 |
self.assertEqual(mixture.scales.shape, (211,)) |
| 247 |
|
| 248 |
R_opt = numpy.eye(3) |
| 249 |
t_opt = numpy.zeros((3,)) |
| 250 |
for k in range(m): |
| 251 |
for i in range(3): |
| 252 |
self.assertAlmostEqual(t[k,i], t_opt[i], delta=2.) |
| 253 |
for j in range(3): |
| 254 |
self.assertAlmostEqual(abs(R[k,i, j]), R_opt[i, j], delta=0.15) |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
if __name__ == '__main__': |
| 260 |
|
| 261 |
test.Console() |
| 262 |
|