Git Product home page Git Product logo

incbeta's Introduction

Build Status

Genann logo

Incomplete Beta Function

incbeta.c contains only one function. It is the regularized incomplete beta function. It is released under the zlib license.

You'll need a compiler with lgamma to compile it. Any C99 complier should work.

More info here.

Example

    /* Call it with a, b, x. */
    double r = incbeta(10, 10, 0.3); /*0.03255*/

How does it work?

This solves the continued fraction using Lentz's algorithm.

I wrote up an article about how it works here.

Why would I use this?

Maybe you're trying to do a statistics test, and you don't want to pull in a huge dependency like the GNU Scientific Library just to get one function you need. Or maybe you don't want to use Cephes math library because it's not very clear how that's licensed. Maybe you don't want to steal code from Numerical Recipes because it is definitely not open-source.

So use this instead. It works, it's very small and easy, and it's released under the zlib license.

What can I do with it?

Well, it's used as a building block in a lot of statistics functions.

For example, you can use this to calculate Student's t cumulative distribution function like this:

double student_t_cdf(double t, double v) {
    /*The cumulative distribution function (CDF) for Student's t distribution*/
    double x = (t + sqrt(t * t + v)) / (2.0 * sqrt(t * t + v));
    double prob = incbeta(v/2.0, v/2.0, x);
    return prob;
}

incbeta's People

Contributors

codeplea avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

incbeta's Issues

Multithreading safety

Hi @codeplea,

I made use of this function in the MRtrix3 software to deal with compilation on systems where our primary mathematics library dependency, Eigen, is missing the corresponding function. Its use is in converting t-values / F-values to Z-scores prior to statistical enhancement within a generalised statistical inference framework that operates on different forms of neuroimaging data.

This code was recently flagged by Thread Sanitiser. Our code generates a non-parametric null distribution by evaluating random permutations of the data across multiple threads. The issue is that std::lgamma() is not thread-safe. It evaluates the logarithm of the absolute value of the gamma function. In order to not lose information where the gamma function is negative, it stores the sign of the gamma function in a static external variable. There may be in some environments an alternative implementation lgamma_r(), which takes as a second input a pointer to an integer where that sign is instead written, which can therefore be used in a thread safe manner.

Can I suggest:

  • Explicitly test that both inputs a and b to the regularised incomplete beta function are positive (ie. I presume you're not handling analytic continuation)?
  • Add a note in the documentation about relevance of threading race conditions (ie. if I'm understanding correctly, these should be irrelevant since the inputs to std::lgamma() are always positive and therefore the sign of the gamma function is always positive)?

Cheers
Rob

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.