Beispiel #1
0
 def test_2(self):
     self.assertEqual(
         select(
             map(EfInt, (0.17, 0.14, 0.19, 0.2, 0.18, 0.13)),
             map(lambda row: map(EfInt, row), (
                 (),
                 (0.075, ),
                 (0.209, 0.286),
                 (0.127, 0.257, 0.229),
                 (0.177, 0.192, 0.158, 0.117),
                 (0.128, 0.132, 0.106, 0.081, 0.062),
             )), EfInt(0.12)), 3)
     self.assertEqual(
         select(
             map(EfInt, (
                 0.14,
                 0.20,
                 0.24,
                 0.21,
                 0.17,
                 0.17,
             )),
             map(lambda row: map(EfInt, row), (
                 (),
                 (0.29, ),
                 (0.32, 0.36),
                 (0.24, 0.22, 0.11),
                 (0.18, 0.15, 0.08, 0.06),
                 (0.18, 0.16, 0.12, 0.13, 0.19),
             )), EfInt(0.12)), 2)
Beispiel #2
0
    def test_1(self):
        projects = (
            Project(-41000, 6100, 0, 7),
            Project(-32000, 6700, 0, 7),
            Project(-28000, 5700, 0, 5),
            Project(-28000, 12600, 0, 5),
            Project(-36000, 9000, 0, 7),
            Project(-27000, 10600, 0, 6),
            Project(-53000, 6700, 0, 5),
            Project(-50000, 15000, 0, 6),
            Project(-32000, 6900, 0, 7),
            Project(-42000, 14600, 0, 5),
        )

        irrs = tuple(irr(project.cash_flows, EfInt(0)) for project in projects)

        self.assertIterableAlmostEqual((irr_.to_ef().rate for irr_ in irrs), (
            0.010261108929599895,
            0.10584583010815002,
            0.005929015028005828,
            0.3494328573992243,
            0.16326709023510008,
            0.31754169406374866,
            -0.13571830650187303,
            0.1990541470961173,
            0.114956469240095,
            0.2178733729868983,
        ))

        self.assertAlmostEqual(
            de_facto_marr(projects, 100000, EfInt(0)).to_ef().rate,
            0.2178733729868983)
Beispiel #3
0
 def test_projects(self):
     self.assertAlmostEqual(
         pw(Project(-20000, 4000 - 1000, 4000, 10).cash_flows, EfInt(0.05)),
         5620.857801717468)
     self.assertAlmostEqual(
         aw(Project(-20000, 4000 - 1000, 4000, 10).cash_flows, EfInt(0.05)),
         727.9268005526942)
Beispiel #4
0
    def test_4(self):
        marr = EfInt(0.12)
        projects = tuple(
            filter(lambda project: irr(project.cash_flows, EfInt(0)) > marr, (
                Project(-80000, 13000, 10000, 10),
                Project(-120000, 23000, 34000, 10),
                Project(-145000, 24000, 25000, 10),
                Project(-145000, 28000, 29000, 10),
            )))
        irrs = (irr(project.cash_flows, EfInt(0)) for project in projects)
        table = irr_table(projects, EfInt(0))

        self.assertEqual(select(irrs, table, marr), 1)
Beispiel #5
0
    def test_8(self):
        bond = Bond.from_rate(100, EfInt(0.05), 1, 5)
        default = 0.02
        rf = EfInt(0.03)
        good, bad = 5 + bond.face, bond.face * 0.4

        for i in range(5):
            good = 5 + ((1 - default) * good + default * bad) / rf.to_factor()

        good -= 5

        self.assertAlmostEqual(good, 103.08377972409899)
        self.assertAlmostEqual(
            yield_(bond.cash_flows, good, EfInt(0)).rate, 0.04301423365637252)
Beispiel #6
0
    def test_4(self):
        e, p, t = EfInt(0.034), 100000, 4

        self.assertAlmostEqual(e.to_nom(2).rate, 0.03371581102178567)
        self.assertAlmostEqual(e.to_nom(12).rate, 0.033481397886386155)
        self.assertAlmostEqual(e.to_nom(365).rate, 0.03343630748129267)
        self.assertAlmostEqual(e.to_cont().rate, 0.03343477608623742)

        self.assertAlmostEqual(p * e.to_nom(2).to_factor(t),
                               p * e.to_nom(12).to_factor(t))
        self.assertAlmostEqual(p * e.to_nom(12).to_factor(t),
                               p * e.to_nom(365).to_factor(t))
        self.assertAlmostEqual(p * e.to_nom(365).to_factor(t),
                               p * e.to_cont().to_factor(t))
        self.assertAlmostEqual(p * e.to_cont().to_factor(t), 114309.4552336)
