Package gmisclib :: Module multivariance_classes
[frames] | no frames]

Source Code for Module gmisclib.multivariance_classes

  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   # 1 for a flat prior in sigma, 
  9  HUGE = 1e30 
 10   
11 -class QuadraticNotNormalizable(ValueError):
12 - def __init__(self, s=''):
13 ValueError.__init__(self, s)
14 15 16 17 18
19 -def vec_inv_variance(start):
20 vv = gpkmisc.vec_variance(start) 21 assert Num.alltrue(Num.greater(vv, 0.0)) 22 return 1.0/vv
23 24
25 -def diag_inv_variance(start):
26 return gpkmisc.make_diag(vec_inv_variance(start))
27 28 29
30 -class modeldesc:
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
36 - def __init__(self, ndim):
37 assert ndim > 0 38 self.dim = ndim
39
40 - def ndim(self):
41 """This returns the dimensionality of the data.""" 42 return self.dim
43
44 - def modeldim(self):
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
50 - def unpack(self, prms):
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
64 -class model_with_numbers:
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
69 - def __init__(self, details, bias):
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
80 - def pack(self):
81 """Returns a vector of parameters.""" 82 raise RuntimeError, 'Virtual method'
83
84 - def ndim(self):
85 return self.desc.ndim()
86
87 - def unpack(self, prms):
88 return self.desc.unpack(prms)
89
90 - def new(self, mu, invsigma):
91 return self.desc.new(mu, invsigma)
92
93 - def start(self, dataset):
94 return self.desc.start(dataset)
95
96 - def offset(self):
97 if self._offset is None: 98 self.addoff() 99 return self._offset
100
101 - def addoff(self): # Should not be called if _offset is not None
102 raise RuntimeError, 'Virtual Function'
103 104 105 106 107 108
109 -def _q_addoff(self): # Will not be called if _offset is not None
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
128 -def _d_addoff(self): # Will not be called if _offset is not None
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