FirmsPrepareLoanApplications#

class bamengine.events.credit_market.FirmsPrepareLoanApplications[source]#

Bases: Event

Firms select banks to apply to, sorted by interest rate (ascending).

Also clears settled loans from the previous period before credit matching begins. Loan records are retained through planning and labor phases so that planning-phase events can reference previous-period interest data.

Firms with positive credit demand build a loan application queue by sampling banks and sorting them by interest rate. Firms prefer lower-rate banks to minimize borrowing costs.

Algorithm

For each firm i with \(B_i > 0\) (credit demand):

  1. Sample min(max_H, n_banks) banks randomly

  2. Sort sampled banks by interest rate (ascending)

  3. Store sorted application queue in firm’s buffer

Mathematical Notation

For firm i with \(B_i > 0\):

\[\text{Sample}_i \sim \text{Random}(\{1, ..., K\}, k=\min(H, K), \text{replace}=False)\]

Then sort by rate:

\[\text{Queue}_i = \text{argsort}_{\text{asc}}(r_k \text{ for } k \in \text{Sample}_i)\]

where \(K\) = n_banks, \(H\) = max_H.

Examples

Execute this event:

>>> import bamengine as be
>>> sim = be.Simulation.init(n_firms=100, n_banks=10, seed=42)
>>> # First calculate credit demand
>>> sim.get_event("firms_decide_credit_demand")().execute(sim)
>>> # Then prepare applications
>>> event = sim.get_event("firms_prepare_loan_applications")
>>> event.execute(sim)

Check firms with credit demand:

>>> import numpy as np
>>> needs_credit = sim.bor.credit_demand > 0
>>> needs_credit.sum()
45

Inspect application queue for a firm:

>>> firm_ids = np.where(needs_credit)[0]
>>> if len(firm_ids) > 0:
...     firm_id = firm_ids[0]
...     targets = sim.bor.loan_apps_targets[firm_id]
...     # First 3 bank targets
...     targets[:3]
array([2, 7, 1])

Verify banks are sorted by rate:

>>> if len(firm_ids) > 0:
...     firm_id = firm_ids[0]
...     bank_ids = sim.bor.loan_apps_targets[firm_id, : sim.config.max_H]
...     bank_ids = bank_ids[bank_ids >= 0]  # Exclude -1 (padding)
...     rates = sim.lend.interest_rate[bank_ids]
...     # Check rates are non-decreasing
...     np.all(rates[:-1] <= rates[1:])
True

Notes

This event must execute after BanksDecideInterestRate and FirmsDecideCreditDemand.

Only firms with positive credit demand prepare applications. Self-financed firms (\(B_i = 0\)) are skipped.

Firms sample banks randomly then sort by rate. This means firms may miss the absolute lowest-rate bank if it’s not in their random sample.

See also

BanksDecideInterestRate

Sets rates used for sorting

CreditMarketRound

Processes applications from queue

Borrower

Financial state with loan application queue

bamengine.events._internal.credit_market.firms_prepare_loan_applications

Implementation

execute(sim)[source]#

Execute the event’s logic.

Mutates simulation state in-place. This method must be implemented by all Event subclasses.

Parameters:

sim (Simulation) – The simulation instance containing all state and configuration.

Returns:

All mutations are in-place.

Return type:

None

Examples

Implement execute in a custom event:

>>> from bamengine import event, ops
>>>
>>> @event
... class CustomPricingEvent:
...     def execute(self, sim):
...         prod = sim.get_role("Producer")
...         # Apply 10% markup to all prices
...         new_prices = ops.multiply(prod.price, 1.1)
...         ops.assign(prod.price, new_prices)

Access configuration and RNG:

>>> @event
... class StochasticEvent:
...     def execute(self, sim):
...         shock = sim.config.h_rho
...         random_values = sim.rng.uniform(0, shock, size=sim.n_firms)
...         # Use random_values in calculations

Notes

The execute method receives full Simulation access, including: - All roles: sim.get_role(“RoleName”) or sim.prod, sim.wrk, etc. - Configuration: sim.config - RNG: sim.rng - Economy state: sim.ec

__init__()#
name = 'firms_prepare_loan_applications'#