Ejemplo n.º 1
0
    def train_with_interface(self, fun, N, NMC=10):
        """Train the response surface with input/output pairs.

        Parameters
        ----------
        fun : function 
            a function that returns the simulation quantity of interest given a 
            point in the input space as an 1-by-m ndarray
        N : int 
            the number of points used in the design-of-experiments for 
            constructing the response surface
        NMC : int, optional 
            the number of points used to estimate the conditional expectation 
            and conditional variance of the function given a value of the active
            variables

        Notes
        -----
        The training methods exploit the eigenvalues from the active subspace
        analysis to determine length scales for each variable when tuning
        the parameters of the radial bases.

        The method sets attributes of the object for further use.

        The method uses the response_surfaces.av_design function to get the
        design for the appropriate `avmap`.
        """
        Y, X, ind = av_design(self.avmap, N, NMC=NMC)

        if isinstance(self.avmap.domain, BoundedActiveVariableDomain):
            X = np.vstack((X, self.avmap.domain.vertX))
            Y = np.vstack((Y, self.avmap.domain.vertY))
            il = np.amax(ind) + 1
            iu = np.amax(ind) + self.avmap.domain.vertX.shape[0] + 1
            iind = np.arange(il, iu)
            ind = np.vstack(( ind, iind.reshape((iind.size,1)) ))

        # run simulation interface at all design points
        if isinstance(fun, SimulationRunner):
            f = fun.run(X)
        else:
            f = SimulationRunner(fun).run(X)

        Ef, Vf = conditional_expectations(f, ind)
        self._train(Y, Ef, v=Vf)
    def train_with_interface(self, fun, N, NMC=10):
        """Train the response surface with input/output pairs.

        Parameters
        ----------
        fun : function 
            a function that returns the simulation quantity of interest given a 
            point in the input space as an 1-by-m ndarray
        N : int 
            the number of points used in the design-of-experiments for 
            constructing the response surface
        NMC : int, optional 
            the number of points used to estimate the conditional expectation 
            and conditional variance of the function given a value of the active
            variables

        Notes
        -----
        The training methods exploit the eigenvalues from the active subspace
        analysis to determine length scales for each variable when tuning
        the parameters of the radial bases.

        The method sets attributes of the object for further use.

        The method uses the response_surfaces.av_design function to get the
        design for the appropriate `avmap`.
        """
        Y, X, ind = av_design(self.avmap, N, NMC=NMC)

        if isinstance(self.avmap.domain, BoundedActiveVariableDomain):
            X = np.vstack((X, self.avmap.domain.vertX))
            Y = np.vstack((Y, self.avmap.domain.vertY))
            il = np.amax(ind) + 1
            iu = np.amax(ind) + self.avmap.domain.vertX.shape[0] + 1
            iind = np.arange(il, iu)
            ind = np.vstack((ind, iind.reshape((iind.size, 1))))

        # run simulation interface at all design points
        if isinstance(fun, SimulationRunner):
            f = fun.run(X)
        else:
            f = SimulationRunner(fun).run(X)

        Ef, Vf = conditional_expectations(f, ind)
        self._train(Y, Ef, v=Vf)
Ejemplo n.º 3
0
def integrate(fun, avmap, N, NMC=10):
    """
    Approximate the integral of a function of m variables.

    :param function fun: An interface to the simulation that returns the
        quantity of interest given inputs as an 1-by-m ndarray.
    :param ActiveVariableMap avmap: a domains.ActiveVariableMap.
    :param int N: The number of points in the quadrature rule.
    :param int NMC: The number of points in the Monte Carlo estimates of the
        conditional expectation and conditional variance.

    :return: mu, An estimate of the integral of the function computed against
        the weight function on the simulation inputs.
    :rtype: float

    :return: lb, An central-limit-theorem 95% lower confidence from the Monte
        Carlo part of the integration.
    :rtype: float

    :return: ub, An central-limit-theorem 95% upper confidence from the Monte
        Carlo part of the integration.
    :rtype: float

    **See Also**

    integrals.quadrature_rule

    **Notes**

    The CLT-based bounds `lb` and `ub` are likely poor estimators of the error.
    They only account for the variance from the Monte Carlo portion. They do
    not include any error from the integration rule on the active variables.
    """
    if not isinstance(avmap, ActiveVariableMap):
        raise TypeError('avmap should be an ActiveVariableMap.')

    if not isinstance(N, int):
        raise TypeError('N should be an integer')

    logging.getLogger(__name__).debug('Integrating a function of {:d} vars with {:d}-dim active subspace using a {:d}-point rule and {:d} MC samples.'\
        .format(avmap.domain.subspaces.W1.shape[0], avmap.domain.subspaces.W1.shape[1], N, NMC))

    # get the quadrature rule
    Xp, Xw, ind = quadrature_rule(avmap, N, NMC=NMC)

    # compute the simulation output at each quadrature node
    if isinstance(fun, SimulationRunner):
        f = fun.run(Xp)
    else:
        f = SimulationRunner(fun).run(Xp)

    # estimate conditional expectations and variances
    Ef, Vf = conditional_expectations(f, ind)

    # get weights for the conditional expectations
    w = conditional_expectations(Xw*NMC, ind)[0]

    # estimate the average
    mu = np.dot(Ef.T, w)

    # estimate the variance due to Monte Carlo
    sig2 = np.dot(Vf.T, w*w) / NMC

    # compute 95% confidence bounds from the Monte Carlo
    lb, ub = mu - 1.96*np.sqrt(sig2), mu + 1.96*np.sqrt(sig2)
    return mu[0,0], lb[0,0], ub[0,0]
