roi-calculator

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

ROI Calculator

ROI 计算器

Comprehensive ROI calculations for marketing, investments, and business decisions.
为营销、投资和商业决策提供全面的ROI计算功能。

Features

功能特性

  • Simple ROI: Basic return on investment calculation
  • Marketing ROI: Campaign performance with attribution
  • Investment ROI: Time-adjusted returns with CAGR
  • Break-Even Analysis: Find profit threshold
  • Payback Period: Time to recover investment
  • Comparative Analysis: Compare multiple options
  • What-If Scenarios: Sensitivity analysis
  • 简易ROI计算:基础投资回报率计算
  • 营销ROI计算:带有归因分析的营销活动绩效评估
  • 投资ROI计算:包含复合年增长率(CAGR)的时间调整回报率
  • 盈亏平衡分析:找出盈利阈值
  • 投资回收期计算:收回投资所需的时间
  • 对比分析:比较多个选项
  • 假设场景分析:敏感性分析

Quick Start

快速开始

python
from roi_calculator import ROICalculator

calc = ROICalculator()
python
from roi_calculator import ROICalculator

calc = ROICalculator()

Simple ROI

Simple ROI

roi = calc.simple_roi(investment=10000, return_value=15000) print(f"ROI: {roi['roi_percent']}%")
roi = calc.simple_roi(investment=10000, return_value=15000) print(f"ROI: {roi['roi_percent']}%")

Marketing ROI

Marketing ROI

marketing = calc.marketing_roi( ad_spend=5000, revenue_generated=25000, cost_of_goods=10000 ) print(f"Marketing ROI: {marketing['roi_percent']}%")
marketing = calc.marketing_roi( ad_spend=5000, revenue_generated=25000, cost_of_goods=10000 ) print(f"Marketing ROI: {marketing['roi_percent']}%")

Break-even analysis

Break-even analysis

breakeven = calc.break_even( fixed_costs=50000, price_per_unit=100, variable_cost_per_unit=60 ) print(f"Break-even units: {breakeven['units']}")
undefined
breakeven = calc.break_even( fixed_costs=50000, price_per_unit=100, variable_cost_per_unit=60 ) print(f"Break-even units: {breakeven['units']}")
undefined

CLI Usage

CLI 使用方式

bash
undefined
bash
undefined

Simple ROI

Simple ROI

python roi_calculator.py --investment 10000 --return 15000
python roi_calculator.py --investment 10000 --return 15000

Marketing campaign ROI

Marketing campaign ROI

python roi_calculator.py --marketing --spend 5000 --revenue 25000 --cogs 10000
python roi_calculator.py --marketing --spend 5000 --revenue 25000 --cogs 10000

Break-even analysis

Break-even analysis

python roi_calculator.py --breakeven --fixed 50000 --price 100 --variable 60
python roi_calculator.py --breakeven --fixed 50000 --price 100 --variable 60

Investment with time (CAGR)

Investment with time (CAGR)

python roi_calculator.py --investment 10000 --return 20000 --years 5
python roi_calculator.py --investment 10000 --return 20000 --years 5

Payback period

Payback period

python roi_calculator.py --payback --investment 100000 --annual-return 25000
python roi_calculator.py --payback --investment 100000 --annual-return 25000

Compare multiple investments

Compare multiple investments

python roi_calculator.py --compare investments.csv
undefined
python roi_calculator.py --compare investments.csv
undefined

API Reference

API 参考

ROICalculator Class

ROICalculator 类

