def VKmeanupper( pa, pb, k1, k2, k3, k4 ): # Interval pa, Interval pb, double k1, double k2, double k3, double k4) #double p1, p2, ec2; lpa = utils.left(pa) rpa = utils.right(pa) lpb = utils.left(pb) rpb = utils.right(pb) touch = touching(pa, pb) p1 = lpa p2 = lpb ec2 = VKmeanup(p1, p2, k1, k2, k3, k4) p1 = lpa p2 = rpb ec2 = np.max([ec2, VKmeanup(p1, p2, k1, k2, k3, k4)]) p1 = rpa p2 = lpb ec2 = np.max([ec2, VKmeanup(p1, p2, k1, k2, k3, k4)]) p1 = rpa p2 = rpb ec2 = np.max([ec2, VKmeanup(p1, p2, k1, k2, k3, k4)]) p1 = np.max([lpa, 1 - rpb]) p2 = 1 - p1 if (touch): ec2 = np.max([ec2, VKmeanup(p1, p2, k1, k2, k3, k4)]) p1 = np.min([rpa, 1 - lpb]) p2 = 1 - p1 if (touch): ec2 = np.max([ec2, VKmeanup(p1, p2, k1, k2, k3, k4)]) return ec2
def VKmeanlower( pa, pb, k1, k2, k3, k4 ): # Interval pa, Interval pb, double k1, double k2, double k3, double k4) # double p1, p2, ec1; lpa = utils.left(pa) rpa = utils.right(pa) lpb = utils.left(pb) rpb = utils.right(pb) touch = touching(pa + pb, 1.0) p1 = lpa p2 = lpb ec1 = VKmeanlo(p1, p2, k1, k2, k3, k4) p1 = lpa p2 = rpb ec1 = np.min([ec1, VKmeanlo(p1, p2, k1, k2, k3, k4)]) p1 = rpa p2 = lpb ec1 = np.min([ec1, VKmeanlo(p1, p2, k1, k2, k3, k4)]) p1 = rpa p2 = rpb ec1 = np.min([ec1, VKmeanlo(p1, p2, k1, k2, k3, k4)]) p1 = np.max([lpa, 1 - rpb]) p2 = 1 - p1 if touch: ec1 = np.min([ec1, VKmeanlo(p1, p2, k1, k2, k3, k4)]) p1 = np.min([rpa, 1 - lpb]) p2 = 1 - p1 if touch: ec1 = np.min([ec1, VKmeanlo(p1, p2, k1, k2, k3, k4)]) return ec1
def touching(a, b): # const Interval& a, const Interval& b ) if np.all(utils.left(a) == utils.right(a)) and np.all( utils.left(b) == utils.right(b)): return utils.left(a) == utils.left(b) if utils.straddles(a - b): return True return False
def VKmeanproduct(a, b): # Interval ea,eb,ec,pa,pb; # double la,ra,lb,rb,k1,k2,k3,k4,ec1,ec2; ec = interval.Interval(-np.inf, np.inf) ea = interval.Interval(a.mean_lo, a.mean_hi) eb = interval.Interval(b.mean_lo, b.mean_hi) la = utils.left(a) ra = utils.right(a) lb = utils.left(b) rb = utils.right(b) k1 = ra * rb k2 = ra * lb k3 = la * rb k4 = la * lb pa = interval.Interval((utils.left(ea) - la) / (ra - la), (utils.right(ea) - la) / (ra - la)) pb = interval.Interval((utils.left(eb) - lb) / (rb - lb), (utils.right(eb) - lb) / (rb - lb)) ec = utils.env_int(VKmeanlower(pa, pb, k1, k2, k3, k4), VKmeanupper(pa, pb, k1, k2, k3, k4)) return ec
def reciprocal(self): if self.shape in [ 'Cauchy', '{min, max, median}', '{min, max, percentile}', '{min, max}' ]: sh = self.shape elif self.shape == 'Pareto': sh = 'power function' elif self.shape == 'power function': sh = 'Pareto' else: sh = '' t = lambda x: 1 / x if utils.left(self) <= 0 and utils.right(self) >= 0: return np.nan elif utils.left(self) > 0: mymean = rowebounds.transformedMean(self, t, False, True) myvar = rowebounds.transformedVariance(self, t, False, True, mymean) else: mymean = rowebounds.transformedMean(self, t, False, False) myvar = rowebounds.transformedVariance(self, t, False, False, mymean) pbox_parms = { "hi": 1 / np.flip(self.lo), "lo": 1 / np.flip(self.hi), "shape": sh, "mean_lo": utils.left(mymean), "mean_hi": utils.right(mymean), "var_lo": utils.left(myvar), "var_hi": utils.right(myvar) } return Pbox(**pbox_parms)
def _naivefrechetconv(self, other, op='*'): if op == '+': return self._frechetconv(other, '+') if op == '-': return self._frechetconv(-other, '+') if op == '/': return self._naivefrechetconv(other.reciprocal(), '*') # x = makepbox(x) # y = makepbox(y) n = len(self.hi) c = (utils.get_op(op)([[i] for i in self.hi], other.hi)).flatten() c.sort() Zd = c[np.array(range(n * n - n, n * n))] c = (utils.get_op(op)([[i] for i in self.lo], other.lo)).flatten() c.sort() Zu = c[np.array(range(n))] # mean m = interval.Interval(self.mean_lo, self.mean_hi) * interval.Interval( other.mean_lo, other.mean_hi) a = np.sqrt( interval.Interval(self.var_lo, self.var_hi) * interval.Interval(other.var_lo, other.var_hi)) ml = m - a mh = m + a VK = vkmean.VKmeanproduct(self, other) m = utils.imp(interval.Interval(ml, mh), VK) # variance vl = 0 vh = np.inf pbox_parms = { "hi": Zd, "lo": Zu, "mean_lo": utils.left(m), "mean_hi": utils.right(m), "var_lo": vl, "var_hi": vh } return Pbox(**pbox_parms)
def _checkmoments(self): a = interval.Interval(self.mean_lo, self.mean_hi) #mean(x) b = utils.dwMean(self) self.mean_lo = np.max([utils.left(a), utils.left(b)]) self.mean_hi = np.min([utils.right(a), utils.right(b)]) if self.mean_hi < self.mean_lo: # use the observed mean self.mean_lo = utils.left(b) self.mean_hi = utils.right(b) a = interval.Interval(self.var_lo, self.var_hi) #var(x) b = utils.dwVariance(self) self.var_lo = np.max([utils.left(a), utils.left(b)]) self.var_hi = np.min([utils.right(a), utils.right(b)]) if self.var_hi < self.var_lo: # use the observed variance self.var_lo = utils.left(b) self.var_hi = utils.right(b)
def __init__(self, lo=None, hi=None, steps=200, shape=None, mean_lo=None, mean_hi=None, var_lo=None, var_hi=None, interpolation='linear'): if isinstance(lo, Pbox): self.lo = lo.lo self.hi = lo.hi self.steps = lo.steps self.n = lo.steps self.shape = lo.shape self.mean_lo = lo.mean_lo self.mean_hi = lo.mean_hi self.var_lo = lo.var_lo self.var_hi = lo.var_hi else: if lo is None and hi is None: lo = -np.inf hi = np.inf if (lo is not None) and (hi is None): hi = lo if isinstance(lo, interval.Interval) or (not hasattr(lo, '__len__')): lo = np.array([utils.left(lo)]) if isinstance(hi, interval.Interval) or (not hasattr(hi, '__len__')): hi = np.array([utils.right(hi)]) if len(lo) != steps: lo = utils.interpolate(lo, interpolation=interpolation, left=True, pbox_steps=steps) if len(hi) != steps: hi = utils.interpolate(hi, interpolation=interpolation, left=False, pbox_steps=steps) self.lo = lo self.hi = hi self.steps = steps self.n = self.steps self.shape = shape self.mean_lo = -np.inf self.mean_hi = np.inf self.var_lo = 0 self.var_hi = np.inf self._computemoments() if shape is not None: self.shape = shape if mean_lo is not None: self.mean_lo = np.max([mean_lo, self.mean_lo]) if mean_hi is not None: self.mean_hi = np.min([mean_hi, self.mean_hi]) if var_lo is not None: self.var_lo = np.max([var_lo, self.var_lo]) if var_hi is not None: self.var_hi = np.min([var_hi, self.var_hi]) self._checkmoments()