I wrote an article titled “The LogBeta and LogGamma Functions Using C#” in the August 2022 edition of Microsoft Visual Studio Magazine. See https://visualstudiomagazine.com/articles/2022/08/02/logbeta-loggamma-functions-csharp.aspx.
The article presents C# implementations of three of the most important classical statistics functions: the log-beta function, the log-gamma function and the regularized incomplete beta function.
The regularized incomplete beta function is usually written as Ix(a,b) or I(x; a,b). It is used for many problems, including statistical analysis of variance (ANOVA). The Ix(a,b) function calls the log-beta function. The log-beta function calls the log-gamma function.
Because beta and gamma return values can be huge, it’s standard to work with the log of each, which doesn’t grow as quickly.
When n is an integer, Gamma(n) = Factorial(n-1). The LogGamma(z) function cannot be computed exactly when z is not an integer and so there are several approximation algorithms. The demo program uses what is called the Lanczos g=5, n=7 algorithm. It is:
static double LogGamma(double z)
{
// Lanczos approximation g=5, n=7
double[] coef = new double[7] { 1.000000000190015,
76.18009172947146, -86.50532032941677,
24.01409824083091, -1.231739572450155,
0.1208650973866179e-2, -0.5395239384953e-5 };
double LogSqrtTwoPi = 0.91893853320467274178;
if (z "lt" 0.5)
return Math.Log(Math.PI / Math.Sin(Math.PI * z)) -
LogGamma(1.0 - z);
double zz = z - 1.0;
double b = zz + 5.5; // g + 0.5
double sum = coef[0];
for (int i = 1; i "lt" coef.Length; ++i)
sum += coef[i] / (zz + i);
return (LogSqrtTwoPi + Math.Log(sum) - b) +
(Math.Log(b) * (zz + 0.5));
}
The log-beta function is relatively simple because it’s defined in terms of log-gamma:
static double LogBeta(double a, double b)
{
double logbeta = LogGamma(a) + LogGamma(b) -
LogGamma(a + b);
return logbeta;
}
The regularized incomplete beta function is extremely tricky to implement. See the article for details.
There are many external code libraries that have implementations of complicated statistical functions such as beta, log-beta, gamma, log-gamma and regularized incomplete beta. However, implementing statistical functions from scratch using C# is useful when you want to avoid an external dependency for technical or copyright reasons.

Beta versions of two famous computers. Left: The IBM 5100 was introduced in 1975 and was the predecessor to the IBM PC. The machine had a PALM processor running at 1.9 MHz and weighed 55 lbs. Right: The Apple-1 was introduced in 1976 and was the predecessor to the Apple II. The machine had a 6502 processor running at 1 MHz.


.NET Test Automation Recipes
Software Testing
SciPy Programming Succinctly
Keras Succinctly
R Programming
2026 Visual Studio Live
2025 Summer MLADS Conference
2026 DevIntersection Conference
2025 Machine Learning Week
2025 Ai4 Conference
2026 G2E Conference
2026 iSC West Conference
You must be logged in to post a comment.