blob: 4ff0bbab14ff1db07176bbde533d805b570a0719 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.statistics.distribution;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
/**
* Test cases for NakagamiDistribution.
*/
class NakagamiDistributionTest extends ContinuousDistributionAbstractTest {
//-------------- Implementations for abstract methods ----------------------
@Override
public NakagamiDistribution makeDistribution() {
return new NakagamiDistribution(0.5, 1);
}
@Override
public double[] makeCumulativeTestPoints() {
return new double[] {
0, 0.2, 0.4, 0.6, 0.8, 1, 1.2, 1.4, 1.6, 1.8, 2
};
}
@Override
public double[] makeDensityTestValues() {
return new double[] {
0.0000000, 0.7820854, 0.7365403, 0.6664492, 0.5793831, 0.4839414,
0.3883721, 0.2994549, 0.2218417, 0.1579003, 0.1079819
};
}
@Override
public double[] makeCumulativeTestValues() {
return new double[] {
0.0000000, 0.1585194, 0.3108435, 0.4514938, 0.5762892, 0.6826895,
0.7698607, 0.8384867, 0.8904014, 0.9281394, 0.9544997
};
}
@Override
public double[] makeCumulativePrecisionTestPoints() {
return new double[] {1e-16, 4e-17};
}
@Override
public double[] makeCumulativePrecisionTestValues() {
// These were created using WolframAlpha
return new double[] {7.978845608028653e-17, 3.1915382432114614e-17};
}
@Override
public double[] makeSurvivalPrecisionTestPoints() {
return new double[] {9, 8.7};
}
@Override
public double[] makeSurvivalPrecisionTestValues() {
// These were created using WolframAlpha
return new double[] {2.2571768119076845e-19, 3.318841739929575e-18};
}
//-------------------- Additional test cases -------------------------------
@Test
void testExtremeLogDensity() {
// XXX: Verify with more test data from a reference distribution
final NakagamiDistribution dist = new NakagamiDistribution(0.5, 1);
final double x = 50;
Assertions.assertEquals(0.0, dist.density(x));
Assertions.assertEquals(-1250.22579, dist.logDensity(x), 1e-4);
}
@Test
void testParameterAccessors() {
final NakagamiDistribution dist = makeDistribution();
Assertions.assertEquals(0.5, dist.getShape());
Assertions.assertEquals(1, dist.getScale());
}
@Test
void testConstructorPrecondition1() {
Assertions.assertThrows(DistributionException.class, () -> new NakagamiDistribution(0.4999, 1.0));
}
@Test
void testConstructorPrecondition2() {
Assertions.assertThrows(DistributionException.class, () -> new NakagamiDistribution(0.5, 0.0));
}
@Test
void testMoments() {
// Values obtained using Matlab, e.g.
// format long;
// pd = makedist('Nakagami','mu',0.5,'omega',1.0);
// disp([pd.mean, pd.var])
NakagamiDistribution dist;
final double eps = 1e-9;
dist = new NakagamiDistribution(0.5, 1.0);
Assertions.assertEquals(0.797884560802866, dist.getMean(), eps);
Assertions.assertEquals(0.363380227632418, dist.getVariance(), eps);
dist = new NakagamiDistribution(1.23, 2.5);
Assertions.assertEquals(1.431786259006201, dist.getMean(), eps);
Assertions.assertEquals(0.449988108521028, dist.getVariance(), eps);
}
@Test
void testSupport() {
final NakagamiDistribution dist = makeDistribution();
Assertions.assertEquals(0, dist.getSupportLowerBound());
Assertions.assertEquals(Double.POSITIVE_INFINITY, dist.getSupportUpperBound());
Assertions.assertTrue(dist.isSupportConnected());
}
}