Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test internal math functions F32, gradF32, grad2F1, gradRegIncBeta for infinite loops #27

Closed
1 of 4 tasks
syclik opened this issue Jul 6, 2015 · 13 comments
Closed
1 of 4 tasks
Assignees
Milestone

Comments

@syclik
Copy link
Member

syclik commented Jul 6, 2015

From @syclik on November 22, 2013 16:21

Issue #333 shows the gradregIncGamma function getting into an infinite loop. The additional functions need to be tested:

  • F32
  • gradF32
  • grad2F1
  • gradRegIncBeta

Copied from original issue: stan-dev/stan#410

@bob-carpenter
Copy link
Contributor

What tests do these function need? If anyone knows, please edit the issue to add a list of tests.

@bob-carpenter bob-carpenter changed the title test remaining src/stan/prob/internal_math.hpp functions test internal math functions F32, gradF32, grad2F1, gradRegIncBeta for infinite loops Aug 17, 2016
@syclik
Copy link
Member Author

syclik commented Sep 11, 2016

I started working on F32, but I'm having lots of trouble. Here's the branch:
feature/issue-27-internal_math_tests

The first problem I run into is F32() isn't returning the correct results for:

  EXPECT_NEAR(0.4,
              stan::math::F32(1.0, 3.0, -1.0, 10.0, 0.5, 1.0),
              1e-6);

This is in the unit test on the branch: ./runTests.py test/unit/math/prim/scal/fun/F32_test.cpp

If there's something wrong with the input, we really need to doc the function.

@syclik syclik modified the milestone: Future Nov 15, 2016
@syclik
Copy link
Member Author

syclik commented Dec 1, 2016

Bump. I've gotten a first test up and it doesn't look good.

Check out branch feature/issue-27-internal_math_tests

Run:

./runTests.py test/unit/math/prim/scal/fun/F32_test.cpp

@sakrejda sakrejda self-assigned this Feb 9, 2017
@sakrejda
Copy link
Contributor

sakrejda commented Feb 9, 2017

I started working on this branch.

@sakrejda
Copy link
Contributor

sakrejda commented Feb 9, 2017

Maybe I just haven't read the functions correctly but I was trying to find consequences to this issue in the user-facing functions and it turns out this test (that I just wrote) fails with a nan for our function.

TEST(ProbBetaBinomial, lcdf_matches_emdbook_lcdf) {
  int n = 8;
  int N = 10;
  double alpha = 3.0;
  double beta = 1.0;

  EXPECT_FLOAT_EQ(-1.849329, (stan::math::beta_binomial_lcdf(n, N, alpha, beta)));
}

With this output:

Running main() from gtest_main.cc
[==========] Running 2 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 2 tests from ProbBetaBinomial
[ RUN      ] ProbBetaBinomial.cdf_log_matches_lcdf
[       OK ] ProbBetaBinomial.cdf_log_matches_lcdf (0 ms)
[ RUN      ] ProbBetaBinomial.lcdf_matches_emdbook_lcdf
test/unit/math/prim/scal/prob/beta_binomial_cdf_log_test.cpp:23: Failure
Value of: (stan::math::beta_binomial_lcdf(n, N, alpha, beta))
  Actual: nan
Expected: -1.849329
[  FAILED  ] ProbBetaBinomial.lcdf_matches_emdbook_lcdf (0 ms)
[----------] 2 tests from ProbBetaBinomial (0 ms total)

@sakrejda
Copy link
Contributor

sakrejda commented Feb 9, 2017

Turns out F32 also needs to terminate when the denominator for the rising factorial bits goes to 0. Once I push it will match mathematica results and have tests for that condition. Also, sign needs to be applied after exp, also fixing that. I'm not sure it was ever right except for a limited number of arguments (?)

@bob-carpenter
Copy link
Contributor

bob-carpenter commented Feb 9, 2017 via email

@sakrejda
Copy link
Contributor

sakrejda commented Feb 9, 2017

It gets called by the beta-binomial CDF and it's related to some functions that make my delay-time models hang so I'm just going to plow through all these if I can. I am setting up some tests against results from Mathematica so if you have any suggestions about how to do that better I appreciate suggestions. It's one of the less common instances of the generalized hypergeometric function

