pep8 + some refactoring of XXXX.link() methods...

master
Per.Andreas.Brodtkorb 11 years ago
parent c070dd4dc4
commit 629ed411c9

File diff suppressed because it is too large Load Diff

@ -23,7 +23,7 @@ from scipy import optimize
from scipy import integrate from scipy import integrate
# to approximate the pdf of a continuous distribution given its cdf # to approximate the pdf of a continuous distribution given its cdf
from scipy.misc import comb, derivative from scipy.misc import comb, derivative # @UnresolvedImport
from numpy import (arange, putmask, ravel, take, ones, sum, shape, from numpy import (arange, putmask, ravel, take, ones, sum, shape,
product, reshape, zeros, floor, logical_and, log, sqrt, exp, product, reshape, zeros, floor, logical_and, log, sqrt, exp,
@ -1553,7 +1553,7 @@ class rv_continuous(rv_generic):
for item in ['callparams', 'default', 'before_notes']: for item in ['callparams', 'default', 'before_notes']:
tempdict[item] = tempdict[item].replace( tempdict[item] = tempdict[item].replace(
"\n%(shapes)s : array_like\n shape parameters", "") "\n%(shapes)s : array_like\n shape parameters", "")
for i in range(2): for _i in range(2):
if self.shapes is None: if self.shapes is None:
# necessary because we use %(shapes)s in two forms (w w/o ", ") # necessary because we use %(shapes)s in two forms (w w/o ", ")
self.__doc__ = self.__doc__.replace("%(shapes)s, ", "") self.__doc__ = self.__doc__.replace("%(shapes)s, ", "")
@ -1953,13 +1953,40 @@ class rv_continuous(rv_generic):
return output[()] return output[()]
return output return output
def link(self, x, logSF, theta, i): def link(self, x, logSF, theta, i):
''' Return dist. par. no. i as function of quantile (x) and log survival probability (sf) '''
where Return theta[i] as function of quantile, survival probability and
theta is the list containing all parameters including location and scale. theta[j] for j!=i.
Parameters
----------
x : quantile
logSF : logarithm of the survival probability
theta : list
all distribution parameters including location and scale.
Returns
-------
theta[i] : real scalar
fixed distribution parameter theta[i] as function of x, logSF and
theta[j] where j != i.
LINK is a function connecting the fixed distribution parameter theta[i]
with the quantile (x) and the survival probability (SF) and the
remaining free distribution parameters theta[j] for j!=i, i.e.:
theta[i] = link(x, logSF, theta, i),
where logSF = log(Prob(X>x; theta)).
See also
estimation.Profile
''' '''
raise ValueError('Link function not implemented for the %s distribution' % self.name) return self._link(x, logSF, theta, i)
return None
def _link(self, x, logSF, theta, i):
msg = ('Link function not implemented for the %s distribution' %
self.name)
raise NotImplementedError(msg)
def _nnlf(self, x, *args): def _nnlf(self, x, *args):
return -sum(self._logpdf(x, *args), axis=0) return -sum(self._logpdf(x, *args), axis=0)
@ -3030,7 +3057,7 @@ class rv_discrete(rv_generic):
for item in ['callparams', 'default', 'before_notes']: for item in ['callparams', 'default', 'before_notes']:
tempdict[item] = tempdict[item].replace( tempdict[item] = tempdict[item].replace(
"\n%(shapes)s : array_like\n shape parameters", "") "\n%(shapes)s : array_like\n shape parameters", "")
for i in range(2): for _i in range(2):
if self.shapes is None: if self.shapes is None:
# necessary because we use %(shapes)s in two forms (w w/o ", ") # necessary because we use %(shapes)s in two forms (w w/o ", ")
self.__doc__ = self.__doc__.replace("%(shapes)s, ", "") self.__doc__ = self.__doc__.replace("%(shapes)s, ", "")
@ -3498,7 +3525,7 @@ class rv_discrete(rv_generic):
else: else:
invfac = 1.0 invfac = 1.0
tot = 0.0 #tot = 0.0
low, upp = self._ppf(0.001, *args), self._ppf(0.999, *args) low, upp = self._ppf(0.001, *args), self._ppf(0.999, *args)
low = max(min(-suppnmin, low), lb) low = max(min(-suppnmin, low), lb)
upp = min(max(suppnmin, upp), ub) upp = min(max(suppnmin, upp), ub)

@ -152,8 +152,8 @@ class Profile(object):
with ML or MPS estimated distribution parameters. with ML or MPS estimated distribution parameters.
**kwds : named arguments with keys **kwds : named arguments with keys
i : scalar integer i : scalar integer
defining which distribution parameter to profile, i.e. which defining which distribution parameter to keep fixed in the
parameter to keep fixed (default first non-fixed parameter) profiling process (default first non-fixed parameter)
pmin, pmax : real scalars pmin, pmax : real scalars
Interval for either the parameter, phat(i), prb, or x, used in the Interval for either the parameter, phat(i), prb, or x, used in the
optimization of the profile function (default is based on the optimization of the profile function (default is based on the
@ -166,8 +166,8 @@ class Profile(object):
log survival probability,i.e., SF = Prob(X>x;phat) (default None) log survival probability,i.e., SF = Prob(X>x;phat) (default None)
link : function connecting the x-quantile and the survival probability link : function connecting the x-quantile and the survival probability
(SF) with the fixed distribution parameter, i.e.: (SF) with the fixed distribution parameter, i.e.:
self.par[i] = link(x,logSF,self.par,i), where self.par[i] = link(x, logSF, self.par, i), where
logSF = log(Prob(X>x;phat)). logSF = log(Prob(X>x;phat)).
This means that if: This means that if:
1) x is not None then x is profiled 1) x is not None then x is profiled
2) logSF is not None then logSF is profiled 2) logSF is not None then logSF is profiled
@ -236,22 +236,21 @@ class Profile(object):
self.fit_dist = fit_dist self.fit_dist = fit_dist
self.data = None self.data = None
self.args = None self.args = None
self.title = 'Profile log' self.title = ''
self.xlabel = '' self.xlabel = ''
self.ylabel = '' self.ylabel = 'Profile log'
(self.i_fixed, self.N, self.alpha, self.pmin, self.pmax, self.x, (self.i_fixed, self.N, self.alpha, self.pmin, self.pmax, self.x,
self.logSF, self.link) = map( self.logSF, self.link) = map(
kwds.get, kwds.get,
['i', 'N', 'alpha', 'pmin', ['i', 'N', 'alpha', 'pmin', 'pmax', 'x', 'logSF', 'link'],
'pmax', 'x', 'logSF', 'link'],
[i0, 100, 0.05, None, None, None, None, None]) [i0, 100, 0.05, None, None, None, None, None])
self.ylabel = '%g%s CI' % (100 * (1.0 - self.alpha), '%') self.title = '%g%s CI' % (100 * (1.0 - self.alpha), '%')
if fit_dist.method.startswith('ml'): if fit_dist.method.startswith('ml'):
self.title = self.title + 'likelihood' self.ylabel = self.ylabel + 'likelihood'
Lmax = fit_dist.LLmax Lmax = fit_dist.LLmax
elif fit_dist.method.startswith('mps'): elif fit_dist.method.startswith('mps'):
self.title = self.title + ' product spacing' self.ylabel = self.ylabel + ' product spacing'
Lmax = fit_dist.LPSmax Lmax = fit_dist.LPSmax
else: else:
raise ValueError( raise ValueError(
@ -267,7 +266,7 @@ class Profile(object):
self.i_fixed = atleast_1d(self.i_fixed) self.i_fixed = atleast_1d(self.i_fixed)
if 1 - isnotfixed[self.i_fixed]: if 1 - isnotfixed[self.i_fixed]:
raise ValueError( raise IndexError(
"Index i must be equal to an index to one of the free " + "Index i must be equal to an index to one of the free " +
"parameters.") "parameters.")
@ -295,7 +294,7 @@ class Profile(object):
self.xlabel = 'phat(%d)' % self.i_fixed self.xlabel = 'phat(%d)' % self.i_fixed
p_opt = self._par[self.i_fixed] p_opt = self._par[self.i_fixed]
elif self.profile_x: elif self.profile_x:
self.logSF = log(fit_dist.sf(self.x)) self.logSF = fit_dist.logsf(self.x)
self._local_link = lambda fix_par, par: self.link( self._local_link = lambda fix_par, par: self.link(
fix_par, self.logSF, par, self.i_fixed) fix_par, self.logSF, par, self.i_fixed)
self.xlabel = 'x' self.xlabel = 'x'
@ -522,16 +521,23 @@ class Profile(object):
CI = ecross(self.args, self.data, ind[[0, -1]], cross_level) CI = ecross(self.args, self.data, ind[[0, -1]], cross_level)
return CI return CI
def plot(self): def plot(self, axis=None):
''' Plot profile function with 100(1-alpha)% CI ''' Plot profile function with 100(1-alpha)% CI
''' '''
plotbackend.plot( if axis is None:
axis = plotbackend.gca()
p_ci = self.get_bounds(self.alpha)
axis.plot(
self.args, self.data, self.args, self.data,
self.args[[0, -1]], [self.Lmax, ] * 2, 'r', self.args[[0, -1]], [self.Lmax, ] * 2, 'r--',
self.args[[0, -1]], [self.alpha_cross_level, ] * 2, 'r') self.args[[0, -1]], [self.alpha_cross_level, ] * 2, 'r--')
plotbackend.title(self.title) axis.vlines(p_ci, ymin=axis.get_ylim()[0],
plotbackend.ylabel(self.ylabel) ymax=self.Lmax, #self.alpha_cross_level,
plotbackend.xlabel(self.xlabel) color='r', linestyles='--')
axis.set_title(self.title)
axis.set_ylabel(self.ylabel)
axis.set_xlabel(self.xlabel)
def _discretize_adaptive(fun, a, b, tol=0.005, n=5): def _discretize_adaptive(fun, a, b, tol=0.005, n=5):

Loading…
Cancel
Save