• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pymc-devs / pymc3 / 9388

pending completion
9388

Pull #3597

travis-ci

web-flow
Add contextvars requirement.

The contextvars library is used to make the vectorized version of sample_posterior_predictive compatible with the legacy version.
contextvars was added in python 3.7, but there is a compatibility library for 3.6
This is an attempt to ensure it will be loaded in python 3.6
Pull Request #3597: WIP: Second try to vectorize sample_posterior_predictive.

513 of 513 new or added lines in 16 files covered. (100.0%)

12635 of 20551 relevant lines covered (61.48%)

0.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

51.85
/pymc3/tuning/scaling.py
1
import numpy as np
1✔
2
from numpy import exp, log, sqrt
1✔
3
from ..model import modelcontext, Point
1✔
4
from ..theanof import hessian_diag, inputvars
1✔
5
from ..blocking import DictToArrayBijection, ArrayOrdering
1✔
6

7
__all__ = ['find_hessian', 'trace_cov', 'guess_scaling']
1✔
8

9

10
def fixed_hessian(point, vars=None, model=None):
1✔
11
    """
12
    Returns a fixed Hessian for any chain location.
13

14
    Parameters
15
    ----------
16
    model : Model (optional if in `with` context)
17
    point : dict
18
    vars : list
19
        Variables for which Hessian is to be calculated.
20
    """
21

22
    model = modelcontext(model)
×
23
    if vars is None:
×
24
        vars = model.cont_vars
×
25
    vars = inputvars(vars)
×
26

27
    point = Point(point, model=model)
×
28

29
    bij = DictToArrayBijection(ArrayOrdering(vars), point)
×
30
    rval = np.ones(bij.map(point).size) / 10
×
31
    return rval
×
32

33

34
def find_hessian(point, vars=None, model=None):
1✔
35
    """
36
    Returns Hessian of logp at the point passed.
37

38
    Parameters
39
    ----------
40
    model : Model (optional if in `with` context)
41
    point : dict
42
    vars : list
43
        Variables for which Hessian is to be calculated.
44
    """
45
    model = modelcontext(model)
×
46
    H = model.fastd2logp(vars)
×
47
    return H(Point(point, model=model))
×
48

49

50
def find_hessian_diag(point, vars=None, model=None):
1✔
51
    """
52
    Returns Hessian of logp at the point passed.
53

54
    Parameters
55
    ----------
56
    model : Model (optional if in `with` context)
57
    point : dict
58
    vars : list
59
        Variables for which Hessian is to be calculated.
60
    """
61
    model = modelcontext(model)
1✔
62
    H = model.fastfn(hessian_diag(model.logpt, vars))
1✔
63
    return H(Point(point, model=model))
1✔
64

65

66
def guess_scaling(point, vars=None, model=None, scaling_bound=1e-8):
1✔
67
    model = modelcontext(model)
1✔
68
    try:
1✔
69
        h = find_hessian_diag(point, vars, model=model)
1✔
70
    except NotImplementedError:
×
71
        h = fixed_hessian(point, vars, model=model)
×
72
    return adjust_scaling(h, scaling_bound)
1✔
73

74

75
def adjust_scaling(s, scaling_bound):
1✔
76
    if s.ndim < 2:
1✔
77
        return adjust_precision(s, scaling_bound)
1✔
78
    else:
79
        val, vec = np.linalg.eigh(s)
×
80
        val = adjust_precision(val, scaling_bound)
×
81
        return eig_recompose(val, vec)
×
82

83

84
def adjust_precision(tau, scaling_bound=1e-8):
1✔
85
    mag = sqrt(abs(tau))
1✔
86

87
    bounded = bound(log(mag), log(scaling_bound), log(1./scaling_bound))
1✔
88
    return exp(bounded)**2
1✔
89

90

91
def bound(a, l, u):
1✔
92
    return np.maximum(np.minimum(a, u), l)
1✔
93

94

95
def eig_recompose(val, vec):
1✔
96
    return vec.dot(np.diag(val)).dot(vec.T)
×
97

98

99
def trace_cov(trace, vars=None, model=None):
1✔
100
    """
101
    Calculate the flattened covariance matrix using a sample trace
102

103
    Useful if you want to base your covariance matrix for further sampling on some initial samples.
104

105
    Parameters
106
    ----------
107
    trace : Trace
108
    vars : list
109
        variables for which to calculate covariance matrix
110

111
    Returns
112
    -------
113
    r : array (n,n)
114
        covariance matrix
115
    """
116
    model = modelcontext(model)
×
117

118
    if model is not None:
×
119
        vars = model.free_RVs
×
120
    elif vars is None:
×
121
        vars = trace.varnames
×
122

123
    def flat_t(var):
×
124
        x = trace[str(var)]
×
125
        return x.reshape((x.shape[0], np.prod(x.shape[1:], dtype=int)))
×
126

127
    return np.cov(np.concatenate(list(map(flat_t, vars)), 1).T)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc