def test_distribution_loc_scale(self):
        """
        Tests if scale is set to default when it has value 'None'.
        """

        # Define parameters.
        shape = ConstantParam(0.8888)
        loc = ConstantParam(2.776)
        scale = None
        par1 = (shape, loc, scale)
        rv_values = [0.8, 1, 8]
        dependencies = (0, 1, 1)

        dist = NormalDistribution(*par1)
        scale_test = dist._get_parameter_values(rv_values, dependencies)[2]
        self.assertEqual(scale_test, 1)
Exemple #2
0
def test_normal_param_out_of_bounds(normal_param_name):
    dist = NormalDistribution()
    setattr(dist, normal_param_name, ConstantParam(-np.inf))
    with pytest.raises(ValueError):
        dist.cdf([0, 100], [0, 100], (None, None, None))

    dist = NormalDistribution()
    setattr(dist, normal_param_name, ConstantParam(np.inf))
    with pytest.raises(ValueError):
        dist.cdf([0, 100], [0, 100], (None, None, None))
def setup_mul_dist(probabilistic_model: ProbabilisticModel):
    """
    Generates a MultiVariateDistribution from a ProbabilisticModel.

    MultiVariateDistribution objects are used to perform the statistical
    computations in the viroconcom package. ProbabilisticModel objects are used
    in the viroconweb package to be saved in the data base.

    Parameters
    ----------
    probabilistic_model : ProbabilisticModel,
        The probabilistic model, which should be converted.

    Returns
    -------
    mutivar_distribution : MultivariateDistribution,
        The object, which can be used in the viroconcom package.

    """
    distributions_model = DistributionModel.objects.filter(
        probabilistic_model=probabilistic_model)
    distributions = []
    dependencies = []

    for dist in distributions_model:
        dependency = []
        parameters = []
        parameters_model = ParameterModel.objects.filter(distribution=dist)
        for param in parameters_model:
            dependency.append(adjust(param.dependency))

            if adjust(param.function) is not None:
                parameters.append(
                    FunctionParam(float(param.x0), float(param.x1),
                                  float(param.x2), param.function))
            else:
                parameters.append(ConstantParam(float(param.x0)))

        dependencies.append(dependency)

        if dist.distribution == 'Normal':
            distributions.append(NormalDistribution(*parameters))
        elif dist.distribution == 'Weibull':
            distributions.append(WeibullDistribution(*parameters))
        elif dist.distribution == 'Lognormal_SigmaMu':
            distributions.append(
                LognormalDistribution(sigma=parameters[0], mu=parameters[2]))
        elif dist.distribution == 'KernelDensity':
            distributions.append(KernelDensityDistribution(*parameters))
        else:
            raise KeyError('{} is not a matching distribution'.format(
                dist.distribution))

    mutivar_distribution = MultivariateDistribution(distributions,
                                                    dependencies)
    return mutivar_distribution
    def test_HDC3d_WLN(self):

        dep1 = (None, None, None)
        dep2 = (0, None, 0)
        dep3 = (None, 0, 0)

        #define parameters
        shape = ConstantParam(1.471)
        loc = ConstantParam(0.8888)
        scale = ConstantParam(2.776)
        par1 = (shape, loc, scale)

        shape = None
        loc = FunctionParam(4, 10, 0.02, "f1")
        scale = FunctionParam(0.1, 0.02, -0.1, "f2")
        par2 = (shape, loc, scale)

        mu = FunctionParam(0.1, 1.5, 0.2, "f1")
        sigma = FunctionParam(0.1, 0.2, -0.2, "f2")

        #create distributions
        dist1 = WeibullDistribution(*par1)
        dist2 = LognormalDistribution(mu=mu, sigma=sigma)
        dist3 = NormalDistribution(*par2)

        distributions = [dist1, dist2, dist3]
        dependencies = [dep1, dep2, dep3]

        mul_dist = MultivariateDistribution(distributions, dependencies)

        del mu, sigma
        #del dist1, dist2, par1, par2, dep1, dep2, dependencies, distributions
        #calc contour
        n_years = 50
        limits = [(0, 20), (0, 20), (0, 20)]
        deltas = [0.5, 0.5, 0.05]
        test_contour_HDC = HighestDensityContour(mul_dist, n_years, 3, limits,
                                                 deltas)

        finaldt3 = pd.DataFrame({
            'x': test_contour_HDC.coordinates[0][0],
            'y': test_contour_HDC.coordinates[0][1],
            'z': test_contour_HDC.coordinates[0][2]
        })

        matlab3 = pd.read_csv(testfiles_path + "/hdc3d_wln.csv",
                              names=['x', 'y', 'z'])

        result3 = pd.read_csv(testfiles_path + "/HDC3dWLN_coordinates.csv")
        for m, n in [(m, n) for m in result3.index for n in result3.columns]:
            self.assertAlmostEqual(result3.loc[m, n],
                                   finaldt3.loc[m, n],
                                   places=8)
    def test_HDC2d_WN(self):
        """
        Creating Contour example
        """

        #define dependency tuple
        dep1 = (None, None, None)
        dep2 = (None, 0, 0)

        #define parameters
        shape = ConstantParam(1.471)
        loc = ConstantParam(0.8888)
        scale = ConstantParam(2.776)
        par1 = (shape, loc, scale)

        shape = None
        loc = FunctionParam(4, 10, 0.02, "f1")
        scale = FunctionParam(0.1, 0.02, -0.1, "f2")
        par2 = (shape, loc, scale)

        #del shape, loc, scale

        #create distributions
        dist1 = WeibullDistribution(*par1)
        dist2 = NormalDistribution(*par2)

        distributions = [dist1, dist2]
        dependencies = [dep1, dep2]

        mul_dist = MultivariateDistribution(distributions, dependencies)

        #del dist1, dist2, par1, par2, dep1, dep2, dependencies, distributions
        #calc contour
        n_years = 50
        limits = [(0, 20), (0, 20)]
        deltas = [0.05, 0.01]
        test_contour_HDC = HighestDensityContour(mul_dist, n_years, 3, limits,
                                                 deltas)

        finaldt2 = pd.DataFrame({
            'x': test_contour_HDC.coordinates[0][0],
            'y': test_contour_HDC.coordinates[0][1]
        })

        result2 = pd.read_csv(testfiles_path + "/HDC2dWN_coordinates.csv")

        for k, l in [(k, l) for k in result2.index for l in result2.columns]:
            self.assertAlmostEqual(result2.loc[k, l],
                                   finaldt2.loc[k, l],
                                   places=8)
