Created
March 19, 2023 14:49
-
-
Save eppspacjar/3dcb40b1f237e60b336b0b251eacc1d9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* -- APR Calculator Scripting rule -- */ | |
| // -------------------- | |
| // STEP 1: Initialize variables | |
| // 1.1: Define and assign the loan variables using the input object. | |
| const loanAmount = input.loanAmount; | |
| const monthlyPayment = input.monthlyPayment; | |
| const loanDuration = input.loanDuration; | |
| // 1.2: Define the APR variable, but do not assign a value to it yet. | |
| let apr; | |
| // -------------------- | |
| // -------------------- | |
| // STEP 2: Define two functions: calculateF(x) and calculateFDerivative(x). These functions are used in Newton's method algorithm to find the value of x that makes f(x) = 0, where f(x) is defined as the sum of the loan amount divided by the monthly payment minus the sum of x to the power of i (where i ranges from 1 to loanDuration). | |
| // 2.1: Declare a function that calculates the value of f(x). | |
| function calculateF(x) { | |
| let sumOfPowers = 0; | |
| for (let i = 1; i <= loanDuration; i++) { | |
| sumOfPowers += Math.pow(x, i); | |
| } | |
| return sumOfPowers - loanAmount / monthlyPayment; | |
| } | |
| // 2.2: Declare a function that calculates the derivative of f(x). | |
| function calculateFDerivative(x) { | |
| let sumOfPowerTimesIndex = 0; | |
| for (let i = 1; i <= loanDuration; i++) { | |
| sumOfPowerTimesIndex += i * Math.pow(x, i - 1); | |
| } | |
| return sumOfPowerTimesIndex; | |
| } | |
| // -------------------- | |
| // -------------------- | |
| // STEP 3: Define the variables required to solve the APR and use Newton's method algorithm. | |
| // 3.1: Define a variable to solve the APR. It starts with an initial guess of 1. | |
| let x = 1; | |
| // 3.2: Define the level of precision that we aim for in the solution. In our case, we use the value of Epsilon. | |
| const EPSILON = 1e-6; | |
| // 3.3: Define the amount that we use to adjust the value of x on each iteration of Newton's method algorithm. | |
| let delta = 0; | |
| // 3.4: Use Newton's method algorithm to iteratively adjust the value of x until f(x) = 0 with a level of precision specified by the EPSILON constant. | |
| do { | |
| // 3.4.1: Calculate the change in x that is needed to make f(x) = 0. | |
| delta = -calculateF(x) / calculateFDerivative(x); | |
| // 3.4.2: Update the value of x | |
| x += delta; | |
| } while (Math.abs(delta) > EPSILON); | |
| // -------------------- | |
| // -------------------- | |
| // STEP 4: Calculate the APR value, round it to 2 decimal places, and set the rounded value as an output. | |
| // 4.1: Calculate the APR using the following formula: | |
| apr = Math.pow(x, -12) - 1; | |
| // 4.2: Round the calculated APR value to 2 decimal places. | |
| let aprRounded = parseFloat((apr * 100).toFixed(2)); | |
| // 4.3: Set the rounded value as an output. | |
| output.apr = aprRounded; | |
| // 4.4: Return the output. | |
| return output | |
| // -------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment