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) # doctest: +NORMALIZE_WHITESPACE
<Array Type="real_8" Name="H1:nearestleaftree:array">
<Dim>2</Dim>
<Dim>2</Dim>
<Stream Type="Local" Delimiter=" ">
100 102
120 110
</Stream>
</Array>
Definition at line 80 of file horizonhistory.py.
| def stats.horizonhistory.NearestLeafTree.__init__ |
( |
|
self, |
|
|
|
items = () |
|
) |
| |
Initialize a NearestLeafTree.
Example:
>>> x = NearestLeafTree()
>>> x = NearestLeafTree([(100., 120.), (104., 100.), (102., 110.)])
>>> y = {100.: 120., 104.: 100., 102.: 100.}
>>> x = NearestLeafTree(y.items())
Definition at line 124 of file horizonhistory.py.
| def stats.horizonhistory.NearestLeafTree.__delitem__ |
( |
|
self, |
|
|
|
x |
|
) |
| |
Example:
>>> x = NearestLeafTree([(100., 0.), (150., 1.), (200., 0.)])
>>> del x[150.]
>>> x
NearestLeafTree([(100, 0), (200, 0)])
>>> del x[190.]
Traceback (most recent call last):
...
IndexError: 190.0
>>> del x[:]
>>> x
NearestLeafTree([])
Definition at line 200 of file horizonhistory.py.
| def stats.horizonhistory.NearestLeafTree.__setitem__ |
( |
|
self, |
|
|
|
x, |
|
|
|
val |
|
) |
| |
Example:
>>> x = NearestLeafTree()
>>> x[100.:200.] = 0.
>>> x[150.] = 1.
>>> x
NearestLeafTree([(100, 0), (150, 1), (200, 0)])
>>> x[210:] = 5.
>>> x[:90] = 5.
>>> x
NearestLeafTree([(90, 5), (100, 0), (150, 1), (200, 0), (210, 5)])
Definition at line 141 of file horizonhistory.py.
| def stats.horizonhistory.NearestLeafTree.functional_integral |
( |
|
self, |
|
|
|
lohi, |
|
|
|
w = lambda f: f |
|
) |
| |
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
Definition at line 316 of file horizonhistory.py.
| def stats.horizonhistory.NearestLeafTree.weighted_mean |
( |
|
self, |
|
|
|
lohi, |
|
|
|
weight = lambda y: 1. |
|
) |
| |
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
Definition at line 376 of file horizonhistory.py.