Retrospective Analysis¶
In this example, we will demonstrate some analysis that is reading back results of prior completed simulation run. This is possible because PassengerSim can be configured to write out quite a bit of data to a SQLite database while running. Much of this output is optional, as writing out intermediate results can create very large database files and impose a significant runtime penalty on the simulation, but having the ability to do so is advantageous for research and development purposes.
import passengersim as pax
pax.versions()
passengersim 0.18.1 passengersim.core 0.18.1
We can load in SummaryTables
objects directly from the database created
during a simulation run (assuming it was saved to disk). For this demo,
we have created two small such databases by running two different models
with on-disk database files, and then discarding the results (for now).
We can then load summary tables of results from these two simulations
by using the SummaryTables.from_sqlite
constructor. This command will
run queries against the database to reconstruct the same summary table
objects we would have gotten as the output from running the simulation.
simple = pax.SummaryTables.from_sqlite("simple.sqlite", additional="*")
untrunc = pax.SummaryTables.from_sqlite("untruncated.sqlite", additional="*")
from passengersim.contrast import Contrast
comps = Contrast(
Simple=simple, Untruncated=untrunc
)
All the same visualizations available in our prior demonstrations are also available for use with the pre-computed results. Since we are not actually running the simulation again, this analysis can even be done by users who don't have (or need) access to the PassengerSim core simulation code.
comps.fig_carrier_revenues()
comps.fig_carrier_load_factors()
comps.fig_fare_class_mix()
comps.fig_bookings_by_timeframe(by_carrier="AL1", by_class=True, source_labels=True)
The results of the simulation are stored in database tables. We can write
queries using typical SQLite syntax against the database directly if we like.
For convenience, the SQLite database connection used to populate the summary
tables is available as the cnx
attribute of the SummaryTables object, and
we can use its dataframe
method to run arbitrary queries and get the results
as a pandas DataFrame.
simple.cnx.dataframe("""
SELECT
sample, auth, sold, forecast_mean, forecast_stdev
FROM
leg_bucket_detail
WHERE
flt_no = 101
AND rrd = 21
AND name = 'Y2'
AND sample >= 100
LIMIT 10
""")
sample | auth | sold | forecast_mean | forecast_stdev | |
---|---|---|---|---|---|
0 | 100 | 53 | 2 | 6.769231 | 2.518852 |
1 | 101 | 53 | 2 | 6.500000 | 2.745906 |
2 | 102 | 14 | 6 | 6.346154 | 2.770448 |
3 | 103 | 14 | 5 | 6.307692 | 2.782362 |
4 | 104 | 29 | 6 | 6.307692 | 2.782362 |
5 | 105 | 29 | 3 | 6.384615 | 2.772391 |
6 | 106 | 15 | 1 | 6.192308 | 2.828699 |
7 | 107 | 44 | 3 | 6.461538 | 2.745626 |
8 | 108 | 45 | 2 | 6.461538 | 2.745626 |
9 | 109 | 40 | 2 | 6.615385 | 2.772391 |