예제 #1
0
class ExpectationValueOptRanking(Ranking):
    def __init__(self):
        super(self.__class__, self).__init__()
        self._estimationStrategy = AnalyticEstimationStrategy()
        self.initialized = False

        self._linearForm = LinearGaussQuadratureStrategy()

    def compute_mean_phii(self, gs, gpi, basisi):
        mean_phii, _ = self._estimationStrategy.computeSystemMatrixForMeanList(
            gs, [gpi], basisi, self.W, self.D)
        return mean_phii[0]

    def update(self, grid, v, gpi, params, *args, **kws):
        """
        Compute ranking for variance estimation

        \argmax_{i \in \A} | |v_i| E(\varphi_i)

        @param grid: Grid grid
        @param v: numpy array coefficients
        @param admissibleSet: AdmissibleSet
        """
        # update the quadrature operations
        if not self.initialized:
            self._estimationStrategy.initQuadratureStrategy(grid)
            U = params.getIndependentJointDistribution()
            T = params.getJointTransformation()
            self.vol, self.W, self.D = self._estimationStrategy._extractPDFforMomentEstimation(
                U, T)
            self.initialized = True

        # prepare data
        gs = grid.getStorage()

        # compute stiffness matrix for next run
        basis = getBasis(grid)
        ix = gs.getSequenceNumber(gpi)

        mean_phii = self.compute_mean_phii(gs, gpi, basis)

        return np.abs(v[ix]) * mean_phii
예제 #2
0
class VarianceOptRanking(Ranking):
    def __init__(self):
        super(self.__class__, self).__init__()
        self._estimationStrategy = AnalyticEstimationStrategy()
        self.initialized = False

    def compute_mean_uwi_phii(self, gs, gpswi, w, gpi, basis):
        A, _ = self._estimationStrategy.computeSystemMatrixForVarianceList(
            gs, gpswi, basis, [gpi], basis, self.W, self.D)
        return np.dot(w, A)

    def compute_mean_uwi(self, gs, gpswi, w, basis):
        b, _ = self._estimationStrategy.computeSystemMatrixForMeanList(
            gs, gpswi, basis, self.W, self.D)
        return np.dot(w, b)

    def compute_mean_phii(self, gs, gpi, basisi):
        mean_phii, _ = self._estimationStrategy.computeSystemMatrixForMeanList(
            gs, [gpi], basisi, self.W, self.D)
        return mean_phii[0]

    def compute_var_phii(self, gs, gpi, basisi, mean_phii):
        A_var, _ = self._estimationStrategy.computeSystemMatrixForVarianceList(
            gs, [gpi], basisi, [gpi], basisi, self.W, self.D)
        return A_var[0, 0] - mean_phii**2

    def update(self, grid, v, gpi, params, *args, **kws):
        """
        Compute ranking for variance estimation

        \argmax_{i \in \A} | -v_i^2 V(\varphi_i) - 2 v_i Cov(u_indwli \varphi_i)|

        @param grid: Grid grid
        @param v: numpy array coefficients
        @param admissibleSet: AdmissibleSet
        """
        # update the quadrature operations
        if not self.initialized:
            self._estimationStrategy.initQuadratureStrategy(grid)
            U = params.getIndependentJointDistribution()
            T = params.getJointTransformation()
            self.vol, self.W, self.D = self._estimationStrategy._extractPDFforMomentEstimation(
                U, T)
            self.initialized = True

        # prepare data
        gs = grid.getStorage()
        basis = getBasis(grid)

        # load coefficients and vectors and matrices for variance and mean
        # estimation
        w = np.ndarray(gs.getSize() - 1)
        ix = gs.getSequenceNumber(gpi)

        # prepare list of grid points
        gpswi = []
        jx = 0
        for j in range(gs.getSize()):
            gpj = gs.getPoint(j)
            if gpi.getHash() != gpj.getHash():
                gpswi.append(gpj)
                w[jx] = v[j]
                jx += 1

        # compute the covariance
        mean_uwi_phii = self.compute_mean_uwi_phii(gs, gpswi, w, gpi, basis)
        mean_phii = self.compute_mean_phii(gs, gpi, basis)
        mean_uwi = self.compute_mean_uwi(gs, gpswi, w, basis)

        cov_uwi_phii = mean_uwi_phii - mean_phii * mean_uwi

        # compute the variance of phi_i
        var_phii = self.compute_var_phii(gs, gpi, basis, mean_phii)

        # update the ranking
        return np.abs(-v[ix]**2 * var_phii - 2 * v[ix] * cov_uwi_phii)