{
  "schemaVersion": 1,
  "slug": "car-lease-vs-buy-calculator",
  "category": "automotive",
  "kind": "implementation",
  "independentValidation": false,
  "implementationHash": "cd1d6d3a2cf1be513e93a764ebf6300ccfdbc9111c570a5e25cf0e57f0bb533f",
  "provenance": "Generated from the calculation contract used by the published page. Publication is not external validation.",
  "sourceFiles": [
    "data/calculators/car-lease-vs-buy-calculator.json",
    "src/engine.js"
  ],
  "runtimeSource": "/sources/engine.txt",
  "runtimeHash": "5c888d8898afbdee36267bb9977829e1e2ac568f6677c8ee87469c139864645c",
  "scope": "The compute function records implemented equations, constants, lookup tables and custom rules. Inputs record defaults and allowed values; outputs record precision and units. Defaults are examples, not recommendations. General background references are not evidence for this specific algorithm.",
  "publishedFormula": {
    "formula": "Cost_L = M_L·n + D_L;  Cost_B = D + M·k + B_k − V;  V = P(1 − d ÷ 100)^(n ÷ 12)",
    "vars": [
      [
        "Cost_L",
        "Total lease cost"
      ],
      [
        "Cost_B",
        "Net cost of buying"
      ],
      [
        "M_L",
        "Lease monthly payment"
      ],
      [
        "M",
        "Loan monthly payment"
      ],
      [
        "B_k",
        "Loan balance after k months"
      ],
      [
        "V",
        "Car value after n months"
      ],
      [
        "n",
        "Comparison period, months"
      ],
      [
        "D_L",
        "Lease down payment"
      ],
      [
        "D",
        "Purchase down payment"
      ],
      [
        "k",
        "Months of loan paid: k = min(n, loan term)"
      ],
      [
        "P",
        "Vehicle price"
      ],
      [
        "d",
        "Annual depreciation, %"
      ]
    ],
    "note": "Loan amortizes at annual rate ÷ 12; k = min(n, loan term); buy cost nets out resale value."
  },
  "externalEvidence": null,
  "contract": {
    "compute": "const leaseCost = leaseMonthly*months+leaseDown; const r = loanRate/100/12; const financed = Math.max(0, price-downPayment); const monthly = r > 0 ? financed*r/(1-Math.pow(1+r, -Math.max(loanMonths, 1))) : financed/Math.max(loanMonths, 1); const paidMonths = Math.min(months, loanMonths); const remainingBalance = r > 0 ? financed*Math.pow(1+r, paidMonths)-monthly*((Math.pow(1+r, paidMonths)-1)/r) : Math.max(0, financed-monthly*paidMonths); const value = price*Math.pow(1-depreciationRate/100, months/12); const buyCost = downPayment+monthly*paidMonths+Math.max(0, remainingBalance)-value; return { leaseCost, buyCost, buyMonthly: monthly, depreciatedValue: value, cheaper: leaseCost < buyCost ? 'Leasing is cheaper over this period' : 'Buying is cheaper over this period' };",
    "inputs": [
      {
        "name": "price",
        "type": "number",
        "default": 38000,
        "min": 0,
        "max": 2000000,
        "step": 100
      },
      {
        "name": "months",
        "type": "number",
        "default": 36,
        "min": 12,
        "max": 120,
        "step": 6,
        "slider": true
      },
      {
        "name": "leaseMonthly",
        "type": "number",
        "default": 499,
        "min": 0,
        "max": 20000,
        "step": 10,
        "slider": true
      },
      {
        "name": "leaseDown",
        "type": "number",
        "default": 3000,
        "min": 0,
        "max": 500000,
        "step": 100
      },
      {
        "name": "loanRate",
        "type": "number",
        "default": 6.5,
        "min": 0,
        "max": 40,
        "step": 0.01,
        "slider": true
      },
      {
        "name": "loanMonths",
        "type": "number",
        "default": 60,
        "min": 6,
        "max": 120,
        "step": 6,
        "slider": true
      },
      {
        "name": "downPayment",
        "type": "number",
        "default": 5000,
        "min": 0,
        "max": 500000,
        "step": 100
      },
      {
        "name": "depreciationRate",
        "type": "number",
        "default": 15,
        "min": 0,
        "max": 50,
        "step": 1,
        "slider": true
      }
    ],
    "outputs": [
      {
        "name": "leaseCost",
        "precision": 0,
        "unit": "$"
      },
      {
        "name": "buyCost",
        "precision": 0,
        "unit": "$"
      },
      {
        "name": "buyMonthly",
        "precision": 2,
        "unit": "$"
      },
      {
        "name": "depreciatedValue",
        "precision": 0,
        "unit": "$"
      },
      {
        "name": "cheaper",
        "precision": 0,
        "unit": "",
        "type": "text"
      }
    ],
    "interpret": null
  }
}