python
class ROICalculator:
    def __init__(self)

    # Basic ROI
    def simple_roi(self, investment: float, return_value: float) -> Dict
    def net_roi(self, investment: float, gain: float, costs: float = 0) -> Dict

    # Marketing ROI
    def marketing_roi(self, ad_spend: float, revenue_generated: float,
                      cost_of_goods: float = 0) -> Dict
    def campaign_roi(self, campaigns: List[Dict]) -> pd.DataFrame
    def roas(self, ad_spend: float, revenue: float) -> Dict  # Return on Ad Spend

    # Investment ROI
    def investment_roi(self, initial: float, final: float, years: float) -> Dict
    def cagr(self, initial: float, final: float, years: float) -> float
    def total_return(self, initial: float, final: float, dividends: float = 0) -> Dict

    # Break-Even Analysis
    def break_even(self, fixed_costs: float, price_per_unit: float,
                   variable_cost_per_unit: float) -> Dict
    def break_even_revenue(self, fixed_costs: float, contribution_margin_ratio: float) -> Dict

    # Payback Period
    def payback_period(self, investment: float, annual_cash_flow: float) -> Dict
    def payback_period_uneven(self, investment: float, cash_flows: List[float]) -> Dict

    # Comparative Analysis
    def compare_investments(self, investments: List[Dict]) -> pd.DataFrame
    def rank_by_roi(self, investments: List[Dict]) -> List[Dict]

    # Sensitivity Analysis
    def sensitivity_analysis(self, base_case: Dict, variables: Dict) -> pd.DataFrame
    def scenario_analysis(self, scenarios: List[Dict]) -> pd.DataFrame

    # Reporting
    def generate_report(self, analysis: Dict, output: str) -> str
python
class ROICalculator:
    def __init__(self)

    # Basic ROI
    def simple_roi(self, investment: float, return_value: float) -> Dict
    def net_roi(self, investment: float, gain: float, costs: float = 0) -> Dict

    # Marketing ROI
    def marketing_roi(self, ad_spend: float, revenue_generated: float,
                      cost_of_goods: float = 0) -> Dict
    def campaign_roi(self, campaigns: List[Dict]) -> pd.DataFrame
    def roas(self, ad_spend: float, revenue: float) -> Dict  # Return on Ad Spend

    # Investment ROI
    def investment_roi(self, initial: float, final: float, years: float) -> Dict
    def cagr(self, initial: float, final: float, years: float) -> float
    def total_return(self, initial: float, final: float, dividends: float = 0) -> Dict

    # Break-Even Analysis
    def break_even(self, fixed_costs: float, price_per_unit: float,
                   variable_cost_per_unit: float) -> Dict
    def break_even_revenue(self, fixed_costs: float, contribution_margin_ratio: float) -> Dict

    # Payback Period
    def payback_period(self, investment: float, annual_cash_flow: float) -> Dict
    def payback_period_uneven(self, investment: float, cash_flows: List[float]) -> Dict

    # Comparative Analysis
    def compare_investments(self, investments: List[Dict]) -> pd.DataFrame
    def rank_by_roi(self, investments: List[Dict]) -> List[Dict]

    # Sensitivity Analysis
    def sensitivity_analysis(self, base_case: Dict, variables: Dict) -> pd.DataFrame
    def scenario_analysis(self, scenarios: List[Dict]) -> pd.DataFrame

    # Reporting
    def generate_report(self, analysis: Dict, output: str) -> str

ROI Calculations

ROI 计算方法

Simple ROI

简易ROI计算

python
result = calc.simple_roi(investment=10000, return_value=15000)
python
result = calc.simple_roi(investment=10000, return_value=15000)

Returns:

Returns:

{

{

"investment": 10000,

"investment": 10000,

"return_value": 15000,

"return_value": 15000,

"gain": 5000,

"gain": 5000,

"roi_percent": 50.0,

"roi_percent": 50.0,

"roi_ratio": 0.5

"roi_ratio": 0.5

}

}

undefined
undefined

Net ROI (with additional costs)

净ROI计算(含额外成本)

python
result = calc.net_roi(
    investment=10000,
    gain=8000,
    costs=2000  # Additional costs
)
python
result = calc.net_roi(
    investment=10000,
    gain=8000,
    costs=2000  # Additional costs
)

Returns:

Returns:

{

{

"investment": 10000,

"investment": 10000,

"gross_gain": 8000,

"gross_gain": 8000,

"costs": 2000,

"costs": 2000,

"net_gain": 6000,

"net_gain": 6000,

"roi_percent": 60.0

"roi_percent": 60.0

}

}

undefined
undefined

Marketing ROI

营销ROI计算

Campaign ROI

营销活动ROI

python
result = calc.marketing_roi(
    ad_spend=5000,
    revenue_generated=25000,
    cost_of_goods=10000
)
python
result = calc.marketing_roi(
    ad_spend=5000,
    revenue_generated=25000,
    cost_of_goods=10000
)

Returns:

Returns:

