Skip to content

Commit dfd0b8c

Browse files
aloctavodiatwiecki
authored andcommitted
Plot posterior reliable mode calculation for continuous variables and ignore kde_plot=True for discretes ones (#2294)
* fix mode calculation for continous variables and kde fail for discrete ones * fix typo
1 parent f9917a2 commit dfd0b8c

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

pymc3/plots/artists.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ def display_point_estimate():
8282
if point_estimate == 'mean':
8383
point_value = trace_values.mean()
8484
elif point_estimate == 'mode':
85-
point_value = mode(trace_values.round(round_to))[0][0]
85+
if isinstance(trace_values[0], float):
86+
density, l, u = fast_kde(trace_values)
87+
x = np.linspace(l, u, len(density))
88+
point_value = x[np.argmax(density)]
89+
else:
90+
point_value = mode(trace_values.round(round_to))[0][0]
8691
elif point_estimate == 'median':
8792
point_value = np.median(trace_values)
8893
point_text = '{point_estimate}={point_value:.{round_to}f}'.format(point_estimate=point_estimate,
@@ -121,7 +126,7 @@ def set_key_if_doesnt_exist(d, key, value):
121126
if key not in d:
122127
d[key] = value
123128

124-
if kde_plot:
129+
if kde_plot and isinstance(trace_values[0], float):
125130
kdeplot(trace_values, alpha=kwargs.pop('alpha', 0.35), ax=ax, **kwargs)
126131

127132
else:

pymc3/plots/posteriorplot.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ def plot_posterior(trace, varnames=None, transform=identity_transform, figsize=N
3434
ref_val: bool
3535
display the percentage below and above ref_val
3636
kde_plot: bool
37-
if True plot a KDE instead of a histogram
37+
if True plot a KDE instead of a histogram. For discrete variables this
38+
argument is ignored.
3839
plot_transformed : bool
3940
Flag for plotting automatically transformed variables in addition to
4041
original variables (defaults to False).

0 commit comments

Comments
 (0)