Esempio n. 1
0
def pcm(func,
        porder,
        dist,
        rule="G",
        sorder=None,
        proxy_dist=None,
        orth=None,
        orth_acc=100,
        quad_acc=100,
        sparse=False,
        composit=1,
        antithetic=None,
        lr="LS",
        **kws):
    """
Probabilistic Collocation Method

Parameters
----------
Required arguments

func : callable
    The model to be approximated.
    Must accept arguments on the form `func(z, *args, **kws)`
    where `z` is an 1-dimensional array with `len(z)==len(dist)`.
porder : int
    The order of the polynomial approximation
dist_out : Dist
    Distributions for models parameter
rule : str
    The rule for estimating the Fourier coefficients.
    For spectral projection/quadrature rules, see generate_quadrature.
    For point collocation/nummerical sampling, see samplegen.

Optional arguments

proxy_dist : Dist
    If included, the expansion will be created in proxy_dist and
    values will be mapped to dist using a double Rosenblatt
    transformation.
sorder : float
    The order of the sample scheme used.
    If omited, default values will be used.
orth : int, str, callable, Poly
    Orthogonal polynomial generation.

    int, str :
        orth will be passed to orth_select
        for selection of orthogonalization.
        See orth_select doc for more details.

    callable :
        the return of orth(order, dist) will be used.

    Poly :
        it will be used directly.
        All polynomials must be orthogonal for method to work
        properly if spectral projection is used.
orth_acc : int
    Accuracy used in the estimation of polynomial expansion.

Spectral projection arguments

sparse : bool
    If True, Smolyak sparsegrid will be used instead of full
    tensorgrid.
composit : int
    Use composit rule. Note that the number of evaluations may grow
    quickly.

Point collocation arguments

antithetic : bool, array_like
    Use of antithetic variable
lr : str
    Linear regresion method.
    See fit_regression for more details.
lr_kws : dict
    Extra keyword arguments passed to fit_regression.

Returns
-------
q : Poly
    Polynomial approximation of a given a model.

Examples
--------

Define function and distribution:
>>> func = lambda z: -z[1]**2 + 0.1*z[0]
>>> dist = cp.J(cp.Uniform(), cp.Uniform())

Perform pcm:
>>> q = cp.pcm(func, 2, dist)
>>> print cp.around(q, 10)
0.1q0-q1^2

See also
--------
generate_quadrature         Generator for quadrature rules
samplegen       Generator for sampling schemes
    """

    # Proxy variable
    if proxy_dist is None:
        trans = lambda x: x
    else:
        dist, dist_ = proxy_dist, dist
        trans = lambda x: dist_.inv(dist.fwd(x), **kws)

    # The polynomial expansion
    if orth is None:
        if dist.dependent():
            orth = "svd"
        else:
            orth = "ttr"
    if isinstance(orth, (str, int, long)):
        orth = orthogonal.orth_select(orth, **kws)
    if not isinstance(orth, po.Poly):
        orth = orth(porder, dist, acc=orth_acc, **kws)

    # Applying scheme
    rule = rule.upper()
    if rule in "GEC":
        if sorder is None:
            sorder = porder + 1
        z, w = qu.generate_quadrature(sorder,
                                      dist,
                                      acc=quad_acc,
                                      sparse=sparse,
                                      rule=rule,
                                      composit=composit,
                                      **kws)

        x = trans(z)
        y = np.array(map(func, x.T))
        Q = fit_quadrature(orth, x, w, y, **kws)

    else:
        if sorder is None:
            sorder = 2 * len(orth)
        z = dist.sample(sorder, rule=rule, antithetic=antithetic)

        x = trans(z)
        y = np.array(map(func, x.T))
        Q = fit_regression(orth, x, y, rule=lr, **kws)

    return Q
