def correct_covariance(self, data):
"""Apply a correction to raw Minimum Covariance Determinant estimates.
Correction using the empirical correction factor suggested
by Rousseeuw and Van Driessen in [Rouseeuw1984]_.
Parameters
----------
data : array-like, shape (n_samples, n_features)
The data matrix, with p features and n samples.
The data set must be the one which was used to compute
the raw estimates.
Returns
-------
covariance_corrected : array-like, shape (n_features, n_features)
Corrected robust covariance estimate.
"""
correction = np.median(self.dist_) / chi2(data.shape[1]).isf(0.5)
covariance_corrected = self.raw_covariance_ * correction
self.dist_ /= correction
return covariance_corrected
python类chi2()的实例源码
def get_rv(self,signal):
#extract parameters
N=signal.S.shape[0]
r,p=self.detector.A.shape
if self.type=="PFA":
if self.detector.estimate_sigma2==True:
rv=f(r,N-p)
else:
rv=chi2(r)
if self.type=="PD":
#construct matrices
A=np.matrix(self.detector.A)
b=np.matrix(self.detector.b).T
H=np.matrix(signal.H)
x=np.matrix(signal.X)
sigma2=signal.sigma2
#compute the non central parameter
term1=A*x-b
nc=term1.T*lg.inv(A*lg.inv(H.T*H)*A.T)*term1/sigma2
nc=min(np.asscalar(nc),self.nc_max)
if self.detector.estimate_sigma2==True:
rv=ncf(r,N-p,nc)
else:
rv=ncx2(r,nc)
return rv
###### LINEAR MODEL WITH INTERFERENCE ##################################
def get_rv(self,signal):
#extract parameters
N,p=signal.H1.shape
if self.type=="PFA":
if self.detector.estimate_sigma2==True:
rv=f(p,s-p)
else:
rv=chi2(p)
if self.type=="PD":
H1=np.matrix(signal.H1)
x1=np.matrix(signal.x1).T
H2=np.matrix(signal.H2)
x2=np.matrix(signal.x2).T
sigma2=signal.sigma2
#compute non central parameter
PH2_perp=np.eye(H2.shape[0])-H2*lg.inv(H2.T*H2)*H2.T
y=H1*x1
nc=y.T*PH2_perp*y/sigma2
nc=min(np.asscalar(nc),self.nc_max)
if self.detector.estimate_sigma2==True:
N,t=H2.shape
s=N-t
rv=ncf(p,s-p,nc)
else:
rv=ncx2(p,nc)
return rv
def get_rv(self,signal):
check_MD(signal.X)
#extract parameters
N=signal.N
r,p=self.detector.A.shape
if self.type=="PFA":
if self.detector.estimate_sigma2==True:
rv=f(r,N-p)
else:
rv=chi2(r)
if self.type=="PD":
#construct matrices
A=np.matrix(self.detector.A)
b=np.matrix(self.detector.b).T
H=np.matrix(signal.H)
x=np.matrix(signal.X)
sigma2=signal.sigma2
#compute the non central parameter
term1=A*x-b
nc=term1.T*lg.inv(A*lg.inv(H.T*H)*A.T)*term1/sigma2
nc=min(np.asscalar(nc),self.nc_max)
if self.detector.estimate_sigma2==True:
rv=ncf(r,N-p,nc)
else:
rv=ncx2(r,nc)
return rv
###### LINEAR MODEL WITH INTERFERENCE ##################################
def get_rv(self,signal):
#extract parameters
N,p=signal.H1.shape
if self.type=="PFA":
if self.detector.estimate_sigma2==True:
rv=f(p,s-p)
else:
rv=chi2(p)
if self.type=="PD":
H1=np.matrix(signal.H1)
x1=np.matrix(signal.x1).T
H2=np.matrix(signal.H2)
x2=np.matrix(signal.x2).T
sigma2=signal.sigma2
#compute non central parameter
PH2_perp=np.eye(H2.shape[0])-H2*lg.inv(H2.T*H2)*H2.T
y=H1*x1
nc=y.T*PH2_perp*y/sigma2
nc=min(np.asscalar(nc),self.nc_max)
if self.detector.estimate_sigma2==True:
N,t=H2.shape
s=N-t
rv=ncf(p,s-p,nc)
else:
rv=ncx2(p,nc)
return rv
def __init__(self, stat, null, df, df_denom=None, name=None):
self._stat = stat
self._null = null
self.df = df
self.df_denom = df_denom
self._name = name
if df_denom is None:
self.dist = chi2(df)
self.dist_name = 'chi2({0})'.format(df)
else:
self.dist = f(df, df_denom)
self.dist_name = 'F({0},{1})'.format(df, df_denom)
def reweight_covariance(self, data):
"""Re-weight raw Minimum Covariance Determinant estimates.
Re-weight observations using Rousseeuw's method (equivalent to
deleting outlying observations from the data set before
computing location and covariance estimates). [Rouseeuw1984]_
Parameters
----------
data : array-like, shape (n_samples, n_features)
The data matrix, with p features and n samples.
The data set must be the one which was used to compute
the raw estimates.
Returns
-------
location_reweighted : array-like, shape (n_features, )
Re-weighted robust location estimate.
covariance_reweighted : array-like, shape (n_features, n_features)
Re-weighted robust covariance estimate.
support_reweighted : array-like, type boolean, shape (n_samples,)
A mask of the observations that have been used to compute
the re-weighted robust location and covariance estimates.
"""
n_samples, n_features = data.shape
mask = self.dist_ < chi2(n_features).isf(0.025)
if self.assume_centered:
location_reweighted = np.zeros(n_features)
else:
location_reweighted = data[mask].mean(0)
covariance_reweighted = self._nonrobust_covariance(
data[mask], assume_centered=self.assume_centered)
support_reweighted = np.zeros(n_samples, dtype=bool)
support_reweighted[mask] = True
self._set_covariance(covariance_reweighted)
self.location_ = location_reweighted
self.support_ = support_reweighted
X_centered = data - self.location_
self.dist_ = np.sum(
np.dot(X_centered, self.get_precision()) * X_centered, 1)
return location_reweighted, covariance_reweighted, support_reweighted