<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Estimating Non-Mandatory Tour Frequency
This notebook illustrates how to re-estimate a single model component for ActivitySim.  This process 
includes running ActivitySim in estimation mode to read household travel survey files and write out
the estimation data bundles used in this notebook.  To review how to do so, please visit the other
notebooks in this directory.
Load libraries
Step1:  We'll work in our test directory, where ActivitySim has saved the estimation data bundles.
Step2:  Load data and prep model for estimation
Step3:  This component actually has a distinct choice model for each person type, so
instead of a single model there's a dict of models.
Step4:  Review data loaded from the EDB
We can review the data loaded as well, similarly there is seperate data 
for each person type.
Coefficients
Step5:  Utility specification
Step6:  Chooser data
Step7:  Estimate
With the model setup for estimation, the next step is to estimate the model coefficients.  Make sure to use a sufficiently large enough household sample and set of zones to avoid an over-specified model, which does not have a numerically stable likelihood maximizing solution.  Larch has a built-in estimation methods including BHHH, and also offers access to more advanced general purpose non-linear optimizers in the scipy package, including SLSQP, which allows for bounds and constraints on parameters.  BHHH is the default and typically runs faster, but does not follow constraints on parameters.
Step8:  Estimated coefficients
Step9:  Output Estimation Results
Step10:  Write the model estimation report, including coefficient t-statistic and log likelihood
Step11:  Next Steps
The final step is to either manually or automatically copy the *_coefficients_revised.csv file to the configs folder, rename it to *_coefficients.csv, and run ActivitySim in simulation mode.
<ASSISTANT_TASK:>
Python Code:
import os
import larch  # !conda install larch -c conda-forge # for estimation
import pandas as pd
Explanation: Estimating Non-Mandatory Tour Frequency
This notebook illustrates how to re-estimate a single model component for ActivitySim.  This process 
includes running ActivitySim in estimation mode to read household travel survey files and write out
the estimation data bundles used in this notebook.  To review how to do so, please visit the other
notebooks in this directory.
Load libraries
End of explanation
os.chdir('test')
Explanation: We'll work in our test directory, where ActivitySim has saved the estimation data bundles.
End of explanation
modelname = "nonmand_tour_freq"
from activitysim.estimation.larch import component_model
model, data = component_model(modelname, return_data=True)
Explanation: Load data and prep model for estimation
End of explanation
type(model)
model.keys()
Explanation: This component actually has a distinct choice model for each person type, so
instead of a single model there's a dict of models.
End of explanation
data.coefficients['PTYPE_FULL']
Explanation: Review data loaded from the EDB
We can review the data loaded as well, similarly there is seperate data 
for each person type.
Coefficients
End of explanation
data.spec['PTYPE_FULL']
Explanation: Utility specification
End of explanation
data.chooser_data['PTYPE_FULL']
Explanation: Chooser data
End of explanation
for k, m in model.items():
    m.estimate(method='SLSQP')
Explanation: Estimate
With the model setup for estimation, the next step is to estimate the model coefficients.  Make sure to use a sufficiently large enough household sample and set of zones to avoid an over-specified model, which does not have a numerically stable likelihood maximizing solution.  Larch has a built-in estimation methods including BHHH, and also offers access to more advanced general purpose non-linear optimizers in the scipy package, including SLSQP, which allows for bounds and constraints on parameters.  BHHH is the default and typically runs faster, but does not follow constraints on parameters.
End of explanation
model['PTYPE_FULL'].parameter_summary()
Explanation: Estimated coefficients
End of explanation
from activitysim.estimation.larch import update_coefficients
for k, m in model.items():
    result_dir = data.edb_directory/k/"estimated"
    update_coefficients(
        m, data.coefficients[k], result_dir,
        output_file=f"{modelname}_{k}_coefficients_revised.csv",
    );
Explanation: Output Estimation Results
End of explanation
for k, m in model.items():
    result_dir = data.edb_directory/k/"estimated"
    m.to_xlsx(
        result_dir/f"{modelname}_{k}_model_estimation.xlsx", 
        data_statistics=False,
    )
Explanation: Write the model estimation report, including coefficient t-statistic and log likelihood
End of explanation
result_dir = data.edb_directory/'PTYPE_FULL'/"estimated"
pd.read_csv(result_dir/f"{modelname}_PTYPE_FULL_coefficients_revised.csv")
Explanation: Next Steps
The final step is to either manually or automatically copy the *_coefficients_revised.csv file to the configs folder, rename it to *_coefficients.csv, and run ActivitySim in simulation mode.
End of explanation
<END_TASK>