Skip to content

Commit

Permalink
Update base test class for beta distribution
Browse files Browse the repository at this point in the history
Added exceptions when parameters are not strictly positive.

Do not raise an exception for x=0 or x=1 when alpha or beta<1 and return
the limit.

Add special cases for x=0 and x=1 when alpha or beta = 1.
  • Loading branch information
aherbert committed Sep 28, 2021
1 parent a78ee39 commit 62ea141
Show file tree
Hide file tree
Showing 27 changed files with 653 additions and 364 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ public class BetaDistribution extends AbstractContinuousDistribution {
*/
public BetaDistribution(double alpha,
double beta) {
if (alpha <= 0) {
throw new DistributionException(DistributionException.NOT_STRICTLY_POSITIVE, alpha);
}
if (beta <= 0) {
throw new DistributionException(DistributionException.NOT_STRICTLY_POSITIVE, beta);
}

this.alpha = alpha;
this.beta = beta;
z = LogGamma.value(alpha) + LogGamma.value(beta) - LogGamma.value(alpha + beta);
Expand All @@ -63,28 +70,48 @@ public double getBeta() {
return beta;
}

/** {@inheritDoc} */
/** {@inheritDoc}
*
* <p>The density is not defined when {@code x = 0, alpha < 1}, or {@code x = 1, beta < 1}.
* In this case the limit of infinity is returned.
*/
@Override
public double density(double x) {
return Math.exp(logDensity(x));
}

/** {@inheritDoc} **/
/** {@inheritDoc}
*
* <p>The density is not defined when {@code x = 0, alpha < 1}, or {@code x = 1, beta < 1}.
* In this case the limit of infinity is returned.
*/
@Override
public double logDensity(double x) {
if (x < 0 ||
x > 1) {
return Double.NEGATIVE_INFINITY;
} else if (x == 0) {
if (alpha < 1) {
throw new DistributionException(DistributionException.TOO_SMALL,
alpha, 1.0);
// Distribution is not valid when x=0, alpha<0
// due to a divide by zero error.
// Do not raise an exception and return the limit.
return Double.POSITIVE_INFINITY;
}
// Special case of cancellation: x^(a-1) (1-x)^(b-1) / B(a, b)
if (alpha == 1) {
return -z;
}
return Double.NEGATIVE_INFINITY;
} else if (x == 1) {
if (beta < 1) {
throw new DistributionException(DistributionException.TOO_SMALL,
beta, 1.0);
// Distribution is not valid when x=1, beta<0
// due to a divide by zero error.
// Do not raise an exception and return the limit.
return Double.POSITIVE_INFINITY;
}
// Special case of cancellation: x^(a-1) (1-x)^(b-1) / B(a, b)
if (beta == 1) {
return -z;
}
return Double.NEGATIVE_INFINITY;
} else {
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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.
parameters = 0.1 0.1
# Computed using Matlab
mean = 0.5
variance = 0.208333333333333
lower = 0
upper = 1
cdf.points = -0.1, 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1
cdf.values = 0 0 0.406385093936276 0.439709190223346 0.462804186115552 0.482120045609328 0.500000000000000 0.517879954390672 0.537195813884448 0.560290809776654 0.593614906063724 1 1
pdf.values = 0 Infinity 0.442988958666524 0.263938762316795 0.206639715373253 0.183240320487425 0.176630277977874 0.183240320487425 0.206639715373253 0.263938762316795 0.442988958666524 Infinity 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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.
parameters = 0.5 4.0
# Computed using Matlab
mean = 0.111111111111111
variance = 0.017957351290685
lower = 0
upper = 1
cdf.points = -0.1, 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1
cdf.values = 0 0 0.626625082597741 0.804984471899925 0.898778484206134 0.950264436880598 0.977796095859523 0.991483736629099 0.997455625383593 0.999522385942406 0.999971488851370 1 1
pdf.values = 0 Inf 2.52142232809988 1.25219806739988 0.684938469046825 0.373544048607390 0.193349510480697 0.0903696114115064 0.0352965948694063 0.00978279740156159 0.00115291373026972 0 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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.
parameters = 1.0 0.1
# Computed using Matlab
mean = 0.909090909090909
variance = 0.039354584809130
lower = 0
upper = 1
cdf.points = -0.1, 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1
cdf.values = 0 0 0.0104807417937856 0.0220672314570715 0.0350389048801824 0.0497997834943236 0.0669670084631926 0.0875564634445192 0.113431849434787 0.148660077479215 0.205671765275719 1 1
pdf.values = 0 0.100000000000000 0.109946584245135 0.122241596067866 0.137851585017117 0.158366702750946 0.186606598307362 0.228110884138870 0.295522716855071 0.425669961260392 0.794328234724282 Inf 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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.
parameters = 1.0 0.5
# Computed using Matlab
mean = 0.66666666666666663
variance = 0.088888888888888892
lower = 0
upper = 1
cdf.points = -0.1, 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1
cdf.values = 0 0 0.051316701949486218 0.10557280900008409 0.16333997346592444 0.22540333075851654 0.2928932188134527 0.36754446796632401 0.45227744249483381 0.55278640450004213 0.68377223398316211 1 1
pdf.values = 0 0.5 0.52704627669472992 0.55901699437494745 0.59761430466719689 0.64549722436790291 0.70710678118654768 0.79056941504209488 0.9128709291752769 1.1180339887498951 1.58113883008419 Inf 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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.
parameters = 1.0 1.0
# Computed using Matlab
mean = 0.5
variance = 0.083333333333333329
lower = 0
upper = 1
cdf.points = -0.1, 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1
cdf.values = 0.0, 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.0
pdf.values = 0 1 1 1 1 1 1 1 1 1 1 1 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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.
parameters = 1.0 2.0
# Computed using Matlab
mean = 0.33333333333333331
variance = 0.055555555555555552
lower = 0
upper = 1
cdf.points = -0.1, 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1
cdf.values = 0 0 0.19000000000000006 0.35999999999999999 0.51000000000000023 0.64000000000000001 0.75 0.83999999999999997 0.90999999999999992 0.95999999999999996 0.98999999999999999 1 1
pdf.values = 0 2 1.8 1.6000000000000001 1.3999999999999999 1.2 1 0.80000000000000004 0.60000000000000009 0.39999999999999991 0.19999999999999996 0 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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.
parameters = 1.0 4.0
# Computed using Matlab
mean = 0.20000000000000001
variance = 0.026666666666666672
lower = 0
upper = 1
cdf.points = -0.1, 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1
cdf.values = 0 0 0.34389999999999987 0.59040000000000026 0.75990000000000002 0.87040000000000006 0.9375 0.97440000000000004 0.9919 0.99839999999999995 0.99990000000000001 1 1
pdf.values = 0 4 2.9160000000000008 2.0480000000000005 1.3719999999999999 0.8640000000000001 0.50000000000000022 0.25600000000000012 0.10800000000000005 0.031999999999999994 0.0039999999999999983 0 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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.
parameters = 2.0 0.1
# Computed using Matlab
mean = 0.95238095238095233
variance = 0.01462950771706532
lower = 0
upper = 1
cdf.points = -0.1, 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1
cdf.values = 0 0 0.00058554921172346416 0.0025085760862129224 0.0060900720265878658 0.011791774834096561 0.020315358886352223 0.032809851251190306 0.051372078895221711 0.080552883677552423 0.13418222415053294 1 1
pdf.values = 0 0 0.012094124266964851 0.026893151134930546 0.045491023055648561 0.069681349210416327 0.10263362906904888 0.15055318353165442 0.22755249197840488 0.37458956590914555 0.78638495237703931 Inf 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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.
parameters = 2.0 0.5
# Computed using Matlab
mean = 0.80000000000000004
variance = 0.045714285714285714
lower = 0
upper = 1
cdf.points = -0.1, 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1
cdf.values = 0 0 0.0038825370469605129 0.016130089900092535 0.0378409694858131 0.070483996910219962 0.1161165235168156 0.17780780835622126 0.26057454736802566 0.37390096630005898 0.54146973927558506 1 1
pdf.values = 0 0 0.079056941504209499 0.16770509831248426 0.26892643710023856 0.38729833462074181 0.53033008588991071 0.71151247353788538 0.95851447563404069 1.3416407864998741 2.1345374206136567 Inf 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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.
parameters = 2.0 1.0
# Computed using Matlab
mean = 0.66666666666666663
variance = 0.055555555555555552
lower = 0
upper = 1
cdf.points = -0.1, 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1
cdf.values = 0 0 0.010000000000000009 0.040000000000000029 0.089999999999999983 0.16000000000000003 0.25000000000000006 0.35999999999999999 0.48999999999999977 0.64000000000000012 0.81000000000000005 1 1
pdf.values = 0 0 0.20000000000000007 0.40000000000000002 0.59999999999999987 0.80000000000000004 1 1.2 1.3999999999999999 1.6000000000000001 1.8 2 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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.
parameters = 2.0 2.0
# Computed using Matlab
mean = 0.5
variance = 0.049999999999999996
lower = 0
upper = 1
cdf.points = -0.1, 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1
cdf.values = 0 0 0.028000000000000008 0.10400000000000008 0.2159999999999998 0.35200000000000004 0.5 0.64799999999999991 0.78400000000000003 0.89600000000000002 0.97199999999999998 1 1
pdf.values = 0 0 0.54000000000000004 0.96000000000000008 1.2599999999999998 1.4400000000000002 1.5 1.4400000000000002 1.26 0.95999999999999985 0.53999999999999981 0 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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.
parameters = 0.1 0.5
# Computed using Matlab
mean = 0.166666666666667
variance = 0.086805555555556
lower = 0
upper = 1
cdf.points = -0.1, 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1
cdf.values = 0 0 0.704833622051221 0.759304219445319 0.795176530367368 0.823494838536657 0.848001712399770 0.870603436980005 0.892658587836406 0.915640640375004 0.942366288315790 1 1
pdf.values = 0 Infinity 0.739458664410644 0.420303655461325 0.311944266317603 0.260079151351645 0.233065049073820 0.221140881157234 0.222272786172717 0.241401058813832 0.307055512274369 Infinity 0
Loading

0 comments on commit 62ea141

Please sign in to comment.