I am writing a toy microeconomics library. As of now, I have 2 classes representing supply and demand curves. Both of these classes are basically equal in everything except some methods/fields have “supply” in their name for the supply curve, and “demand” for the demand curve (e.g. price_at_demand
method, given a demand for a good returns its price according to the curve).
I was thinking of implementing a SupplyDemandBase
class to use as the parent class… but I want the other classes to retain their field and method names for semantics.
This is the proposed base class:
@dataclass
class _SupplyDemandBase:
location: Number
scale: Number
quant_offset: Number = 0
price_offset: Number = 0
curve_type: AllowedCurveTypes = "linear"
def __post_init__(self):
self._curve = (
Linear(slope=self.scale, intercept=self.location)
if self.curve_type == "linear"
else Power1(exponent=self.scale, coefficient=self.location)
)
@classmethod
def from_two_points(
cls,
p1: tuple[Number, Number],
p2: tuple[Number, Number],
curve_type: AllowedCurveTypes = "linear",
) -> Self:
...
def quantity_at_price(self, price: Number) -> Number:
...
def price_at_quantity(self, demand: Number) -> Number:
...
def elasticity(
self, quantity: Optional[Number] = None, price: Optional[Number] = None
) -> Number:
"""Find the elasticity of the curve using point price formula."""
...
def displace(self, quant_offset=0, price_offset=0):
"""Displaces the curve along X or Y axis.
This can be the result of technological advancements or new substitutive
products entering the market.
"""
...
Both Demand and Supply classes have these methods and are implemented the same (except for “quantity” being called either “demand” or “supply”). It’s not necessary that they are dataclasses (used the decorator for convenience at first).
I want both classes to be separated from the users POV, and have the different naming of fields/methods so the classes directly map to economic theory, for ease of use. Also, some functions in the library could check that exactly one demand and one supply curve are passed (e.g. computing equilibrium prices).
Any way of doing this easily? I’m using Python 3.12.