1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
import numpy as N
from numpy.linalg import inv
#from scipy import optimize
from scipy.sandbox.models.contrast import ContrastResults
from scipy.sandbox.models.utils import recipr
class Model(object):
"""
A (predictive) statistical model. The class Model itself does nothing
but lays out the methods expected of any subclass.
"""
def __init__(self):
pass
def initialize(self):
"""
Initialize (possibly re-initialize) a Model instance. For
instance, the design matrix of a linear model may change
and some things must be recomputed.
"""
raise NotImplementedError
def fit(self):
"""
Fit a model to data.
"""
raise NotImplementedError
def predict(self, design=None):
"""
After a model has been fit, results are (assumed to be) stored
in self.results, which itself should have a predict method.
"""
self.results.predict(design)
def view(self):
"""
View results of a model.
"""
raise NotImplementedError
class likelihood_model(Model):
def logL(self, theta):
"""
Log-likelihood of model.
"""
raise NotImplementedError
def score(self, theta):
"""
Score function of model = gradient of logL with respect to
theta.
"""
raise NotImplementedError
def information(self, theta):
"""
Score function of model = - Hessian of logL with respect to
theta.
"""
raise NotImplementedError
def newton(self, theta):
raise NotImplementedError
# def f(theta):
# return -self.logL(theta)
# self.results = optimize.fmin(f, theta)
class likelihood_model_results(object):
''' Class to contain results from likelihood models '''
def __init__(self, beta, normalized_cov_beta=None, scale=1.):
''' Set up results structure
beta - parameter estimates from estimated model
normalized_cov_beta -
Normalized (before scaling) covariance of betas
scale - scalar
normalized_cov_betas is also known as the hat matrix or H
(Semiparametric regression, Ruppert, Wand, Carroll; CUP 2003)
The covariance of betas is given by scale times
normalized_cov_beta
For (some subset of models) scale will typically be the
mean square error from the estimated model (sigma^2)
'''
self.beta = beta
self.normalized_cov_beta = normalized_cov_beta
self.scale = scale
def t(self, column=None):
"""
Return the t-statistic for a given parameter estimate.
Use Tcontrast for more complicated t-statistics.
"""
if self.normalized_cov_beta is None:
raise ValueError, 'need covariance of parameters for computing T statistics'
if column is None:
column = range(self.beta.shape[0])
column = N.asarray(column)
_beta = self.beta[column]
_cov = self.cov_beta(column=column)
if _cov.ndim == 2:
_cov = N.diag(_cov)
_t = _beta * recipr(N.sqrt(_cov))
return _t
def cov_beta(self, matrix=None, column=None, scale=None, other=None):
"""
Returns the variance/covariance matrix of a linear contrast
of the estimates of beta, multiplied by scale which
will usually be an estimate of sigma^2.
The covariance of
interest is either specified as a (set of) column(s) or a matrix.
"""
if self.normalized_cov_beta is None:
raise ValueError, 'need covariance of parameters for computing (unnormalized) covariances'
if scale is None:
scale = self.scale
if column is not None:
column = N.asarray(column)
if column.shape == ():
return self.normalized_cov_beta[column, column] * scale
else:
return self.normalized_cov_beta[column][:,column] * scale
elif matrix is not None:
if other is None:
other = matrix
tmp = N.dot(matrix, N.dot(self.normalized_cov_beta, N.transpose(other)))
return tmp * scale
if matrix is None and column is None:
return self.normalized_cov_beta * scale
def Tcontrast(self, matrix, t=True, sd=True, scale=None):
"""
Compute a Tcontrast for a row vector matrix. To get the t-statistic
for a single column, use the 't' method.
"""
if self.normalized_cov_beta is None:
raise ValueError, 'need covariance of parameters for computing T statistics'
_t = _sd = None
_effect = N.dot(matrix, self.beta)
if sd:
_sd = N.sqrt(self.cov_beta(matrix=matrix))
if t:
_t = _effect * recipr(_sd)
return ContrastResults(effect=_effect, t=_t, sd=_sd, df_denom=self.df_resid)
def Fcontrast(self, matrix, eff=True, t=True, sd=True, scale=None, invcov=None):
"""
Compute an Fcontrast for a contrast matrix.
Here, matrix M is assumed to be non-singular. More precisely,
M pX pX' M'
is assumed invertible. Here, pX is the generalized inverse of the
design matrix of the model. There can be problems in non-OLS models
where the rank of the covariance of the noise is not full.
See the contrast module to see how to specify contrasts.
In particular, the matrices from these contrasts will always be
non-singular in the sense above.
"""
if self.normalized_cov_beta is None:
raise ValueError, 'need covariance of parameters for computing F statistics'
cbeta = N.dot(matrix, self.beta)
q = matrix.shape[0]
if invcov is None:
invcov = inv(self.cov_beta(matrix=matrix, scale=1.0))
F = N.add.reduce(N.dot(invcov, cbeta) * cbeta, 0) * recipr((q * self.scale))
return ContrastResults(F=F, df_denom=self.df_resid, df_num=invcov.shape[0])
|