예제 #1
0
    def testSettings(self):
        builder = ParameterBuilder()
        dp = builder.defineDeterministicParameters()
        up = builder.defineUncertainParameters()

        up.new().isCalled('v').withUniformDistribution(
            0, 10).withRosenblattTransformation()
        dp.new().isCalled('density').hasValue(.3)
        up.new().isCalled('theta').withTNormalDistribution(
            1, 1, -2, 2).withLinearTransformation()
        dp.new().isCalled('radius').hasValue(10)
        up.new().isCalled('height').withBetaDistribution(
            3, 3, 0, 2).withRosenblattTransformation()
        params = builder.andGetResult()

        ap = params.activeParams()

        dist = ap.getIndependentJointDistribution()
        trans = ap.getJointTransformation()

        for _ in range(1000):
            prob1 = dist.rvs()[0]
            unit = trans.probabilisticToUnit(prob1)
            prob2 = trans.unitToProbabilistic(unit)

            assert all(["%g" % x == "%g" % y for x, y in zip(prob1, prob2)])
예제 #2
0
    def testSettings(self):
        builder = ParameterBuilder()
        dp = builder.defineDeterministicParameters()
        up = builder.defineUncertainParameters()

        # ============================================
        # 1)
        up.new().isCalled('v')\
                .withDistribution(Uniform(0, 1))\
                .withRosenblattTransformation()
        # --------------------------------------------
        # 2)
        up.new().isCalled('density')\
                .withDistribution(Uniform(-1, 1))\
                .hasValue(0.0)
        # --------------------------------------------
        # 3)
        up.new().isCalled('K')\
                .withDistribution(TNormal(0, 1, -3, 2))\
                .hasValue(-3)
        # --------------------------------------------
        # 4)
        up.new().isCalled('theta')\
                .withDistribution(TNormal(0, 1, -2, 2))\
                .withLinearTransformation()
        # --------------------------------------------
        # 5)
        up.new().isCalled('blub')\
                .withUniformDistribution(-1, 1)
        # --------------------------------------------
        # 6)
        dp.new().isCalled('radius').hasValue(2)
        # ============================================

        params = builder.andGetResult()

        # test dimensions
        assert params.getDim() == 6
        assert params.getStochasticDim() == 3
        assert len(params.activeParams()) == 3
        assert params.getStochasticDim() == len(params.activeParams())
        assert params.getDim() - len(params.uncertainParams()) == \
            len(params.deterministicParams())
        assert params.getStochasticDim() == len(params.getDistributions()) - 2

        jsonStr = params.getJointTransformation().toJson()
        jsonObject = json.loads(jsonStr)
        trans = Transformation.fromJson(jsonObject)

        # test transformations
        ap = params.activeParams()
        assert params.getStochasticDim() == len(ap)
        sampler = MCSampler.withNaiveSampleGenerator(params)

        for sample in sampler.nextSamples(100):
            for x in sample.getActiveUnit():
                assert 0 <= x <= 1
            bounds = params.getBounds()
            q = sample.getExpandedProbabilistic()
            for xlim1, xlim2, x in np.vstack((bounds.T, q)).T:
                assert xlim1 <= x <= xlim2

        params.removeParam(0)
        assert params.getStochasticDim() == len(ap) - 1
        sampler = MCSampler.withNaiveSampleGenerator(params)

        for sample in sampler.nextSamples(100):
            for x in sample.getActiveUnit():
                assert 0 <= x <= 1
            bounds = params.getBounds()
            q = sample.getExpandedProbabilistic()
            for xlim1, xlim2, x in np.vstack((bounds.T, q)).T:
                assert xlim1 <= x <= xlim2