Beispiel #7
0
    def test_8(self):
        project, i = Project(1000, 500, 300, 10), EfInt(0.6)

        x, y = project.cash_flows, link(
            (project.cash_flows, project.cash_flows))

        self.assertAlmostEqual(aw(x, i), aw(y, i))
Beispiel #8
0
    def test_3(self):
        cases = (
            (1, 99.52, 0.05943811, 0.05773868),
            (4, 99.01, 0.03029791, 0.02984799),
            (8, 97.64, 0.03647384, 0.03582441),
            (12, 95.85, 0.04329682, 0.04238572),
        )

        for m, p, a, b in cases:
            self.assertAlmostEqual(
                np.float_(
                    fsolve(lambda y: p * EfInt(y).to_factor(m / 12) - 100,
                           np.array(0))), a)
            self.assertAlmostEqual(
                np.float_(
                    fsolve(lambda y: p * ContInt(y).to_factor(m / 12) - 100,
                           np.array(0))), b)

        self.assertAlmostEqual(np.interp(7, (4, 8), (0.03029791, 0.03647384)),
                               0.0349298575)
        self.assertAlmostEqual(np.interp(7, (4, 8), (0.02984799, 0.03582441)),
                               0.034330305)

        self.assertAlmostEqual(
            np.float_(
                fsolve(
                    lambda y: 1.03029791**(4. / 12) *
                    (1 + y)**(8. / 12) - 1.04329682, np.array(0))), 0.04985765)
Beispiel #9
0
def capm(b, rf, em):
    """Calculates the expected return using the CAPM model.

    :param b: The company risk.
    :param rf: The risk-free rate.
    :param em: The expected market return rate.
    :return: The expected return.
    """
    return EfInt(rf.to_ef().rate + b * (em.to_ef().rate - rf.to_ef().rate))
Beispiel #10
0
    def test_3(self):
        i = EfInt(0.05)

        aw_cr = aw(Project(300000, 14000, 0, 20).cash_flows, i)
        aw_ed = aw(Project(220000, 21000, 0, 20).cash_flows, i) + aw(
            (CashFlow(8, 35000), ), i)

        self.assertAlmostEqual(aw_cr, 38072.77615720737)
        self.assertAlmostEqual(aw_ed, 42318.6326589209)
Beispiel #11
0
    def test_4(self):
        a, b = Project(425, 48, 0, 3), Project(450, 45, 0, 4)
        i = EfInt(0.1)

        self.assertAlmostEqual(rpw(a.cash_flows, i, 5), 926.2665553147758)
        self.assertAlmostEqual(rpw(link((a.cash_flows, b.cash_flows)), i, 5),
                               941.1376210020427)
        self.assertAlmostEqual(rpw(b.cash_flows, i, 5), 927.9414595376618)
        self.assertAlmostEqual(rpw(link((b.cash_flows, a.cash_flows)), i, 5),
                               912.7288871227124)
Beispiel #12
0
    def test_6(self):
        i = EfInt(0.06)
        mortgage = Mortgage(580000, i, 1)

        self.assertAlmostEqual(
            1 - mortgage.pay(2).principal * i.rate / mortgage.payment,
            0.26179726123417735)
        self.assertAlmostEqual(
            1 - mortgage.pay(9).principal * i.rate / mortgage.payment,
            0.39364628371277466)
Beispiel #13
0
def ror(price, bull, bear, p):
    """Calculates the expected rate of return of an asset.

    :param price: The price of an asset.
    :param bull: The asset price in a bullish market.
    :param bear: The asset price in a bearish market.
    :param p: The percentage of a bull market.
    :return: The expected rate of return.
    """
    return EfInt((bull * p + bear * (1 - p)) / price - 1)
Beispiel #14
0
    def test_2(self):
        basis, salvage, life, t = 210000, 10000, 20, 6

        self.assertAlmostEqual(
            StrLineDeprec(basis, salvage, life).book(t), 150000)
        self.assertAlmostEqual(
            DeclBalDeprec(basis, salvage, life).book(t), 84246.81795174461)
        self.assertAlmostEqual(
            DeclBalDeprec.from_rate(basis, life, EfInt(0.2)).book(t), 55050.24)
        self.assertAlmostEqual(
            DblDeclBalDeprec(basis, salvage, life).book(t), 111602.61)

        self.assertAlmostEqual(
            StrLineDeprec(basis, salvage, life).amount(t), 10000)
        self.assertAlmostEqual(
            DeclBalDeprec(basis, salvage, life).amount(t), 13852.157371177402)
        self.assertAlmostEqual(
            DeclBalDeprec.from_rate(basis, life, EfInt(0.2)).amount(t),
            13762.56)
        self.assertAlmostEqual(
            DblDeclBalDeprec(basis, salvage, life).amount(t), 12400.29)
