Skip to content

economy

cybersyn.economy

Dataclasses to save the economy and the planned economy returned by the optimizer. The Economy class is implemented using Pydantic to perform certain checks in the data, which will normally come from a database, making it prone to mistakes when loading the data.

Economy

Bases: BaseModel

Dataclass with validations that stores the whole economy's information.

periods: int property

Number of products in the economy.

products: int property

Number of products in the economy.

sectors: int property

Number of products in the economy.

__post_init__()

Run after initial validation. Validates that the shapes of the matrices are compatible with each other (same number of products and sectors).

Source code in cybersyn/economy.py
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def __post_init__(self) -> None:
    """Run after initial validation. Validates that the shapes of the
    matrices are compatible with each other (same number of products
    and sectors).
    """
    self.validate_matrix_shape(
        self.use_domestic[0], self.use_import[0], shape=(self.products, self.sectors)
    )
    self.validate_matrix_shape(self.depreciation[0], shape=(self.products, self.products))
    self.validate_matrix_shape(
        self.prices_import[0],
        self.prices_export[0],
        shape=(self.products,),
    )
    self.validate_matrix_shape(self.worked_hours[0], shape=(self.sectors,))

    if self.product_names is not None and len(self.product_names) != self.products:
        raise ValueError(f"\nList of PRODUCT names must be of length {self.products}.\n\n")

    if self.sector_names is not None and len(self.product_names) != self.products:
        raise ValueError(f"\nList of SECTOR names must be of length {self.sectors}.\n\n")

equal_shapes(matrices, info)

Assert that all the inputed matrices have the same shape.

Source code in cybersyn/economy.py
54
55
56
57
58
59
60
61
@field_validator(*ECONOMY_FIELDS)
def equal_shapes(cls, matrices: MatrixList, info: FieldValidationInfo) -> MatrixList:
    """Assert that all the inputed matrices have the same shape."""
    shapes = [matrix.shape for matrix in matrices]
    if not all([shape == shapes[0] for shape in shapes]):
        raise ShapesNotEqualError
    logging.info(f"{info.field_name} has shape {shapes[0]}")
    return matrices

validate_matrix_shape(*matrices, shape) staticmethod

Assert that all the inputed matrices have the same shape.

Source code in cybersyn/economy.py
93
94
95
96
97
98
@staticmethod
def validate_matrix_shape(*matrices: MatrixList, shape: tuple[int, int]) -> None:
    """Assert that all the inputed matrices have the same shape."""
    for matrix in matrices:
        if matrix.shape != shape:
            raise ShapeError(shape, matrix.shape)

PlannedEconomy(activity=list(), production=list(), surplus=list(), total_import=list(), export_deficit=list(), worked_hours=list()) dataclass

Dataclass that stores the whole planned economy.

Parameters:

Name Type Description Default
activity list[NDArray]

list with the planned activity for all sectors in each period.

list()
production list[NDArray]

list with the planned production for all product in each period.

list()
surplus list[NDArray]

The surplus production at the end of each period.

list()
total_import list[NDArray]

list of total imports in each period.

list()
export_deficit list[float]

list export deficit at the end of each period.

list()
worked_hours list[float]

list of total worked hours in each period.

list()

TargetEconomy

Bases: BaseModel

Dataclass with validations that stores the whole economy's information.

periods: int property

Number of products in the economy.

products: int property

Number of products in the economy.

equal_shapes(matrices, info)

Assert that all the inputed matrices have the same size.

Source code in cybersyn/economy.py
133
134
135
136
137
138
139
140
@field_validator("domestic", "exports", "imports")
def equal_shapes(cls, matrices: MatrixList, info: FieldValidationInfo) -> MatrixList:
    """Assert that all the inputed matrices have the same size."""
    sizes = [matrix.size for matrix in matrices]
    if not all([size == sizes[0] for size in sizes]):
        raise ShapesNotEqualError
    logging.info(f"{info.field_name} has shape {sizes[0]}")
    return matrices