Esempio n. 2
0
def pcm_gq(func,
           order,
           dist_out,
           dist_in=None,
           acc=None,
           orth=None,
           retall=False,
           sparse=False):
    """
Probabilistic Collocation Method using optimal Gaussian quadrature

Parameters
----------
Required arguments

func : callable
    The model to be approximated.
    Must accept arguments on the form `func(z, *args, **kws)`
    where `z` is an 1-dimensional array with `len(z)==len(dist)`.
order : int
    The order of the polynomial approximation
dist_out : Dist
    Distributions for models parameter

Optional arguments

dist_in : Dist
    If included, space will be mapped using a Rosenblatt
    transformation from dist_out to dist_in before creating an
    expansin in terms of dist_in
acc : float
    The order of the sample scheme used
    If omitted order+1 will be used
orth : int, str, callable, Poly
    Orthogonal polynomial generation.

    int, str :
        orth will be passed to orth_select
        for selection of orthogonalization.
        See orth_select doc for more details.

    callable :
        the return of orth(M, dist) will be used.

    Poly :
        it will be used directly.
        All polynomials must be orthogonal for method to work
        properly.
args : itterable
    Extra positional arguments passed to `func`.
kws : dict
    Extra keyword arguments passed to `func`.
retall : bool
    If True, return also number of evaluations
sparse : bool
    If True, Smolyak sparsegrid will be used instead of full
    tensorgrid

Returns
-------
Q[, X]

Q : Poly
    Polynomial estimate of a given a model.
X : np.ndarray
    Values used in evaluation

#  Examples
#  --------
#  Define function:
#  >>> func = lambda z: z[1]*z[0]
#
#  Define distribution:
#  >>> dist = cp.J(cp.Normal(), cp.Normal())
#
#  Perform pcm:
#  >>> p, x, w, y = cp.pcm_gq(func, 2, dist, acc=3, retall=True)
#  >>> print cp.around(p, 10)
#  q0q1
#  >>> print len(w)
#  16

    """
    if acc is None:
        acc = order + 1

    if dist_in is None:
        z, w = qu.generate_quadrature(acc,
                                      dist_out,
                                      100,
                                      sparse=sparse,
                                      rule="G")
        x = z
        dist = dist_out
    else:
        z, w = qu.generate_quadrature(acc,
                                      dist_in,
                                      100,
                                      sparse=sparse,
                                      rule="G")
        x = dist_out.ppf(dist_in.cdf(z))
        dist = dist_in

    y = np.array(map(func, x.T))
    shape = y.shape
    y = y.reshape(w.size, y.size / w.size)

    if orth is None:
        if dist.dependent:
            orth = "chol"
        else:
            orth = "ttr"
    if isinstance(orth, (str, int, long)):
        orth = orth_select(orth)
    if not isinstance(orth, po.Poly):
        orth = orth(order, dist)

    ovals = orth(*z)
    vals1 = [(val * y.T * w).T for val in ovals]
    vals2 = [(val**2 * w).T for val in ovals]
    coef = (np.sum(vals1, 1).T / np.sum(vals2, 1)).T

    coef = coef.reshape(len(coef), *shape[1:])
    Q = po.transpose(po.sum(orth * coef.T, -1))

    if retall:
        return Q, x, w, y
    return Q
