
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "_auto_examples/aequilibrae_without_a_model/plot_ipf_without_model.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download__auto_examples_aequilibrae_without_a_model_plot_ipf_without_model.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr__auto_examples_aequilibrae_without_a_model_plot_ipf_without_model.py:


.. _plot_ipf_without_model:

Running IPF without an AequilibraE model
========================================

In this example, we show you how to use AequilibraE's IPF function without a model.
This is a complement to the application in :ref:`example_usage_forecasting`.

Let's consider that you have an OD-matrix, the future production and future attraction values.

*How would your trip distribution matrix using IPF look like?*

The data used in this example comes from Table 5.6 in 
`Ortúzar & Willumsen (2011) <https://www.wiley.com/en-us/Modelling+Transport%2C+4th+Edition-p-9780470760390>`_.

.. GENERATED FROM PYTHON SOURCE LINES 19-23

.. admonition:: References

  * :ref:`all_about_aeq_matrices` 
  * :ref:`validation`

.. GENERATED FROM PYTHON SOURCE LINES 25-30

.. seealso::
    Several functions, methods, classes and modules are used in this example:

    * :func:`aequilibrae.matrix.AequilibraeMatrix`
    * :func:`aequilibrae.distribution.Ipf`

.. GENERATED FROM PYTHON SOURCE LINES 32-43

.. code-block:: Python


    # Imports
    from os.path import join
    from tempfile import gettempdir

    import numpy as np
    import pandas as pd

    from aequilibrae.distribution import Ipf
    from aequilibrae.matrix import AequilibraeMatrix








.. GENERATED FROM PYTHON SOURCE LINES 45-47

.. code-block:: Python

    folder = gettempdir()








.. GENERATED FROM PYTHON SOURCE LINES 48-55

.. code-block:: Python

    matrix = np.array([[5, 50, 100, 200], [50, 5, 100, 300], 
                       [50, 100, 5, 100], [100, 200, 250, 20]], dtype="float64")
    future_prod = np.array([400, 460, 400, 702], dtype="float64")
    future_attr = np.array([260, 400, 500, 802], dtype="float64")

    num_zones = matrix.shape[0]








.. GENERATED FROM PYTHON SOURCE LINES 56-62

.. code-block:: Python

    mtx = AequilibraeMatrix()
    mtx.create_empty(file_name=join(folder, "matrix.aem"), zones=num_zones)
    mtx.index[:] = np.arange(1, num_zones + 1)[:]
    mtx.matrices[:, :, 0] = matrix[:]
    mtx.computational_view()








.. GENERATED FROM PYTHON SOURCE LINES 63-71

.. code-block:: Python

    args = {
        "entries": mtx.index.shape[0],
        "field_names": ["productions", "attractions"],
        "data_types": [np.float64, np.float64],
        "file_path": join(folder, "vectors.aem"),
    }

    vectors = pd.DataFrame({"productions": future_prod, "attractions":future_attr}, index=mtx.index)







.. GENERATED FROM PYTHON SOURCE LINES 72-82

.. code-block:: Python

    args = {
        "matrix": mtx,
        "vectors": vectors,
        "row_field": "productions",
        "column_field": "attractions",
        "nan_as_zero": True,
    }
    fratar = Ipf(**args)
    fratar.fit()








.. GENERATED FROM PYTHON SOURCE LINES 83-85

.. code-block:: Python

    fratar.output.matrix_view





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    array([[  5.19523253,  43.60117896,  97.1907419 , 254.02026689],
           [ 44.70942645,   3.75225496,  83.64095928, 327.90930057],
           [ 76.67583276, 128.70094592,   7.17213428, 187.45277887],
           [133.41950826, 223.94562016, 311.99616454,  32.61765367]])



.. GENERATED FROM PYTHON SOURCE LINES 86-89

.. code-block:: Python

    for line in fratar.report:
        print(line)
    




.. rst-class:: sphx-glr-script-out

 .. code-block:: none

      #####    IPF computation    #####  

    Target convergence criteria: 0.0001
    Maximum iterations: 5000

    Rows/columns: 4
    Total of seed matrix:                   1,635.0000
    Total of target vectors:                1,962.0000

    Iteration,   Convergence
    7   ,   0.0000902085

    Running time: 0.001s





.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.044 seconds)


.. _sphx_glr_download__auto_examples_aequilibrae_without_a_model_plot_ipf_without_model.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_ipf_without_model.ipynb <plot_ipf_without_model.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_ipf_without_model.py <plot_ipf_without_model.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: plot_ipf_without_model.zip <plot_ipf_without_model.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
