下面列出了怎么用org.apache.commons.math3.distribution.WeibullDistribution的API类实例代码及写法,或者点击链接到github查看源代码。
@operator (
value = "weibull_rnd",
can_be_const = false,
category = { IOperatorCategory.RANDOM },
concept = { IConcept.RANDOM })
@doc (
value = "returns a random value from a Weibull distribution with specified values of the shape (alpha) and scale (beta) parameters. See https://mathworld.wolfram.com/WeibullDistribution.html for more details (equations 1 and 2). ",
examples = { @example (
value = "weibull_rnd(2,3) ",
equals = "0.731",
test = false) },
see = { "binomial", "gamma_rnd", "gauss_rnd", "lognormal_rnd", "poisson", "rnd", "skew_gauss",
"truncated_gauss", "weibull_trunc_rnd" })
@no_test (Reason.IMPOSSIBLE_TO_TEST)
public static Double OpWeibullDist(final IScope scope, final Double shape, final Double scale)
throws GamaRuntimeException {
final WeibullDistribution dist = new WeibullDistribution(scope.getRandom().getGenerator(), shape, scale);
return dist.sample();
}
@operator (
value = "weibull_density",
can_be_const = false,
category = { IOperatorCategory.RANDOM },
concept = { IConcept.RANDOM })
@doc (
value = "weibull_density(x,shape,scale) returns the probability density function (PDF) at the specified point x "
+ "of the Weibull distribution with the given shape and scale.",
examples = { @example ( value = "weibull_rnd(1,2,3) ", equals = "0.731", test = false) },
see = { "binomial", "gamma_rnd", "gauss_rnd", "lognormal_rnd", "poisson", "rnd", "skew_gauss",
"lognormal_density", "gamma_density"})
@no_test (Reason.IMPOSSIBLE_TO_TEST)
public static Double OpWeibullDistDensity(final IScope scope, final Double x, final Double shape, final Double scale)
throws GamaRuntimeException {
final WeibullDistribution dist = new WeibullDistribution(scope.getRandom().getGenerator(), shape, scale);
return dist.density(x);
}
@Override
public DistributionFactory getFactory(List<String> params)
{
if (params.size() != 2)
throw new IllegalArgumentException("Invalid parameter list for extreme (Weibull) distribution: " + params);
try
{
String[] bounds = params.get(0).split("\\.\\.+");
final long min = parseLong(bounds[0]);
final long max = parseLong(bounds[1]);
if (min == max)
return new FixedFactory(min);
final double shape = Double.parseDouble(params.get(1));
WeibullDistribution findBounds = new WeibullDistribution(shape, 1d);
// max probability should be roughly equal to accuracy of (max-min) to ensure all values are visitable,
// over entire range, but this results in overly skewed distribution, so take sqrt
final double scale = (max - min) / findBounds.inverseCumulativeProbability(1d - Math.sqrt(1d/(max-min)));
return new ExtremeFactory(min, max, shape, scale);
} catch (Exception ignore)
{
throw new IllegalArgumentException("Invalid parameter list for extreme (Weibull) distribution: " + params);
}
}
@Override
public DistributionFactory getFactory(List<String> params)
{
if (params.size() != 3)
throw new IllegalArgumentException("Invalid parameter list for quantized extreme (Weibull) distribution: " + params);
try
{
String[] bounds = params.get(0).split("\\.\\.+");
final long min = parseLong(bounds[0]);
final long max = parseLong(bounds[1]);
final double shape = Double.parseDouble(params.get(1));
final int quantas = Integer.parseInt(params.get(2));
WeibullDistribution findBounds = new WeibullDistribution(shape, 1d);
// max probability should be roughly equal to accuracy of (max-min) to ensure all values are visitable,
// over entire range, but this results in overly skewed distribution, so take sqrt
final double scale = (max - min) / findBounds.inverseCumulativeProbability(1d - Math.sqrt(1d/(max-min)));
if (min == max)
return new FixedFactory(min);
return new QuantizedExtremeFactory(min, max, shape, scale, quantas);
} catch (Exception ignore)
{
throw new IllegalArgumentException("Invalid parameter list for quantized extreme (Weibull) distribution: " + params);
}
}
@Override
public Object doWork(Object first, Object second) throws IOException{
if(null == first){
throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the first value",toExpression(constructingFactory)));
}
if(null == second){
throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the second value",toExpression(constructingFactory)));
}
Number shape = (Number)first;
Number scale = (Number)second;
return new WeibullDistribution(shape.doubleValue(), scale.doubleValue());
}
@Test
public void testNextWeibull() {
double[] quartiles = TestUtils.getDistributionQuartiles(new WeibullDistribution(1.2, 2.1));
long[] counts = new long[4];
randomData.reSeed(1000);
for (int i = 0; i < 1000; i++) {
double value = randomData.nextWeibull(1.2, 2.1);
TestUtils.updateCounts(value, counts, quartiles);
}
TestUtils.assertChiSquareAccept(expected, counts, 0.001);
}
@Test
public void testNextWeibull() {
double[] quartiles = TestUtils.getDistributionQuartiles(new WeibullDistribution(1.2, 2.1));
long[] counts = new long[4];
randomData.reSeed(1000);
for (int i = 0; i < 1000; i++) {
double value = randomData.nextWeibull(1.2, 2.1);
TestUtils.updateCounts(value, counts, quartiles);
}
TestUtils.assertChiSquareAccept(expected, counts, 0.001);
}
@Test
public void testNextWeibull() {
double[] quartiles = TestUtils.getDistributionQuartiles(new WeibullDistribution(1.2, 2.1));
long[] counts = new long[4];
randomData.reSeed(1000);
for (int i = 0; i < 1000; i++) {
double value = randomData.nextWeibull(1.2, 2.1);
TestUtils.updateCounts(value, counts, quartiles);
}
TestUtils.assertChiSquareAccept(expected, counts, 0.001);
}
@Test
public void testNextWeibull() {
double[] quartiles = TestUtils.getDistributionQuartiles(new WeibullDistribution(1.2, 2.1));
long[] counts = new long[4];
randomData.reSeed(1000);
for (int i = 0; i < 1000; i++) {
double value = randomData.nextWeibull(1.2, 2.1);
TestUtils.updateCounts(value, counts, quartiles);
}
TestUtils.assertChiSquareAccept(expected, counts, 0.001);
}
@Test
public void testNextWeibull() throws Exception {
double[] quartiles = TestUtils.getDistributionQuartiles(new WeibullDistribution(1.2, 2.1));
long[] counts = new long[4];
randomData.reSeed(1000);
for (int i = 0; i < 1000; i++) {
double value = randomData.nextWeibull(1.2, 2.1);
TestUtils.updateCounts(value, counts, quartiles);
}
TestUtils.assertChiSquareAccept(expected, counts, 0.001);
}
@Test
public void testNextWeibull() {
double[] quartiles = TestUtils.getDistributionQuartiles(new WeibullDistribution(1.2, 2.1));
long[] counts = new long[4];
randomData.reSeed(1000);
for (int i = 0; i < 1000; i++) {
double value = randomData.nextWeibull(1.2, 2.1);
TestUtils.updateCounts(value, counts, quartiles);
}
TestUtils.assertChiSquareAccept(expected, counts, 0.001);
}
@Test
public void testNextWeibull() {
double[] quartiles = TestUtils.getDistributionQuartiles(new WeibullDistribution(1.2, 2.1));
long[] counts = new long[4];
randomData.reSeed(1000);
for (int i = 0; i < 1000; i++) {
double value = randomData.nextWeibull(1.2, 2.1);
TestUtils.updateCounts(value, counts, quartiles);
}
TestUtils.assertChiSquareAccept(expected, counts, 0.001);
}
@Test
public void testNextWeibull() {
double[] quartiles = TestUtils.getDistributionQuartiles(new WeibullDistribution(1.2, 2.1));
long[] counts = new long[4];
randomData.reSeed(1000);
for (int i = 0; i < 1000; i++) {
double value = randomData.nextWeibull(1.2, 2.1);
TestUtils.updateCounts(value, counts, quartiles);
}
TestUtils.assertChiSquareAccept(expected, counts, 0.001);
}
@Override
public Distribution get()
{
return new DistributionOffsetApache(new WeibullDistribution(new JDKRandomGenerator(), shape, scale, WeibullDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY), min, max);
}
@Override
public Distribution get()
{
return new DistributionQuantized(new DistributionOffsetApache(new WeibullDistribution(new JDKRandomGenerator(), shape, scale, WeibullDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY), min, max), quantas);
}
/**
* Generates a random value from the {@link WeibullDistribution Weibull Distribution}.
*
* @param shape the shape parameter of the Weibull distribution
* @param scale the scale parameter of the Weibull distribution
* @return random value sampled from the Weibull(shape, size) distribution
* @throws NotStrictlyPositiveException if {@code shape <= 0} or
* {@code scale <= 0}.
*/
public double nextWeibull(double shape, double scale) throws NotStrictlyPositiveException {
return new WeibullDistribution(getRandomGenerator(), shape, scale,
WeibullDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample();
}
/**
* Generates a random value from the {@link WeibullDistribution Weibull Distribution}.
*
* @param shape the shape parameter of the Weibull distribution
* @param scale the scale parameter of the Weibull distribution
* @return random value sampled from the Weibull(shape, size) distribution
* @throws NotStrictlyPositiveException if {@code shape <= 0} or
* {@code scale <= 0}.
*/
public double nextWeibull(double shape, double scale) throws NotStrictlyPositiveException {
return new WeibullDistribution(getRan(), shape, scale,
WeibullDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample();
}
/**
* Generates a random value from the {@link WeibullDistribution Weibull Distribution}.
* This implementation uses {@link #nextInversionDeviate(RealDistribution) inversion}
* to generate random values.
*
* @param shape the shape parameter of the Weibull distribution
* @param scale the scale parameter of the Weibull distribution
* @return random value sampled from the Weibull(shape, size) distribution
* @since 2.2
*/
public double nextWeibull(double shape, double scale) {
return nextInversionDeviate(new WeibullDistribution(shape, scale));
}
/**
* Generates a random value from the {@link WeibullDistribution Weibull Distribution}.
*
* @param shape the shape parameter of the Weibull distribution
* @param scale the scale parameter of the Weibull distribution
* @return random value sampled from the Weibull(shape, size) distribution
* @throws NotStrictlyPositiveException if {@code shape <= 0} or
* {@code scale <= 0}.
*/
public double nextWeibull(double shape, double scale) throws NotStrictlyPositiveException {
return new WeibullDistribution(getRandomGenerator(), shape, scale,
WeibullDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample();
}
/**
* Generates a random value from the {@link WeibullDistribution Weibull Distribution}.
* This implementation uses {@link #nextInversionDeviate(RealDistribution) inversion}
* to generate random values.
*
* @param shape the shape parameter of the Weibull distribution
* @param scale the scale parameter of the Weibull distribution
* @return random value sampled from the Weibull(shape, size) distribution
* @since 2.2
*/
public double nextWeibull(double shape, double scale) {
return nextInversionDeviate(new WeibullDistribution(shape, scale));
}
/**
* Generates a random value from the {@link WeibullDistribution Weibull Distribution}.
*
* @param shape the shape parameter of the Weibull distribution
* @param scale the scale parameter of the Weibull distribution
* @return random value sampled from the Weibull(shape, size) distribution
* @throws NotStrictlyPositiveException if {@code shape <= 0} or
* {@code scale <= 0}.
*/
public double nextWeibull(double shape, double scale) throws NotStrictlyPositiveException {
return new WeibullDistribution(getRandomGenerator(), shape, scale,
WeibullDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample();
}
/**
* Generates a random value from the {@link WeibullDistribution Weibull Distribution}.
*
* @param shape the shape parameter of the Weibull distribution
* @param scale the scale parameter of the Weibull distribution
* @return random value sampled from the Weibull(shape, size) distribution
* @throws NotStrictlyPositiveException if {@code shape <= 0} or
* {@code scale <= 0}.
*/
public double nextWeibull(double shape, double scale) throws NotStrictlyPositiveException {
return new WeibullDistribution(getRandomGenerator(), shape, scale,
WeibullDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample();
}
/**
* Generates a random value from the {@link WeibullDistribution Weibull Distribution}.
*
* @param shape the shape parameter of the Weibull distribution
* @param scale the scale parameter of the Weibull distribution
* @return random value sampled from the Weibull(shape, size) distribution
* @throws NotStrictlyPositiveException if {@code shape <= 0} or
* {@code scale <= 0}.
*/
public double nextWeibull(double shape, double scale) throws NotStrictlyPositiveException {
return new WeibullDistribution(getRandomGenerator(), shape, scale,
WeibullDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample();
}