def mean(pmf: PMF) -> num: fs = pmf >> names ws = pmf >> values try: return np.average(fs, weights=ws) >> to(_, pyfloat) except TypeError: fs, ws = list([fs, ws] >> zip) >> select >> ( lambda fv: not isinstance(fv[0], str)) >> zip return np.average(fs, weights=ws) >> to(_, pyfloat)
def testBTFn(): fn = (tSally + null) ^ (tFred * tJoe) rep = [tSally, null] \ >> sortUsing >> (lambda x: x.id) \ >> each >> (lambda x: x >> to(_, pystr)) \ >> joinAll(_, ' + ') repr(fn) >> check >> equal >> f'(({rep}) -> (fred * joe))' assert isinstance(fn, BTFn)
def rep1(pad: cluedo_pad, handId) -> display_table: cfg = tvstruct(nameWidth=17, cellWidth=12, hasWidth=2, noCountWidth=5, suggestCountWidth=3, likeWidth=7) handIds = (pad >> values >> first >> keys >> to(_, pylist) | (N**pystr)[tvseq]) >> drop >> TBI titlesPadding = ' ' * (cfg.nameWidth + cfg.hasWidth + cfg.noCountWidth + cfg.suggestCountWidth + cfg.likeWidth) tTitle = [ titlesPadding + (handIds >> each >> ((lambda hId: ('Me' if hId == handId else hId) >> padWith(_, left=cfg.cellWidth)) | pystr ^ pystr) >> joinAll) ] | display_table tNames = (people, weapons, rooms) \ >> interleave >> ['----'] \ >> each >> (lambda c: c >> padWith(_, left=cfg.nameWidth)) | display_table # show TBI, countHands - noCount, sum priors, sum suggests stats = genStats(pad, handId) cHands = handIds >> count # i.e. the number of players in the game, the number of hands less the TBI hand tSummary = (people, weapons, rooms) \ >> each >> (lambda g: g >> each >> (lambda c: c >> ppCardSummary(_, handId, pad, stats, cfg, cHands) ) ) \ >> interleave >> [''] \ >> each >> (lambda c: c >> padWith(_, left=cfg.nameWidth)) \ | display_table tHands = (people, weapons, rooms) \ >> each >> (lambda g: g >> each >> (lambda c: handIds \ >> each >> ((lambda hId: \ pad[c][hId] >> ppCell(_, stats[c], cfg) ) | pystr^pystr) >> joinAll ) ) \ >> interleave >> [''] \ >> each >> (lambda c: c >> padWith(_, left=cfg.nameWidth)) \ | display_table return tTitle >> join >> (tNames >> hjoin >> tSummary >> hjoin >> tHands | display_table)
def testDrop(): card = pystr['card'] cards = (N**card)[tvseq].setPP('deck').setConstructor(tvseq) Gr = 'Green' Mu = 'Mustard' Or = 'Orchid' Pe = 'Peacock' Pl = 'Plum' Sc = 'Scarlet' people = [Gr, Mu, Or, Pe, Pl, Sc] >> to(_, cards) people >> drop >> [0, 5] >> _v >> check >> equal >> [Mu, Or, Pe, Pl]
def test(): renames = { 'Date': 'date', '1 Mo': '_1m', '2 Mo': '_2m', '3 Mo': '_3m', '6 Mo': '_6m', '1 Yr': '_1y', '2 Yr': '_2y', '3 Yr': '_3y', '5 Yr': '_5y', '7 Yr': '_7y', '10 Yr': '_10y', '20 Yr': '_20y', '30 Yr': '_30y' } conversions = dict( date=vec.to(_, pydate, 'MM/DD/YY'), _1m=vec.to(_, pyfloat), _2m=vec.to(_, pyfloat), _3m=vec.to(_, pyfloat), _6m=vec.to(_, pyfloat), _1y=vec.to(_, pyfloat), _2y=vec.to(_, pyfloat), _3y=vec.to(_, pyfloat), _5y=vec.to(_, pyfloat), _7y=vec.to(_, pyfloat), _10y=vec.to(_, pyfloat), _20y=vec.to(_, pyfloat), _30y=vec.to(_, pyfloat), ) filename = 'us yields.csv' ytms = (path + '/' + filename) >> fromCsv(_, renames, conversions) # take logs ytms2 = ytms \ >> names >> drop >> ['date', '_2m'] \ >> inject(_, agg(ytms), _) >> (lambda prior, name: prior >> atPut(_, 'log' >> join >> (name >> drop >> 1), prior >> at(_, name) >> vec.log ) ) # select the desired date range d1 = '2021.01.01' >> to(_, pydate, 'YYYY.MM.DD') d2 = '2021.04.01' >> to(_, pydate, 'YYYY.MM.DD') usD1ToD2 = ytms2 >> select >> (lambda r: d1 <= r.date and r.date < d2) # diff and calc covariance matrices usDiffs = usD1ToD2 \ >> names >> drop >> 'date' \ >> inject(_, agg(), _) >> (lambda p, f: p >> atPut(_, f, usD1ToD2 >> at(_, f) >> vec.diff) ) t1 = usDiffs >> names t2 = t1 >> select >> startsWith(_, '_') usDiffCov = usDiffs \ >> take >> (usDiffs >> names >> select >> startsWith(_, '_')) \ >> to(_, tvarray) >> stats.core.cov usLogDiffCov = usDiffs \ >> take >> (usDiffs >> names >> select >> startsWith(_, 'log')) \ >> to(_, tvarray) >> stats.core.cov usDiffCov >> check >> sameShape >> usLogDiffCov usDiffCov >> check >> different >> usLogDiffCov
def dateAsDayString(d): return d >> day >> to(_, pystr) >> pad(_, right=3)
def test_repr_or_str(): [Fred(1)] >> to(_, str) >> assertEquals >> '[rep(1)]'
def test_misc(): [[1, 2], [3, 5]] >> to(_,matrix[tvarray]) >> shape >> check >> equal >> (2,2) adhoc(a=1,b=2,c=3) >> drop >> ['a', 'b'] >> to(_,pydict) >> check >> equal >> dict(c=3) [dict(a=1)] >> at(_, 0) >> at(_, "a") >> check >> equal >> 1 dict(b=1, a=2) >> keys >> to(_, pylist) >> sort