.. comment 
    Visual Predictive Check for Two Compartment |poppk| Model
    =============================================================

    The :ref:`builtin_fit_example` section showed fitting a |pkpd| model to a data set. 

    As shown previous in :ref:`simple_msim_example1`. It is possible to use the fitted |fes| values, i.e the optimised |fx| variables, to generate a :term:`visual predictive check`, often abbreviated to 'VPC'.

    Running the MSim Script
    -----------------------------------

    It is presumed that you have already run the 'builtin_fit_example.pyml' script from :ref:`builtin_fit_example`. 
    If you have then you should have access to the following output folder:-

    .. code-block::
    
        builtin_fit_example.pyml_output/
            builtin_fit_example_msim.pyml

    You need to :ref:`open_a_popy_command_prompt` in the 'msim' sub folder then run the script using:-
        
    .. code-block:: console

        popy run builtin_fit_example_msim.pyml
        
    If you run this script the following .svg file is output:-

    .. code-block::

        builtin_fit_example.pyml_output/
            builtin_fit_example_msim.pyml_output/
                builtin_fit_example_vpc.pyml_output/
                    allOBS_vs_TIME_VPC/
                        vpc.svg

    This graphic should look something like :numref:`fig_builtin_msim_vpc`:-
        
    .. _fig_builtin_msim_vpc:

    .. figure:: /case-studies/quick_start/builtin_tut_example/allOBS_vs_TIME_VPC/vpc.*
        :width: 80%
        :align: center
        
        Visual Predictive Check for Complex |poppk| model.

    Here the y axis is the concentration in the |central| compartment and the x axis is the time since the last dose (:term:`TSLD`). See :ref:`simple_msim_example1` for a more general description of the |vpc| plot shown in :numref:`fig_builtin_msim_vpc`.

    In this case, the :term:`TSLD` values are grouped into 12 equally spaced bins along the x axis. Note you need a minimum number of data points in each bin and there are only 250 data points in this toy example. Hence the small number of bins.

    The blue dots (original data) are mainly shown to give some visual corroboration of the quantiles (solid blue line). In this graph there are only 12 bins and therefore each bin is quite wide, therefore some data points are grouped together inappropriately. This grouping issue is most obvious at the smaller values of TSLD, during the drug uptake period, when the drug is being mainly absorbed into |central| compartment and has not been cleared from the blood plasma yet (see :numref:`fig_builtin_msim_vpc`). Only more data and more bins can really fix the issue.  
        
    .. _syntax_in_builtin_msim_script:
        
    Syntax in the MSim Script
    ------------------------------------------

    For each individual in the original data set, new synthetic data sets are created by sampling new |res| |rx| variables and new measurement noise for all data rows. |ie| The synthetic populations vary due to sampling the |rx| for each individual here:-

    .. code-block:: pyml

        EFFECTS:
            ID: |
                r[KA, CL, V1, Q, V2] ~ mnorm([0,0,0,0,0], f[KA_isv,CL_isv,V1_isv,Q_isv,V2_isv])

    And adding measurement noise here:-

    .. code-block:: pyml

        PREDICTIONS: |
            p[CEN_sim] = s[CENTRAL]/m[V1]
            var = m[ANOISE]**2 + m[PNOISE]**2 * p[CEN_sim]**2
            c[DV_CENTRAL_sim] ~ norm(p[CEN_sim], var)

    This procedure creates a set of N new data sets, which can be compared with the original data set. Where N is defined here:-

    .. code-block:: pyml

        OUTPUT_OPTIONS: 
            n_pop_samples: 100

    You can increase the number of samples, to make the |vpc| more representative of your model. The more complex the |pkpd| model, the more synthetic data samples you will need. 

    As this model has more parameters compared to the :ref:`simple_fit_example`, it may be worth increasing the 'n_pop_samples' and re-running the :ref:`msim_script`. This is left as an exercise for the reader.
    
    



    A :ref:`tut_script` can be used as a theoretical tool to quickly 
    investigate identifiability of |pkpd| models
    because the true |fx| parameters and the structure of the data are known.
    
