Skip to content

Commit

Permalink
Introduce random_state arguments.
Browse files Browse the repository at this point in the history
  • Loading branch information
asnelt committed Jun 12, 2021
1 parent 9d8970f commit a81c4ca
Show file tree
Hide file tree
Showing 13 changed files with 256 additions and 211 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ The full documentation for the mixedvines package is available at
Requirements
------------

The package is compatible with Python 2.7 and 3.x and additionaly requires
The package requires Python 3.4 or greater and additionaly requires
`NumPy and SciPy
<http://www.scipy.org/install.html>`_.

Expand Down Expand Up @@ -158,7 +158,7 @@ References
License
-------

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

This file is part of the mixedvines package.

Expand Down
11 changes: 5 additions & 6 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
#
# mixedvines documentation build configuration file, created by
# sphinx-quickstart on Mon Apr 24 17:23:24 2017.
# mixedvines documentation build configuration file.
#
# This file is execfile()d with the current directory set to its
# containing dir.
Expand Down Expand Up @@ -68,17 +67,17 @@ def __getattr__(cls, name):

# General information about the project.
project = u'mixedvines'
copyright = u'2017-2019 Arno Onken'
copyright = u'2017-2019, 2021 Arno Onken'
author = u'Arno Onken'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = u'1.2'
version = u'1.3'
# The full version, including alpha/beta/rc tags.
release = u'1.2.2'
release = u'1.3.0'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down Expand Up @@ -152,7 +151,7 @@ def __getattr__(cls, name):
# The name for this set of Sphinx documents.
# "<project> v<release> documentation" by default.
#
# html_title = u'mixedvines v1.2.2'
# html_title = u'mixedvines v1.3.0'

# A shorter title for the navigation bar. Default is the same as html_title.
#
Expand Down
6 changes: 0 additions & 6 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
.. mixedvines documentation master file, created by
sphinx-quickstart on Mon Apr 24 17:23:24 2017.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
Welcome to mixedvines's documentation!
======================================

Expand All @@ -26,4 +21,3 @@ Indices and tables
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

2 changes: 1 addition & 1 deletion docs/requirements.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ Requirements
============


The package is compatible with Python 2.7 and 3.x and additionaly requires
The package requires Python 3.4 or greater and additionaly requires
`NumPy and SciPy
<http://www.scipy.org/install.html>`_.
3 changes: 1 addition & 2 deletions mixedvines/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2017-2019 Arno Onken
# Copyright (C) 2017-2019, 2021 Arno Onken
#
# This file is part of the mixedvines package.
#
Expand All @@ -15,7 +15,6 @@
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import absolute_import
from . import marginal
from . import copula
from . import mixedvine
Expand Down
58 changes: 20 additions & 38 deletions mixedvines/copula.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2017-2019 Arno Onken
# Copyright (C) 2017-2019, 2021 Arno Onken
#
# This file is part of the mixedvines package.
#
Expand All @@ -18,23 +18,14 @@
'''
This module implements copula distributions.
'''
from __future__ import division
import sys
import abc
from scipy.optimize import minimize
from scipy.stats import norm, uniform, mvn
from scipy.stats import norm, uniform, multivariate_normal
import numpy as np


# Ensure abstract base class compatibility
if sys.version_info[0] == 3 and sys.version_info[1] >= 4 \
or sys.version_info[0] > 3:
ABC = abc.ABC
else:
ABC = abc.ABCMeta('ABC', (), {})


class Copula(ABC):
class Copula(abc.ABC):
'''
This abstract class represents a copula.
Expand Down Expand Up @@ -64,11 +55,11 @@ class Copula(ABC):
Log of the cumulative distribution function.
cdf(samples)
Cumulative distribution function.
ccdf(samples, axis=1)
ccdf(samples, axis)
Conditional cumulative distribution function.
ppcf(samples, axis=1)
ppcf(samples, axis)
Inverse of the conditional cumulative distribution function.
rvs(size=1)
rvs(size=1, random_state)
Generate random variates.
estimate_theta(samples)
Estimates the `theta` parameters from the given samples.
Expand Down Expand Up @@ -309,7 +300,7 @@ def __axis_wrapper(self, fun, samples, axis):
Function to be called with `samples` as argument.
samples : array_like
n-by-2 matrix of samples where n is the number of samples.
axis : integer
axis : int
The axis to condition the cumulative distribution function on.
Returns
Expand Down Expand Up @@ -368,7 +359,7 @@ def ccdf(self, samples, axis=1):
----------
samples : array_like
n-by-2 matrix of samples where n is the number of samples.
axis : integer, optional
axis : int, optional
The axis to condition the cumulative distribution function on.
(Default: 1)
Expand Down Expand Up @@ -409,7 +400,7 @@ def ppcf(self, samples, axis=1):
----------
samples : array_like
n-by-2 matrix of samples where n is the number of samples.
axis : integer, optional
axis : int, optional
The axis to condition the cumulative distribution function on.
(Default: 1)
Expand All @@ -421,21 +412,27 @@ def ppcf(self, samples, axis=1):
'''
return self.__axis_wrapper(self._ppcf, samples, axis)

def rvs(self, size=1):
def rvs(self, size=1, random_state=None):
'''
Generates random variates from the copula.
Parameters
----------
size : integer, optional
size : int, optional
The number of samples to generate. (Default: 1)
random_state : {None, int, RandomState, Generator}, optional
The random state to use for random variate generation. `None`
corresponds to the `RandomState` singleton. For an int, a new
`RandomState` is generated and seeded. For a `RandomState` or
`Generator`, the object is used. (Default: `None`)
Returns
-------
samples : array_like
n-by-2 matrix of samples where n is the number of samples.
'''
samples = np.stack((uniform.rvs(size=size), uniform.rvs(size=size)),
samples = np.stack((uniform.rvs(size=size, random_state=random_state),
uniform.rvs(size=size, random_state=random_state)),
axis=1)
samples[:, 0] = self.ppcf(samples)
return samples
Expand Down Expand Up @@ -565,24 +562,9 @@ def _logpdf(self, samples):
return vals

def _logcdf(self, samples):
lower = np.full(2, -np.inf)
upper = norm.ppf(samples)
limit_flags = np.zeros(2)
if upper.shape[0] > 0:

def func1d(upper1d):
'''
Calculates the multivariate normal cumulative distribution
function of a single sample.
'''
return mvn.mvndst(lower, upper1d, limit_flags, self.theta)[1]

vals = np.apply_along_axis(func1d, -1, upper)
else:
vals = np.empty((0, ))
old_settings = np.seterr(divide='ignore')
vals = np.log(vals)
np.seterr(**old_settings)
cov = [[1.0, self.theta], [self.theta, 1.0]]
vals = multivariate_normal.logcdf(upper, None, cov)
vals[np.any(samples == 0.0, axis=1)] = -np.inf
vals[samples[:, 0] == 1.0] = np.log(samples[samples[:, 0] == 1.0, 1])
vals[samples[:, 1] == 1.0] = np.log(samples[samples[:, 1] == 1.0, 0])
Expand Down
16 changes: 10 additions & 6 deletions mixedvines/marginal.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2017-2019 Arno Onken
# Copyright (C) 2017-2019, 2021 Arno Onken
#
# This file is part of the mixedvines package.
#
Expand All @@ -19,7 +19,6 @@
This module implements univariate marginal distribution that are either
continuous or discrete.
'''
from __future__ import division
from scipy.stats import rv_continuous, norm, gamma, poisson, binom, nbinom
import numpy as np

Expand Down Expand Up @@ -54,7 +53,7 @@ class Marginal(object):
Cumulative distribution function.
ppf(samples)
Inverse of the cumulative distribution function.
rvs(size=1)
rvs(size, random_state)
Generate random variates.
fit(samples, is_continuous)
Fit a distribution to samples.
Expand Down Expand Up @@ -147,21 +146,26 @@ def ppf(self, samples):
'''
return self.rv_mixed.ppf(samples)

def rvs(self, size=1):
def rvs(self, size=1, random_state=None):
'''
Generates random variates from the distribution.
Parameters
----------
size : integer, optional
size : int, optional
The number of samples to generate. (Default: 1)
random_state : {None, int, RandomState, Generator}, optional
The random state to use for random variate generation. `None`
corresponds to the `RandomState` singleton. For an int, a new
`RandomState` is generated and seeded. For a `RandomState` or
`Generator`, the object is used. (Default: `None`)
Returns
-------
array_like
Array of samples.
'''
return self.rv_mixed.rvs(size)
return self.rv_mixed.rvs(size, random_state=random_state)

@staticmethod
def fit(samples, is_continuous):
Expand Down
Loading

0 comments on commit a81c4ca

Please sign in to comment.