@sakrejda
Copy link
Contributor

sakrejda commented Feb 10, 2017

I think this just became a meta-issue after I dug through these and figured out what they needed. I'll open and fix smaller ones as I go.

Currently, check as fixed some issues in these functions produce wrong results or infinite loops.

  • F32 assumes -1 * log(x) == log(-x), wrong result for x < 0
  • F32 fix for above issue may not respect logT accumulator (but passes test?)
  • F32 returns nan rather than terminating when denominator hits zero.
  • grad_F32 also assumes -1 * log(x) == log(-x), wrong result for x < 0
  • grad_F32 fix for above issue may not respect logT accumulator (but passes test?)
  • grad_F32 can hang indefinitely
  • grad_2F1 will silently return wrong results if not converging
  • grad_2F1 terminates after an arbitrary number of steps
  • grad_2F1 returns nan rather than terminating when denominator hits zero.
  • grad_2F1 does not have a log-scale accumulator, probably should

Design across these functions:

  • max_steps argument
  • throw domain_error if not converging
  • do ... while as in (new) F32
  • return through reference to output argument (?)

Some issues are maintenance-related

  • tests for values resulting in positive accumulator
  • tests for values resulting in positive accumulator
  • tests for values resulting in positive accumulator increment
  • tests for values resulting in positive accumulator increment
  • tests for throw-on-divergence, in general non-convergence should be caused by all positive a1,a2,a3,b1,b2 with large ratio or z > 1

Some issues are cosmetic but it makes it hard to figure out errors and write tests:

  • grad_F32 needs doc
  • grad_2F1 needs doc
  • common argument naming for hypergeometric functions
  • common internal naming
  • common doc format
  • hypergeometric functions should reference generalized hypergeometric function
  • common max_steps argument, applied in same way
  • prefer reference for out-type arguments over pointer (currently have both kinds!)
  • grad_F32 requires a pointer-to-array[6] but doesn't doc it, only clear from reading body
  • grad_reg_inc_beta needs doc
  • grad_inc_beta needs doc

@hoanguc3m
Copy link

I have a problem with grad_reg_inc_beta function.
void grad_reg_inc_beta(T& g1, T& g2, const T& a, const T& b,
const T& z, const T& digammaA, const T& digammaB,
const T& digammaSum, const T& betaAB)
For example, when z = 1, I would expect that g1 is 0.
I also compare different value for z and find that it is not close to the value in the "Derivatives of the Incomplete Beta Function" (https://www.jstatsoft.org/article/view/v003i01). I dont know this function is the same with this one or not. Thanks.

@hoanguc3m
Copy link

hoanguc3m commented Mar 28, 2017

For example, in the grad_reg_inc_beta_test.cpp

#include <stan/math/prim/scal.hpp>
#include <gtest/gtest.h>

TEST(grad_reg_inc_beta, 1) {
  double alpha = 1.0;
  double beta = 1.0;
  double y = 1.0;
  double digamma_alpha = stan::math::digamma(alpha);
  double digamma_beta = stan::math::digamma(beta);
  double digamma_sum = stan::math::digamma(alpha + beta);
  double betafunc = std::exp(stan::math::lbeta(alpha, beta));

  double g1 = 0;
  double g2 = 0;
  stan::math::grad_reg_inc_beta(g1, g2, alpha, beta, y,
                                digamma_alpha, digamma_beta, digamma_sum,
                                betafunc);
  EXPECT_FLOAT_EQ(0, g1);
  EXPECT_FLOAT_EQ(-std::numeric_limits<double>::infinity(), g2);
}

If I change alpha = 2.5, beta = 0.5. I would expect the same g1 = 0.

@sakrejda
Copy link
Contributor

sakrejda commented Mar 28, 2017 via email

yizhang-yiz referenced this issue in yizhang-yiz/math Feb 27, 2018
@sakrejda
Copy link
Contributor

sakrejda commented Apr 7, 2018

Basically all the boxes in this issue are checked, including for grad_reg_inc_beta that relies on a hypergeometric function deep down on the inside (like most of us).

@sakrejda sakrejda closed this as completed Apr 7, 2018
@mcol mcol modified the milestones: Future, v2.15.0 Jan 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants