Skip to content

Commit

Permalink
Add rcParams (#615)
Browse files Browse the repository at this point in the history
* add rcparams

* add rcparams

* lint

* add example
  • Loading branch information
aloctavodia authored Jan 4, 2025
1 parent 3bc6793 commit 52fd62f
Show file tree
Hide file tree
Showing 12 changed files with 836 additions and 49 deletions.
1 change: 1 addition & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ space like in the previous examples. In all these example the prior predictive d
examples/Direct_elicitation_in_1D
examples/Direct_elicitation_in_ND
examples/predictive_elicitation
examples/Global_settings
361 changes: 361 additions & 0 deletions docs/examples/Global_settings.ipynb

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions preliz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
"""
from os import path as os_path

from matplotlib import rcParams
from matplotlib import rcParams as mpl_rcParams
from matplotlib import style

from .distributions import *
from .predictive import *
from .unidimensional import *
from .multidimensional import *
from .internal.rcparams import rc_context, rcParams


__version__ = "0.13.0"


# Allow legend outside plot in maxent to be included when saving a figure
# We may want to make this more explicit by having preliz.rcParams
rcParams["savefig.bbox"] = "tight"
# Allow legend outside plot in maxent to be included in the saved figure
mpl_rcParams["savefig.bbox"] = "tight"


# add PreliZ's styles to matplotlib's styles
Expand All @@ -28,4 +28,4 @@
style.core.reload_library()

# clean namespace
del os_path, rcParams, _preliz_style_path
del os_path, mpl_rcParams, _preliz_style_path
39 changes: 23 additions & 16 deletions preliz/distributions/continuous_multivariate.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def plot_pdf(
self,
marginals=True,
pointinterval=False,
interval="hdi",
interval=None,
levels=None,
support="full",
legend="title",
Expand All @@ -114,8 +114,9 @@ def plot_pdf(
Whether to include a plot of the quantiles. Defaults to False. If True the default is to
plot the median and two interquantile ranges.
interval : str
Type of interval. Available options are highest density interval `"hdi"` (default),
Type of interval. Available options are highest density interval `"hdi"`,
equal tailed interval `"eti"` or intervals defined by arbitrary `"quantiles"`.
Defaults to the value in rcParams["stats.ci_kind"].
levels : list
Mass of the intervals. For hdi or eti the number of elements should be 2 or 1.
For quantiles the number of elements should be 5, 3, 1 or 0
Expand All @@ -142,7 +143,7 @@ def plot_pdf(
def plot_cdf(
self,
pointinterval=False,
interval="hdi",
interval=None,
levels=None,
support="full",
legend="title",
Expand All @@ -158,8 +159,9 @@ def plot_cdf(
Whether to include a plot of the quantiles. Defaults to False. If True the default is to
plot the median and two interquantile ranges.
interval : str
Type of interval. Available options are highest density interval `"hdi"` (default),
Type of interval. Available options are highest density interval `"hdi"`,
equal tailed interval `"eti"` or intervals defined by arbitrary `"quantiles"`.
Defaults to the value in rcParams["stats.ci_kind"].
levels : list
Mass of the intervals. For hdi or eti the number of elements should be 2 or 1.
For quantiles the number of elements should be 5, 3, 1 or 0
Expand All @@ -186,7 +188,7 @@ def plot_cdf(
def plot_ppf(
self,
pointinterval=False,
interval="hdi",
interval=None,
levels=None,
legend="title",
figsize=None,
Expand All @@ -201,8 +203,9 @@ def plot_ppf(
Whether to include a plot of the quantiles. Defaults to False. If True the default is to
plot the median and two interquantile ranges.
interval : str
Type of interval. Available options are highest density interval `"hdi"` (default),
Type of interval. Available options are highest density interval `"hdi"`,
equal tailed interval `"eti"` or intervals defined by arbitrary `"quantiles"`.
Defaults to the value in rcParams["stats.ci_kind"].
levels : list
Mass of the intervals. For hdi or eti the number of elements should be 2 or 1.
For quantiles the number of elements should be 5, 3, 1 or 0
Expand All @@ -228,7 +231,7 @@ def plot_interactive(
kind="pdf",
xy_lim="both",
pointinterval=True,
interval="hdi",
interval=None,
levels=None,
figsize=None,
):
Expand All @@ -249,7 +252,7 @@ def plot_interactive(
Whether to include a plot of the quantiles. Defaults to False.
If `True` the default is to plot the median and two inter-quantiles ranges.
interval : str
Type of interval. Available options are the highest density interval `"hdi"` (default),
Type of interval. Available options are the highest density interval `"hdi"`,
equal tailed interval `"eti"` or intervals defined by arbitrary `"quantiles"`.
levels : list
Mass of the intervals. For hdi or eti the number of elements should be 2 or 1.
Expand Down Expand Up @@ -424,7 +427,7 @@ def plot_pdf(
self,
marginals=True,
pointinterval=False,
interval="hdi",
interval=None,
levels=None,
support="full",
legend="title",
Expand All @@ -444,8 +447,9 @@ def plot_pdf(
Whether to include a plot of the quantiles. Defaults to False. If True the default is to
plot the median and two interquantiles ranges.
interval : str
Type of interval. Available options are highest density interval `"hdi"` (default),
Type of interval. Available options are highest density interval `"hdi"`,
equal tailed interval `"eti"` or intervals defined by arbitrary `"quantiles"`.
Defaults to the value in rcParams["stats.ci_kind"].
levels : list
Mass of the intervals. For hdi or eti the number of elements should be 2 or 1.
For quantiles the number of elements should be 5, 3, 1 or 0
Expand All @@ -472,7 +476,7 @@ def plot_pdf(
def plot_cdf(
self,
pointinterval=False,
interval="hdi",
interval=None,
levels=None,
support="full",
legend="title",
Expand All @@ -488,8 +492,9 @@ def plot_cdf(
Whether to include a plot of the quantiles. Defaults to False. If True the default is to
plot the median and two interquantiles ranges.
interval : str
Type of interval. Available options are highest density interval `"hdi"` (default),
Type of interval. Available options are highest density interval `"hdi"`,
equal tailed interval `"eti"` or intervals defined by arbitrary `"quantiles"`.
Defaults to the value in rcParams["stats.ci_kind"].
levels : list
Mass of the intervals. For hdi or eti the number of elements should be 2 or 1.
For quantiles the number of elements should be 5, 3, 1 or 0
Expand All @@ -516,7 +521,7 @@ def plot_cdf(
def plot_ppf(
self,
pointinterval=False,
interval="hdi",
interval=None,
levels=None,
legend="title",
figsize=None,
Expand All @@ -531,8 +536,9 @@ def plot_ppf(
Whether to include a plot of the quantiles. Defaults to False. If True the default is to
plot the median and two interquantiles ranges.
interval : str
Type of interval. Available options are highest density interval `"hdi"` (default),
Type of interval. Available options are highest density interval `"hdi"`,
equal tailed interval `"eti"` or intervals defined by arbitrary `"quantiles"`.
Defaults to the value in rcParams["stats.ci_kind"].
levels : list
Mass of the intervals. For hdi or eti the number of elements should be 2 or 1.
For quantiles the number of elements should be 5, 3, 1 or 0
Expand All @@ -558,7 +564,7 @@ def plot_interactive(
kind="pdf",
xy_lim="both",
pointinterval=True,
interval="hdi",
interval=None,
levels=None,
figsize=None,
):
Expand All @@ -579,8 +585,9 @@ def plot_interactive(
Whether to include a plot of the quantiles. Defaults to False.
If `True` the default is to plot the median and two inter-quantiles ranges.
interval : str
Type of interval. Available options are the highest density interval `"hdi"` (default),
Type of interval. Available options are the highest density interval `"hdi"`,
equal tailed interval `"eti"` or intervals defined by arbitrary `"quantiles"`.
Defaults to the value in rcParams["stats.ci_kind"].
levels : list
Mass of the intervals. For hdi or eti the number of elements should be 2 or 1.
For quantiles the number of elements should be 5, 3, 1 or 0
Expand Down
41 changes: 29 additions & 12 deletions preliz/distributions/distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
)

from ..internal.optimization import optimize_hdi
from ..internal.rcparams import rcParams


class Distribution:
Expand Down Expand Up @@ -75,25 +76,29 @@ def params_dict(self):
else:
return None

def summary(self, mass=0.94, interval="hdi", fmt=".2f"):
def summary(self, mass=None, interval=None, fmt=".2f"):
"""
Namedtuple with the mean, median, standard deviation, and lower and upper bounds
of the equal-tailed interval.
Parameters
----------
mass: float
Probability mass for the equal-tailed interval. Defaults to 0.94
Probability mass for the equal-tailed interval. Defaults to None,
which results in the value of rcParams["stats.ci_prob"] being used.
interval : str or list-like
Type of interval. Available options are highest density interval `"hdi"` (default),
Type of interval. Available options are highest density interval `"hdi"`,
equal tailed interval `"eti"` or arbitrary interval defined by a list-like object
with a pair of values.
with a pair of values. Defaults to the value in rcParams["stats.ci_kind"].
fmt : str
fmt used to represent results using f-string fmt for floats. Default to ".2f"
i.e. 2 digits after the decimal point.
"""
valid_distribution(self)

if interval is None:
interval = rcParams["stats.ci_kind"]

if not isinstance(fmt, str):
raise ValueError("Invalid format string.")

Expand Down Expand Up @@ -248,19 +253,23 @@ def moments(self, types="mvsk"):

return moments

def eti(self, mass=0.94, fmt=".2f"):
def eti(self, mass=None, fmt=".2f"):
"""Equal-tailed interval containing `mass`.
Parameters
----------
mass: float
Probability mass in the interval. Defaults to 0.94
Probability mass in the interval. Defaults to None,
which results in the value of rcParams["stats.ci_prob"] being used.
fmt : str
fmt used to represent results using f-string fmt for floats. Default to ".2f"
i.e. 2 digits after the decimal point. Use `"none"` for no format.
"""
valid_distribution(self)

if mass is None:
mass = rcParams["stats.ci_prob"]

if not isinstance(fmt, str):
raise ValueError("Invalid format string.")

Expand All @@ -277,19 +286,23 @@ def eti(self, mass=0.94, fmt=".2f"):
else:
return None

def hdi(self, mass=0.94, fmt=".2f"):
def hdi(self, mass=None, fmt=".2f"):
"""Highest density interval containing `mass`.
Parameters
----------
mass: float
Probability mass in the interval. Defaults to 0.94
Probability mass in the interval. Defaults to None,
which results in the value of rcParams["stats.ci_prob"] being used.
fmt : str
fmt used to represent results using f-string fmt for floats. Default to ".2f"
i.e. 2 digits after the decimal point. Use `"none"` for no format.
"""
valid_distribution(self)

if mass is None:
mass = rcParams["stats.ci_prob"]

if not isinstance(fmt, str):
raise ValueError("Invalid format string.")

Expand Down Expand Up @@ -491,7 +504,7 @@ def plot_pdf(
self,
moments=None,
pointinterval=False,
interval="hdi",
interval=None,
levels=None,
support="restricted",
legend="legend",
Expand All @@ -515,6 +528,7 @@ def plot_pdf(
interval : str
Type of interval. Available options are highest density interval `"hdi"` (default),
equal tailed interval `"eti"` or intervals defined by arbitrary `"quantiles"`.
Defaults to the value in rcParams["stats.ci_kind"].
levels : list
Mass of the intervals. For hdi or eti the number of elements should be 2 or 1.
For quantiles the number of elements should be 5, 3, 1 or 0
Expand Down Expand Up @@ -556,7 +570,7 @@ def plot_cdf(
self,
moments=None,
pointinterval=False,
interval="hdi",
interval=None,
levels=None,
support="restricted",
legend="legend",
Expand All @@ -580,6 +594,7 @@ def plot_cdf(
interval : str
Type of interval. Available options are highest density interval `"hdi"` (default),
equal tailed interval `"eti"` or intervals defined by arbitrary `"quantiles"`.
Defaults to the value in rcParams["stats.ci_kind"].
levels : list
Mass of the intervals. For hdi or eti the number of elements should be 2 or 1.
For quantiles the number of elements should be 5, 3, 1 or 0
Expand Down Expand Up @@ -620,7 +635,7 @@ def plot_ppf(
self,
moments=None,
pointinterval=False,
interval="hdi",
interval=None,
levels=None,
legend="legend",
color=None,
Expand All @@ -643,6 +658,7 @@ def plot_ppf(
interval : str
Type of interval. Available options are highest density interval `"hdi"` (default),
equal tailed interval `"eti"` or intervals defined by arbitrary `"quantiles"`.
Defaults to the value in rcParams["stats.ci_kind"].
levels : list
Mass of the intervals. For hdi or eti the number of elements should be 2 or 1.
For quantiles the number of elements should be 5, 3, 1 or 0
Expand Down Expand Up @@ -671,7 +687,7 @@ def plot_interactive(
kind="pdf",
xy_lim="both",
pointinterval=True,
interval="hdi",
interval=None,
levels=None,
figsize=None,
):
Expand All @@ -694,6 +710,7 @@ def plot_interactive(
interval : str
Type of interval. Available options are highest density interval `"hdi"` (default),
equal tailed interval `"eti"` or intervals defined by arbitrary `"quantiles"`.
Defaults to the value in rcParams["stats.ci_kind"].
levels : list
Mass of the intervals. For hdi or eti the number of elements should be 2 or 1.
For quantiles the number of elements should be 5, 3, 1 or 0
Expand Down
4 changes: 2 additions & 2 deletions preliz/distributions/vonmises.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ def _fit_mle(self, sample):
mu = np.mod(mu + np.pi, 2 * np.pi) - np.pi
self._update(mu, kappa)

def eti(self, mass=0.94, fmt=".2f"):
def eti(self, mass=None, fmt=".2f"):
mean = self.mu
self.mu = 0
hdi_min, hdi_max = super().eti(mass=mass, fmt=fmt)
self.mu = mean
return _warp_interval(hdi_min, hdi_max, self.mu, fmt)

def hdi(self, mass=0.94, fmt=".2f"):
def hdi(self, mass=None, fmt=".2f"):
mean = self.mu
self.mu = 0
hdi_min, hdi_max = super().hdi(mass=mass, fmt=fmt)
Expand Down
Loading

0 comments on commit 52fd62f

Please sign in to comment.