Skip to content

models

Loan and analysis data models.

LoanParams dataclass

Parameters for a mortgage loan.

Attributes:

Name Type Description
balance float

Loan balance.

rate float

Annual interest rate as a decimal.

term_years float

Loan term in years.

Source code in src/refi_calculator/core/models.py
 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
@dataclass
class LoanParams:
    """Parameters for a mortgage loan.

    Attributes:
        balance: Loan balance.
        rate: Annual interest rate as a decimal.
        term_years: Loan term in years.
    """

    balance: float
    rate: float
    term_years: float

    @property
    def monthly_rate(self) -> float:
        """Monthly interest rate.

        Returns:
            Monthly interest rate as a decimal.
        """
        return self.rate / 12

    @property
    def num_payments(self) -> int:
        """Total number of monthly payments.

        Returns:
            Total number of payments.
        """
        return int(self.term_years * 12)

    @property
    def monthly_payment(self) -> float:
        """Monthly payment using the standard amortization formula.

        Returns:
            Monthly payment amount.
        """
        r = self.monthly_rate
        n = self.num_payments
        if r == 0:
            return self.balance / n
        return self.balance * (r * (1 + r) ** n) / ((1 + r) ** n - 1)

    @property
    def total_interest(self) -> float:
        """Total interest paid over the life of the loan."""
        return (self.monthly_payment * self.num_payments) - self.balance

monthly_payment property

Monthly payment using the standard amortization formula.

Returns:

Type Description
float

Monthly payment amount.

monthly_rate property

Monthly interest rate.

Returns:

Type Description
float

Monthly interest rate as a decimal.

num_payments property

Total number of monthly payments.

Returns:

Type Description
int

Total number of payments.

total_interest property

Total interest paid over the life of the loan.

RefinanceAnalysis dataclass

Results of refinance breakeven analysis.

Attributes:

Name Type Description
current_payment float

Current monthly payment.

new_payment float

New monthly payment.

monthly_savings float

Monthly savings from refinancing.

simple_breakeven_months float | None

Months to simple breakeven (nominal).

npv_breakeven_months int | None

Months to NPV breakeven.

current_total_interest float

Total interest of the current loan.

new_total_interest float

Total interest of the new loan.

interest_delta float

Interest difference between new and current loans.

five_year_npv float

NPV of savings over five years.

cumulative_savings list[tuple[int, float, float]]

Cumulative savings timeline.

current_after_tax_payment float

Current payment after tax benefit.

new_after_tax_payment float

New payment after tax benefit.

after_tax_monthly_savings float

After-tax monthly savings.

after_tax_simple_breakeven_months float | None

After-tax simple breakeven.

after_tax_npv_breakeven_months int | None

After-tax NPV breakeven.

after_tax_npv float

After-tax NPV of savings.

current_after_tax_total_interest float

Current loan interest after tax.

new_after_tax_total_interest float

New loan interest after tax.

after_tax_interest_delta float

Interest delta after tax.

new_loan_balance float

Balance of the new loan.

cash_out_amount float

Cash out amount included in the refinance.

accelerated_months int | None

Months to payoff when maintaining payment.

accelerated_total_interest float | None

Total interest when accelerating payoff.

accelerated_interest_savings float | None

Interest savings from acceleration.

accelerated_time_savings_months int | None

Months saved by accelerating payoff.

current_total_cost_npv float

NPV of the current loan total cost.

new_total_cost_npv float

NPV of the new loan total cost.

total_cost_npv_advantage float

NPV advantage of refinancing.

Source code in src/refi_calculator/core/models.py
 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
@dataclass
class RefinanceAnalysis:
    """Results of refinance breakeven analysis.

    Attributes:
        current_payment: Current monthly payment.
        new_payment: New monthly payment.
        monthly_savings: Monthly savings from refinancing.
        simple_breakeven_months: Months to simple breakeven (nominal).
        npv_breakeven_months: Months to NPV breakeven.
        current_total_interest: Total interest of the current loan.
        new_total_interest: Total interest of the new loan.
        interest_delta: Interest difference between new and current loans.
        five_year_npv: NPV of savings over five years.
        cumulative_savings: Cumulative savings timeline.
        current_after_tax_payment: Current payment after tax benefit.
        new_after_tax_payment: New payment after tax benefit.
        after_tax_monthly_savings: After-tax monthly savings.
        after_tax_simple_breakeven_months: After-tax simple breakeven.
        after_tax_npv_breakeven_months: After-tax NPV breakeven.
        after_tax_npv: After-tax NPV of savings.
        current_after_tax_total_interest: Current loan interest after tax.
        new_after_tax_total_interest: New loan interest after tax.
        after_tax_interest_delta: Interest delta after tax.
        new_loan_balance: Balance of the new loan.
        cash_out_amount: Cash out amount included in the refinance.
        accelerated_months: Months to payoff when maintaining payment.
        accelerated_total_interest: Total interest when accelerating payoff.
        accelerated_interest_savings: Interest savings from acceleration.
        accelerated_time_savings_months: Months saved by accelerating payoff.
        current_total_cost_npv: NPV of the current loan total cost.
        new_total_cost_npv: NPV of the new loan total cost.
        total_cost_npv_advantage: NPV advantage of refinancing.
    """

    current_payment: float
    new_payment: float
    monthly_savings: float
    simple_breakeven_months: float | None
    npv_breakeven_months: int | None
    current_total_interest: float
    new_total_interest: float
    interest_delta: float
    five_year_npv: float
    cumulative_savings: list[tuple[int, float, float]]
    current_after_tax_payment: float
    new_after_tax_payment: float
    after_tax_monthly_savings: float
    after_tax_simple_breakeven_months: float | None
    after_tax_npv_breakeven_months: int | None
    after_tax_npv: float
    current_after_tax_total_interest: float
    new_after_tax_total_interest: float
    after_tax_interest_delta: float
    new_loan_balance: float
    cash_out_amount: float
    accelerated_months: int | None
    accelerated_total_interest: float | None
    accelerated_interest_savings: float | None
    accelerated_time_savings_months: int | None
    current_total_cost_npv: float
    new_total_cost_npv: float
    total_cost_npv_advantage: float