Skip to content

Commit

Permalink
Fix densities for new element order and adapt documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Arno Onken committed Jun 9, 2017
1 parent 7d94e7e commit 84cd513
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 33 deletions.
13 changes: 1 addition & 12 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ continuous whereas the second element is discrete:

.. code-block:: python
import numpy as np
is_continuous = np.full((3), True, dtype=bool)
is_continuous[1] = False
is_continuous = [True, False, True]
To fit a mixed vine to the samples:

Expand All @@ -83,15 +81,6 @@ calculate their density and estimate the distribution entropy in units of bits:
logpdf = vine.logpdf(samples)
(entropy, standard_error_mean) = vine.entropy(sem_tol=1e-2)
Note that for the canonical vine, the order of elements is important. Elements
should be sorted according to the importance of their dependencies to other
elements where elements with important dependencies to many other elements
should come first. A heuristic way to select the order of elements is to
calculate Kendall's tau between all element pairs
(see ``scipy.stats.kendalltau``), to obtain a score for each element by summing
the tau's of the pairs the element occurs in and to sort elements in descending
order according to their scores.

To manually construct and visualize a simple mixed vine model:

.. code-block:: python
Expand Down
30 changes: 16 additions & 14 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ continuous whereas the second element is discrete:

.. code-block:: python
import numpy as np
is_continuous = np.full((3), True, dtype=bool)
is_continuous[1] = False
is_continuous = [True, False, True]
To fit a mixed vine to the samples:

Expand All @@ -21,20 +19,24 @@ To fit a mixed vine to the samples:
from 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:
``vine`` is now a ``MixedVine`` object. Note that for the canonical vine, the
order of elements is important. Elements should be sorted according to the
importance of their dependencies to other elements where elements with important
dependencies to many other elements should come first. A heuristic way to
select the order of elements is to calculate Kendall's tau between all element
pairs, to obtain a score for each element by summing the taus of the pairs the
element occurs in and to sort elements in descending order according to their
scores. This is what the ``MixedVine.fit`` method does internally by default to
construct an improved canonical vine tree. This internal sorting is used to
construct the vine tree only, so the order of elements is not changed in a user
visible way. To prevent this internal sorting, set the ``keep_order`` argument
to ``True``.

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)
Note that for the canonical vine, the order of elements is important. Elements
should be sorted according to the importance of their dependencies to other
elements where elements with important dependencies to many other elements
should come first. A heuristic way to select the order of elements is to
calculate Kendall's tau between all element pairs
(see ``scipy.stats.kendalltau``), to obtain a score for each element by summing
the tau's of the pairs the element occurs in and to sort elements in descending
order according to their scores.
12 changes: 5 additions & 7 deletions mixedvines/mixedvine.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ class MixedVine(object):
Attributes
----------
vine_type : string
Type of the vine tree.
root : VineLayer
The root layer of the vine tree.
Expand All @@ -56,7 +54,7 @@ class MixedVine(object):
Sets a particular marginal distribution in the mixed vine tree.
set_copula(layer_index, copula_index, copula)
Sets a particular pair copula in the mixed vine tree.
fit(samples, is_continuous, vine_type, trunc_level, do_refine)
fit(samples, is_continuous, trunc_level, do_refine, keep_order)
Fits the mixed vine to the given samples.
'''

Expand Down Expand Up @@ -228,7 +226,7 @@ def _marginal_densities(self, samples):
old_settings = np.seterr(divide='ignore')
logp[:, k] = np.log(np.maximum(0, cdfp[:, k] - cdfm[:, k]))
np.seterr(**old_settings)
logpdf = logp[:, 0]
logpdf = logp[:, self.output_layer.input_indices[0][0]]
dout = {'logpdf': logpdf, 'logp': logp, 'cdfp': cdfp, 'cdfm': cdfm,
'is_continuous': is_continuous}
return dout
Expand Down Expand Up @@ -783,7 +781,7 @@ def _heuristic_element_order(samples):
Finds an order of elements that heuristically facilitates vine
modelling. For this purpose, Kendall's tau is calculated between
samples of pairs of elements and elements are scored according to the
sum of absolute Kendall's tau of pairs the elements appear in.
sum of absolute Kendall's taus of pairs the elements appear in.
Parameters
----------
Expand All @@ -802,8 +800,8 @@ def _heuristic_element_order(samples):
for i in range(1, dim):
for j in range(i):
tau, _ = kendalltau(samples[:, i], samples[:, j])
score[i] += tau
score[j] += tau
score[i] += np.abs(tau)
score[j] += np.abs(tau)
# Get order indices for descending score
order = score.argsort()[::-1]
return order
Expand Down

0 comments on commit 84cd513

Please sign in to comment.