Fixed bugs in:

link method of frechet_r_gen class
_reduce_func method of rv_continuous and FitDistribution classes 
_myprbfun method in Profile class
master
Per.Andreas.Brodtkorb 14 years ago
parent e8ab556234
commit db8e835b51

@ -2098,10 +2098,10 @@ class rv_continuous(rv_generic):
def _reduce_func(self, args, kwds): def _reduce_func(self, args, kwds):
args = list(args) args = list(args)
Nargs = len(args) - 2 Nargs = len(args)
fixedn = [] fixedn = []
index = range(Nargs) + [-2, -1] index = range(Nargs)
names = ['f%d' % n for n in range(Nargs)] + ['floc', 'fscale'] names = ['f%d' % n for n in range(Nargs-2)] + ['floc', 'fscale']
x0 = args[:] x0 = args[:]
for n, key in zip(index, names): for n, key in zip(index, names):
if kwds.has_key(key): if kwds.has_key(key):
@ -3192,16 +3192,15 @@ class frechet_r_gen(rv_continuous):
def link(self, x, logSF, phat, ix): def link(self, x, logSF, phat, ix):
u = phat[1] u = phat[1]
if ix == 0: if ix == 0:
phati = (x - phat[1]) / (-logSF) ** (1. / phat[2]) phati = log(-logSF) / log((x - phat[1]) / phat[2])
elif ix == 2:
phati = log(-logSF) / log((x - phat[1]) / phat[0])
elif ix == 1: elif ix == 1:
phati = x - phat[0] * (-logSF) ** (1. / phat[2]); phati = x - phat[2] * (-logSF) ** (1. / phat[0])
elif ix == 2:
phati = (x - phat[1]) / (-logSF) ** (1. / phat[0])
else: else:
raise IndexError('Index to the fixed parameter is out of bounds') raise IndexError('Index to the fixed parameter is out of bounds')
return phati return phati
def _pdf(self, x, c): def _pdf(self, x, c):
return c*pow(x,c-1)*exp(-pow(x,c)) return c*pow(x,c-1)*exp(-pow(x,c))
def _logpdf(self, x, c): def _logpdf(self, x, c):

