.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/swissmetro/plot_b27_monte_carlo_diagnostic.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_swissmetro_plot_b27_monte_carlo_diagnostic.py: .. _plot_b27_monte_carlo_diagnostic: 27. Post-estimation Monte Carlo draw-stability diagnostic ========================================================= This example estimates a small mixed logit model and then runs the post-estimation Monte Carlo draw-stability diagnostic. The diagnostic keeps the estimated parameters fixed, evaluates the objective and its gradient with fresh draw designs, and writes a separate YAML checkpoint and Markdown report. The estimation is performed only when no saved result is available. The diagnostic can therefore be interrupted and resumed without re-estimating the model. Michel Bierlaire, EPFL .. GENERATED FROM PYTHON SOURCE LINES 18-22 .. code-block:: Python import shutil from pathlib import Path .. GENERATED FROM PYTHON SOURCE LINES 23-24 See the data processing script: :ref:`swissmetro_data`. .. GENERATED FROM PYTHON SOURCE LINES 24-46 .. code-block:: Python from swissmetro_data import ( CAR_AV_SP, CAR_CO_SCALED, CAR_TT_SCALED, CHOICE, SM_AV, SM_COST_SCALED, SM_TT_SCALED, TRAIN_AV_SP, TRAIN_COST_SCALED, TRAIN_TT_SCALED, database, ) import biogeme.biogeme_logging as blog from biogeme.biogeme import BIOGEME from biogeme.expressions import Beta, Draws, MonteCarlo, log from biogeme.models import logit logger = blog.get_screen_logger(level=blog.INFO) logger.info('Example plot_b27_monte_carlo_diagnostic.py') .. GENERATED FROM PYTHON SOURCE LINES 47-48 Parameters of the mixed logit model. .. GENERATED FROM PYTHON SOURCE LINES 48-58 .. code-block:: Python asc_car = Beta('asc_car', 0, None, None, 0) asc_train = Beta('asc_train', 0, None, None, 0) asc_sm = Beta('asc_sm', 0, None, None, 1) b_cost = Beta('b_cost', 0, None, None, 0) b_time = Beta('b_time', 0, None, None, 0) b_time_s = Beta('b_time_s', 1, None, None, 0) # The random coefficient is integrated by Monte Carlo simulation. b_time_rnd = b_time + b_time_s * Draws('b_time_rnd', 'NORMAL') .. GENERATED FROM PYTHON SOURCE LINES 59-60 Utilities and availability conditions. .. GENERATED FROM PYTHON SOURCE LINES 60-69 .. code-block:: Python v_train = asc_train + b_time_rnd * TRAIN_TT_SCALED + b_cost * TRAIN_COST_SCALED v_swissmetro = asc_sm + b_time_rnd * SM_TT_SCALED + b_cost * SM_COST_SCALED v_car = asc_car + b_time_rnd * CAR_TT_SCALED + b_cost * CAR_CO_SCALED utilities = {1: v_train, 2: v_swissmetro, 3: v_car} availability = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP} conditional_probability = logit(utilities, availability, CHOICE) log_probability = log(MonteCarlo(conditional_probability)) .. GENERATED FROM PYTHON SOURCE LINES 70-73 The diagnostic is deliberately small enough for a documentation example. Its default draw schedule is replaced here by 0.5R, R, and 2R, with one fresh design per level and a five-minute time budget. .. GENERATED FROM PYTHON SOURCE LINES 73-93 .. code-block:: Python the_biogeme = BIOGEME( database, log_probability, user_notes=( 'Post-estimation Monte Carlo draw-stability diagnostic for a mixed ' 'logit model using the Swissmetro data.' ), number_of_draws=2_000, seed=1223, calculating_second_derivatives='never', save_iterations=False, generate_html=False, monte_carlo_diagnostic_auto=False, monte_carlo_diagnostic_draw_factors='0.5,1.0,2.0', monte_carlo_diagnostic_replications=1, monte_carlo_diagnostic_time_budget=300, monte_carlo_diagnostic_max_draws=4_000, ) the_biogeme.model_name = 'b27_monte_carlo' .. GENERATED FROM PYTHON SOURCE LINES 94-97 Load the archived result when available. Otherwise, estimate once and save the normal estimation result in the current directory. This makes the example work both from a clean checkout and from the JED archive. .. GENERATED FROM PYTHON SOURCE LINES 97-113 .. code-block:: Python saved_result = Path('saved_results') / f'{the_biogeme.model_name}.yaml' estimation_file = ( saved_result if saved_result.is_file() else Path(f'{the_biogeme.model_name}.yaml') ) results = the_biogeme.estimate_or_load(yaml_file_name=str(estimation_file)) # JED archives root-level diagnostic files in ``saved_results``. Restore an # archived checkpoint before running so an interrupted diagnostic resumes in a # fresh isolated working directory instead of starting from its first level. saved_diagnostic = Path('saved_results') / 'b27_monte_carlo_diagnostic.yaml' diagnostic_checkpoint = Path('b27_monte_carlo_diagnostic.yaml') if saved_diagnostic.is_file() and not diagnostic_checkpoint.is_file(): shutil.copy2(saved_diagnostic, diagnostic_checkpoint) .. GENERATED FROM PYTHON SOURCE LINES 114-116 Run the post-estimation diagnostic. It never re-estimates the model and writes b27_monte_carlo_diagnostic.yaml and b27_monte_carlo_diagnostic.md. .. GENERATED FROM PYTHON SOURCE LINES 116-127 .. code-block:: Python diagnostic = the_biogeme.check_monte_carlo_stability( estimation_results=results, output_directory='.', basename='b27', ) print(f'Execution status: {diagnostic.execution_status}') print(f'Diagnostic conclusion: {diagnostic.diagnostic_conclusion}') print(f'Recommendation: {diagnostic.recommendation}') print(f'Raw diagnostic results: {diagnostic.yaml_file}') print(f'Diagnostic report: {diagnostic.markdown_file}') .. _sphx_glr_download_auto_examples_swissmetro_plot_b27_monte_carlo_diagnostic.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b27_monte_carlo_diagnostic.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b27_monte_carlo_diagnostic.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b27_monte_carlo_diagnostic.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_