Ejemplo n.º 4
0
def integrate(fun, avmap, N, NMC=10):
    """Approximate the integral of a function of m variables.

    Parameters
    ----------
    fun : function
        an interface to the simulation that returns the quantity of interest
        given inputs as an 1-by-m ndarray
    avmap : ActiveVariableMap
        a domains.ActiveVariableMap
    N : int
        the number of points in the quadrature rule
    NMC : int, optional
        the number of points in the Monte Carlo estimates of the conditional
        expectation and conditional variance (default 10)

    Returns
    -------
    mu : float
        an estimate of the integral of the function computed against the weight
        function on the simulation inputs
    lb : float
        a central-limit-theorem 95% lower confidence from the Monte Carlo part
        of the integration
    ub : float
        a central-limit-theorem 95% upper confidence from the Monte Carlo part
        of the integration

    See Also
    --------
    integrals.quadrature_rule

    Notes
    -----
    The CLT-based bounds `lb` and `ub` are likely poor estimators of the error.
    They only account for the variance from the Monte Carlo portion. They do
    not include any error from the integration rule on the active variables.
    """
    if not isinstance(avmap, ActiveVariableMap):
        raise TypeError('avmap should be an ActiveVariableMap.')

    if not isinstance(N, Integral):
        raise TypeError('N should be an integer')

    # get the quadrature rule
    Xp, Xw, ind = quadrature_rule(avmap, N, NMC=NMC)

    # compute the simulation output at each quadrature node
    if isinstance(fun, SimulationRunner):
        f = fun.run(Xp)
    else:
        f = SimulationRunner(fun).run(Xp)

    # estimate conditional expectations and variances
    Ef, Vf = conditional_expectations(f, ind)

    # get weights for the conditional expectations
    w = conditional_expectations(Xw * NMC, ind)[0]

    # estimate the average
    mu = np.dot(Ef.T, w)

    # estimate the variance due to Monte Carlo
    sig2 = np.dot(Vf.T, w * w) / NMC

    # compute 95% confidence bounds from the Monte Carlo
    lb, ub = mu - 1.96 * np.sqrt(sig2), mu + 1.96 * np.sqrt(sig2)
    return mu[0, 0], lb[0, 0], ub[0, 0]
Ejemplo n.º 5
0
def integrate(fun, avmap, N, NMC=10):
    """Approximate the integral of a function of m variables.
    
    Parameters
    ----------
    fun : function 
        an interface to the simulation that returns the quantity of interest 
        given inputs as an 1-by-m ndarray
    avmap : ActiveVariableMap 
        a domains.ActiveVariableMap
    N : int 
        the number of points in the quadrature rule
    NMC : int, optional
        the number of points in the Monte Carlo estimates of the conditional 
        expectation and conditional variance (default 10)

    Returns
    -------
    mu : float 
        an estimate of the integral of the function computed against the weight 
        function on the simulation inputs
    lb : float 
        a central-limit-theorem 95% lower confidence from the Monte Carlo part 
        of the integration
    ub : float 
        a central-limit-theorem 95% upper confidence from the Monte Carlo part 
        of the integration

    See Also
    --------
    integrals.quadrature_rule

    Notes
    -----
    The CLT-based bounds `lb` and `ub` are likely poor estimators of the error.
    They only account for the variance from the Monte Carlo portion. They do
    not include any error from the integration rule on the active variables.
    """
    if not isinstance(avmap, ActiveVariableMap):
        raise TypeError('avmap should be an ActiveVariableMap.')

    if not isinstance(N, int):
        raise TypeError('N should be an integer')

    # get the quadrature rule
    Xp, Xw, ind = quadrature_rule(avmap, N, NMC=NMC)

    # compute the simulation output at each quadrature node
    if isinstance(fun, SimulationRunner):
        f = fun.run(Xp)
    else:
        f = SimulationRunner(fun).run(Xp)

    # estimate conditional expectations and variances
    Ef, Vf = conditional_expectations(f, ind)

    # get weights for the conditional expectations
    w = conditional_expectations(Xw*NMC, ind)[0]

    # estimate the average
    mu = np.dot(Ef.T, w)

    # estimate the variance due to Monte Carlo
    sig2 = np.dot(Vf.T, w*w) / NMC

    # compute 95% confidence bounds from the Monte Carlo
    lb, ub = mu - 1.96*np.sqrt(sig2), mu + 1.96*np.sqrt(sig2)
    return mu[0,0], lb[0,0], ub[0,0]