06 — Build a model#

Open In Colab

v0.8.0 offers two extension paths without requiring a CGE-Core inheritance hierarchy: functional Python authoring and an experimental deterministic .cge.md specification.

%pip install -q "https://github.com/miraflor/CGE-core/releases/download/v0.8.0/cge_core-0.8.0-py3-none-any.whl"

Parse and compile .cge.md#

from importlib.resources import files
from cge_core.experimental.spec import parse_file, validate_document, compile_document

path = files("cge_core.experimental.spec").joinpath("examples", "two_good_exchange.cge.md")
doc = validate_document(parse_file(path))
model = compile_document(doc)

print("equations:", [eq.name for eq in doc.equations])
print("shockable groups:", [group.names for group in doc.shockables])
print("compiled Pyomo model:", model.name)

Only fenced cge blocks affect computation. Ordinary Markdown prose is documentation and cannot silently supply equations, parameters, or closure choices.

Functional Python authoring#

A model module declares ordinary functions and small metadata sets:

example = '''
def build_model(data):
    ...
    return model

def apply_default_closure(model):
    ...

benchmark_only = {"SAM0"}
shockable = {"tax", "endowment"}
'''
print(example)

Then model_from_module("my_model.py", data=...) wraps that economic model in the same benchmark/scenario lifecycle. See examples/custom_python_model/model.py for a complete small example.