financial-calculator

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Financial Calculator Suite

金融计算器套件

Professional-grade financial calculations with detailed breakdowns, visualizations, and exportable reports. Handles everything from simple loan payments to complex retirement projections with Monte Carlo simulations.
专业级金融计算工具,提供详细的分解说明、可视化图表及可导出的报告。可处理从简单贷款还款到基于Monte Carlo模拟的复杂退休规划等各类计算需求。

Core Calculators

核心计算器

  • Loan Calculator: Amortization schedules, payment breakdowns, prepayment scenarios
  • Investment Calculator: Future value, compound growth, recurring contributions
  • NPV/IRR Calculator: Net present value, internal rate of return, payback period
  • Retirement Calculator: Savings projections, withdrawal strategies, longevity analysis
  • Monte Carlo Simulator: Risk analysis with probability distributions
  • Mortgage Calculator: Home affordability, refinance comparison
  • Savings Goal Calculator: Time to goal, required contributions
  • 贷款计算器:摊销计划表、还款明细、提前还款场景
  • 投资计算器:未来价值、复利增长、定期缴款
  • NPV/IRR计算器:净现值、内部收益率、投资回收期
  • 退休计算器:储蓄规划、提取策略、寿命分析
  • Monte Carlo模拟器:基于概率分布的风险分析
  • 抵押贷款计算器:购房负担能力、再融资对比
  • 储蓄目标计算器:达成目标所需时间、必要缴款金额

Quick Start

快速开始

python
from scripts.financial_calc import FinancialCalculator
python
from scripts.financial_calc import FinancialCalculator

Loan calculation

Loan calculation

calc = FinancialCalculator() loan = calc.loan_payment(principal=250000, rate=6.5, years=30) print(f"Monthly payment: ${loan['monthly_payment']:,.2f}")
calc = FinancialCalculator() loan = calc.loan_payment(principal=250000, rate=6.5, years=30) print(f"Monthly payment: ${loan['monthly_payment']:,.2f}")

Investment growth

Investment growth

growth = calc.investment_growth( principal=10000, rate=7, years=20, monthly_contribution=500 ) print(f"Final value: ${growth['final_value']:,.2f}")
undefined
growth = calc.investment_growth( principal=10000, rate=7, years=20, monthly_contribution=500 ) print(f"Final value: ${growth['final_value']:,.2f}")
undefined

Loan Calculator

贷款计算器

Basic Loan Payment

基础贷款还款计算

python
from scripts.financial_calc import FinancialCalculator

calc = FinancialCalculator()
python
from scripts.financial_calc import FinancialCalculator

calc = FinancialCalculator()

Calculate monthly payment

Calculate monthly payment