Esempio n. 3
0
def pcm_cc(func,
           order,
           dist_out,
           dist_in=None,
           acc=None,
           orth=None,
           retall=False,
           sparse=False):
    """
Probabilistic Collocation Method using Clenshaw-Curtis quadrature

Parameters
----------
Required arguments

func : callable
    The model to be approximated.
    Must accept arguments on the form `func(z, *args, **kws)`
    where `z` is an 1-dimensional array with `len(z)==len(dist)`.
order : int
    The order of the polynomial approximation
dist_out : Dist
    Distributions for models parameter

Optional arguments

dist_in : Dist
    If included, space will be mapped using a Rosenblatt
    transformation from dist_out to dist_in before creating an
    expansin in terms of dist_in
acc : float
    The order of the sample scheme used
    If omitted order+1 will be used
orth : int, str, callable, Poly
    Orthogonal polynomial generation.

    int, str :
        orth will be passed to orth_select
        for selection of orthogonalization.
        See orth_select doc for more details.

    callable :
        the return of orth(M, dist) will be used.

    Poly :
        it will be used directly.
        All polynomials must be orthogonal for method to work
        properly.
retall : bool
    If True, return extra values.
sparse : bool
    If True, Smolyak sparsegrid will be used instead of full
    tensorgrid

Returns
-------
q[, x, w, y]

q : Poly
    Polynomial estimate of a given a model.
x : np.ndarray
    Nodes used in quadrature with `x.shape=(dim, K)` where K is the
    number of samples.
w : np.ndarray
    Weights used in quadrature with `w.shape=(K,)`.
y : np.ndarray
    Evauluations of func with `len(y)=K`.

#  Examples
#  --------
#
#  Define function and distribution:
#  >>> func = lambda z: -z[1]**2 + 0.1*z[0]
#  >>> dist = cp.J(cp.Uniform(), cp.Uniform())
#
#  Perform pcm:
#  >>> q, x, w, y = cp.pcm_cc(func, 2, dist, acc=2, retall=1)
#  >>> print cp.around(q, 10)
#  -q1^2+0.1q0
#  >>> print len(w)
#  9
#
#  With Smolyak sparsegrid
#  >>> q, x, w, y = cp.pcm_cc(func, 2, dist, acc=2, retall=1, sparse=1)
#  >>> print cp.around(q, 10)
#  -q1^2+0.1q0
#  >>> print len(w)
#  13
    """
    if acc is None:
        acc = order + 1

    if dist_in is None:
        z, w = qu.generate_quadrature(acc,
                                      dist_out,
                                      100,
                                      sparse=sparse,
                                      rule="C")
        x = z
        dist = dist_out
    else:
        z, w = qu.generate_quadrature(acc,
                                      dist_in,
                                      100,
                                      sparse=sparse,
                                      rule="C")
        x = dist_out.ppf(dist_in.cdf(z))
        dist = dist_in

    if orth is None:
        if dist.dependent:
            orth = "chol"
        else:
            orth = "ttr"
    if isinstance(orth, (str, int, long)):
        orth = orth_select(orth)
    if not isinstance(orth, po.Poly):
        orth = orth(order, dist)

    y = np.array(map(func, x.T))
    Q = fit_quadrature(orth, x, w, y)

    if retall:
        return Q, x, w, y
    return Q
Esempio n. 4
0
def pcm(func, porder, dist, rule="G", sorder=None, proxy_dist=None,
        orth=None, orth_acc=100, quad_acc=100, sparse=False, composit=1,
        antithetic=None, lr="LS", **kws):
    """
Probabilistic Collocation Method

Parameters
----------
Required arguments

func : callable
    The model to be approximated.
    Must accept arguments on the form `func(z, *args, **kws)`
    where `z` is an 1-dimensional array with `len(z)==len(dist)`.
porder : int
    The order of the polynomial approximation
dist_out : Dist
    Distributions for models parameter
rule : str
    The rule for estimating the Fourier coefficients.
    For spectral projection/quadrature rules, see generate_quadrature.
    For point collocation/nummerical sampling, see samplegen.

Optional arguments

proxy_dist : Dist
    If included, the expansion will be created in proxy_dist and
    values will be mapped to dist using a double Rosenblatt
    transformation.
sorder : float
    The order of the sample scheme used.
    If omited, default values will be used.
orth : int, str, callable, Poly
    Orthogonal polynomial generation.

    int, str :
        orth will be passed to orth_select
        for selection of orthogonalization.
        See orth_select doc for more details.

    callable :
        the return of orth(order, dist) will be used.

    Poly :
        it will be used directly.
        All polynomials must be orthogonal for method to work
        properly if spectral projection is used.
orth_acc : int
    Accuracy used in the estimation of polynomial expansion.

Spectral projection arguments

sparse : bool
    If True, Smolyak sparsegrid will be used instead of full
    tensorgrid.
composit : int
    Use composit rule. Note that the number of evaluations may grow
    quickly.

Point collocation arguments

antithetic : bool, array_like
    Use of antithetic variable
lr : str
    Linear regresion method.
    See fit_regression for more details.
lr_kws : dict
    Extra keyword arguments passed to fit_regression.

Returns
-------
q : Poly
    Polynomial approximation of a given a model.

Examples
--------

Define function and distribution:
>>> func = lambda z: -z[1]**2 + 0.1*z[0]
>>> dist = cp.J(cp.Uniform(), cp.Uniform())

Perform pcm:
>>> q = cp.pcm(func, 2, dist)
>>> print cp.around(q, 10)
0.1q0-q1^2

See also
--------
generate_quadrature         Generator for quadrature rules
samplegen       Generator for sampling schemes
    """

    # Proxy variable
    if proxy_dist is None:
        trans = lambda x:x
    else:
        dist, dist_ = proxy_dist, dist
        trans = lambda x: dist_.inv(dist.fwd(x), **kws)

    # The polynomial expansion
    if orth is None:
        if dist.dependent():
            orth = "svd"
        else:
            orth = "ttr"
    if isinstance(orth, (str, int, long)):
        orth = orth_select(orth, **kws)
    if not isinstance(orth, po.Poly):
        orth = orth(porder, dist, acc=orth_acc, **kws)

    # Applying scheme
    rule = rule.upper()
    if rule in "GEC":
        if sorder is None:
            sorder = porder+1
        z,w = qu.generate_quadrature(sorder, dist, acc=quad_acc, sparse=sparse,
                rule=rule, composit=composit, **kws)

        x = trans(z)
        y = np.array(map(func, x.T))
        Q = fit_quadrature(orth, x, w, y, **kws)

    else:
        if sorder is None:
            sorder = 2*len(orth)
        z = dist.sample(sorder, rule=rule, antithetic=antithetic)

        x = trans(z)
        y = np.array(map(func, x.T))
        Q = fit_regression(orth, x, y, rule=lr, **kws)

    return Q
Esempio n. 5
0
def pcm_gq(func, order, dist_out, dist_in=None, acc=None,
        orth=None, retall=False, sparse=False):
    """
Probabilistic Collocation Method using optimal Gaussian quadrature

Parameters
----------
Required arguments

func : callable
    The model to be approximated.
    Must accept arguments on the form `func(z, *args, **kws)`
    where `z` is an 1-dimensional array with `len(z)==len(dist)`.
order : int
    The order of the polynomial approximation
dist_out : Dist
    Distributions for models parameter

Optional arguments

dist_in : Dist
    If included, space will be mapped using a Rosenblatt
    transformation from dist_out to dist_in before creating an
    expansin in terms of dist_in
acc : float
    The order of the sample scheme used
    If omitted order+1 will be used
orth : int, str, callable, Poly
    Orthogonal polynomial generation.

    int, str :
        orth will be passed to orth_select
        for selection of orthogonalization.
        See orth_select doc for more details.

    callable :
        the return of orth(M, dist) will be used.

    Poly :
        it will be used directly.
        All polynomials must be orthogonal for method to work
        properly.
args : itterable
    Extra positional arguments passed to `func`.
kws : dict
    Extra keyword arguments passed to `func`.
retall : bool
    If True, return also number of evaluations
sparse : bool
    If True, Smolyak sparsegrid will be used instead of full
    tensorgrid

Returns
-------
Q[, X]

Q : Poly
    Polynomial estimate of a given a model.
X : np.ndarray
    Values used in evaluation

#  Examples
#  --------
#  Define function:
#  >>> func = lambda z: z[1]*z[0]
#  
#  Define distribution:
#  >>> dist = cp.J(cp.Normal(), cp.Normal())
#  
#  Perform pcm:
#  >>> p, x, w, y = cp.pcm_gq(func, 2, dist, acc=3, retall=True)
#  >>> print cp.around(p, 10)
#  q0q1
#  >>> print len(w)
#  16

    """
    if acc is None:
        acc = order+1

    if dist_in is None:
        z,w = qu.generate_quadrature(acc, dist_out, 100, sparse=sparse,
                rule="G")
        x = z
        dist = dist_out
    else:
        z,w = qu.generate_quadrature(acc, dist_in, 100, sparse=sparse,
                rule="G")
        x = dist_out.ppf(dist_in.cdf(z))
        dist = dist_in

    y = np.array(map(func, x.T))
    shape = y.shape
    y = y.reshape(w.size, y.size/w.size)

    if orth is None:
        if dist.dependent:
            orth = "chol"
        else:
            orth = "ttr"
    if isinstance(orth, (str, int, long)):
        orth = orth_select(orth)
    if not isinstance(orth, po.Poly):
        orth = orth(order, dist)

    ovals = orth(*z)
    vals1 = [(val*y.T*w).T for val in ovals]
    vals2 = [(val**2*w).T for val in ovals]
    coef = (np.sum(vals1, 1).T/np.sum(vals2, 1)).T

    coef = coef.reshape(len(coef), *shape[1:])
    Q = po.transpose(po.sum(orth*coef.T, -1))

    if retall:
        return Q, x, w, y
    return Q