Beispiel #15
0
    def test_1(self):
        i, n = 0.05, 6
        y = pw(Project(-700, 370, 0, n).cash_flows, EfInt(i))

        self.assertAlmostEqual(y, 1178.006064888955)
        self.assertAlmostEqual(y * fp(i, n), 1578.6407921875)
        self.assertAlmostEqual(y * fp(i, 4), 1431.8737344104306)
        self.assertAlmostEqual(
            np.float_(
                fsolve(lambda x: y - x * pf(i, 1) - 5 * x * pf(i, 3),
                       np.array(0))), 223.464034554222)

        i, n = 0.09, 6
        y = pw(Project(-700, 370, 0, n).cash_flows, EfInt(i))

        self.assertAlmostEqual(y, 959.7898783854448)
        self.assertAlmostEqual(y * fp(i, n), 1609.6637114243001)
        self.assertAlmostEqual(y * fp(i, 4), 1354.821741793031)
        self.assertAlmostEqual(
            np.float_(
                fsolve(lambda x: y - x * pf(i, 1) - 5 * x * pf(i, 3),
                       np.array(0))), 200.862256010023)
Beispiel #16
0
    def test_5(self):
        rf, p_bull = EfInt(0.022), 0.6
        a_bull, a_bear = 102, 77
        m_price, m_bull, m_bear = 105, 127, 90
        ap = fair(102, 77, m_price, m_bull, m_bear, rf)
        ar, mr = ror(ap, a_bull, a_bear, p_bull), ror(m_price, m_bull, m_bear,
                                                      p_bull)
        b = beta(ar, mr, rf)

        self.assertAlmostEqual(ap, 86.78663986883166)
        self.assertAlmostEqual(ar.rate, 0.060070998704959244)
        self.assertAlmostEqual(mr.rate, 0.0685714285714285)
        self.assertAlmostEqual(b, 0.8174754323150769)
Beispiel #17
0
    def test_2(self):
        cr = tuple(Project(300000, 14000, 0, 20).cash_flows)
        ed = tuple(Project(220000, 21000, 0, 20).cash_flows) + (CashFlow(
            8, 35000), CashFlow(16, 35000))
        i = EfInt(0.05)

        self.assertAlmostEqual(pw(cr, i), 474470.94479555974)
        self.assertAlmostEqual(pw(ed, i), 521429.6981340426)
        self.assertAlmostEqual(aw(cr, i), 38072.77615720737)
        self.assertAlmostEqual(aw(ed, i), 41840.86801633675)

        self.assertLess(pw(cr, i), pw(ed, i))
        self.assertLess(aw(cr, i), aw(ed, i))
Beispiel #18
0
 def test_6(self):
     self.assertAlmostEqual(EfInt(0.08).to_sp(12).rate, 0.00643403011000343)
     self.assertAlmostEqual(
         NomInt(0.035, 252).to_ef().rate, 0.03561719190449408)
     self.assertAlmostEqual(
         NomInt(0.04, 4).to_cont().rate, 0.039801323412672354)
     self.assertAlmostEqual(
         CompInt.from_factor(SPInt(0.015, 12).to_factor(),
                             4).to_cont().rate, 0.04466583748125169)
     self.assertAlmostEqual(
         CompInt.from_factor(
             CompInt.from_factor(NomInt(0.012, 3).to_factor(),
                                 1 / 4).to_factor(3)).to_nom(6).rate,
         0.1454477030768886)
Beispiel #19
0
    def test_7(self):
        deprec = DeclBalDeprec.from_rate(5000, 5, EfInt(0.15))

        self.assertAlmostEqual(deprec.cap_gain(3000), 0)
        self.assertAlmostEqual(deprec.recap_deprec(3000), 781.4734375)
        self.assertAlmostEqual(deprec.loss_on_disp(3000), 0)

        self.assertAlmostEqual(deprec.cap_gain(2000), 0)
        self.assertAlmostEqual(deprec.recap_deprec(2000), 0)
        self.assertAlmostEqual(deprec.loss_on_disp(2000), 218.5265625)

        self.assertAlmostEqual(deprec.cap_gain(6000), 1000)
        self.assertAlmostEqual(deprec.recap_deprec(6000), 2781.4734375)
        self.assertAlmostEqual(deprec.loss_on_disp(6000), 0)
