stats.horizonhistory module

class stats.horizonhistory.HorizonHistories[source]

Bases: dict

all()[source]

Returns a list of the unique sets of horizon distances recorded in the histories.

compress(threshold=0.03, remove_deviations=False, deviation_percent=0.5, verbose=False)[source]

Remove distances that are non-zero and differ fractionally from both neighbours by less than the selected threshold.

Also allows removal of uncharacteristic horizon distance values.

copy()[source]

Override of dict’s .copy() that (a) returns the correct type and (b) makes copies of the HorizonHistories’ contents.

classmethod from_xml(xml, name)[source]
functional_integral(lohi, w=<function HorizonHistories.<lambda>>)[source]

Given the function f(x) = self.getdict(x), compute

int_{lo}^{hi} w(f(x)) dx

The arguments are the lo and hi bounds and the functional w(f). The default functional is w(f) = max(f.values()).

Example:

>>> x = HorizonHistories({
...     "H1": NearestLeafTree([(100., 0.), (150., 2.), (200., 0.)]),
...     "L1": NearestLeafTree([(100., 0.), (150., 20.), (200., 0.)]),
... })
>>> x.functional_integral((130., 170.))
800.0
>>> x.functional_integral((100., 150.))
500.0
>>> x.functional_integral((300., 500.))
0.0
>>> x["H1"].functional_integral((100., 150.), w = lambda f: f**3)
200.0
functional_integral_dict(*args, **kwargs)[source]

Return a dictionary of the result of invoking .functional_integral() on each of the histories. args and kwargs are passed to the .functional_integral() method of the histories objects, see their documentation for more information.

getdict(x)[source]
maxkey()[source]

Return the maximum key stored in the trees.

minkey()[source]

Return the minimum key stored in the trees.

randhorizons()[source]

Generator yielding a sequence of random horizon distance dictionaries chosen by drawing random times uniformly distributed between the lowest and highest times recorded in the history and returning the dictionary of horizon distances for each of those times.

to_xml(name)[source]
weighted_mean_dict(*args, **kwargs)[source]

Return a dictionary of the result of invoking .weighted_mean() on each of the histories. args and kwargs are passed to the .weighted_mean() method of the histories objects, see their documentation for more information.

class stats.horizonhistory.NearestLeafTree(items=())[source]

Bases: object

A simple binary tree in which look-ups return the value of the closest leaf. Only float objects are supported for the keys and values. Look-ups raise KeyError if the tree is empty.

Example:

>>> x = NearestLeafTree()
>>> bool(x)
False
>>> x[100.0] = 120.
>>> bool(x)
True
>>> x[104.0] = 100.
>>> x[102.0] = 110.
>>> x[90.]
120.0
>>> x[100.999]
120.0
>>> x[101.001]
110.0
>>> x[200.]
100.0
>>> del x[104]
>>> x[200.]
110.0
>>> x.keys()
[100.0, 102.0]
>>> 102 in x
True
>>> 103 in x
False
>>> import sys
>>> x.to_xml(u"H1").write(sys.stdout) 
<Array Type="real_8" Name="H1:nearestleaftree:array">
        <Dim>2</Dim>
        <Dim>2</Dim>
        <Stream Type="Local" Delimiter=" ">
                100 102
                120 110
        </Stream>
</Array>
classmethod from_xml(xml, name)[source]
functional_integral(lohi, w=<function NearestLeafTree.<lambda>>)[source]

Given the function f(x) = self[x], compute

int_{lo}^{hi} w(f(x)) dx

The arguments are the lo and hi bounds and the functional w(f). The default functional is w(f) = f.

Example:

>>> x = NearestLeafTree([(100., 0.), (150., 2.), (200., 0.)])
>>> x.functional_integral((130., 170.))
80.0
>>> x.functional_integral((100., 150.))
50.0
>>> x.functional_integral((300., 500.))
0.0
>>> x.functional_integral((100., 150.), lambda f: f**3)
200.0
items()[source]
keys()[source]
max()[source]

Return the maximum value stored in the tree. This is O(n).

maxkey()[source]

Return the maximum key stored in the tree. This is O(1).

min()[source]

Return the minimum value stored in the tree. This is O(n).

minkey()[source]

Return the minimum key stored in the tree. This is O(1).

to_xml(name)[source]
values()[source]
weighted_mean(lohi, weight=<function NearestLeafTree.<lambda>>)[source]

Given the function f(x) = self[x], compute

int_{lo}^{hi} w(f(x)) f(x) dx

int_{lo}^{hi} w(f(x)) dx

where w(y) is a weighting function. The default weight function is w(y) = 1.

If the numerator is identically 0 and the denominator is also 0 then a value of 0 is returned rather than raising a divide-by-0 error.

Example:

>>> x = NearestLeafTree([(100., 0.), (150., 2.), (200., 0.)])
>>> x.weighted_mean((130., 170.))
2.0
>>> x.weighted_mean((100., 150.))
1.0
>>> x.weighted_mean((300., 500.))
0.0
>>> x.weighted_mean((100., 150.), lambda x: x**3)
2.0