-
Notifications
You must be signed in to change notification settings - Fork 0
/
Explanation_simple
116 lines (77 loc) · 3.75 KB
/
Explanation_simple
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
This script handles a loan amortization system and creates detailed schedules for loans based on predefined parameters. Let me break it down step by step:
Overview
The script calculates monthly payments for multiple loans, generates detailed amortization schedules for each loan, and then aggregates and saves this data into CSV and Excel files.
StepbyStep Explanation
1. Importing Libraries
Pandas & Numpy: For data manipulation.
Datetime & Timedelta: For date calculations.
2. Function: Calculate Monthly Payment
Purpose: Calculates the fixed monthly payment using the loan amortization formula:
\[
M = \frac{P \cdot r \cdot (1 + r)^n}{(1 + r)^n 1}
\]
Where:
\( M \): Monthly payment
\( P \): Loan amount
\( r \): Monthly interest rate (annual rate ÷ 12)
\( n \): Loan term in months
Zero Interest Case: Handles special case when \( r = 0 \) (no interest).
3. Read Loan Data
Input: Reads `SimpleAmortizationTest.xlsx`, which contains:
Loan start dates
Loan term (in months)
CPR (Conditional Prepayment Rate).
Loan Parameters: A predefined list of loan amounts and a fixed annual interest rate (8%).
4. Data Preparation
Processing Each Loan:
Calculates the monthly payment using the `calculate_monthly_payment` function.
Converts CPR from percentage strings to decimals (e.g., "5%" → 0.05).
Stores loan data in a list.
Output: Saves loan data into a CSV file (`converted_loan_data.csv`).
5. Amortization Schedule
Key Functionality:
Generates a detailed schedule for each loan:
SMM (Single Monthly Mortality): Derived from CPR:
\[
SMM = 1 (1 \text{CPR})^{\frac{1}{12}}
\]
Monthly Breakdown:
Opening Balance: Loan amount at the start of each period.
Interest: \( \text{Opening Balance} \times \text{Monthly Rate} \).
Prepayment: \( \text{Opening Balance} \times \text{SMM} \).
Principal: Calculated as:
\[
\text{Principal} = \text{Monthly Payment} + \text{Prepayment} \text{Interest}
\]
Closing Balance: Remaining balance after the principal is deducted.
Loop:
Iterates over each loan period until the balance becomes zero.
Appends data for each period to a DataFrame.
6. Consolidating Schedules
Generates amortization schedules for all loans and consolidates them into a single dataset:
Fields: Includes loan number, date, payment, interest, principal, prepayment, and closing balance.
Sorting: Organizes by loan number and date.
Output: Saves the consolidated schedule to:
`consolidated_amortization_schedule.csv`
`consolidated_amortization_schedule.xlsx`
7. Aggregation
Aggregates data by date and loan number:
Sum Fields: Total payment, prepayment, interest, and principal.
Last Closing Balance: Tracks the final balance for the aggregated date.
Output: Saves aggregated schedule to:
`aggregated_amortization_schedule.csv`
`aggregated_amortization_schedule.xlsx`
Deliverables
1. CSV Files:
`converted_loan_data.csv`: Loan details and calculated payments.
`consolidated_amortization_schedule.csv`: Full amortization schedule.
`aggregated_amortization_schedule.csv`: Aggregated schedule.
2. Excel Files:
`consolidated_amortization_schedule.xlsx`: Full amortization schedule.
`aggregated_amortization_schedule.xlsx`: Aggregated schedule.
Key Benefits for the Client
1. Automation: Efficiently processes loan data for multiple loans at once.
2. Detailed Insights: Breaks down each payment period into components (interest, principal, prepayment).
3. Customization: Handles CPR variability and calculates SMM dynamically.
4. Exportable Outputs: Data is saved in widely used formats (CSV/Excel) for further analysis or reporting.
Let me know if you need a specific section clarified!