mixedvines


Namemixedvines JSON
Version 1.3.4 PyPI version JSON
download
home_page
SummaryCanonical vine copula trees with mixed marginals
upload_time2023-12-20 22:39:23
maintainer
docs_urlNone
author
requires_python>=3.10
license
keywords copula mixed vine continuous discrete entropy
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            =============================
mixedvines Package for Python
=============================

Package for canonical vine copula trees with mixed continuous and discrete
marginals.  If you use this software for publication, please cite
[ONKEN2016]_.


Description
-----------

This package contains a complete framework based on canonical vine copulas for
modeling multivariate data that are partly discrete and partly continuous.
The resulting multivariate distributions are flexible with rich dependence
structures and marginals.

For continuous marginals, implementations of the normal and the gamma
distributions are provided.  For discrete marginals, Poisson, binomial and
negative binomial distributions are provided.  As bivariate copula building
blocks, the Gaussian, Frank and Clayton families as well as rotation
transformed families are provided.  Additional marginal and pair-copula
distributions can be added easily.

The package includes methods for sampling, likelihood calculation and
inference, all of which have quadratic complexity.  These procedures are
combined to estimate entropy by means of Monte Carlo integration.

Please see [ONKEN2016]_ for a more detailed description of the framework.


Documentation
-------------

The full documentation for the mixedvines package is available at
`Read the Docs
<http://mixedvines.readthedocs.io/>`_.


Requirements
------------

The package requires Python 3.10 or greater and additionally requires
`NumPy and SciPy
<http://www.scipy.org/install.html>`_.


Installation
------------

To install the mixedvines package, run::

    pip install mixedvines


Usage
-----

Suppose that data are given in a NumPy array ``samples`` with shape
``(n, d)``, where ``n`` is the number of samples and ``d`` is the number of
elements per sample.  First, specify which of the elements are continuous.
If, for instance, the distribution has three elements and the first and last
elements are continuous whereas the second element is discrete:

.. code-block:: python

    is_continuous = [True, False, True]

To fit a mixed vine to the samples:

.. code-block:: python

    from mixedvines.mixedvine import MixedVine
    vine = MixedVine.fit(samples, is_continuous)

``vine`` is now a ``MixedVine`` object.  To draw samples from the
distribution, calculate their density and estimate the distribution entropy in
units of bits:

.. code-block:: python

    samples = vine.rvs(size=100)
    logpdf = vine.logpdf(samples)
    entropy, standard_error_mean = vine.entropy(sem_tol=1e-2)

To manually construct and visualize a simple mixed vine model:

.. code-block:: python

    from scipy.stats import norm, gamma, poisson
    import numpy as np
    from mixedvines.copula import GaussianCopula, ClaytonCopula, FrankCopula
    from mixedvines.mixedvine import MixedVine
    import matplotlib.pyplot as plt
    import itertools
    # Manually construct mixed vine
    dim = 3  # Dimension
    vine = MixedVine(dim)
    # Specify marginals
    vine.set_marginal(0, norm(0, 1))
    vine.set_marginal(1, poisson(5))
    vine.set_marginal(2, gamma(2, 0, 4))
    # Specify pair-copulas
    vine.set_copula(1, 0, GaussianCopula(0.5))
    vine.set_copula(1, 1, FrankCopula(4))
    vine.set_copula(2, 0, ClaytonCopula(5))
    # Calculate probability density function on lattice
    bnds = np.empty((3), dtype=object)
    bnds[0] = [-3, 3]
    bnds[1] = [0, 15]
    bnds[2] = [0.5, 25]
    x0, x1, x2 = np.mgrid[bnds[0][0]:bnds[0][1]:0.05, bnds[1][0]:bnds[1][1],
                          bnds[2][0]:bnds[2][1]:0.1]
    points = np.array([x0.ravel(), x1.ravel(), x2.ravel()]).T
    pdf = vine.pdf(points)
    pdf = np.reshape(pdf, x1.shape)
    # Generate random variates
    size = 100
    samples = vine.rvs(size)
    # Visualize 2d marginals and samples
    comb = list(itertools.combinations(range(dim), 2))
    for i, cmb in enumerate(comb):
        # Sum over all axes not in cmb
        cmb_inv = tuple(set(range(dim)) - set(cmb))
        margin = np.sum(pdf, axis=cmb_inv).T
        plt.subplot(2, len(comb), i + 1)
        plt.imshow(margin, aspect='auto', interpolation='none', cmap='hot',
                   origin='lower', extent=[bnds[cmb[0]][0], bnds[cmb[0]][1],
                                           bnds[cmb[1]][0], bnds[cmb[1]][1]])
        plt.ylabel('$x_' + str(cmb[1]) + '$')
        plt.subplot(2, len(comb), len(comb) + i + 1)
        plt.scatter(samples[:, cmb[0]], samples[:, cmb[1]], s=1)
        plt.xlim(bnds[cmb[0]][0], bnds[cmb[0]][1])
        plt.ylim(bnds[cmb[1]][0], bnds[cmb[1]][1])
        plt.xlabel('$x_' + str(cmb[0]) + '$')
        plt.ylabel('$x_' + str(cmb[1]) + '$')
    plt.tight_layout()
    plt.show()

This code shows the 2d marginals and 100 samples of a 3d mixed vine.


Source code
-----------

The source code of the mixedvines package is hosted on
`GitHub
<https://github.com/asnelt/mixedvines/>`_.


References
----------

.. [ONKEN2016] A. Onken and S. Panzeri (2016).  Mixed vine copulas as joint
   models of spike counts and local field potentials.  In D. D. Lee,
   M. Sugiyama, U. V. Luxburg, I. Guyon and R. Garnett, editors, Advances in
   Neural Information Processing Systems 29 (NIPS 2016), pages 1325-1333.


License
-------

Copyright (C) 2017-2019, 2021-2023 Arno Onken

This file is part of the mixedvines package.

The mixedvines package is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by the Free
Software Foundation, version 3.

The mixedvines package is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.

You should have received a copy of the GNU General Public License along with
this program; if not, see <http://www.gnu.org/licenses/>.


            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "mixedvines",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": "",
    "keywords": "copula,mixed,vine,continuous,discrete,entropy",
    "author": "",
    "author_email": "Arno Onken <asnelt@asnelt.org>",
    "download_url": "https://files.pythonhosted.org/packages/0e/84/073d95fbf51c86722ff2461c5ff01791a9dc0cd6d9448bab436be1fe1ccd/mixedvines-1.3.4.tar.gz",
    "platform": null,
    "description": "=============================\nmixedvines Package for Python\n=============================\n\nPackage for canonical vine copula trees with mixed continuous and discrete\nmarginals.  If you use this software for publication, please cite\n[ONKEN2016]_.\n\n\nDescription\n-----------\n\nThis package contains a complete framework based on canonical vine copulas for\nmodeling multivariate data that are partly discrete and partly continuous.\nThe resulting multivariate distributions are flexible with rich dependence\nstructures and marginals.\n\nFor continuous marginals, implementations of the normal and the gamma\ndistributions are provided.  For discrete marginals, Poisson, binomial and\nnegative binomial distributions are provided.  As bivariate copula building\nblocks, the Gaussian, Frank and Clayton families as well as rotation\ntransformed families are provided.  Additional marginal and pair-copula\ndistributions can be added easily.\n\nThe package includes methods for sampling, likelihood calculation and\ninference, all of which have quadratic complexity.  These procedures are\ncombined to estimate entropy by means of Monte Carlo integration.\n\nPlease see [ONKEN2016]_ for a more detailed description of the framework.\n\n\nDocumentation\n-------------\n\nThe full documentation for the mixedvines package is available at\n`Read the Docs\n<http://mixedvines.readthedocs.io/>`_.\n\n\nRequirements\n------------\n\nThe package requires Python 3.10 or greater and additionally requires\n`NumPy and SciPy\n<http://www.scipy.org/install.html>`_.\n\n\nInstallation\n------------\n\nTo install the mixedvines package, run::\n\n    pip install mixedvines\n\n\nUsage\n-----\n\nSuppose that data are given in a NumPy array ``samples`` with shape\n``(n, d)``, where ``n`` is the number of samples and ``d`` is the number of\nelements per sample.  First, specify which of the elements are continuous.\nIf, for instance, the distribution has three elements and the first and last\nelements are continuous whereas the second element is discrete:\n\n.. code-block:: python\n\n    is_continuous = [True, False, True]\n\nTo fit a mixed vine to the samples:\n\n.. code-block:: python\n\n    from mixedvines.mixedvine import MixedVine\n    vine = MixedVine.fit(samples, is_continuous)\n\n``vine`` is now a ``MixedVine`` object.  To draw samples from the\ndistribution, calculate their density and estimate the distribution entropy in\nunits of bits:\n\n.. code-block:: python\n\n    samples = vine.rvs(size=100)\n    logpdf = vine.logpdf(samples)\n    entropy, standard_error_mean = vine.entropy(sem_tol=1e-2)\n\nTo manually construct and visualize a simple mixed vine model:\n\n.. code-block:: python\n\n    from scipy.stats import norm, gamma, poisson\n    import numpy as np\n    from mixedvines.copula import GaussianCopula, ClaytonCopula, FrankCopula\n    from mixedvines.mixedvine import MixedVine\n    import matplotlib.pyplot as plt\n    import itertools\n    # Manually construct mixed vine\n    dim = 3  # Dimension\n    vine = MixedVine(dim)\n    # Specify marginals\n    vine.set_marginal(0, norm(0, 1))\n    vine.set_marginal(1, poisson(5))\n    vine.set_marginal(2, gamma(2, 0, 4))\n    # Specify pair-copulas\n    vine.set_copula(1, 0, GaussianCopula(0.5))\n    vine.set_copula(1, 1, FrankCopula(4))\n    vine.set_copula(2, 0, ClaytonCopula(5))\n    # Calculate probability density function on lattice\n    bnds = np.empty((3), dtype=object)\n    bnds[0] = [-3, 3]\n    bnds[1] = [0, 15]\n    bnds[2] = [0.5, 25]\n    x0, x1, x2 = np.mgrid[bnds[0][0]:bnds[0][1]:0.05, bnds[1][0]:bnds[1][1],\n                          bnds[2][0]:bnds[2][1]:0.1]\n    points = np.array([x0.ravel(), x1.ravel(), x2.ravel()]).T\n    pdf = vine.pdf(points)\n    pdf = np.reshape(pdf, x1.shape)\n    # Generate random variates\n    size = 100\n    samples = vine.rvs(size)\n    # Visualize 2d marginals and samples\n    comb = list(itertools.combinations(range(dim), 2))\n    for i, cmb in enumerate(comb):\n        # Sum over all axes not in cmb\n        cmb_inv = tuple(set(range(dim)) - set(cmb))\n        margin = np.sum(pdf, axis=cmb_inv).T\n        plt.subplot(2, len(comb), i + 1)\n        plt.imshow(margin, aspect='auto', interpolation='none', cmap='hot',\n                   origin='lower', extent=[bnds[cmb[0]][0], bnds[cmb[0]][1],\n                                           bnds[cmb[1]][0], bnds[cmb[1]][1]])\n        plt.ylabel('$x_' + str(cmb[1]) + '$')\n        plt.subplot(2, len(comb), len(comb) + i + 1)\n        plt.scatter(samples[:, cmb[0]], samples[:, cmb[1]], s=1)\n        plt.xlim(bnds[cmb[0]][0], bnds[cmb[0]][1])\n        plt.ylim(bnds[cmb[1]][0], bnds[cmb[1]][1])\n        plt.xlabel('$x_' + str(cmb[0]) + '$')\n        plt.ylabel('$x_' + str(cmb[1]) + '$')\n    plt.tight_layout()\n    plt.show()\n\nThis code shows the 2d marginals and 100 samples of a 3d mixed vine.\n\n\nSource code\n-----------\n\nThe source code of the mixedvines package is hosted on\n`GitHub\n<https://github.com/asnelt/mixedvines/>`_.\n\n\nReferences\n----------\n\n.. [ONKEN2016] A. Onken and S. Panzeri (2016).  Mixed vine copulas as joint\n   models of spike counts and local field potentials.  In D. D. Lee,\n   M. Sugiyama, U. V. Luxburg, I. Guyon and R. Garnett, editors, Advances in\n   Neural Information Processing Systems 29 (NIPS 2016), pages 1325-1333.\n\n\nLicense\n-------\n\nCopyright (C) 2017-2019, 2021-2023 Arno Onken\n\nThis file is part of the mixedvines package.\n\nThe mixedvines package is free software: you can redistribute it and/or modify\nit under the terms of the GNU General Public License as published by the Free\nSoftware Foundation, version 3.\n\nThe mixedvines package is distributed in the hope that it will be useful, but\nWITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\nFITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more\ndetails.\n\nYou should have received a copy of the GNU General Public License along with\nthis program; if not, see <http://www.gnu.org/licenses/>.\n\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Canonical vine copula trees with mixed marginals",
    "version": "1.3.4",
    "project_urls": {
        "Bug Tracker": "https://github.com/asnelt/mixedvines/issues",
        "Documentation": "https://mixedvines.readthedocs.io",
        "Homepage": "https://github.com/asnelt/mixedvines"
    },
    "split_keywords": [
        "copula",
        "mixed",
        "vine",
        "continuous",
        "discrete",
        "entropy"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "914574b72e646b45ae33b8946af56d359e50fe2f0cb3efd1c1f47622091d1535",
                "md5": "9153db7a1e75f9e7197c5872acdef58c",
                "sha256": "5aa99006a781accf58485c77a9b8e6f62bdab43d18cc92461827f93556dd209a"
            },
            "downloads": -1,
            "filename": "mixedvines-1.3.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "9153db7a1e75f9e7197c5872acdef58c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 30242,
            "upload_time": "2023-12-20T22:39:21",
            "upload_time_iso_8601": "2023-12-20T22:39:21.776481Z",
            "url": "https://files.pythonhosted.org/packages/91/45/74b72e646b45ae33b8946af56d359e50fe2f0cb3efd1c1f47622091d1535/mixedvines-1.3.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0e84073d95fbf51c86722ff2461c5ff01791a9dc0cd6d9448bab436be1fe1ccd",
                "md5": "bd6edac75d531a46dca90b8bd372f072",
                "sha256": "96c3fdbf443340332123d00b0c7cb46ee453f39c90b65a8b53ed2ec3f90e4675"
            },
            "downloads": -1,
            "filename": "mixedvines-1.3.4.tar.gz",
            "has_sig": false,
            "md5_digest": "bd6edac75d531a46dca90b8bd372f072",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 27871,
            "upload_time": "2023-12-20T22:39:23",
            "upload_time_iso_8601": "2023-12-20T22:39:23.771632Z",
            "url": "https://files.pythonhosted.org/packages/0e/84/073d95fbf51c86722ff2461c5ff01791a9dc0cd6d9448bab436be1fe1ccd/mixedvines-1.3.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-12-20 22:39:23",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "asnelt",
    "github_project": "mixedvines",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "mixedvines"
}
        
Elapsed time: 0.26158s