Beispiel #1
0
    def _mom(self, kloc, index):
        """
        Example:
            >>> d0 = chaospy.Uniform()
            >>> dist = chaospy.J(d0, d0+chaospy.Uniform())
            >>> dist.mom([1, 1]).round(4)
            0.5834
            >>> dist = chaospy.J(chaospy.Uniform(), chaospy.Normal())
            >>> dist.mom([[0, 0, 1], [0, 1, 1]]).round(4)
            array([1., 0., 0.])
        """
        output = 1.
        for idx1 in range(len(self._owners)):
            _, dist1 = self._owners[idx1]
            for idx2 in range(idx1 + 1, len(self._owners)):
                _, dist2 = self._owners[idx2]
                if chaospy.shares_dependencies(dist1, dist2):
                    raise chaospy.UnsupportedFeature(
                        "Shared dependencies across joint")

        kloc = kloc[self._rotation]
        for unique_idx in numpy.unique(index[self._rotation]):
            output *= self._owners[unique_idx][1]._get_mom(
                kloc[index == unique_idx])
        return output
Beispiel #2
0
    def _mom(self, key, left, right, cache):
        """
        Statistical moments.

        Example:
            >>> chaospy.Uniform().mom([0, 1, 2, 3]).round(4)
            array([1.    , 0.5   , 0.3333, 0.25  ])
            >>> Multiply(chaospy.Uniform(), 2).mom([0, 1, 2, 3]).round(4)
            array([1.    , 1.    , 1.3333, 2.    ])
            >>> Multiply(2, chaospy.Uniform()).mom([0, 1, 2, 3]).round(4)
            array([1.    , 1.    , 1.3333, 2.    ])
            >>> Multiply(chaospy.Uniform(), chaospy.Uniform()).mom([0, 1, 2, 3]).round(4)
            array([1.    , 0.25  , 0.1111, 0.0625])

        """
        del cache
        if isinstance(left, Distribution):
            if chaospy.shares_dependencies(left, right):
                raise chaospy.StochasticallyDependentError(
                    "product of dependent distributions not feasible: "
                    "{} and {}".format(left, right)
                )
            left = left._get_mom(key)
        else:
            left = (numpy.array(left).T ** key).T
        if isinstance(right, Distribution):
            right = right._get_mom(key)
        else:
            right = (numpy.array(right).T ** key).T
        return numpy.prod(left) * numpy.prod(right)
Beispiel #3
0
    def _mom(self, keys, left, right, cache):
        """
        Statistical moments.

        Example:
            >>> chaospy.Uniform().mom([0, 1, 2, 3]).round(4)
            array([1.    , 0.5   , 0.3333, 0.25  ])
            >>> chaospy.Add(chaospy.Uniform(), 2).mom([0, 1, 2, 3]).round(4)
            array([ 1.    ,  2.5   ,  6.3333, 16.25  ])
            >>> chaospy.Add(2, chaospy.Uniform()).mom([0, 1, 2, 3]).round(4)
            array([ 1.    ,  2.5   ,  6.3333, 16.25  ])

        """
        del cache
        keys_ = numpy.mgrid[tuple(slice(0, key + 1, 1) for key in keys)]
        keys_ = keys_.reshape(len(self), -1)

        if isinstance(left, Distribution):
            if chaospy.shares_dependencies(left, right):
                raise chaospy.StochasticallyDependentError(
                    "%s: left and right side of sum stochastically dependent."
                    % self)
            left = [left._get_mom(key) for key in keys_.T]
        else:
            left = list(reversed(numpy.array(left).T**keys_.T))

        if isinstance(right, Distribution):
            right = [right._get_mom(key) for key in keys_.T]
        else:
            right = list(
                reversed(numpy.prod(numpy.array(right).T**keys_.T, -1)))

        out = 0.0
        for idx in range(keys_.shape[1]):
            key = keys_.T[idx]
            coef = numpy.prod(comb(keys, key))
            out += coef * left[idx] * right[idx] * numpy.all(key <= keys)
        return out