Exemple #6
0
    def test_IForm2d_WN(self):
        """
        2-d IFORM contour.
        """

        # Define dependency tuple.
        dep1 = (None, None, None)
        dep2 = (None, 0, 0)

        # Define parameters.
        shape = ConstantParam(1.471)
        loc = ConstantParam(0.8888)
        scale = ConstantParam(2.776)
        par1 = (shape, loc, scale)

        shape = None
        loc = FunctionParam(7, 1.489, 0.1901, "power3")
        scale = FunctionParam(1.5, 0.1748, -0.2243, "exp3")
        par2 = (shape, loc, scale)

        # Create distributions.
        dist1 = WeibullDistribution(*par1)
        dist2 = NormalDistribution(*par2)

        distributions = [dist1, dist2]
        dependencies = [dep1, dep2]

        mul_dist = MultivariateDistribution(distributions, dependencies)

        test_contour_IForm = IFormContour(mul_dist, 50, 3, 50)

        calculated_coordinates = pd.DataFrame({
            'x':
            test_contour_IForm.coordinates[0][0],
            'y':
            test_contour_IForm.coordinates[0][1]
        })

        true_coordinates = pd.read_csv(testfiles_path +
                                       "/IForm2dWN_coordinates.csv")

        for r, s in [(r, s) for r in true_coordinates.index
                     for s in true_coordinates.columns]:
            self.assertAlmostEqual(calculated_coordinates.loc[r, s],
                                   true_coordinates.loc[r, s],
                                   places=8)
    def test_IForm2d_WN(self):
        """
        Creating Contour example
        """

        #define dependency tuple
        dep1 = (None, None, None)
        dep2 = (None, 0, 0)

        #define parameters
        shape = ConstantParam(1.471)
        loc = ConstantParam(0.8888)
        scale = ConstantParam(2.776)
        par1 = (shape, loc, scale)

        shape = None
        loc = FunctionParam(7, 1.489, 0.1901, "f1")
        scale = FunctionParam(1.5, 0.1748, -0.2243, "f2")
        par2 = (shape, loc, scale)

        #del shape, loc, scale

        #create distributions
        dist1 = WeibullDistribution(*par1)
        dist2 = NormalDistribution(*par2)

        distributions = [dist1, dist2]
        dependencies = [dep1, dep2]

        mul_dist = MultivariateDistribution(distributions, dependencies)

        test_contour_IForm = IFormContour(mul_dist, 50, 3, 400)

        finaldt5 = pd.DataFrame({
            'x': test_contour_IForm.coordinates[0][0],
            'y': test_contour_IForm.coordinates[0][1]
        })

        result5 = pd.read_csv(testfiles_path + "/IForm2dWN_coordinates.csv")

        for r, s in [(r, s) for r in result5.index for s in result5.columns]:
            self.assertAlmostEqual(result5.loc[r, s],
                                   finaldt5.loc[r, s],
                                   places=8)
Exemple #8
0
def test_normal_i_cdf(normal_loc, normal_scale):
    x = np.linspace(0, 1)
    ref_cdf = sts.norm.ppf(x, normal_loc(None), normal_scale(None))
    dist = NormalDistribution(None, normal_loc, normal_scale)
    my_cdf = dist.i_cdf(x, x, (None, None, None))
    assert np.allclose(ref_cdf, my_cdf)
Exemple #9
0
def test_normal_draw_sample(normal_number, normal_loc, normal_scale):
    ref_points = normal_number
    dist = NormalDistribution(normal_loc, normal_scale)
    my_points = dist.draw_sample(normal_number)
    my_points = my_points.size
    assert ref_points == my_points