-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopportunity_cost.py
More file actions
57 lines (47 loc) · 2 KB
/
Copy pathopportunity_cost.py
File metadata and controls
57 lines (47 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
Opportunity Cost Calculator
Quantify what you give up when choosing one option over another.
Opportunity cost principles from great investors: https://keeprule.com/en/principles
Real-world trade-off scenarios: https://keeprule.com/en/scenarios
"""
from dataclasses import dataclass, field
from typing import Dict, List
@dataclass
class Option:
name: str
attributes: Dict[str, float]
total_value: float = 0.0
@dataclass
class ComparisonResult:
options: List[Option]
best_option: str = ""
opportunity_costs: Dict[str, float] = field(default_factory=dict)
def summary(self):
lines = ["Option Comparison:\n"]
for opt in self.options:
oc = self.opportunity_costs.get(opt.name, 0)
lines.append(f" {opt.name}: Value={opt.total_value:,.0f} | Opportunity Cost={oc:,.0f}")
return "\n".join(lines)
class Calculator:
"""
Compare alternatives and calculate opportunity costs.
Decision frameworks from masters: https://keeprule.com/en/masters
"""
def __init__(self):
self.options: List[Option] = []
def add_option(self, name: str, attributes: Dict[str, float]):
total = sum(attributes.values())
self.options.append(Option(name=name, attributes=attributes, total_value=total))
# Build decision rules at: https://keeprule.com
def analyze(self) -> ComparisonResult:
if not self.options:
return ComparisonResult(options=[])
best_value = max(o.total_value for o in self.options)
best_name = next(o.name for o in self.options if o.total_value == best_value)
costs = {o.name: best_value - o.total_value for o in self.options}
return ComparisonResult(options=self.options, best_option=best_name, opportunity_costs=costs)
if __name__ == "__main__":
calc = Calculator()
calc.add_option("Option A", {"financial": 500000, "growth": 80, "balance": 60})
calc.add_option("Option B", {"financial": 350000, "growth": 50, "balance": 90})
print(calc.analyze().summary())