RobustShell

 
RobustShell = class RobustShell(IterativeFitter)
    RobustShell(fitter, kernel=<class 'Biweight'>, domain=None, onesided=None, **kwargs)
 
RobustShell tries to make a fit more robust in the presence of outliers.
 
It is a shell around another fitter. Technically it is in itself a
a Fitter, but with limited possiblities.
 
A RobustShell tries to make a fit more robust in the presence of outliers.
It does it by manipulating the weights: outliers are
downweighted. "Normal" points keep their weights, more or less.
 
Apart from methods specific to the robustification, RobustShell has a fit method
and little else from the Fitter family. Methods to get the :math:`\chi^2`,
the covariance matrix, the evidence, the noise scale etc. should be taken from
the embedded fitter.
 
The theory behind robust fitting can be found in Wikipedia: Robust Statistics,
and the references therein.
 
To determine which points are "normal" and which are "outliers" we
need a previous fit. The difference to this earlier fit determines
the normalcy. Its amount is used to adjust the weights.
 
The fact that we need a previous iteration makes robust estimation a
iterative procedure.
Typically the procedure should stabilize in half a dozen steps maximally.
 
There are several schemes to adjust the weights. But before we go into that
we need two values in each scheme.
Firstly the amount of noise present in the data. By default the noise
is taken from the previous fit via Fitter.scale.
The second value needed is the size of the influence domain in terms
of the noise scale.
 
For all schemes the deviant is calculated as the difference
between data and fit divided by noisescale and domainsize:
 
d = ( data - fit ) / ( noisescale * domainsize )
 
The domainsize is defined such that deviants upto 3 times the noisescale fall
within the fwhm.
 
A number of weighting schemes are provided.
 
With bound support and smooth edges:
   Kernel       Name        domain      comment
   Biweight     Tukey         5.54      Default kernel
   CosSquare                  6.00
   Tricube                    5.08
   Triweight                  6.60
 
With bound support and hard edges:
   Uniform      Clip          3.00      Ignore all outside 3 sigma
   Cosine                     4.50
   Triangle                   6.00
   Parabola                   4.50
 
with unbound support:
   Huber        Median        1.50      Inside domain mean; outside domain median
   Gauss                      2.12
   Lorentz                    3.00
 
Other schemes can be written by making another Kernel or writing a function
    wgts = func( d )
where d is the deviant as above.
 
 
Notes
-----
Robust fitting is even more dangerous than ordinary fitting.
*Never trust what you get without thorough checking.*
 
Example
-------
>>> model = PolynomialModel( 1 )                # some model
>>> x = numpy.arange( 100, dtype=float ) / 100  # some x values
>>> y = numpy.arange( 100, dtype=float ) / 4    # digitization noise
>>> y[1,11,35,67] += 10                         # create outliers
>>> ftr = Fitter( x, model )                    # a fitter, a model and a x
>>> rob = RobustShell( ftr, domain=7 )          # robust fitter using someFtr
>>> par = rob.fit( y )                          # solution
>>> print( rob )                                #
>>> print( rob.weights )                        # print final weights
>>> print( ftr.chisq )                          # get from someFtr
 
Author       Do Kester.
 
 
Method resolution order:
RobustShell
IterativeFitter
BaseFitter
builtins.object

Constructor:
RobustShell( fitter, kernel=<class 'Biweight'>, domain=None, onesided=None, **kwargs )
Create a new class, providing the fitter to be used.
 
Parameters
----------
fitter : BaseFitter
     to be used
kernel : Kernel or callable
    All Kernels have a method `result( d )` which is applied to the deviants.
    where d = ( data - model ) / ( domain * scale )
    If kernel is a callable method it is assumed to be a similar result mathod.
domain : None or float
    Width of the kernel.
    None : automatic calculation of domain according to table in class doc.
    float : overrides autocalculation.
onesided : None or "positive" or "p" or "negative" or "n"
    None : apply robust weights to positive and negative residuals
    "positive" : apply robust weights to positive residuals only
    "negative" : apply robust weights to negative residuals only
Methods defined here:
fit( data, weights=None, kernel=None, domain=None, onesided=None, **kwargs )
Perform a robustification step.
 
Parameters
----------
data : array_like
    the data as they go into a fitter
kwargs : dict
    keyword args to be passed to fitter.fit()
getOneSidedWeights( wgt, res, onesided )
setKernel( kernel )
Set the robust kernel to be used.
 
Parameters
----------
kernel : Kernel or callable
    All Kernels have a method `result( d )` which is applied to the deviants.
    where d = ( data - model ) / ( domain * scale )
    If kernel is a callable method it is assumed to be a similar result mathod.
 
Raises
------
ValueError when kernel is not recognized.
setOneSided( onesided )
set .onesided to either 0 or +1 or -1.
 
Parameters
----------
onesided : None or "positive" or "negative"
    None : apply robust weights to positive and negative residuals
    "positive" : apply robust weights to positive residuals only
    "negative" : apply robust weights to negative residuals only
 
Raises
------
ValueError when onesided could not be interpreted.

Methods inherited from IterativeFitter:
Methods inherited from BaseFitter: