示例#1
0
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
示例#2
0
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
示例#3
0
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
示例#4
0
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
示例#5
0
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
示例#6
0
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
示例#7
0
def replace(d:adhoc, f:pystr, new):
    d = adhoc(d)
    d[f] = new
    return d
示例#8
0
def rename(d:adhoc, old, new):
    d = adhoc(d)
    d[new] = d._pop(old)
    return d
示例#9
0
def drop(s:adhoc, name:pystr) -> adhoc:
    answer = adhoc(s)
    del answer[name]
    return answer
示例#10
0
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
示例#11
0
def select(d: adhoc, f2) -> adhoc:
    filteredKVs = []
    for k, v in d._nvs():
        if f2(k, v): filteredKVs.append((k, v))
    return adhoc(filteredKVs)
示例#12
0
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
示例#13
0
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
示例#14
0
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))
示例#15
0
def sort(x: adhoc) -> adhoc:
    return adhoc(sorted(x._nvs(), key=None, reverse=False))
示例#16
0
def QR(A: _matrix) -> adhoc:
    Q, R = numpy.linalg.qr(A)
    return adhoc(Q=_matrix(Q) | +orth, R=_matrix(R) | +right)
示例#17
0
def override(a:adhoc, b:adhoc) -> adhoc:
    answer = adhoc(a)
    answer._update(b._nvs())
    return answer
示例#18
0
def each(a:adhoc, fn2) -> adhoc:
    answer = adhoc()
    for f, v in a._nvs():
        answer[f] = fn2(f, v)
    return answer