def __call__(self, state, scope, pos, paramTypes, x, *args): if len(args) == 3: numbins, low, high = args if low >= high or math.isnan(low) or math.isnan(high): raise PFARuntimeException("bad histogram range", self.errcodeBase + 0, self.name, pos) if numbins < 1: raise PFARuntimeException("bad histogram scale", self.errcodeBase + 1, self.name, pos) if math.isnan(x) or x < low or x >= high: raise PFARuntimeException("x out of range", self.errcodeBase + 2, self.name, pos) out = int(math.floor(numbins * div((x - low), (high - low)))) if out < 0 or out >= numbins: raise PFARuntimeException("x out of range", self.errcodeBase + 2, self.name, pos) return out else: origin, width = args if math.isnan(origin) or math.isinf(origin): raise PFARuntimeException("bad histogram range", self.errcodeBase + 0, self.name, pos) if width <= 0.0 or math.isnan(width): raise PFARuntimeException("bad histogram scale", self.errcodeBase + 1, self.name, pos) if math.isnan(x) or math.isinf(x): raise PFARuntimeException("x out of range", self.errcodeBase + 2, self.name, pos) else: return int(math.floor(div((x - origin), width)))
def __call__(self, state, scope, pos, paramTypes, x, alpha, beta, gamma, theState): if alpha < 0.0 or alpha > 1.0: raise PFARuntimeException("alpha out of range", self.errcodeBase + 0, self.name, pos) if beta < 0.0 or beta > 1.0: raise PFARuntimeException("beta out of range", self.errcodeBase + 1, self.name, pos) if gamma < 0.0 or gamma > 1.0: raise PFARuntimeException("gamma out of range", self.errcodeBase + 2, self.name, pos) level_prev = theState["level"] trend_prev = theState["trend"] cycle_unrotated = theState["cycle"] if len(cycle_unrotated) == 0: raise PFARuntimeException("empty cycle", self.errcodeBase + 3, self.name, pos) cycle_rotated = cycle_unrotated[1:] + [cycle_unrotated[0]] cycle_prev = cycle_rotated[0] if theState["multiplicative"]: level = div(alpha * x, cycle_prev) + (1.0 - alpha) * (level_prev + trend_prev) trend = beta * (level - level_prev) + (1.0 - beta) * trend_prev cycle = div(gamma * x, level) + (1.0 - gamma) * cycle_prev else: level = alpha * (x - cycle_prev) + (1.0 - alpha) * (level_prev + trend_prev) trend = beta * (level - level_prev) + (1.0 - beta) * trend_prev cycle = gamma * (x - level) + (1.0 - gamma) * cycle_prev return dict(theState, level=level, trend=trend, cycle=([cycle] + cycle_rotated[1:]))
def __call__(self, state, scope, paramTypes, x, alpha, beta, gamma, theState): if alpha < 0.0 or alpha > 1.0: raise PFARuntimeException("alpha is out of range") if beta < 0.0 or beta > 1.0: raise PFARuntimeException("beta is out of range") if gamma < 0.0 or gamma > 1.0: raise PFARuntimeException("gamma is out of range") level_prev = theState["level"] trend_prev = theState["trend"] cycle_unrotated = theState["cycle"] if len(cycle_unrotated) == 0: raise PFARuntimeException("empty cycle") cycle_rotated = cycle_unrotated[1:] + [cycle_unrotated[0]] cycle_prev = cycle_rotated[0] if theState["multiplicative"]: level = div(alpha * x, cycle_prev) + (1.0 - alpha) * (level_prev + trend_prev) trend = beta * (level - level_prev) + (1.0 - beta) * trend_prev cycle = div(gamma * x, level) + (1.0 - gamma) * cycle_prev else: level = alpha * (x - cycle_prev) + (1.0 - alpha) * (level_prev + trend_prev) trend = beta * (level - level_prev) + (1.0 - beta) * trend_prev cycle = gamma * (x - level) + (1.0 - gamma) * cycle_prev return dict(theState, level=level, trend=trend, cycle=([cycle] + cycle_rotated[1:]))
def __call__(self, state, scope, pos, paramTypes, observation, prediction, uncertainty): if isinstance(observation, dict): if set(observation.keys()) != set(prediction.keys()): raise PFARuntimeException("misaligned prediction", self.errcodeBase + 0, self.name, pos) if set(observation.keys()) != set(uncertainty.keys()): raise PFARuntimeException("misaligned uncertainty", self.errcodeBase + 1, self.name, pos) return dict( (k, div(observation[k] - prediction[k], uncertainty[k])) for k in observation) elif isinstance(observation, (tuple, list)): if len(observation) != len(prediction): raise PFARuntimeException("misaligned prediction", self.errcodeBase + 0, self.name, pos) if len(observation) != len(uncertainty): raise PFARuntimeException("misaligned uncertainty", self.errcodeBase + 1, self.name, pos) return [ float(div(o - p, u)) for o, p, u in zip(observation, prediction, uncertainty) ] else: return div(observation - prediction, uncertainty)
def interpolateSingle(datum, one, two): onex = one["x"] twox = two["x"] oney = one["to"] twoy = two["to"] unitless = div((datum - onex), (twox - onex)) return (1.0 - unitless)*oney + unitless*twoy
def __call__(self, state, scope, pos, paramTypes, x, y): length = len(x) if len(y) != length: raise PFARuntimeException("dimensions of vectors do not match", self.errcodeBase + 0, self.name, pos) a00, a01, a10, a11 = self.countPairs(x, y) return div((a11 + a00), (a11 + 2 * (a10 + a01) + a00))
def __call__(self, state, scope, pos, paramTypes, x, w, theState, level): originalCount = theState["count"] count = originalCount + w if level == 0: return dict(theState, count=count) else: mean = theState["mean"] delta = x - mean shift = div(delta * w, count) mean += shift if level == 1: return dict(theState, count=count, mean=mean) else: varianceTimesCount = theState["variance"] * originalCount varianceTimesCount += originalCount * delta * shift return dict(theState, count=count, mean=mean, variance=div(varianceTimesCount, count))
def __call__(self, state, scope, paramTypes, x, w, theState, level): originalCount = theState["count"] count = originalCount + w if level == 0: return dict(theState, count=count) else: mean = theState["mean"] delta = x - mean shift = div(delta * w, count) mean += shift if level == 1: return dict(theState, count=count, mean=mean) else: varianceTimesCount = theState["variance"] * originalCount varianceTimesCount += originalCount * delta * shift return dict(theState, count=count, mean=mean, variance=div(varianceTimesCount, count))
def __call__(self, state, scope, pos, paramTypes, data, cluster, weight): if len(data) == 0: raise PFARuntimeException("no data", self.errcodeBase + 0, self.name, pos) dimension = len(data[0]) summ = [0.0 for i in xrange(dimension)] for vec in data: if len(vec) != dimension: raise PFARuntimeException("dimensions of vectors do not match", self.errcodeBase + 1, self.name, pos) for i in xrange(dimension): summ[i] += vec[i] vec = cluster["center"] if len(vec) != dimension: raise PFARuntimeException("dimensions of vectors do not match", self.errcodeBase + 1, self.name, pos) for i in xrange(dimension): summ[i] += weight * vec[i] denom = len(data) + weight for i in xrange(dimension): summ[i] = div(summ[i], denom) return dict(cluster, center=summ)
def __call__(self, state, scope, pos, paramTypes, observation, prediction, uncertainty): if isinstance(observation, dict): if set(observation.keys()) != set(prediction.keys()): raise PFARuntimeException("misaligned prediction", self.errcodeBase + 0, self.name, pos) if set(observation.keys()) != set(uncertainty.keys()): raise PFARuntimeException("misaligned uncertainty", self.errcodeBase + 1, self.name, pos) return dict((k, div(observation[k] - prediction[k], uncertainty[k])) for k in observation) elif isinstance(observation, (tuple, list)): if len(observation) != len(prediction): raise PFARuntimeException("misaligned prediction", self.errcodeBase + 0, self.name, pos) if len(observation) != len(uncertainty): raise PFARuntimeException("misaligned uncertainty", self.errcodeBase + 1, self.name, pos) return [float(div(o - p, u)) for o, p, u in zip(observation, prediction, uncertainty)] else: return div(observation - prediction, uncertainty)
def __call__(self, state, scope, pos, paramTypes, x, y, c00, c01, c10, c11, d00, d01, d10, d11): length = len(x) if len(y) != length: raise PFARuntimeException("dimensions of vectors do not match", self.errcodeBase + 0, self.name, pos) a00, a01, a10, a11 = self.countPairs(x, y) return div((c11 * a11 + c10 * a10 + c01 * a01 + c00 * a00), (d11 * a11 + d10 * a10 + d01 * a01 + d00 * a00))
def interpolateMulti(datum, one, two, code, fcnName, pos): onex = one["x"] twox = two["x"] oney = one["to"] twoy = two["to"] unitless = div((datum - onex), (twox - onex)) if len(oney) != len(twoy): raise PFARuntimeException("inconsistent dimensionality", code, fcnName, pos) return [(1.0 - unitless)*oney[i] + unitless*twoy[i] for i in xrange(len(oney))]
def __call__(self, state, scope, pos, paramTypes, x, meanVariance, *args): mean = meanVariance["mean"] variance = meanVariance["variance"] if variance >= 0.0: sigma = math.sqrt(variance) else: sigma = float("nan") if len(args) == 0: return div(x - mean, sigma) else: unbiased, = args count = meanVariance["count"] if count / (count - 1.0) >= 0.0: correction = math.sqrt((count) / (count - 1.0)) else: correction = float("nan") if unbiased: return div(x - mean, sigma) * correction else: return div(x - mean, sigma)
def calculate(self, x, size): symm = (x + x.T) * 0.5 evals, evects = np().linalg.eig(symm) evects = np().array(evects) evects2 = [evects[:,i] * (-1.0 if evects[0,i] < 0.0 else 1.0) for i in range(size)] eigvalm2 = [div(1.0, math.sqrt(abs(ei))) for ei in evals] order = np().argsort(eigvalm2) out = np().empty((size, size), dtype=np().double) for i in range(size): for j in range(size): out[i,j] = evects2[order[i]][j] * eigvalm2[order[i]] return out
def calculate(self, x, size): symm = (x + x.T) * 0.5 evals, evects = np().linalg.eig(symm) evects = np().array(evects) evects2 = [evects[:,i] * (-1.0 if evects[0,i] < 0.0 else 1.0) for i in xrange(size)] eigvalm2 = [div(1.0, math.sqrt(abs(ei))) for ei in evals] order = np().argsort(eigvalm2) out = np().empty((size, size), dtype=np().double) for i in xrange(size): for j in xrange(size): out[i,j] = evects2[order[i]][j] * eigvalm2[order[i]] return out
def __call__(self, state, scope, paramTypes, data, cluster, weight): if len(data) == 0: raise PFARuntimeException("no data") dimension = len(data[0]) summ = [0.0 for i in xrange(dimension)] for vec in data: if len(vec) != dimension: raise PFARuntimeException("dimensions of vectors do not match") for i in xrange(dimension): summ[i] += vec[i] vec = cluster["center"] if len(vec) != dimension: raise PFARuntimeException("dimensions of vectors do not match") for i in xrange(dimension): summ[i] += weight * vec[i] denom = len(data) + weight for i in xrange(dimension): summ[i] = div(summ[i], denom) return dict(cluster, center=summ)
def __call__(self, state, scope, pos, paramTypes, x, w, theState, windowSize, level): if windowSize < 2: raise PFARuntimeException("windowSize must be at least 2", self.errcodeBase + 0, self.name, pos) if len(theState) == 0: out = {} paramType = state.parser.getAvroType(paramTypes[2]) for field in self._getRecord(paramType).fields: if field.name == "x": out["x"] = x elif field.name == "w": out["w"] = w elif field.name == "count": out["count"] = w elif field.name == "mean": out["mean"] = x elif field.name == "variance": out["variance"] = 0.0 else: raise PFARuntimeException( "cannot initialize unrecognized fields", self.errcodeBase + 1, self.name, pos) return [out] elif len(theState) >= windowSize: record = theState[-1] splitAt = len(theState) - windowSize + 1 remove, keep = theState[:splitAt], theState[splitAt:] oldx = [xi["x"] for xi in remove] oldw = [-xi["w"] for xi in remove] originalCount = record["count"] count = originalCount + w count2 = count + sum(oldw) if level == 0: return keep + [dict(record, x=x, w=w, count=count2)] else: mean = record["mean"] delta = x - mean shift = div(delta * w, count) mean += shift accumulatedCount = count varianceCorrection = 0.0 for ox, ow in zip(oldx, oldw): accumulatedCount += ow delta2 = ox - mean shift2 = div(delta2 * ow, accumulatedCount) mean += shift2 varianceCorrection += (accumulatedCount - ow) * delta2 * shift2 if level == 1: return keep + [ dict(record, x=x, w=w, count=count2, mean=mean) ] else: varianceTimesCount = record["variance"] * originalCount varianceTimesCount += originalCount * delta * shift varianceTimesCount += varianceCorrection return keep + [ dict(record, x=x, w=w, count=count2, mean=mean, variance=div(varianceTimesCount, count2)) ] else: record = theState[-1] originalCount = record["count"] count = originalCount + w if level == 0: return theState + [dict(record, x=x, w=w, count=count)] else: mean = record["mean"] delta = x - mean shift = div(delta * w, count) mean += shift if level == 1: return theState + [ dict(record, x=x, w=w, count=count, mean=mean) ] else: varianceTimesCount = record["variance"] * originalCount varianceTimesCount += originalCount * delta * shift return theState + [ dict(record, x=x, w=w, count=count, mean=mean, variance=div(varianceTimesCount, count)) ]
def __call__(self, state, scope, pos, paramTypes, x, w, theState): size = len(x) if size < 2: raise PFARuntimeException("too few components", self.errcodeBase + 1, self.name, pos) if paramTypes[0]["type"] == "map": oldCount = theState["count"] oldMean = theState["mean"] oldCovariance = theState["covariance"] countKeys = set(oldCount.keys()).union( reduce(lambda a, b: a.union(b), (set(v.keys()) for v in list(oldCount.values())), set())) covarKeys = set(oldCovariance.keys()).union( reduce(lambda a, b: a.union(b), (set(v.keys()) for v in list(oldCovariance.values())), set())) keys = set(x.keys()).union(countKeys).union(covarKeys) newCount = {} for i in keys: row = {} for j in keys: old = oldCount.get(i, {}).get(j, 0.0) if (i in x) and (j in x): row[j] = old + w else: row[j] = old newCount[i] = row newMean = {} for i in keys: old = oldMean.get(i, 0.0) if i in x: newMean[i] = old + div((x[i] - old) * w, newCount[i][i]) else: newMean[i] = old newCovariance = {} for i in keys: row = {} for j in keys: oldCov = oldCovariance.get(i, {}).get(j, 0.0) if (i in x) and (j in x): oldC = oldCount.get(i, {}).get(j, 0.0) oldMi = oldMean.get(i, 0.0) oldMj = oldMean.get(j, 0.0) row[j] = div(((oldCov * oldC) + div( (x[i] - oldMi) * (x[j] - oldMj) * w * oldC, newCount[i][j])), newCount[i][j]) else: row[j] = oldCov newCovariance[i] = row return dict(theState, count=newCount, mean=newMean, covariance=newCovariance) else: oldCount = theState["count"] oldMean = theState["mean"] oldCovariance = theState["covariance"] if (size != len(oldMean) or size != len(oldCovariance) or any(size != len(xi) for xi in oldCovariance)): raise PFARuntimeException("unequal length arrays", self.errcodeBase + 2, self.name, pos) newCount = oldCount + w newMean = [ oldm + div((x[i] - oldm) * w, newCount) for i, oldm in enumerate(oldMean) ] newCovariance = [] for i in range(size): row = [] for j in range(size): row.append( div((oldCovariance[i][j] * oldCount) + div( (x[i] - oldMean[i]) * (x[j] - oldMean[j]) * w * oldCount, newCount), newCount)) newCovariance.append(row) return dict(theState, count=newCount, mean=newMean, covariance=newCovariance)
def __call__(self, state, scope, pos, paramTypes, x, y): return div(x, y)
def __call__(self, state, scope, pos, paramTypes, x, y): length = len(x) if len(y) != length: raise PFARuntimeException("dimensions of vectors do not match", self.errcodeBase + 0, self.name, pos) a00, a01, a10, a11 = self.countPairs(x, y) return div((a11 + a00), (a11 + 2*(a10 + a01) + a00))
def __call__(self, state, scope, pos, paramTypes, x, w, theState): size = len(x) if size < 2: raise PFARuntimeException("too few components", self.errcodeBase + 1, self.name, pos) if paramTypes[0]["type"] == "map": oldCount = theState["count"] oldMean = theState["mean"] oldCovariance = theState["covariance"] countKeys = set(oldCount.keys()).union(reduce(lambda a, b: a.union(b), (set(v.keys()) for v in oldCount.values()), set())) covarKeys = set(oldCovariance.keys()).union(reduce(lambda a, b: a.union(b), (set(v.keys()) for v in oldCovariance.values()), set())) keys = set(x.keys()).union(countKeys).union(covarKeys) newCount = {} for i in keys: row = {} for j in keys: old = oldCount.get(i, {}).get(j, 0.0) if (i in x) and (j in x): row[j] = old + w else: row[j] = old newCount[i] = row newMean = {} for i in keys: old = oldMean.get(i, 0.0) if i in x: newMean[i] = old + div((x[i] - old) * w, newCount[i][i]) else: newMean[i] = old newCovariance = {} for i in keys: row = {} for j in keys: oldCov = oldCovariance.get(i, {}).get(j, 0.0) if (i in x) and (j in x): oldC = oldCount.get(i, {}).get(j, 0.0) oldMi = oldMean.get(i, 0.0) oldMj = oldMean.get(j, 0.0) row[j] = div(((oldCov*oldC) + div((x[i] - oldMi) * (x[j] - oldMj) * w*oldC, newCount[i][j])), newCount[i][j]) else: row[j] = oldCov newCovariance[i] = row return dict(theState, count=newCount, mean=newMean, covariance=newCovariance) else: oldCount = theState["count"] oldMean = theState["mean"] oldCovariance = theState["covariance"] if (size != len(oldMean) or size != len(oldCovariance) or any(size != len(xi) for xi in oldCovariance)): raise PFARuntimeException("unequal length arrays", self.errcodeBase + 2, self.name, pos) newCount = oldCount + w newMean = [oldm + div((x[i] - oldm) * w, newCount) for i, oldm in enumerate(oldMean)] newCovariance = [] for i in xrange(size): row = [] for j in xrange(size): row.append(div((oldCovariance[i][j]*oldCount) + div((x[i] - oldMean[i]) * (x[j] - oldMean[j]) * w*oldCount, newCount), newCount)) newCovariance.append(row) return dict(theState, count=newCount, mean=newMean, covariance=newCovariance)
def __call__(self, state, scope, pos, paramTypes, state_): return div(state_["chi2"], state_["dof"])
def __call__(self, state, scope, pos, paramTypes, x, y, c00, c01, c10, c11, d00, d01, d10, d11): length = len(x) if len(y) != length: raise PFARuntimeException("dimensions of vectors do not match", self.errcodeBase + 0, self.name, pos) a00, a01, a10, a11 = self.countPairs(x, y) return div((c11*a11 + c10*a10 + c01*a01 + c00*a00), (d11*a11 + d10*a10 + d01*a01 + d00*a00))
def __call__(self, state, scope, pos, paramTypes, x, y, sigma): return math.exp(div(-math.log(2) * (x - y)**2, sigma**2))
def __call__(self, state, scope, paramTypes, x, w, theState, windowSize, level): if windowSize < 2: raise PFARuntimeException("windowSize must be at least 2") if len(theState) == 0: out = {} paramType = state.parser.getAvroType(paramTypes[2]) for field in self._getRecord(paramType).fields: if field.name == "x": out["x"] = x elif field.name == "w": out["w"] = w elif field.name == "count": out["count"] = w elif field.name == "mean": out["mean"] = x elif field.name == "variance": out["variance"] = 0.0 else: raise PFARuntimeException("cannot initialize unrecognized fields") return [out] elif len(theState) >= windowSize: record = theState[-1] splitAt = len(theState) - windowSize + 1 remove, keep = theState[:splitAt], theState[splitAt:] oldx = [xi["x"] for xi in remove] oldw = [-xi["w"] for xi in remove] originalCount = record["count"] count = originalCount + w count2 = count + sum(oldw) if level == 0: return keep + [dict(record, x=x, w=w, count=count2)] else: mean = record["mean"] delta = x - mean shift = div(delta * w, count) mean += shift accumulatedCount = count varianceCorrection = 0.0 for ox, ow in zip(oldx, oldw): accumulatedCount += ow delta2 = ox - mean shift2 = div(delta2 * ow, accumulatedCount) mean += shift2 varianceCorrection += (accumulatedCount - ow) * delta2 * shift2 if level == 1: return keep + [dict(record, x=x, w=w, count=count2, mean=mean)] else: varianceTimesCount = record["variance"] * originalCount varianceTimesCount += originalCount * delta * shift varianceTimesCount += varianceCorrection return keep + [dict(record, x=x, w=w, count=count2, mean=mean, variance=div(varianceTimesCount, count2))] else: record = theState[-1] originalCount = record["count"] count = originalCount + w if level == 0: return theState + [dict(record, x=x, w=w, count=count)] else: mean = record["mean"] delta = x - mean shift = div(delta * w, count) mean += shift if level == 1: return theState + [dict(record, x=x, w=w, count=count, mean=mean)] else: varianceTimesCount = record["variance"] * originalCount varianceTimesCount += originalCount * delta * shift return theState + [dict(record, x=x, w=w, count=count, mean=mean, variance=(varianceTimesCount / count))]