Esempio n. 6
0
def pcm_cc(func, order, dist_out, dist_in=None, acc=None,
        orth=None, retall=False, sparse=False):
    """
Probabilistic Collocation Method using Clenshaw-Curtis quadrature

Parameters
----------
Required arguments

func : callable
    The model to be approximated.
    Must accept arguments on the form `func(z, *args, **kws)`
    where `z` is an 1-dimensional array with `len(z)==len(dist)`.
order : int
    The order of the polynomial approximation
dist_out : Dist
    Distributions for models parameter

Optional arguments

dist_in : Dist
    If included, space will be mapped using a Rosenblatt
    transformation from dist_out to dist_in before creating an
    expansin in terms of dist_in
acc : float
    The order of the sample scheme used
    If omitted order+1 will be used
orth : int, str, callable, Poly
    Orthogonal polynomial generation.

    int, str :
        orth will be passed to orth_select
        for selection of orthogonalization.
        See orth_select doc for more details.

    callable :
        the return of orth(M, dist) will be used.

    Poly :
        it will be used directly.
        All polynomials must be orthogonal for method to work
        properly.
retall : bool
    If True, return extra values.
sparse : bool
    If True, Smolyak sparsegrid will be used instead of full
    tensorgrid

Returns
-------
q[, x, w, y]

q : Poly
    Polynomial estimate of a given a model.
x : np.ndarray
    Nodes used in quadrature with `x.shape=(dim, K)` where K is the
    number of samples.
w : np.ndarray
    Weights used in quadrature with `w.shape=(K,)`.
y : np.ndarray
    Evauluations of func with `len(y)=K`.

#  Examples
#  --------
#  
#  Define function and distribution:
#  >>> func = lambda z: -z[1]**2 + 0.1*z[0]
#  >>> dist = cp.J(cp.Uniform(), cp.Uniform())
#  
#  Perform pcm:
#  >>> q, x, w, y = cp.pcm_cc(func, 2, dist, acc=2, retall=1)
#  >>> print cp.around(q, 10)
#  -q1^2+0.1q0
#  >>> print len(w)
#  9
#  
#  With Smolyak sparsegrid
#  >>> q, x, w, y = cp.pcm_cc(func, 2, dist, acc=2, retall=1, sparse=1)
#  >>> print cp.around(q, 10)
#  -q1^2+0.1q0
#  >>> print len(w)
#  13
    """
    if acc is None:
        acc = order+1

    if dist_in is None:
        z,w = qu.generate_quadrature(acc, dist_out, 100, sparse=sparse,
                rule="C")
        x = z
        dist = dist_out
    else:
        z,w = qu.generate_quadrature(acc, dist_in, 100, sparse=sparse,
                rule="C")
        x = dist_out.ppf(dist_in.cdf(z))
        dist = dist_in

    if orth is None:
        if dist.dependent:
            orth = "chol"
        else:
            orth = "ttr"
    if isinstance(orth, (str, int, long)):
        orth = orth_select(orth)
    if not isinstance(orth, po.Poly):
        orth = orth(order, dist)

    y = np.array(map(func, x.T))
    Q = fit_quadrature(orth, x, w, y)

    if retall:
        return Q, x, w, y
    return Q