Beispiel #20
0
 def test_3(self):
     self.assertAlmostEqual(
         yield_(
             Bond.from_rate(100, NomInt(0.07, 2), 2, 3).cash_flows, 100,
             EfInt(0)).to_nom(2).rate, 0.07)
     self.assertAlmostEqual(
         pw(
             Bond.from_rate(100, NomInt(0.04, 2), 2, 3).cash_flows,
             NomInt(0.05, 2)) + 100 * 0.04 / 2, 99.24593731921009)
     self.assertAlmostEqual(
         yield_(
             Bond.from_rate(100, NomInt(0.03, 2), 2, 2.25).cash_flows, 100,
             EfInt(0)).to_nom(2).rate, 0.026754568040623247)
     self.assertAlmostEqual(
         pw(
             Bond.from_rate(100, NomInt(0.07, 2), 2, 2.25).cash_flows,
             NomInt(0.05, 2)), 102.65033622528411)
     self.assertAlmostEqual(
         np.float_(
             fsolve(
                 lambda c: pw(
                     Bond.from_rate(100, NomInt(c, 2), 2, 2.25).cash_flows,
                     NomInt(0.03, 2)) - 114, np.array(0.1)), ),
         0.10627047075771787)
Beispiel #21
0
    def test_6(self):
        bb = ((90, 102, 66), (85, 115, 77), (150, 200, 132))
        m_price, m_bull, m_bear = 18, 22, 16
        p = 0.6
        rf = EfInt(0.03)
        fps = tuple(
            fair(bb[i][1], bb[i][2], m_price, m_bull, m_bear, rf)
            for i in range(3))

        self.assertIterableEqual(
            (i for i, fp_ in enumerate(fps) if fp_ > bb[i][0]), (1, 2))
        self.assertAlmostEqual(fps[1] + fps[2] - bb[1][0] - bb[2][0],
                               11.47896440129449)
        self.assertAlmostEqual(fps[1] + fps[2], 246.4789644012945)

        a_ror = ror(fps[1] + fps[2], bb[1][1] + bb[2][1], bb[1][2] + bb[2][2],
                    p)
        m_ror = ror(m_price, m_bull, m_bear, p)

        self.assertAlmostEqual(beta(a_ror, m_ror, rf), 1.2901709513930817)
Beispiel #22
0
    def test_4(self):
        basis, salvage, life = 110000, 25000, 4

        self.assertIterableAlmostEqual(
            StrLineDeprec(basis, salvage, life).books,
            (110000, 88750, 67500, 46250, 25000))
        self.assertIterableAlmostEqual(
            DeclBalDeprec(basis, salvage, life).books,
            (110000, 75950.3039160202, 52440.44240850757, 36207.88671287913,
             25000))
        self.assertIterableAlmostEqual(
            DeclBalDeprec.from_rate(basis, life, EfInt(0.35)).books,
            (110000, 71500, 46475, 30208.75, 19635.6875))
        self.assertIterableAlmostEqual(
            DblDeclBalDeprec(basis, salvage, life).books,
            (110000, 55000, 27500, 13750, 6875))
        self.assertIterableAlmostEqual(
            SYDDeprec(basis, salvage, life).books,
            (110000, 76000, 50500, 33500, 25000))
        self.assertIterableAlmostEqual(
            UPDeprec(basis, salvage, (80000, 65000, 50000, 35000)).books,
            (110000, 80434.78260869565, 56413.043478260865, 37934.78260869565,
             25000))
Beispiel #23
0
    def test_1(self):
        basis, salvage, life = 92000, 19000, 4

        self.assertIterableAlmostEqual(
            StrLineDeprec(basis, salvage, life).books,
            (92000, 73750, 55500, 37250, 19000))
        self.assertIterableAlmostEqual(
            DeclBalDeprec(basis, salvage, life).books,
            (92000, 62019.64424847585, 41809.08992073374, 28184.618296048306,
             19000))
        self.assertIterableAlmostEqual(
            DeclBalDeprec.from_rate(basis, life, EfInt(0.25)).books,
            (92000, 69000, 51750, 38812.5, 29109.375))
        self.assertIterableAlmostEqual(
            DblDeclBalDeprec(basis, salvage, life).books,
            (92000, 46000, 23000, 11500, 5750))
        self.assertIterableAlmostEqual(
            SYDDeprec(basis, salvage, life).books,
            (92000, 62800, 40900, 26300, 19000))
        self.assertIterableAlmostEqual(
            UPDeprec(basis, salvage, (37000, 37000, 32000, 30000)).books,
            (92000, 72139.70588235295, 52279.41176470589, 35102.94117647059,
             19000))
Beispiel #24
0
    def test_consistency(self):
        nr, sc, t, f = 0.1, 4, 2.5, 1.2800845441963565
        counts = range(1, 366)

        interests = (
            EfInt((1 + nr / sc)**sc - 1),
            ContInt(np.log((1 + nr / sc)**sc)),
            NomInt(nr, sc),
            SPInt(nr / sc, sc),
        )

        for interest in interests:
            self.assertAlmostEqual(interest.to_factor(t), f)
            self.assertAlmostEqual(interest.to_ef().to_factor(t), f)
            self.assertAlmostEqual(interest.to_cont().to_factor(t), f)

            for count in counts:
                self.assertAlmostEqual(interest.to_nom(count).to_factor(t), f)
                self.assertAlmostEqual(interest.to_sp(count).to_factor(t), f)

        self.assertAlmostEqual(NomInt(nr, sc).to_nom().to_factor(t), f)
        self.assertAlmostEqual(NomInt(nr, sc).to_sp().to_factor(t), f)
        self.assertAlmostEqual(SPInt(nr / sc, sc).to_nom().to_factor(t), f)
        self.assertAlmostEqual(SPInt(nr / sc, sc).to_sp().to_factor(t), f)
Beispiel #25
0
    def test_5(self):
        i = EfInt(0.07)
        sa = aw(
            chain((CashFlow(0, 5e6), ),
                  repeated(Project(0, 2e5, 1e6, 6).cash_flows, 25)), i, 25)
        sb = aw(
            chain((CashFlow(0, 6e6), ),
                  repeated(Project(0, 1.5e5, 1.1e6, 11).cash_flows, 25)), i,
            25)
        ba = aw(
            chain((CashFlow(0, 5e6), ),
                  repeated(Project(0, 5e5 - 3e5, 1e6, 14).cash_flows, 25)), i,
            25)
        bb = aw(
            chain((CashFlow(0, 3e6), ),
                  repeated(Project(0, 3e5, 6e5, 5).cash_flows, 25)), i, 25)

        self.assertAlmostEqual(sa, 766638.1419621999)
        self.assertAlmostEqual(sb, 731013.277560719)
        self.assertAlmostEqual(ba, 662331.3841421161)
        self.assertAlmostEqual(bb, 661765.9683268208)

        self.assertAlmostEqual(min(sa + ba, sa + bb, sb + ba, sb + bb),
                               sb + bb)
Beispiel #26
0
    def test_3(self):
        irrs = tuple(
            map(EfInt, (0.1096, 0.132, 0.1205, 0.1293, 0.1286, 0.1113)))
        table = tuple(
            map(lambda row: tuple(map(EfInt, row)), (
                (),
                (0.286, ),
                (0.17, -0.058),
                (0.189, 0.112, 0.228),
                (0.177, 0.112, 0.187, 0.113),
                (0.113, 0.079, 0.094, 0.069, 0.063),
            )))

        self.assertEqual(select(irrs, table, EfInt(0.04)), 5)
        self.assertEqual(select(irrs, table, EfInt(0.06)), 5)
        self.assertEqual(select(irrs, table, EfInt(0.08)), 4)
        self.assertEqual(select(irrs, table, EfInt(0.10)), 4)
        self.assertEqual(select(irrs, table, EfInt(0.12)), 1)
        self.assertEqual(select(irrs, table, EfInt(0.14)), None)
Beispiel #27
0
    def test_diff(self):
        r, p, t, s, c = 0.07, 24, 2020 - 1626, 685.92, 9066082143624.828

        self.assertAlmostEqual(p * SimpleInt(r).to_factor(t), s)
        self.assertAlmostEqual(p * EfInt(r).to_factor(t), c)
Beispiel #28
0
    def test_2(self):
        p, t, r = 42, 3, capm(0.86, EfInt(0.015), EfInt(0.08))

        self.assertAlmostEqual(r.rate, 0.0709)
        self.assertAlmostEqual(p * r.to_factor(t), 51.581746894818)
Beispiel #29
0
 def test_3(self):
     self.assertAlmostEqual(
         2 * perp(capm(1.2, EfInt(0.012), EfInt(0.07)).rate),
         24.509803921568626)
Beispiel #30
0
 def test_4(self):
     r = capm(0.8, EfInt(0.017), EfInt(0.07))
     self.assertAlmostEqual(
         41 * r.to_factor(2.5) * EfInt(-0.03).to_factor(2),
         44.56329005026732)