1 """Support module for multivariance.py"""
2
3 from gmisclib import gpkmisc
4 from gmisclib import Num
5 from gmisclib import die
6
7
8 BAYES_PRIOR_SPACE = 0
9 HUGE = 1e30
10
14
15
16
17
18
23
24
27
28
29
31 __doc__ = """Virtual base class for a description of a model
32 of a particular size."""
33
34 LF = (1.0/3.0)**2
35
39
41 """This returns the dimensionality of the data."""
42 return self.dim
43
45 """This gives the dimensionality of the model,
46 i.e. the number of parameters required to specify
47 the means and covariance matrix(ces)."""
48 raise RuntimeError, 'Virtual method'
49
51 """This returns some subclass of model."""
52 raise RuntimeError, 'Virtual method'
53
54 - def new(self, mu, invsigma):
55 """Creates a model that contains data."""
56 raise RuntimeError, 'Virtual method'
57
58 - def start(self, dataset):
59 """Selects a random starting point from the dataset."""
60 raise RuntimeError, 'Virtual method'
61
62
63
65 __doc__ = """Virtual base class for adding in the functions
66 you need when you create a model with known parameters
67 (like mu and sigma)."""
68
70 """Bias is an overall shift of the log probability up or
71 down. In a classifier, it is used to bias things toward
72 one class or another."""
73 self.desc = details
74 self._offset = None
75 self.bias = bias
76
77 - def logp(self, datum):
78 raise RuntimeError, 'Virtual method'
79
81 """Returns a vector of parameters."""
82 raise RuntimeError, 'Virtual method'
83
85 return self.desc.ndim()
86
89
90 - def new(self, mu, invsigma):
91 return self.desc.new(mu, invsigma)
92
93 - def start(self, dataset):
95
97 if self._offset is None:
98 self.addoff()
99 return self._offset
100
102 raise RuntimeError, 'Virtual Function'
103
104
105
106
107
108
110 phasespacefac = BAYES_PRIOR_SPACE+1
111 determinantfac = 1 - phasespacefac
112 trace = Num.sum(Num.diagonal(self.invsigma))
113 if trace <= 0.0:
114 raise QuadraticNotNormalizable, "Input trace is nonpositive."
115 try:
116 self.ev = Num.LA.Heigenvalues(self.invsigma)
117 except Num.LA.LinAlgError, x:
118 die.warn('While computing the volume of the probability distribution: %s' % str(x))
119 raise QuadraticNotNormalizable, x
120 if not Num.alltrue(self.ev > 0.0):
121 raise QuadraticNotNormalizable, "Some eigenvalues are zero or negative."
122 assert abs(Num.sum(self.ev)-trace) < 1e-10 + 1e-6*trace, "Bad Eigenvalues run: trace not matched: %g to %g" % (trace, Num.sum(self.ev))
123 assert Num.sum(self.ev) > 0.0, "Input trace is nonpositive."
124 self._offset = Num.sum(Num.log(self.ev))*determinantfac
125
126
127
129 phasespacefac = BAYES_PRIOR_SPACE+1
130 determinantfac = 1 - phasespacefac
131 self.ev = Num.array(self.invsigma, copy=True)
132 if not Num.alltrue(self.ev > 0.0):
133 raise QuadraticNotNormalizable, "Some eigenvalues are zero or negative."
134 self._offset = Num.sum(Num.log(self.ev))*determinantfac
135