@ -133,7 +133,7 @@ class rv_frozen(object):
class Profile(object): class Profile(object):
''' Profile Log- likelihood or Product Spacing-function. ''' Profile Log- likelihood or Product Spacing-function.
which can be used for constructing confidence interval for which can be used for constructing confidence interval for
either phat(i), probability or quantile. either phat[i], probability or quantile.
Parameters Parameters
---------- ----------
@ -290,17 +290,18 @@ class Profile(object):
self.xlabel = self.xlabel + ' (' + fit_dist.dist.name + ')' self.xlabel = self.xlabel + ' (' + fit_dist.dist.name + ')'
## Check that par are actually at the optimum
phatfree = phatv[self.i_free].copy() phatfree = phatv[self.i_free].copy()
foundNewphat = False
mylogfun = self._nlogfun mylogfun = self._nlogfun
if True: if True:
phatfree = optimize.fmin(mylogfun, phatfree, args=(phatv[self.i_fixed],) , disp=0) ## Check that par are actually at the optimum
LLt = -mylogfun(phatfree,phatv[self.i_fixed]) phatfree = optimize.fmin(mylogfun, phatfree, args=(p_opt,) , disp=0)
LLt = -mylogfun(phatfree, p_opt)
if LLt>Lmax: if LLt>Lmax:
foundNewphat = True foundNewphat = True
warnings.warn('Something wrong with fit') warnings.warn('The fitted parameters does not provide the optimum fit. Something wrong with fit')
dL = Lmax-LLt dL = Lmax-LLt
self.alpha_cross_level -= dL self.alpha_cross_level -= dL
self.Lmax = LLt self.Lmax = LLt
@ -363,7 +364,6 @@ class Profile(object):
pcov = self.fit_dist.par_cov[i_notfixed, :][:, i_notfixed] pcov = self.fit_dist.par_cov[i_notfixed, :][:, i_notfixed]
pvar = sum(numpy.dot(drl, pcov) * drl) pvar = sum(numpy.dot(drl, pcov) * drl)
#p_crit = _WAFODIST.norm.isf(self.alpha / 2.0) * sqrt(numpy.ravel(pvar)) * 1.5
p_crit = -norm_ppf(self.alpha / 2.0) * sqrt(numpy.ravel(pvar)) * 1.5 p_crit = -norm_ppf(self.alpha / 2.0) * sqrt(numpy.ravel(pvar)) * 1.5
if self.pmin == None: if self.pmin == None:
self.pmin = p_opt - 5.0 * p_crit self.pmin = p_opt - 5.0 * p_crit
@ -390,7 +390,7 @@ class Profile(object):
def _myprbfun(self, phatnotfixed): def _myprbfun(self, phatnotfixed):
mphat = self._par.copy() mphat = self._par.copy()
mphat[self.i_notfixed] = phatnotfixed; mphat[self.i_notfixed] = phatnotfixed;
return self.fit_dist.dist.sf(self.x, *mphat) return self.fit_dist.dist.logsf(self.x, *mphat)
def _nlogfun(self, free_par, fix_par): def _nlogfun(self, free_par, fix_par):
@ -535,7 +535,7 @@ class FitDistribution(rv_frozen):
>>> x = phat.isf(SF) >>> x = phat.isf(SF)
# CI for x # CI for x
>>> Lx = Profile(phat, i=0,x=x,link=phat.dist.link) >>> Lx = phat.profile(i=0,x=x,link=phat.dist.link)
>>> Lx.plot() >>> Lx.plot()
>>> x_ci = Lx.get_bounds(alpha=0.2) >>> x_ci = Lx.get_bounds(alpha=0.2)
@ -641,10 +641,10 @@ class FitDistribution(rv_frozen):
def _reduce_func(self, args, kwds): def _reduce_func(self, args, kwds):
args = list(args) args = list(args)
Nargs = len(args) - 2 Nargs = len(args)
fixedn = [] fixedn = []
index = range(Nargs) + [-2, -1] index = range(Nargs)
names = ['f%d' % n for n in range(Nargs)] + ['floc', 'fscale'] names = ['f%d' % n for n in range(Nargs-2)] + ['floc', 'fscale']
x0 = args[:] x0 = args[:]
for n, key in zip(index, names): for n, key in zip(index, names):
if kwds.has_key(key): if kwds.has_key(key):
@ -706,16 +706,8 @@ class FitDistribution(rv_frozen):
optimizer = getattr(optimize, optimizer) optimizer = getattr(optimize, optimizer)
except AttributeError: except AttributeError:
raise ValueError, "%s is not a valid optimizer" % optimizer raise ValueError, "%s is not a valid optimizer" % optimizer
loglike = numpy.inf
for num_times in range(5):
vals = optimizer(func,x0,args=(ravel(data),),disp=0)
ll = func(vals, ravel(data))
if ll<loglike:
loglike = ll
x0 = vals
else:
vals = x0
vals = optimizer(func,x0,args=(ravel(data),),disp=0)
vals = tuple(vals) vals = tuple(vals)
else: else:
vals = tuple(x0) vals = tuple(x0)
@ -1020,7 +1012,20 @@ def main():
phat = FitDistribution(ws.weibull_min, R, 1, scale=1, floc=0.0) phat = FitDistribution(ws.weibull_min, R, 1, scale=1, floc=0.0)
# Better CI for phat.par[i=0] # Better CI for phat.par[i=0]
Lp = Profile(phat, i=0) Lp1 = Profile(phat, i=0)
Lp2 = Profile(phat, i=2)
SF = 1./990
x = phat.isf(SF)
# CI for x
Lx = Profile(phat, i=0,x=x,link=phat.dist.link)
Lx.plot()
x_ci = Lx.get_bounds(alpha=0.2)
# CI for logSF=log(SF)
Lsf = phat.profile(i=0, logSF=log(SF), link=phat.dist.link)
Lsf.plot()
sf_ci = Lsf.get_bounds(alpha=0.2)
pass pass

Loading…
Cancel
Save