{

{

"ad_spend": 5000,

"ad_spend": 5000,

"revenue": 25000,

"revenue": 25000,

"cost_of_goods": 10000,

"cost_of_goods": 10000,

"gross_profit": 15000,

"gross_profit": 15000,

"net_profit": 10000, # After ad spend

"net_profit": 10000, # After ad spend

"roi_percent": 200.0,

"roi_percent": 200.0,

"roas": 5.0 # $5 revenue per $1 ad spend

"roas": 5.0 # $5 revenue per $1 ad spend

}

}

undefined
undefined

Return on Ad Spend (ROAS)

广告支出回报率(ROAS)

python
result = calc.roas(ad_spend=1000, revenue=4000)
python
result = calc.roas(ad_spend=1000, revenue=4000)

Returns:

Returns:

{

{

"ad_spend": 1000,

"ad_spend": 1000,

"revenue": 4000,

"revenue": 4000,

"roas": 4.0,

"roas": 4.0,

"roas_percent": 400.0

"roas_percent": 400.0

}

}

undefined
undefined

Multi-Campaign Comparison

多营销活动对比

python
campaigns = [
    {"name": "Facebook", "spend": 2000, "revenue": 8000, "cogs": 3000},
    {"name": "Google", "spend": 3000, "revenue": 15000, "cogs": 6000},
    {"name": "Email", "spend": 500, "revenue": 3000, "cogs": 1000}
]
results = calc.campaign_roi(campaigns)
python
campaigns = [
    {"name": "Facebook", "spend": 2000, "revenue": 8000, "cogs": 3000},
    {"name": "Google", "spend": 3000, "revenue": 15000, "cogs": 6000},
    {"name": "Email", "spend": 500, "revenue": 3000, "cogs": 1000}
]
results = calc.campaign_roi(campaigns)

Returns DataFrame with ROI for each campaign

Returns DataFrame with ROI for each campaign

undefined
undefined

Investment ROI

投资ROI计算

Time-Adjusted ROI with CAGR

含CAGR的时间调整ROI

python
result = calc.investment_roi(
    initial=10000,
    final=20000,
    years=5
)
python
result = calc.investment_roi(
    initial=10000,
    final=20000,
    years=5
)

Returns:

Returns:

{

{

"initial": 10000,

"initial": 10000,

"final": 20000,

"final": 20000,

"total_gain": 10000,

"total_gain": 10000,

"total_roi_percent": 100.0,

"total_roi_percent": 100.0,

"cagr_percent": 14.87, # Compound Annual Growth Rate

"cagr_percent": 14.87, # Compound Annual Growth Rate

"years": 5

"years": 5

}

}

undefined
undefined

Total Return with Dividends

含股息的总回报率

python
result = calc.total_return(
    initial=10000,
    final=12000,
    dividends=1500
)
python
result = calc.total_return(
    initial=10000,
    final=12000,
    dividends=1500
)

Returns:

Returns:

{

{

"initial": 10000,

"initial": 10000,

"final": 12000,

"final": 12000,

"capital_gain": 2000,

"capital_gain": 2000,

"dividends": 1500,

"dividends": 1500,

"total_return": 3500,

"total_return": 3500,

"total_return_percent": 35.0

"total_return_percent": 35.0

}

}

undefined
undefined

Break-Even Analysis

盈亏平衡分析

Unit Break-Even

单位盈亏平衡

python
result = calc.break_even(
    fixed_costs=50000,
    price_per_unit=100,
    variable_cost_per_unit=60
)
python
result = calc.break_even(
    fixed_costs=50000,
    price_per_unit=100,
    variable_cost_per_unit=60
)

Returns:

Returns:

{

{

"fixed_costs": 50000,

"fixed_costs": 50000,

"price_per_unit": 100,

"price_per_unit": 100,

"variable_cost_per_unit": 60,

"variable_cost_per_unit": 60,

"contribution_margin": 40,

"contribution_margin": 40,

"contribution_margin_ratio": 0.4,

"contribution_margin_ratio": 0.4,

"break_even_units": 1250,

"break_even_units": 1250,

"break_even_revenue": 125000

"break_even_revenue": 125000

}

}

undefined
undefined

Revenue Break-Even

营收盈亏平衡

