Skip to content

Commit

Permalink
Also added the remaining documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
nils-braun committed Nov 30, 2018
1 parent 30b0595 commit aae4475
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
# html_theme = 'sphinx_rtd_theme'
html_theme = 'sphinx_rtd_theme'


# Theme options are theme-specific and customize the look and feel of a theme
Expand Down
18 changes: 17 additions & 1 deletion docs/pages/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,30 @@ Advanced functionality
----------------------

Actually, the return values of the uncertainty calculations are not only "bare numbers with uncertainties".
They have a bunch of additional functionality:
They are instances of `BootstrapResult` and have a bunch of additional functionality:

.. py:currentmodule:: uncertain_panda
.. autoclass:: BootstrapResult
:members:
:undoc-members:


Plotting
--------

The package also adds another function to the basic pandas objects (``Series``, ``DataFrame``) to plot
values with uncertainties correctly.
You can call it with

.. code-block:: python
df.plot_with_uncertainty()
It is 1:1 equivalent to the normal call to ``plot``, so you can put in the same argument

.. code-block:: python
df.plot_with_uncertainty(kind="bar", label="Something", figsize=(20, 10))
.. _`uncertainties`: https://pythonhosted.org/uncertainties/
34 changes: 34 additions & 0 deletions uncertain_panda/uncertainties/bootstrap_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,34 @@ def _apply_bs(x):


class BootstrapResult(Variable):
"""
Result of any calculation performed with the `unc` wrapper.
It is an instance of :class:`uncertainties.core.Variable`, so
it behaves like a normal number with uncertainties.
"""
def __init__(self, nominal_value, bootstrap):
self._bootstrap = bootstrap

super().__init__(nominal_value, self.bs().std())

def bs(self):
"""
Return the full data sample of bootstrapped results.
Usually used for visualisations, such as::
df["var"].unc.mean().bs().plot(kind="hist")
"""
return self._bootstrap

def ci(self, a=ONE_SIGMA / 100, b=None):
"""
Return the confidence interval between ``a`` and ``b``.
This is the pair of values [left, right], so that
a fraction ``a`` of the bootstrapped results is left of ``left``
and ``b`` of the results is right of ``right``.
If you only give one parameter, the symmetric interval with ``b = a`` is returned.
:return: a :class:`pd.Series` with the columns ``value``, ``left`` and ``right``.
"""
if b is None:
left = (1 - a) / 2
right = (1 + a) / 2
Expand All @@ -38,19 +57,34 @@ def ci(self, a=ONE_SIGMA / 100, b=None):
value=self.nominal_value))

def strip(self):
"""
This result still includes the full sample of bootstrapped results.
So it can be quite heavy (in terms of memory).
The function returns an uncertainty number without the bootstrapped histogram.
"""
return Variable(self.nominal_value, self.std_dev)

def compare_lt(self, rhs):
"""How many of the values are < than ``rhs``?"""
return (self.bs() < _apply_bs(rhs)).mean()

def compare_le(self, rhs):
"""How many of the values are <= than ``rhs``?"""
return (self.bs() <= _apply_bs(rhs)).mean()

def compare_gt(self, rhs):
"""How many of the values are > than ``rhs``?"""
return (self.bs() > _apply_bs(rhs)).mean()

def compare_ge(self, rhs):
"""How many of the values are >= than ``rhs``?"""
return (self.bs() >= _apply_bs(rhs)).mean()

def prob(self, value):
"""
Return the probability to have a resut equal or greater than ``value``.
If we assume the bootstrapped results are a probability density function,
this is equivalent to the p-value.
"""
return self.compare_gt(value)

0 comments on commit aae4475

Please sign in to comment.