Skip to content
This repository was archived by the owner on Mar 19, 2021. It is now read-only.

Commit d7b7db1

Browse files
authored
Add docstring for stansummary
Docstring for stansummary with Parameters, Returns and Examples.
1 parent a837f3f commit d7b7db1

File tree

1 file changed

+61
-24
lines changed

1 file changed

+61
-24
lines changed

pystan/misc.py

Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,30 +51,67 @@
5151

5252

5353
def stansummary(fit, pars=None, probs=(0.025, 0.25, 0.5, 0.75, 0.975), digits_summary=2):
54-
if fit.mode == 1:
55-
return "Stan model '{}' is of mode 'test_grad';\n"\
56-
"sampling is not conducted.".format(fit.model_name)
57-
elif fit.mode == 2:
58-
return "Stan model '{}' does not contain samples.".format(fit.model_name)
59-
60-
n_kept = [s - w for s, w in zip(fit.sim['n_save'], fit.sim['warmup2'])]
61-
header = "Inference for Stan model: {}.\n".format(fit.model_name)
62-
header += "{} chains, each with iter={}; warmup={}; thin={}; \n"
63-
header = header.format(fit.sim['chains'], fit.sim['iter'], fit.sim['warmup'],
64-
fit.sim['thin'], sum(n_kept))
65-
header += "post-warmup draws per chain={}, total post-warmup draws={}.\n\n"
66-
header = header.format(n_kept[0], sum(n_kept))
67-
footer = "\n\nSamples were drawn using {} at {}.\n"\
68-
"For each parameter, n_eff is a crude measure of effective sample size,\n"\
69-
"and Rhat is the potential scale reduction factor on split chains (at \n"\
70-
"convergence, Rhat=1)."
71-
sampler = fit.sim['samples'][0]['args']['sampler_t']
72-
date = fit.date.strftime('%c') # %c is locale's representation
73-
footer = footer.format(sampler, date)
74-
s = _summary(fit, pars, probs)
75-
body = _array_to_table(s['summary'], s['summary_rownames'],
76-
s['summary_colnames'], digits_summary)
77-
return header + body + footer
54+
"""
55+
Summary statistics table.
56+
57+
Parameters
58+
----------
59+
fit : StanFit4Model object
60+
pars : str or sequence of str, optional
61+
Parameter names. By default use all parameters
62+
probs : sequence of float, optional
63+
Quantiles. By default, (0.025, 0.25, 0.5, 0.75, 0.975)
64+
digits_summary : int, optional
65+
Number of significant digits. By default, 2
66+
67+
Returns
68+
-------
69+
summary : string
70+
Table includes mean, se_mean, sd, probs_0, ..., probs_n, n_eff and Rhat.
71+
72+
Examples
73+
--------
74+
>>> model_code = 'parameters {real y;} model {y ~ normal(0,1);}'
75+
>>> m = StanModel(model_code=model_code, model_name="example_model")
76+
>>> fit = m.sampling()
77+
>>> print(stansummary(fit))
78+
Inference for Stan model: example_model.
79+
4 chains, each with iter=2000; warmup=1000; thin=1;
80+
post-warmup draws per chain=1000, total post-warmup draws=4000.
81+
82+
mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
83+
y 0.01 0.03 1.0 -2.01 -0.68 0.02 0.72 1.97 1330 1.0
84+
lp__ -0.5 0.02 0.68 -2.44 -0.66 -0.24 -0.05-5.5e-4 1555 1.0
85+
86+
Samples were drawn using NUTS at Thu Aug 17 00:52:25 2017.
87+
For each parameter, n_eff is a crude measure of effective sample size,
88+
and Rhat is the potential scale reduction factor on split chains (at
89+
convergence, Rhat=1).
90+
"""
91+
if fit.mode == 1:
92+
return "Stan model '{}' is of mode 'test_grad';\n"\
93+
"sampling is not conducted.".format(fit.model_name)
94+
elif fit.mode == 2:
95+
return "Stan model '{}' does not contain samples.".format(fit.model_name)
96+
97+
n_kept = [s - w for s, w in zip(fit.sim['n_save'], fit.sim['warmup2'])]
98+
header = "Inference for Stan model: {}.\n".format(fit.model_name)
99+
header += "{} chains, each with iter={}; warmup={}; thin={}; \n"
100+
header = header.format(fit.sim['chains'], fit.sim['iter'], fit.sim['warmup'],
101+
fit.sim['thin'], sum(n_kept))
102+
header += "post-warmup draws per chain={}, total post-warmup draws={}.\n\n"
103+
header = header.format(n_kept[0], sum(n_kept))
104+
footer = "\n\nSamples were drawn using {} at {}.\n"\
105+
"For each parameter, n_eff is a crude measure of effective sample size,\n"\
106+
"and Rhat is the potential scale reduction factor on split chains (at \n"\
107+
"convergence, Rhat=1)."
108+
sampler = fit.sim['samples'][0]['args']['sampler_t']
109+
date = fit.date.strftime('%c') # %c is locale's representation
110+
footer = footer.format(sampler, date)
111+
s = _summary(fit, pars, probs)
112+
body = _array_to_table(s['summary'], s['summary_rownames'],
113+
s['summary_colnames'], digits_summary)
114+
return header + body + footer
78115

79116
def _print_stanfit(fit, pars=None, probs=(0.025, 0.25, 0.5, 0.75, 0.975), digits_summary=2):
80117
# warning added in PyStan 2.17.0

0 commit comments

Comments
 (0)