loan = calc.loan_payment( principal=250000, # Loan amount rate=6.5, # Annual interest rate (%) years=30 # Loan term )
print(f"Monthly Payment: ${loan['monthly_payment']:,.2f}") print(f"Total Payments: ${loan['total_payments']:,.2f}") print(f"Total Interest: ${loan['total_interest']:,.2f}")
undefined
loan = calc.loan_payment( principal=250000, # Loan amount rate=6.5, # Annual interest rate (%) years=30 # Loan term )
print(f"Monthly Payment: ${loan['monthly_payment']:,.2f}") print(f"Total Payments: ${loan['total_payments']:,.2f}") print(f"Total Interest: ${loan['total_interest']:,.2f}")
undefined

Amortization Schedule

摊销计划表

python
undefined
python
undefined

Get full amortization schedule

Get full amortization schedule

schedule = calc.amortization_schedule( principal=250000, rate=6.5, years=30 )
schedule = calc.amortization_schedule( principal=250000, rate=6.5, years=30 )

Schedule is a list of monthly payments

Schedule is a list of monthly payments

for payment in schedule[:12]: # First year print(f"Month {payment['month']}: " f"Payment ${payment['payment']:,.2f}, " f"Principal ${payment['principal']:,.2f}, " f"Interest ${payment['interest']:,.2f}, " f"Balance ${payment['balance']:,.2f}")
for payment in schedule[:12]: # First year print(f"Month {payment['month']}: " f"Payment ${payment['payment']:,.2f}, " f"Principal ${payment['principal']:,.2f}, " f"Interest ${payment['interest']:,.2f}, " f"Balance ${payment['balance']:,.2f}")

Export to CSV

Export to CSV

calc.export_amortization(schedule, "loan_schedule.csv")
undefined
calc.export_amortization(schedule, "loan_schedule.csv")
undefined

Prepayment Analysis

提前还款分析

python
undefined
python
undefined

Compare with extra payments

Compare with extra payments

comparison = calc.prepayment_comparison( principal=250000, rate=6.5, years=30, extra_monthly=200 )
print(f"With extra payments:") print(f" Months saved: {comparison['months_saved']}") print(f" Interest saved: ${comparison['interest_saved']:,.2f}") print(f" New payoff: {comparison['new_term_years']:.1f} years")
undefined
comparison = calc.prepayment_comparison( principal=250000, rate=6.5, years=30, extra_monthly=200 )
print(f"With extra payments:") print(f" Months saved: {comparison['months_saved']}") print(f" Interest saved: ${comparison['interest_saved']:,.2f}") print(f" New payoff: {comparison['new_term_years']:.1f} years")
undefined

Investment Calculator

投资计算器

Future Value

未来价值计算

python
undefined
python
undefined

Simple compound growth

Simple compound growth

result = calc.future_value( principal=10000, rate=7, # Annual return (%) years=20 ) print(f"Future value: ${result['future_value']:,.2f}")
result = calc.future_value( principal=10000, rate=7, # Annual return (%) years=20 ) print(f"Future value: ${result['future_value']:,.2f}")

With monthly contributions

With monthly contributions

result = calc.investment_growth( principal=10000, rate=7, years=20, monthly_contribution=500 ) print(f"Final value: ${result['final_value']:,.2f}") print(f"Total contributions: ${result['total_contributions']:,.2f}") print(f"Total growth: ${result['total_growth']:,.2f}")
undefined
result = calc.investment_growth( principal=10000, rate=7, years=20, monthly_contribution=500 ) print(f"Final value: ${result['final_value']:,.2f}") print(f"Total contributions: ${result['total_contributions']:,.2f}") print(f"Total growth: ${result['total_growth']:,.2f}")
undefined

Investment Comparison

投资方案对比

python
undefined
python
undefined

Compare different scenarios

Compare different scenarios

scenarios = calc.compare_investments([ {'name': 'Conservative', 'rate': 4, 'principal': 10000, 'monthly': 500}, {'name': 'Moderate', 'rate': 7, 'principal': 10000, 'monthly': 500}, {'name': 'Aggressive', 'rate': 10, 'principal': 10000, 'monthly': 500}, ], years=20)
for s in scenarios: print(f"{s['name']}: ${s['final_value']:,.2f}")
undefined
scenarios = calc.compare_investments([ {'name': 'Conservative', 'rate': 4, 'principal': 10000, 'monthly': 500}, {'name': 'Moderate', 'rate': 7, 'principal': 10000, 'monthly': 500}, {'name': 'Aggressive', 'rate': 10, 'principal': 10000, 'monthly': 500}, ], years=20)
for s in scenarios: print(f"{s['name']}: ${s['final_value']:,.2f}")
undefined

NPV/IRR Calculator

NPV/IRR计算器

Net Present Value

净现值计算

python
undefined
python
undefined

Calculate NPV of cash flows

Calculate NPV of cash flows

cash_flows = [-100000, 30000, 35000, 40000, 45000, 50000] # Initial + 5 years npv = calc.npv(cash_flows, discount_rate=10) print(f"NPV: ${npv:,.2f}")
undefined
cash_flows = [-100000, 30000, 35000, 40000, 45000, 50000] # Initial + 5 years npv = calc.npv(cash_flows, discount_rate=10) print(f"NPV: ${npv:,.2f}")
undefined

Internal Rate of Return

内部收益率计算

python
undefined
python
undefined

Calculate IRR

Calculate IRR

irr = calc.irr(cash_flows) print(f"IRR: {irr:.2f}%")
undefined
irr = calc.irr(cash_flows) print(f"IRR: {irr:.2f}%")
undefined

Payback Period

投资回收期计算

python
undefined
python
undefined

Simple and discounted payback

Simple and discounted payback

payback = calc.payback_period(cash_flows, discount_rate=10) print(f"Simple payback: {payback['simple']:.2f} years") print(f"Discounted payback: {payback['discounted']:.2f} years")
undefined
payback = calc.payback_period(cash_flows, discount_rate=10) print(f"Simple payback: {payback['simple']:.2f} years") print(f"Discounted payback: {payback['discounted']:.2f} years")
undefined

Project Comparison

项目对比

python
undefined
python
undefined

Compare multiple projects

Compare multiple projects

projects = [ {'name': 'Project A', 'flows': [-100000, 30000, 40000, 50000, 60000]}, {'name': 'Project B', 'flows': [-80000, 25000, 30000, 35000, 40000]}, ] comparison = calc.compare_projects(projects, discount_rate=10)
undefined
projects = [ {'name': 'Project A', 'flows': [-100000, 30000, 40000, 50000, 60000]}, {'name': 'Project B', 'flows': [-80000, 25000, 30000, 35000, 40000]}, ] comparison = calc.compare_projects(projects, discount_rate=10)
undefined

Retirement Calculator

退休计算器

Basic Retirement Projection

基础退休规划

python
undefined
python
undefined

Project retirement savings

Project retirement savings

retirement = calc.retirement_projection( current_age=35, retirement_age=65, current_savings=100000, monthly_contribution=1000, expected_return=7, inflation=2.5 )
print(f"Projected savings at retirement: ${retirement['nominal_value']:,.2f}") print(f"Real value (today's dollars): ${retirement['real_value']:,.2f}")
undefined
retirement = calc.retirement_projection( current_age=35, retirement_age=65, current_savings=100000, monthly_contribution=1000, expected_return=7, inflation=2.5 )
print(f"Projected savings at retirement: ${retirement['nominal_value']:,.2f}") print(f"Real value (today's dollars): ${retirement['real_value']:,.2f}")
undefined

Withdrawal Strategy

提取策略计算

python
undefined
python
undefined

Calculate sustainable withdrawals

Calculate sustainable withdrawals

withdrawal = calc.retirement_withdrawal( savings=1000000, annual_spending=40000, expected_return=5, inflation=2.5, years=30 # Retirement duration )
print(f"Success probability: {withdrawal['success_rate']:.1f}%") print(f"Median ending balance: ${withdrawal['median_ending']:,.2f}")
undefined
withdrawal = calc.retirement_withdrawal( savings=1000000, annual_spending=40000, expected_return=5, inflation=2.5, years=30 # Retirement duration )
print(f"Success probability: {withdrawal['success_rate']:.1f}%") print(f"Median ending balance: ${withdrawal['median_ending']:,.2f}")
undefined

FIRE Calculator

FIRE计算器

python
undefined
python
undefined

Financial Independence calculation

Financial Independence calculation

fire = calc.fire_calculator( annual_expenses=50000, current_savings=200000, annual_savings=30000, expected_return=7, safe_withdrawal_rate=4 )
print(f"FIRE number: ${fire['fire_number']:,.2f}") print(f"Years to FIRE: {fire['years_to_fire']:.1f}")
undefined
fire = calc.fire_calculator( annual_expenses=50000, current_savings=200000, annual_savings=30000, expected_return=7, safe_withdrawal_rate=4 )
print(f"FIRE number: ${fire['fire_number']:,.2f}") print(f"Years to FIRE: {fire['years_to_fire']:.1f}")
undefined

Monte Carlo Simulation

Monte Carlo模拟

Investment Simulation

投资模拟

python
undefined
python
undefined

Run Monte Carlo simulation

Run Monte Carlo simulation

simulation = calc.monte_carlo_investment( principal=100000, monthly_contribution=1000, years=20, mean_return=7, std_dev=15, # Volatility simulations=1000 )
print(f"Median outcome: ${simulation['median']:,.2f}") print(f"10th percentile: ${simulation['p10']:,.2f}") print(f"90th percentile: ${simulation['p90']:,.2f}") print(f"Probability > $1M: {simulation['prob_above_1m']:.1f}%")
undefined
simulation = calc.monte_carlo_investment( principal=100000, monthly_contribution=1000, years=20, mean_return=7, std_dev=15, # Volatility simulations=1000 )
print(f"Median outcome: ${simulation['median']:,.2f}") print(f"10th percentile: ${simulation['p10']:,.2f}") print(f"90th percentile: ${simulation['p90']:,.2f}") print(f"Probability > $1M: {simulation['prob_above_1m']:.1f}%")
undefined

Retirement Simulation

退休模拟

python
undefined
python
undefined

Monte Carlo retirement analysis

Monte Carlo retirement analysis

retirement_sim = calc.monte_carlo_retirement( savings=1000000, annual_withdrawal=40000, years=30, mean_return=5, std_dev=10, inflation_mean=2.5, inflation_std=1, simulations=1000 )
print(f"Success rate: {retirement_sim['success_rate']:.1f}%") print(f"Median final balance: ${retirement_sim['median_ending']:,.2f}")
undefined
retirement_sim = calc.monte_carlo_retirement( savings=1000000, annual_withdrawal=40000, years=30, mean_return=5, std_dev=10, inflation_mean=2.5, inflation_std=1, simulations=1000 )
print(f"Success rate: {retirement_sim['success_rate']:.1f}%") print(f"Median final balance: ${retirement_sim['median_ending']:,.2f}")
undefined

Mortgage Calculator

抵押贷款计算器

Affordability

购房负担能力计算

python
undefined
python
undefined

Calculate affordable home price

Calculate affordable home price

affordability = calc.mortgage_affordability( annual_income=100000, monthly_debt=500, down_payment=50000, rate=6.5, term_years=30, dti_limit=43 # Debt-to-income limit (%) )
print(f"Max home price: ${affordability['max_price']:,.2f}") print(f"Max loan amount: ${affordability['max_loan']:,.2f}") print(f"Monthly payment: ${affordability['monthly_payment']:,.2f}")
undefined
affordability = calc.mortgage_affordability( annual_income=100000, monthly_debt=500, down_payment=50000, rate=6.5, term_years=30, dti_limit=43 # Debt-to-income limit (%) )
print(f"Max home price: ${affordability['max_price']:,.2f}") print(f"Max loan amount: ${affordability['max_loan']:,.2f}") print(f"Monthly payment: ${affordability['monthly_payment']:,.2f}")
undefined

Refinance Comparison

再融资对比分析

python
undefined
python
undefined

Should you refinance?

Should you refinance?

refinance = calc.refinance_analysis( current_balance=200000, current_rate=7.0, current_payment=1330, remaining_months=300, new_rate=5.5, new_term_years=30, closing_costs=5000 )
print(f"New payment: ${refinance['new_payment']:,.2f}") print(f"Monthly savings: ${refinance['monthly_savings']:,.2f}") print(f"Break-even: {refinance['break_even_months']} months") print(f"Lifetime savings: ${refinance['lifetime_savings']:,.2f}")
undefined
refinance = calc.refinance_analysis( current_balance=200000, current_rate=7.0, current_payment=1330, remaining_months=300, new_rate=5.5, new_term_years=30, closing_costs=5000 )
print(f"New payment: ${refinance['new_payment']:,.2f}") print(f"Monthly savings: ${refinance['monthly_savings']:,.2f}") print(f"Break-even: {refinance['break_even_months']} months") print(f"Lifetime savings: ${refinance['lifetime_savings']:,.2f}")
undefined

Savings Goal Calculator

储蓄目标计算器

python
undefined
python
undefined

Time to reach goal

Time to reach goal

goal = calc.savings_goal( target=100000, current=10000, rate=5, monthly_contribution=500 )
print(f"Time to goal: {goal['months']} months ({goal['years']:.1f} years)")
goal = calc.savings_goal( target=100000, current=10000, rate=5, monthly_contribution=500 )
print(f"Time to goal: {goal['months']} months ({goal['years']:.1f} years)")

Required monthly savings

Required monthly savings

required = calc.required_savings( target=100000, current=10000, rate=5, years=10 )
print(f"Required monthly: ${required['monthly_needed']:,.2f}")
undefined
required = calc.required_savings( target=100000, current=10000, rate=5, years=10 )
print(f"Required monthly: ${required['monthly_needed']:,.2f}")
undefined

Visualization

可视化功能

python
undefined
python
undefined

Generate charts

Generate charts

calc.plot_amortization(schedule, "amortization.png") calc.plot_investment_growth(growth_data, "growth.png") calc.plot_monte_carlo(simulation, "monte_carlo.png") calc.plot_comparison(scenarios, "comparison.png")
undefined
calc.plot_amortization(schedule, "amortization.png") calc.plot_investment_growth(growth_data, "growth.png") calc.plot_monte_carlo(simulation, "monte_carlo.png") calc.plot_comparison(scenarios, "comparison.png")
undefined

Export Options

导出选项

python
undefined
python
undefined

Export to CSV

Export to CSV

calc.export_amortization(schedule, "schedule.csv") calc.export_simulation(simulation, "simulation.csv")
calc.export_amortization(schedule, "schedule.csv") calc.export_simulation(simulation, "simulation.csv")

Export to JSON

Export to JSON

calc.export_json(results, "results.json")
calc.export_json(results, "results.json")

Generate PDF report

Generate PDF report

calc.generate_report( analysis_type='loan', data=loan_data, output="loan_report.pdf" )
undefined
calc.generate_report( analysis_type='loan', data=loan_data, output="loan_report.pdf" )
undefined

CLI Usage

命令行界面(CLI)使用方法

bash
undefined
bash
undefined

Loan calculation

Loan calculation

python financial_calc.py loan --principal 250000 --rate 6.5 --years 30
python financial_calc.py loan --principal 250000 --rate 6.5 --years 30

Investment growth

Investment growth

python financial_calc.py invest --principal 10000 --rate 7 --years 20 --monthly 500
python financial_calc.py invest --principal 10000 --rate 7 --years 20 --monthly 500

NPV calculation

NPV calculation

python financial_calc.py npv --flows "-100000,30000,35000,40000,45000" --rate 10
python financial_calc.py npv --flows "-100000,30000,35000,40000,45000" --rate 10

Retirement projection

Retirement projection

python financial_calc.py retire --age 35 --retire-age 65 --savings 100000 --monthly 1000
python financial_calc.py retire --age 35 --retire-age 65 --savings 100000 --monthly 1000

Monte Carlo simulation

Monte Carlo simulation

python financial_calc.py montecarlo --principal 100000 --years 20 --return 7 --volatility 15
undefined
python financial_calc.py montecarlo --principal 100000 --years 20 --return 7 --volatility 15
undefined

Formulas Reference

公式参考

Loan Payment (PMT)

贷款还款公式(PMT)

PMT = P * [r(1+r)^n] / [(1+r)^n - 1]
where: P = principal, r = monthly rate, n = total payments
PMT = P * [r(1+r)^n] / [(1+r)^n - 1]
where: P = principal, r = monthly rate, n = total payments

Future Value (FV)

未来价值公式(FV)

FV = PV * (1 + r)^n + PMT * [((1 + r)^n - 1) / r]
where: PV = present value, r = rate, n = periods, PMT = periodic payment
FV = PV * (1 + r)^n + PMT * [((1 + r)^n - 1) / r]
where: PV = present value, r = rate, n = periods, PMT = periodic payment

Net Present Value (NPV)

净现值公式(NPV)

NPV = Σ [CF_t / (1 + r)^t] for t = 0 to n
where: CF = cash flow, r = discount rate, t = time period
NPV = Σ [CF_t / (1 + r)^t] for t = 0 to n
where: CF = cash flow, r = discount rate, t = time period

Internal Rate of Return (IRR)

内部收益率公式(IRR)

0 = Σ [CF_t / (1 + IRR)^t] for t = 0 to n
(Solved iteratively)
0 = Σ [CF_t / (1 + IRR)^t] for t = 0 to n
(Solved iteratively)

Error Handling

错误处理

python
from scripts.financial_calc import FinancialCalculator, FinanceError

try:
    result = calc.loan_payment(principal=-1000, rate=5, years=30)
except FinanceError as e:
    print(f"Error: {e}")
python
from scripts.financial_calc import FinancialCalculator, FinanceError

try:
    result = calc.loan_payment(principal=-1000, rate=5, years=30)
except FinanceError as e:
    print(f"Error: {e}")

Dependencies

依赖项

numpy>=1.24.0
numpy-financial>=1.0.0
pandas>=2.0.0
matplotlib>=3.7.0
scipy>=1.10.0
numpy>=1.24.0
numpy-financial>=1.0.0
pandas>=2.0.0
matplotlib>=3.7.0
scipy>=1.10.0