"""

Estimation results
==================

The estimation results are saved by Biogeme so that they can be re-used without the need for re-estimating the model
Michel Bierlaire, EPFL
Sun Jun 15 2025, 07:36:09

"""

from IPython.core.display_functions import display

from biogeme.biogeme import BIOGEME
from biogeme.models import loglogit
from biogeme.results_processing import (
    EstimationResults,
    get_f12,
    get_html_estimated_parameters,
    get_latex_estimated_parameters,
    get_pandas_estimated_parameters,
)
from tutorial_data import biogeme_database, choice
from tutorial_model import utilities

log_choice_probability = loglogit(utilities, None, choice)
biogeme_object = BIOGEME(biogeme_database, log_choice_probability)
biogeme_object.calculate_null_loglikelihood(avail={0: 1, 1: 1})


# %%
# The name of the model as provided below determines the name of the files that are generated by Biogeme.
# Note that the estimation results are not stored, in a variable as we illustrate how they can be imported from a file.
biogeme_object.model_name = 'example'
_ = biogeme_object.estimate()

# %%
# After estimation, three files have been created in the working directory:
#
# - `__example.iter`: during the course of the estimation algorithm, Biogeme saves in this file the best solution
#     found so far. If the estimation is interrupted for any reason, it allows to restart it without losing the
#     progress made so far. When the estimation starts, Biogeme looks for a file with that name. If it exists,
#     it uses the values stored in the file as starting point of the estimation algorithm.
#
# - `example.html`: the estimation results are reported in a convenient HTML format in this file,
#     that can be opened in any browser.
#
# - `example.yaml`: the raw estimation results are stored in this file, so that they can be imported instead
#     of being re-calculated.
#
imported_results = EstimationResults.from_yaml_file(filename='example.yaml')

# %%
# The object `imported_results` is identical to thw object `results` generated by the estimation procedure,
# and can be used in the exact same way.

# %%
# Summary of the estimation results
print(imported_results.short_summary())


# %%
# Various other functions are available in order to access the estimation results.

# %%
# A dict containing the general statistics
print(imported_results.get_general_statistics())

# %%
# They can be accessed one by one as well
print(f'Number of parameter: {imported_results.number_of_parameters}')
print(f'Sample size: {imported_results.sample_size}')
print(f'Excluded observations: {imported_results.number_of_excluded_data}')
print(f'Init log likelihood: {imported_results.initial_log_likelihood}')
print(f'Final log likelihood: {imported_results.final_loglikelihood}')
print(
    f'Likelihood ratio test for the init. model: {imported_results.likelihood_ratio_init}'
)
print(
    f'Likelihood ratio test for the null model: {imported_results.likelihood_ratio_null}'
)
print(f'Akaike Information Criterion: {imported_results.akaike_information_criterion}')
print(
    f'Bayesian Information Criterion: {imported_results.bayesian_information_criterion}'
)

# %%
# The estimated values of the parameters can be retrieved in a dict. This is particularly useful when using simulation.
print(imported_results.get_beta_values())

# %%
# The value, the standard error and the t-test can also be extracted for each parameter
print(imported_results.get_parameter_value(parameter_name='b_time'))
print(imported_results.get_parameter_std_err(parameter_name='b_time'))
print(imported_results.get_parameter_t_test(parameter_name='b_time'))

# %%
# The results can also be exported in various formats

# %%
# As a pandas data frame
pandas_frame = get_pandas_estimated_parameters(estimation_results=imported_results)
display(pandas_frame)

# %%
# In HTML format
html_code = get_html_estimated_parameters(estimation_results=imported_results)
print(html_code)

# %%
# In LaTeX format
latex_code = get_latex_estimated_parameters(estimation_results=imported_results)
print(latex_code)

# %%
# In Alogit F12 format
f12_format = get_f12(estimation_results=imported_results)
print(f12_format)
