Monte Carlo Process Simulator
Overview
Credit Monte Carlo computes the risk due to default for a set of instruments by
Simulating
the default of those instruments a large number of times and measuring the resulting distribution.
Simulation Runner
The monte carlo in this section utilizes the following simulation runner.
def simulate(iterations, items, simulate_item, dates = None, process=None, context=None):
if process == None: process = lambda x,y:x
for i in range(iterations):
citems = [item.copy() for item in items]
iteration = []
contexts = []
idates = [{}]
idates = dates if dates != None else [{}]
for date in idates:
date_simulation = []
if context!= None:
context1 = context(date, contexts)
if context1 != None: contexts.append(context1)
for index, item in enumerate(citems):
sim = simulate_item(item, contexts, date)
if sim != None and isinstance(date, str): date_simulation.append({"simulation":sim, "item":item, "date":date})
elif sim != None: date_simulation.append({"simulation":sim, "item":item})
pass
iteration.append(process(date_simulation, contexts))
pass
yield iteration
pass
pass
The runner iterates through the number of iterations. For each date in the list of dates
(this doesnt have to actually be a date, it can be any id for the given period),
and for each item in the inputted items (for the credit application, each item is a loan record)
it calls the simulate_itemmethod. The simulate_item method should run a single simulation for the inputted item
and return a result. If the result is not None, then it is collected in an array, and that array is passed
to the next call to simulate_item for that item.
Simulate One Period
When the simulate_item method is called, it is passed four arguments.
- item - the record of the item (loan) being simulated
- simulation - for each item, a list of simulation results is retained. It collects the
results of each call to simulate_item. This list is passed to each call to simulate_item. This means
that each simulation can maintain a history for each item, so that the simulation
can have path dependencies
- date - the id of the period being simulated
- contexts - if a context function is passed into the simulation, the for each date in the
iteration, the context function is called and the results saved in a contextts list. This
list is then passed to simulate_item method and can be used in the simulation.
Contexts are simulated objects that may affect an outcome, but is not specific to a particular
loan. So for instance, you can simulate the interest rate curve, and this could a context that
is passed into the simulate_item method for each loan, which may determine its probability of default
based on the current curve.
Process Function
After each simulation, the process function is called to process the simulation before adding the results to
the simulation. This is just a utility function that is used to extract the pertinent information that needs to
be saved from each simulation, without having to change the function doing the simulating.