def subset(a:adhoc, f2:pyfunc) -> pytuple: A, B = adhoc(), adhoc() for k, v in a._nvs(): if f2(k, v): A[k] = v else: B[k] = v return A, B
def toCmf(pmf: PMF + adhoc): running = 0.0 answer = adhoc() answer2 = adhoc() for k, v in pmf._nvs(): if isinstance(v, float): running += v answer[k] = running else: answer2[k] = v cmf = np.array(list(answer._nvs())) # cmf[:, 1] = np.cumsum(cmf[:, 1]) answer = answer >> override >> answer2 answer['_cmf'] = cmf return answer
def _rvOp(lhs, rhs, op): xps = {} for x1, p1 in lhs._nvs(): for x2, p2 in rhs._nvs(): x = op(x1, x2) xps[x] = xps.setdefault(x, 0.0) + p1 * p2 return _normalisedInPlace(adhoc(sorted(xps.items(), key=lambda xp: xp[0]))) | PMF
def drop(s:adhoc, names:pylist) -> adhoc: # names may be a list[str] or list[int] answer = adhoc(s) if not names: return answer if type(names[0]) is str: del answer[names] elif type(names[0]) is int: raise NotImplementedError() else: raise TypeError(f'Unhandled type list[{str(type(names[0]))}]') return answer
def uniform(nOrXs: pylist) -> PMF: '''Makes a uniform PMF. xs can be sequence of values or [length]''' # if a single int it is a count else there must be many xs answer = adhoc() | PMF if len(nOrXs) == 1: if isinstance(nOrXs[0], int): n = nOrXs[0] p = 1.0 / n for x in sequence(0, n - 1): answer[float(x)] = p return answer p = 1.0 / len(nOrXs) for x in nOrXs: answer[float(x)] = p return answer
def where(s:adhoc, bools) -> adhoc: assert isinstance(s, adhoc) answer = adhoc(s) for f, v in s._nvs(): answer[f] = v[bools].view(tvarray) return answer
def replace(d:adhoc, f:pystr, new): d = adhoc(d) d[f] = new return d
def rename(d:adhoc, old, new): d = adhoc(d) d[new] = d._pop(old) return d
def drop(s:adhoc, name:pystr) -> adhoc: answer = adhoc(s) del answer[name] return answer
def normalise(pmf: pydict) -> PMF: # immutable, asssumes non-numeric values are tags and all numeric values are part of pmf return _normalisedInPlace(adhoc(pmf)) | PMF
def select(d: adhoc, f2) -> adhoc: filteredKVs = [] for k, v in d._nvs(): if f2(k, v): filteredKVs.append((k, v)) return adhoc(filteredKVs)
def pmfMul(lhs: numsEtAl[T1], rhs: numsEtAl[T2]) -> numsEtAl: # pmf(lhs kvs '{(x.k, x.v*(y.v)} (rhs kvs)) normalise return adhoc( both(lhs >> nvs, lambda fv1, fv2: (fv1[0], fv1[1] * fv2[1]), rhs >> nvs)) | numsEtAl
def to(xs: pylist, t: PMF, kde: scipy.stats.kde.gaussian_kde) -> adhoc: answer = adhoc() answer._kde = kde for x in xs: answer[x] = kde.evaluate(x)[0] return answer >> normalise
def normalise(pmf: (T2**T1)[adhoc][T3]) -> (T2**T1)[adhoc][T3]: # immutable, asssumes non-numeric values are tags and all numeric values are part of pmf return _normalisedInPlace(adhoc(pmf))
def sort(x: adhoc) -> adhoc: return adhoc(sorted(x._nvs(), key=None, reverse=False))
def QR(A: _matrix) -> adhoc: Q, R = numpy.linalg.qr(A) return adhoc(Q=_matrix(Q) | +orth, R=_matrix(R) | +right)
def override(a:adhoc, b:adhoc) -> adhoc: answer = adhoc(a) answer._update(b._nvs()) return answer
def each(a:adhoc, fn2) -> adhoc: answer = adhoc() for f, v in a._nvs(): answer[f] = fn2(f, v) return answer