python
result = calc.break_even_revenue(
    fixed_costs=50000,
    contribution_margin_ratio=0.4  # 40% margin
)
python
result = calc.break_even_revenue(
    fixed_costs=50000,
    contribution_margin_ratio=0.4  # 40% margin
)

Returns:

Returns:

{

{

"fixed_costs": 50000,

"fixed_costs": 50000,

"contribution_margin_ratio": 0.4,

"contribution_margin_ratio": 0.4,

"break_even_revenue": 125000

"break_even_revenue": 125000

}

}

undefined
undefined

Payback Period

投资回收期计算

Simple Payback

简易投资回收期

python
result = calc.payback_period(
    investment=100000,
    annual_cash_flow=25000
)
python
result = calc.payback_period(
    investment=100000,
    annual_cash_flow=25000
)

Returns:

Returns:

{

{

"investment": 100000,

"investment": 100000,

"annual_cash_flow": 25000,

"annual_cash_flow": 25000,

"payback_years": 4.0,

"payback_years": 4.0,

"payback_months": 48

"payback_months": 48

}

}

undefined
undefined

Uneven Cash Flows

非均匀现金流投资回收期

python
result = calc.payback_period_uneven(
    investment=100000,
    cash_flows=[20000, 30000, 40000, 35000, 25000]  # Per year
)
python
result = calc.payback_period_uneven(
    investment=100000,
    cash_flows=[20000, 30000, 40000, 35000, 25000]  # Per year
)

Returns:

Returns:

{

{

"investment": 100000,

"investment": 100000,

"payback_years": 2.75, # Recovered in year 3

"payback_years": 2.75, # Recovered in year 3

"cumulative_flows": [20000, 50000, 90000, 125000, 150000]

"cumulative_flows": [20000, 50000, 90000, 125000, 150000]

}

}

undefined
undefined

Comparative Analysis

对比分析

Compare Multiple Investments

多投资选项对比

python
investments = [
    {"name": "Project A", "investment": 50000, "return": 75000, "years": 2},
    {"name": "Project B", "investment": 30000, "return": 48000, "years": 3},
    {"name": "Project C", "investment": 100000, "return": 180000, "years": 5}
]
comparison = calc.compare_investments(investments)
python
investments = [
    {"name": "Project A", "investment": 50000, "return": 75000, "years": 2},
    {"name": "Project B", "investment": 30000, "return": 48000, "years": 3},
    {"name": "Project C", "investment": 100000, "return": 180000, "years": 5}
]
comparison = calc.compare_investments(investments)

Returns DataFrame:

Returns DataFrame:

name | investment | return | roi% | cagr% | payback

name | investment | return | roi% | cagr% | payback

Project A | 50000 | 75000 | 50.0 | 22.5 | 1.33

Project A | 50000 | 75000 | 50.0 | 22.5 | 1.33

Project B | 30000 | 48000 | 60.0 | 17.0 | 1.88

Project B | 30000 | 48000 | 60.0 | 17.0 | 1.88

Project C | 100000 | 180000 | 80.0 | 12.5 | 2.78

Project C | 100000 | 180000 | 80.0 | 12.5 | 2.78

undefined
undefined

Rank by ROI

按ROI排序

python
ranked = calc.rank_by_roi(investments)
python
ranked = calc.rank_by_roi(investments)

Returns investments sorted by ROI percentage

Returns investments sorted by ROI percentage

undefined
undefined

Sensitivity Analysis

敏感性分析

Variable Sensitivity

变量敏感性分析

python
base_case = {
    "fixed_costs": 50000,
    "price_per_unit": 100,
    "variable_cost_per_unit": 60,
    "units_sold": 2000
}
python
base_case = {
    "fixed_costs": 50000,
    "price_per_unit": 100,
    "variable_cost_per_unit": 60,
    "units_sold": 2000
}

Test impact of price changes

Test impact of price changes

sensitivity = calc.sensitivity_analysis( base_case=base_case, variables={ "price_per_unit": [80, 90, 100, 110, 120], "units_sold": [1500, 1750, 2000, 2250, 2500] } )
sensitivity = calc.sensitivity_analysis( base_case=base_case, variables={ "price_per_unit": [80, 90, 100, 110, 120], "units_sold": [1500, 1750, 2000, 2250, 2500] } )

Returns DataFrame showing profit at each combination

Returns DataFrame showing profit at each combination

undefined
undefined

Scenario Analysis

场景分析

python
scenarios = [
    {"name": "Pessimistic", "units": 1500, "price": 90},
    {"name": "Base", "units": 2000, "price": 100},
    {"name": "Optimistic", "units": 2500, "price": 110}
]
results = calc.scenario_analysis(scenarios)
python
scenarios = [
    {"name": "Pessimistic", "units": 1500, "price": 90},
    {"name": "Base", "units": 2000, "price": 100},
    {"name": "Optimistic", "units": 2500, "price": 110}
]
results = calc.scenario_analysis(scenarios)

Example Workflows

示例工作流

Marketing Campaign Analysis

营销活动分析

python
calc = ROICalculator()
python
calc = ROICalculator()

Analyze Q4 campaigns

Analyze Q4 campaigns

campaigns = [ {"name": "Black Friday Email", "spend": 2000, "revenue": 45000, "cogs": 20000}, {"name": "Holiday Facebook", "spend": 8000, "revenue": 35000, "cogs": 14000}, {"name": "Google Shopping", "spend": 5000, "revenue": 28000, "cogs": 12000} ]
results = calc.campaign_roi(campaigns) print(results.sort_values("roi_percent", ascending=False))
campaigns = [ {"name": "Black Friday Email", "spend": 2000, "revenue": 45000, "cogs": 20000}, {"name": "Holiday Facebook", "spend": 8000, "revenue": 35000, "cogs": 14000}, {"name": "Google Shopping", "spend": 5000, "revenue": 28000, "cogs": 12000} ]
results = calc.campaign_roi(campaigns) print(results.sort_values("roi_percent", ascending=False))

Find best performing campaign

Find best performing campaign

best = results.loc[results["roi_percent"].idxmax()] print(f"Best ROI: {best['name']} at {best['roi_percent']:.1f}%")
undefined
best = results.loc[results["roi_percent"].idxmax()] print(f"Best ROI: {best['name']} at {best['roi_percent']:.1f}%")
undefined

Business Investment Decision

商业投资决策

python
calc = ROICalculator()
python
calc = ROICalculator()

Compare expansion options

Compare expansion options

options = [ {"name": "New Location", "investment": 200000, "annual_return": 45000}, {"name": "Equipment Upgrade", "investment": 75000, "annual_return": 20000}, {"name": "Digital Marketing", "investment": 30000, "annual_return": 12000} ]
for opt in options: payback = calc.payback_period(opt["investment"], opt["annual_return"]) roi = calc.simple_roi(opt["investment"], opt["investment"] + opt["annual_return"] * 5) print(f"{opt['name']}: Payback={payback['payback_years']:.1f}yr, 5yr ROI={roi['roi_percent']:.1f}%")
undefined
options = [ {"name": "New Location", "investment": 200000, "annual_return": 45000}, {"name": "Equipment Upgrade", "investment": 75000, "annual_return": 20000}, {"name": "Digital Marketing", "investment": 30000, "annual_return": 12000} ]
for opt in options: payback = calc.payback_period(opt["investment"], opt["annual_return"]) roi = calc.simple_roi(opt["investment"], opt["investment"] + opt["annual_return"] * 5) print(f"{opt['name']}: Payback={payback['payback_years']:.1f}yr, 5yr ROI={roi['roi_percent']:.1f}%")
undefined

Pricing Strategy Analysis

定价策略分析

python
calc = ROICalculator()
python
calc = ROICalculator()

Break-even at different price points

Break-even at different price points

fixed_costs = 100000 variable_cost = 25
for price in [40, 50, 60, 75, 100]: be = calc.break_even(fixed_costs, price, variable_cost) print(f"Price ${price}: Break-even at {be['break_even_units']:,} units (${be['break_even_revenue']:,.0f})")
undefined
fixed_costs = 100000 variable_cost = 25
for price in [40, 50, 60, 75, 100]: be = calc.break_even(fixed_costs, price, variable_cost) print(f"Price ${price}: Break-even at {be['break_even_units']:,} units (${be['break_even_revenue']:,.0f})")
undefined

Dependencies

依赖项

  • pandas>=2.0.0
  • numpy>=1.24.0
  • pandas>=2.0.0
  • numpy>=1.24.0