The Internal Rate of Return (IRR) is a metric used in capital budgeting to estimate the profitability of potential investments. It is the discount rate that makes the net present value (NPV) of all cash flows from a particular project equal to zero. In simpler terms, it's the expected annual rate of return that an investment is projected to generate.
function calculateIRR() {
var initialInvestment = parseFloat(document.getElementById("initialInvestment").value);
var cashFlowsInput = document.getElementById("cashFlows").value;
if (isNaN(initialInvestment)) {
document.getElementById("irr-result").innerHTML = "Please enter a valid initial investment.";
return;
}
var cashFlowsArray = [];
if (cashFlowsInput) {
var flows = cashFlowsInput.split(',');
for (var i = 0; i < flows.length; i++) {
var flow = parseFloat(flows[i]);
if (isNaN(flow)) {
document.getElementById("irr-result").innerHTML = "Please enter valid comma-separated cash flows.";
return;
}
cashFlowsArray.push(flow);
}
} else {
document.getElementById("irr-result").innerHTML = "Please enter at least one cash flow.";
return;
}
// Combine initial investment (as a negative cash flow) with subsequent cash flows
var allCashFlows = [-initialInvestment].concat(cashFlowsArray);
// Simple IRR calculation using Newton-Raphson method for approximation
// This is a simplified implementation and might not converge for all cases.
// More robust libraries or iterative methods might be needed for complex scenarios.
var guess = 0.1; // Initial guess for IRR
var maxIterations = 100;
var tolerance = 0.00001;
var irr = 0;
for (var i = 0; i < maxIterations; i++) {
var npv = 0;
var derivative = 0;
for (var t = 0; t < allCashFlows.length; t++) {
npv += allCashFlows[t] / Math.pow(1 + guess, t);
derivative -= t * allCashFlows[t] / Math.pow(1 + guess, t + 1);
}
if (Math.abs(npv) < tolerance) {
irr = guess;
break;
}
if (derivative === 0) { // Avoid division by zero
document.getElementById("irr-result").innerHTML = "Could not converge. Derivative is zero.";
return;
}
guess -= npv / derivative;
}
if (i === maxIterations) {
document.getElementById("irr-result").innerHTML = "IRR calculation did not converge within the maximum number of iterations.";
} else {
document.getElementById("irr-result").innerHTML = "Estimated Internal Rate of Return (IRR): " + (irr * 100).toFixed(2) + "%";
}
}
#irr-calculator-wrapper {
font-family: sans-serif;
border: 1px solid #ccc;
padding: 20px;
border-radius: 8px;
max-width: 500px;
margin: 20px auto;
background-color: #f9f9f9;
}
#irr-calculator-wrapper h2 {
text-align: center;
color: #333;
margin-bottom: 20px;
}
.calculator-inputs .input-group {
margin-bottom: 15px;
display: flex;
flex-direction: column;
}
.calculator-inputs label {
margin-bottom: 5px;
font-weight: bold;
color: #555;
}
.calculator-inputs input[type="number"],
.calculator-inputs input[type="text"] {
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 1rem;
}
#irr-calculator-wrapper button {
display: block;
width: 100%;
padding: 12px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
font-size: 1.1rem;
cursor: pointer;
transition: background-color 0.3s ease;
margin-top: 15px;
}
#irr-calculator-wrapper button:hover {
background-color: #0056b3;
}
.calculator-result {
margin-top: 20px;
padding: 15px;
background-color: #e9ecef;
border: 1px solid #ced4da;
border-radius: 4px;
text-align: center;
font-size: 1.1rem;
color: #333;
}
Example:
Let's say you are considering an investment with an initial cost of $5,000. You project the following net cash inflows over the next three years: Year 1: $1,500, Year 2: $2,000, Year 3: $2,500.
To calculate the IRR:
Initial Investment: 5000
Cash Flows: 1500, 2000, 2500
Using the calculator, you would input '5000' for the Initial Investment and '1500,2000,2500' for the Cash Flows. The calculator will then estimate the IRR.