Skip to content
Snippets Groups Projects
test_data_finance.py 46.4 KiB
Newer Older
  • Learn to ignore specific revisions
  • Pedro L. Magalhães's avatar
    Pedro L. Magalhães committed
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
    # imports
    
    # local, internal
    from src.topupopt.data.finance.invest import Investment, discount_factor, npv
    from src.topupopt.data.finance.invest import salvage_value_linear_depreciation
    from src.topupopt.data.finance.invest import present_salvage_value_annuity
    from src.topupopt.data.finance.invest import salvage_value_annuity
    from src.topupopt.data.finance.utils import ArcInvestments
    
    # local, external
    import math
    
    # ******************************************************************************
    # ******************************************************************************
    
    
    class TestArcInvestments:
        def test_object_creation(self):
            discount_rate = 0.035
            analysis_period = 20
            discount_rates = tuple(discount_rate for i in range(analysis_period))
            cash_flows_npv = [3.673079208612225, 1.5610827143687658, 17, 117, 1842]
            abs_cash_flows_npv = [1e-1, 1e-1, 1e-1, 0.5, 1]
            investments = []
    
            # limited longevity operation, does not go beyond planning horizon
            cash_flow = 1
            cash_flow_start = 1
            myinv = Investment(discount_rates=discount_rates)
            myinv.add_operational_cash_flows(
                cash_flow=cash_flow, start_period=cash_flow_start, longevity=4
            )
            investments.append(myinv)
    
            # limited longevity operation, goes beyond planning horizon
            cash_flow = 1
            cash_flow_start = 18
            myinv = Investment(discount_rates=discount_rates)
            myinv.add_operational_cash_flows(
                cash_flow=cash_flow, start_period=cash_flow_start, longevity=4
            )
            investments.append(myinv)
    
            # D&V omkostninger kundeanlæg: Sengeløse Skole
            cash_flow = 1.2
            cash_flow_start = 1
            myinv = Investment(discount_rates=discount_rates)
            myinv.add_operational_cash_flows(
                cash_flow=cash_flow, start_period=cash_flow_start, longevity=None
            )
            investments.append(myinv)
    
            # D&V omkostninger kundeanlæg: Større forbrugere
            cash_flow = 6 / 10
            myinv = Investment(discount_rates=discount_rates)
            myinv.add_operational_cash_flows(
                cash_flow=10 * cash_flow, start_period=1, longevity=None
            )
            myinv.add_operational_cash_flows(
                cash_flow=3 * cash_flow, start_period=2, longevity=None
            )
            myinv.add_operational_cash_flows(
                cash_flow=1 * cash_flow, start_period=3, longevity=None
            )
            investments.append(myinv)
    
            # D&V omkostninger kundeanlæg: Mindre forbrugere
            cash_flow = 0.320
            myinv = Investment(discount_rates=discount_rates)
            myinv.add_operational_cash_flows(
                cash_flow=197 * cash_flow, start_period=1, longevity=None
            )
            myinv.add_operational_cash_flows(
                cash_flow=(233 - 197) * cash_flow, start_period=2, longevity=None
            )
            myinv.add_operational_cash_flows(
                cash_flow=(269 - 233) * cash_flow, start_period=3, longevity=None
            )
            myinv.add_operational_cash_flows(
                cash_flow=(305 - 269) * cash_flow, start_period=4, longevity=None
            )
            myinv.add_operational_cash_flows(
                cash_flow=(341 - 305) * cash_flow, start_period=5, longevity=None
            )
            myinv.add_operational_cash_flows(
                cash_flow=(377 - 341) * cash_flow, start_period=6, longevity=None
            )
            myinv.add_operational_cash_flows(
                cash_flow=(413 - 377) * cash_flow, start_period=7, longevity=None
            )
            myinv.add_operational_cash_flows(
                cash_flow=(449 - 413) * cash_flow, start_period=8, longevity=None
            )
            myinv.add_operational_cash_flows(
                cash_flow=(488 - 449) * cash_flow, start_period=9, longevity=None
            )
            investments.append(myinv)
            # check
            for inv, true_npv, _abs in zip(investments, cash_flows_npv, abs_cash_flows_npv):
                assert math.isclose(inv.net_present_value(), true_npv, abs_tol=_abs)
            # create object
            number_options = 5
            static_loss = {
                (h, q, k): 1 + q * 0.25 + h * 0.15 + k * 0.05
                for h in range(number_options)
                for q in range(2)
                for k in range(3)
            }
            arc_invs = ArcInvestments(
                investments=investments,
                name="any",
                efficiency=None,
                efficiency_reverse=None,
                capacity=tuple(1 + o for o in range(number_options)),
                # minimum_cost=tuple(1+o for o in range(number_options)),
                specific_capacity_cost=1,
                capacity_is_instantaneous=False,
                static_loss=static_loss,
                validate=True,
            )
            # check the costs
            for _npv, true_npv, _abs in zip(
                arc_invs.minimum_cost, cash_flows_npv, abs_cash_flows_npv
            ):
                assert math.isclose(_npv, true_npv, abs_tol=_abs)
            # change something in the investments
            for inv in arc_invs.investments:
                inv.add_investment(
                    investment=1, investment_period=0, investment_longevity=10
                )
            # update the minimum costs
            arc_invs.update_minimum_cost()
            # make sure the comparison fails
            for _npv, true_npv, _abs in zip(
                arc_invs.minimum_cost, cash_flows_npv, abs_cash_flows_npv
            ):
                error_raised = False
                try:
                    assert math.isclose(_npv, true_npv, abs_tol=_abs)
                except AssertionError:
                    error_raised = True
                assert error_raised
            # new true values
            new_true_npv = [
                4.673079208612225,
                2.561082714368766,
                18.054883962342764,
                117.50524073646935,
                1843.1804103901486,
            ]
            # print([inv.net_present_value() for inv in arc_invs])
            for _npv, true_npv, _abs in zip(
                arc_invs.minimum_cost, new_true_npv, abs_cash_flows_npv
            ):
                assert math.isclose(_npv, true_npv, abs_tol=_abs)
    
    
    # *****************************************************************************
    # *****************************************************************************
    
    
    class TestDataFinance:
        # TODO: make sure that all methods work with variable discount rates
    
        # assert that the discount factors match
    
        def test_operational_cash_flows(self):
            discount_rate = 0.035
    
            analysis_period = 20
    
            discount_rates = tuple(discount_rate for i in range(analysis_period))
    
            # limited longevity operation, does not go beyond planning horizon
    
            cash_flow = 1
            cash_flow_start = 1
            cash_flows_npv = 3.673079208612225
            myinv = Investment(discount_rates=discount_rates)
            myinv.add_operational_cash_flows(
                cash_flow=cash_flow, start_period=cash_flow_start, longevity=4
            )
            mynpv = myinv.net_present_value()
            assert math.isclose(mynpv, cash_flows_npv, abs_tol=1e-1)
    
            # limited longevity operation, goes beyond planning horizon
    
            cash_flow = 1
            cash_flow_start = 18
            cash_flows_npv = 1.5610827143687658
            myinv = Investment(discount_rates=discount_rates)
            myinv.add_operational_cash_flows(
                cash_flow=cash_flow, start_period=cash_flow_start, longevity=4
            )
            mynpv = myinv.net_present_value()
            assert math.isclose(mynpv, cash_flows_npv, abs_tol=1e-1)
    
            # D&V omkostninger kundeanlæg: Sengeløse Skole
    
            cash_flow = 1.2
    
            cash_flow_start = 1
    
            cash_flows_npv = 17
    
            myinv = Investment(discount_rates=discount_rates)
            myinv.add_operational_cash_flows(
                cash_flow=cash_flow, start_period=cash_flow_start, longevity=None
            )
            mynpv = myinv.net_present_value()
    
            assert math.isclose(mynpv, cash_flows_npv, abs_tol=1e-1)
    
            # D&V omkostninger kundeanlæg: Større forbrugere
    
            cash_flows_npv = 117
            cash_flow = 6 / 10
    
            myinv = Investment(discount_rates=discount_rates)
            myinv.add_operational_cash_flows(
                cash_flow=10 * cash_flow, start_period=1, longevity=None
            )
            myinv.add_operational_cash_flows(
                cash_flow=3 * cash_flow, start_period=2, longevity=None
            )
            myinv.add_operational_cash_flows(
                cash_flow=1 * cash_flow, start_period=3, longevity=None
            )
            mynpv = myinv.net_present_value()
    
            assert math.isclose(mynpv, cash_flows_npv, abs_tol=0.5)
    
            # D&V omkostninger kundeanlæg: Mindre forbrugere
    
            cash_flows_npv = 1842
            cash_flow = 0.320
    
            myinv = Investment(discount_rates=discount_rates)
            myinv.add_operational_cash_flows(
                cash_flow=197 * cash_flow, start_period=1, longevity=None
            )
            myinv.add_operational_cash_flows(
                cash_flow=(233 - 197) * cash_flow, start_period=2, longevity=None
            )
            myinv.add_operational_cash_flows(
                cash_flow=(269 - 233) * cash_flow, start_period=3, longevity=None
            )
            myinv.add_operational_cash_flows(
                cash_flow=(305 - 269) * cash_flow, start_period=4, longevity=None
            )
            myinv.add_operational_cash_flows(
                cash_flow=(341 - 305) * cash_flow, start_period=5, longevity=None
            )
            myinv.add_operational_cash_flows(
                cash_flow=(377 - 341) * cash_flow, start_period=6, longevity=None
            )
            myinv.add_operational_cash_flows(
                cash_flow=(413 - 377) * cash_flow, start_period=7, longevity=None
            )
            myinv.add_operational_cash_flows(
                cash_flow=(449 - 413) * cash_flow, start_period=8, longevity=None
            )
            myinv.add_operational_cash_flows(
                cash_flow=(488 - 449) * cash_flow, start_period=9, longevity=None
            )
            mynpv = myinv.net_present_value()
    
            assert math.isclose(mynpv, cash_flows_npv, abs_tol=1)
    
        # *************************************************************************
        # *************************************************************************
    
        # assert that the discount factors match
    
        def test_discount_factors(self):
            years = [
                2021,
                2022,
                2023,
                2024,
                2025,
                2026,
                2027,
                2028,
                2029,
                2030,
                2031,
                2036,
                2037,
                2038,
                2039,
                2040,
                2041,
            ]
    
            factors = [
                1.000,
                0.966,
                0.934,
                0.902,
                0.871,
                0.842,
                0.814,
                0.786,
                0.759,
                0.734,
                0.709,
                0.597,
                0.577,
                0.557,
                0.538,
                0.520,
                0.503,
            ]
    
            test_factors = [
                discount_factor([0.035 for i in range(year - years[0])]) for year in years
            ]
    
            for i, factor in enumerate(factors):
                assert math.isclose(factor, test_factors[i], abs_tol=0.001)
    
            # *********************************************************************
    
            # variable discount factors
    
            discount_rates = [0.035, 0.05, 0.075, 0.06]
            discount_factors = [1.00000, 0.96618, 0.92017, 0.85598, 0.80753]
    
            test_discount_factors = [
                discount_factor(discount_rates[0:t]) for t in range(len(discount_rates) + 1)
            ]
    
            assert len(discount_factors) == len(test_discount_factors)
    
            for df_true, df in zip(discount_factors, test_discount_factors):
                assert math.isclose(df_true, df, abs_tol=0.001)
    
        # *************************************************************************
        # *************************************************************************
    
        def test_salvage_value_sengelose_linear_depreciation(self):
            # *********************************************************************
            # *********************************************************************
    
            # example # 1: Investering kundeanlæg, Sengeløse Skole
    
            commissioning_delay_after_investment = 0
    
            investment_period = 1
    
            investment = 180  # 1E3 DKK
    
            investment_longevity = (
                25  # if simultaneous_commissioning_investment else 25 # years
            )
    
            analysis_period_span = 20  # years
    
            discount_rate = 0.035
    
            # discount rates' tuple: size does not change with first_period_is_present_time
    
            discount_rates = tuple(discount_rate for i in range(analysis_period_span))
    
            # *********************************************************************
    
            residual_value = salvage_value_linear_depreciation(
                investment=investment,
                investment_period=investment_period,
                investment_longevity=investment_longevity,
                analysis_period_span=analysis_period_span,
                commissioning_delay_after_investment=commissioning_delay_after_investment,
            )
    
            assert math.isclose(residual_value, 36, abs_tol=1)
    
            net_cash_flows = list(0 for i in range(analysis_period_span + 1))
            net_cash_flows[investment_period] = investment
            net_cash_flows[analysis_period_span] = -residual_value
    
            npv_inv_horizon = npv(
                discount_rates=discount_rates, net_cash_flows=net_cash_flows
            )
            assert math.isclose(npv_inv_horizon, 155.82067163872074, abs_tol=1e-3)
    
            myinv = Investment(discount_rates=discount_rates)
            myinv.add_investment(
                investment=investment,
                investment_period=investment_period,
                investment_longevity=investment_longevity,
                commissioning_delay_after_investment=commissioning_delay_after_investment,
                salvage_value_method="asd",
            )
            mynpv = myinv.net_present_value()
            assert math.isclose(mynpv, 155.82067163872074, abs_tol=1e-3)
    
            # *********************************************************************
            # *********************************************************************
    
            # example # 2: Forsyningsledning
    
            investment_period = 1
    
            investment = 13000  # 1E3 DKK
    
            investment_longevity = 60  # years
    
            analysis_period_span = 20  # years
    
            discount_rate = 0.035
    
            discount_rates = tuple(discount_rate for i in range(analysis_period_span))
    
            # *********************************************************************
    
            # using mean annual asset devaluation method
    
            residual_value = salvage_value_linear_depreciation(
                investment=investment,
                investment_period=investment_period,
                investment_longevity=investment_longevity,
                analysis_period_span=analysis_period_span,
                commissioning_delay_after_investment=commissioning_delay_after_investment,
            )
    
            assert math.isclose(residual_value, 8667, abs_tol=1)
    
            net_cash_flows = list(0 for i in range(analysis_period_span + 1))
            net_cash_flows[investment_period] = investment
            net_cash_flows[analysis_period_span] = -residual_value
    
            npv_inv_horizon = npv(
                discount_rates=discount_rates, net_cash_flows=net_cash_flows
            )
            assert math.isclose(npv_inv_horizon, 8204.815475022142, abs_tol=1e-3)
    
            myinv = Investment(discount_rates=discount_rates)
            myinv.add_investment(
                investment=investment,
                investment_period=investment_period,
                investment_longevity=investment_longevity,
                commissioning_delay_after_investment=commissioning_delay_after_investment,
                salvage_value_method="asd",
            )
            mynpv = myinv.net_present_value()
            assert math.isclose(mynpv, 8204.815475022142, abs_tol=1e-3)
    
            # *********************************************************************
            # *********************************************************************
    
            # example 3: Boosterpumpeanlæg
    
            investment_period = 7
    
            investment = 1500  # 1E3 DKK
    
            investment_longevity = 25  # years
    
            analysis_period_span = 20  # years
    
            discount_rate = 0.035
    
            discount_rates = tuple(discount_rate for i in range(analysis_period_span))
    
            # *********************************************************************
    
            residual_value = salvage_value_linear_depreciation(
                investment=investment,
                investment_period=investment_period,
                investment_longevity=investment_longevity,
                analysis_period_span=analysis_period_span,
                commissioning_delay_after_investment=commissioning_delay_after_investment,
            )
    
            assert math.isclose(residual_value, 660, abs_tol=1e-3)
    
            net_cash_flows = list(0 for i in range(analysis_period_span + 1))
            net_cash_flows[investment_period] = investment
            net_cash_flows[analysis_period_span] = -residual_value
    
            npv_inv_horizon = npv(
                discount_rates=discount_rates, net_cash_flows=net_cash_flows
            )
            assert math.isclose(npv_inv_horizon, 847, abs_tol=0.3)
    
            myinv = Investment(discount_rates=discount_rates)
            myinv.add_investment(
                investment=investment,
                investment_period=investment_period,
                investment_longevity=investment_longevity,
                commissioning_delay_after_investment=commissioning_delay_after_investment,
                salvage_value_method="asd",
            )
            mynpv = myinv.net_present_value()
            assert math.isclose(mynpv, 847, abs_tol=0.3)
    
            # *********************************************************************
            # *********************************************************************
    
            # example 4: Investering, Sengeløse Skole, (varmepumper)
    
            investment_period = 1
    
            investment = 1925  # 1E3 DKK
    
            investment_longevity = 20  # years
    
            analysis_period_span = 20  # years
    
            discount_rate = 0.035
    
            discount_rates = tuple(discount_rate for i in range(analysis_period_span))
    
            # *********************************************************************
    
            residual_value = salvage_value_linear_depreciation(
                investment=investment,
                investment_period=investment_period,
                investment_longevity=investment_longevity,
                analysis_period_span=analysis_period_span,
                commissioning_delay_after_investment=commissioning_delay_after_investment,
            )
    
            assert math.isclose(residual_value, 0, abs_tol=1e-3)
    
            net_cash_flows = list(0 for i in range(analysis_period_span + 1))
            net_cash_flows[investment_period] = investment
            net_cash_flows[analysis_period_span] = -residual_value
    
            npv_inv_horizon = npv(
                discount_rates=discount_rates, net_cash_flows=net_cash_flows
            )
    
            assert math.isclose(npv_inv_horizon, 1860, abs_tol=0.3)
    
            myinv = Investment(discount_rates=discount_rates)
            myinv.add_investment(
                investment=investment,
                investment_period=investment_period,
                investment_longevity=investment_longevity,
                commissioning_delay_after_investment=commissioning_delay_after_investment,
                salvage_value_method="asd",
            )
            mynpv = myinv.net_present_value()
            assert math.isclose(mynpv, 1860, abs_tol=0.3)
    
            # *********************************************************************
            # *********************************************************************
    
        # *************************************************************************
        # *************************************************************************
    
        def test_scrap_value_annuity(self):
            # Source:
            # Vejledning i samfundsøkonomiske analyser på energiområdet, juli 2021
            # Energistyrelsen, page 19
    
            investment_period = 0
    
            investment = 1e6  # 1E3 DKK
    
            investment_longevity = 30  # years
    
            analysis_period_span = 20  # years
    
            discount_rate = 0.035
    
            discount_rates = tuple(
                discount_rate for i in range(investment_longevity + investment_period)
            )
    
            # *********************************************************************
            # *********************************************************************
    
            # calculate the net present value with the salvage value deducted
    
            # annuity method
    
            annuity = (
                investment
                * discount_rate
                / (1 - (1 + discount_rate) ** (-investment_longevity))
            )
    
            net_cash_flows = list(
                annuity for i in range(investment_longevity + investment_period + 1)
            )
    
            for year_index in range(investment_period + 1):
                net_cash_flows[year_index] = 0
    
            npv_inv_horizon = npv(
                discount_rates=discount_rates[0:analysis_period_span],
                net_cash_flows=net_cash_flows[0 : analysis_period_span + 1],
            )
    
            assert math.isclose(npv_inv_horizon, 772747.2928688908, abs_tol=1e-3)
    
            # *********************************************************************
    
            # net present value for the whole investment
    
            npv_asset_long = npv(
                discount_rates=discount_rates, net_cash_flows=net_cash_flows
            )
    
            assert math.isclose(npv_asset_long, 1e6, abs_tol=1e-3)
    
            # calculate discounted salvage value directly
    
            npv_salvage = present_salvage_value_annuity(
                investment=investment,
                investment_longevity=investment_longevity,
                investment_period=investment_period,
                discount_rate=discount_rate,
                analysis_period_span=analysis_period_span,
            )
    
            assert math.isclose(npv_salvage, npv_asset_long - npv_inv_horizon, abs_tol=1e-3)
    
            # salvage value, as seen from the last period
    
            und_salvage_value = salvage_value_annuity(
                investment=investment,
                discount_rate=discount_rate,
                investment_longevity=investment_longevity,
                investment_period=investment_period,
                analysis_period_span=analysis_period_span,
            )
    
            assert math.isclose(und_salvage_value, 452184.9058419504, abs_tol=1e-3)
    
            # *********************************************************************
    
            # use only the part of discount_rates that overlaps with the planni. period
    
            myinv = Investment(discount_rates=discount_rates[0:analysis_period_span])
    
            myinv.add_investment(
                investment=investment,
                investment_period=investment_period,
                investment_longevity=investment_longevity,
            )
            mynpv = myinv.net_present_value()
    
            assert math.isclose(mynpv, npv_inv_horizon, abs_tol=1e-3)
    
            # *********************************************************************
            # *********************************************************************
    
            # trigger ValueError
    
    
    Pedro L. Magalhães's avatar
    Pedro L. Magalhães committed
            investment_period = analysis_period_span + 1
            try:
                npv_salvage = present_salvage_value_annuity(
                    investment=investment,
                    investment_longevity=investment_longevity,
                    investment_period=investment_period,
                    discount_rate=discount_rate,
                    analysis_period_span=analysis_period_span,
                )
            except ValueError:
    
    Pedro L. Magalhães's avatar
    Pedro L. Magalhães committed
    
            # *********************************************************************
    
            investment = 1
    
            investment_period = 0
    
            investment_longevity = 4
    
            analysis_period_span = 3
    
            discount_rate = 0.035
    
            # *********************************************************************
    
            # analysis period equals longevity: no salvage value
    
            npv_salvage, annuity = present_salvage_value_annuity(
                investment=investment,
                investment_longevity=investment_longevity,
                investment_period=investment_period,
                discount_rate=discount_rate,
                analysis_period_span=analysis_period_span + 1,
                return_annuity=True,
            )
    
            assert npv_salvage == 0.0
            assert annuity > 0
    
            # *********************************************************************
    
            # increased longevity
    
            npv_salvage, annuity2 = present_salvage_value_annuity(
                investment=investment,
                investment_longevity=investment_longevity + 1,
                investment_period=investment_period,
                discount_rate=discount_rate,
                analysis_period_span=analysis_period_span,
                return_annuity=True,
            )
    
            assert math.isclose(npv_salvage, 0.37948959437673335, abs_tol=1e-3)
            assert annuity > annuity2
    
            # *********************************************************************
            # *********************************************************************
    
        # *************************************************************************
        # *************************************************************************
    
        def test_scrap_value_annuity_longer_longevity(self):
            # Source:
            # Vejledning i samfundsøkonomiske analyser på energiområdet, juli 2021
            # Energistyrelsen, page 19
    
            investment_period = 0
    
            investment = 1e6  # 1E3 DKK
    
            investment_longevity = 35  # years
    
            analysis_period_span = 20  # years
    
            discount_rate = 0.035
    
            discount_rates = tuple(
                discount_rate for i in range(investment_longevity + investment_period)
            )
    
            # *********************************************************************
            # *********************************************************************
    
            # calculate the net present value with the salvage value deducted
    
            # annuity method
    
            annuity = (
                investment
                * discount_rate
                / (1 - (1 + discount_rate) ** (-investment_longevity))
            )
    
            net_cash_flows = list(
                annuity for i in range(investment_longevity + investment_period + 1)
            )
    
            for year_index in range(investment_period + 1):
                net_cash_flows[year_index] = 0
    
            npv_inv_horizon = npv(
                discount_rates=discount_rates[0:analysis_period_span],
                net_cash_flows=net_cash_flows[0 : analysis_period_span + 1],
            )
    
            assert math.isclose(npv_inv_horizon, 710596.68, abs_tol=1e-2)
    
            # *********************************************************************
    
            # net present value for the whole investment
    
            npv_asset_long = npv(
                discount_rates=discount_rates, net_cash_flows=net_cash_flows
            )
    
            assert math.isclose(npv_asset_long, 1e6, abs_tol=1e-3)
    
            # calculate discounted salvage value directly
    
            npv_salvage = present_salvage_value_annuity(
                investment=investment,
                investment_longevity=investment_longevity,
                investment_period=investment_period,
                discount_rate=discount_rate,
                analysis_period_span=analysis_period_span,
            )
    
            assert math.isclose(npv_salvage, npv_asset_long - npv_inv_horizon, abs_tol=1e-3)
    
            # salvage value, as seen from the last period
    
            und_salvage_value = salvage_value_annuity(
                investment=investment,
                discount_rate=discount_rate,
                investment_longevity=investment_longevity,
                investment_period=investment_period,
                analysis_period_span=analysis_period_span,
            )
    
            assert math.isclose(und_salvage_value, 575851.51, abs_tol=1e-3)
    
        # *************************************************************************
        # *************************************************************************
    
        def test_scrap_value_annuity_starting_later(self):
            # Source:
            # Vejledning i samfundsøkonomiske analyser på energiområdet, juli 2021
            # Energistyrelsen, page 19
    
            investment_period = 10
    
            investment = 1e6  # 1E3 DKK
    
            investment_longevity = 30  # years
    
            analysis_period_span = 20  # years
    
            discount_rate = 0.035
    
            discount_rates = tuple(
                discount_rate for i in range(investment_longevity + investment_period)
            )
    
            # *********************************************************************
            # *********************************************************************
    
            # calculate the net present value with the salvage value deducted
    
            # annuity method
    
            annuity = (
                investment
                * discount_rate
                / (1 - (1 + discount_rate) ** (-investment_longevity))
            )
    
            net_cash_flows = list(
                annuity for i in range(investment_longevity + investment_period + 1)
            )
    
            for year_index in range(investment_period + 1):
                net_cash_flows[year_index] = 0
    
            npv_inv_horizon = npv(
                discount_rates=discount_rates[0:analysis_period_span],
                net_cash_flows=net_cash_flows[0 : analysis_period_span + 1],
            )
    
            assert math.isclose(npv_inv_horizon, 320562.3870269, abs_tol=1e-2)
    
            # *********************************************************************
    
            # net present value for the whole investment
    
            npv_asset_long = npv(
                discount_rates=discount_rates, net_cash_flows=net_cash_flows
            )
    
            assert math.isclose(npv_asset_long, 708918.8137098, abs_tol=1e-3)
    
            # calculate discounted salvage value directly
    
            npv_salvage = present_salvage_value_annuity(
                investment=investment,
                investment_longevity=investment_longevity,
                investment_period=investment_period,
                discount_rate=discount_rate,
                analysis_period_span=analysis_period_span,
            )
    
            assert math.isclose(npv_salvage, npv_asset_long - npv_inv_horizon, abs_tol=1e-3)
            assert math.isclose(npv_salvage, 388356.4266828, abs_tol=1e-3)
    
            # salvage value, as seen from the last period
    
            und_salvage_value = salvage_value_annuity(
                investment=investment,
                discount_rate=discount_rate,
                investment_longevity=investment_longevity,
                investment_period=investment_period,
                analysis_period_span=analysis_period_span,
            )
    
            assert math.isclose(und_salvage_value, 772747.2928689, abs_tol=1e-3)
    
        # *************************************************************************
        # *************************************************************************
    
        def test_scrap_value_annuity_longer_planning_period(self):
            # Source:
            # Vejledning i samfundsøkonomiske analyser på energiområdet, juli 2021
            # Energistyrelsen, page 19
    
            investment_period = 0
    
            investment = 1e6  # 1E3 DKK
    
            investment_longevity = 30  # years
    
            analysis_period_span = 23  # years
    
            discount_rate = 0.035
    
            discount_rates = tuple(
                discount_rate for i in range(investment_longevity + investment_period)
            )
    
            # *********************************************************************
    
            # calculate the net present value with the salvage value deducted
    
            # annuity method
    
            annuity = (
                investment
                * discount_rate
                / (1 - (1 + discount_rate) ** (-investment_longevity))
            )
    
            net_cash_flows = list(
                annuity for i in range(investment_longevity + investment_period + 1)
            )
    
            for year_index in range(investment_period + 1):
                net_cash_flows[year_index] = 0
    
            npv_inv_horizon = npv(
                discount_rates=discount_rates[0:analysis_period_span],
                net_cash_flows=net_cash_flows[0 : analysis_period_span + 1],
            )
    
            assert math.isclose(npv_inv_horizon, 849302.517460684, abs_tol=1e-3)
    
            # *********************************************************************
    
            # net present value for the whole investment
    
            npv_asset_long = npv(
                discount_rates=discount_rates, net_cash_flows=net_cash_flows
            )
    
            assert math.isclose(npv_asset_long, 1e6, abs_tol=1e-3)
    
            # calculate discounted salvage value directly
    
            npv_salvage = present_salvage_value_annuity(
                investment=investment,
                investment_longevity=investment_longevity,
                investment_period=investment_period,
                discount_rate=discount_rate,
                analysis_period_span=analysis_period_span,
            )
    
            assert math.isclose(npv_salvage, npv_asset_long - npv_inv_horizon, abs_tol=1e-3)
    
            # salvage value, as seen from the last period
    
            und_salvage_value = salvage_value_annuity(
                investment=investment,
                discount_rate=discount_rate,
                investment_longevity=investment_longevity,
                investment_period=investment_period,
                analysis_period_span=analysis_period_span,
            )
    
            assert math.isclose(und_salvage_value, 332455.89838989300, abs_tol=1e-3)
    
        # *************************************************************************
        # *************************************************************************
    
        def test_scrap_value_annuity_matching_periods(self):
            # Source:
            # Vejledning i samfundsøkonomiske analyser på energiområdet, juli 2021
            # Energistyrelsen, page 19
    
            investment_period = 0
    
            investment = 1e6  # 1E3 DKK
    
            investment_longevity = 20  # years
    
            analysis_period_span = 20  # years
    
            discount_rate = 0.035
    
            discount_rates = tuple(
                discount_rate for i in range(investment_longevity + investment_period)
            )
    
            # *********************************************************************
    
            # calculate the net present value with the salvage value deducted
    
            # annuity method
    
            annuity = (
                investment
                * discount_rate
                / (1 - (1 + discount_rate) ** (-investment_longevity))
            )
    
            net_cash_flows = list(
                annuity for i in range(investment_longevity + investment_period + 1)
            )