fslaborg / fslaborg/FSharp.Stats

[Feature Request] hard coded edge-case results for approximated functions

Open
#215 2 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
F#
Stars
227
Forks
58
Avg merge
55m
Merged PRs (30d)
1

Description

I am not sure if this can be labeled as a bug, please adjust accordingly. Functions that are approximated (such as gamma, erfcx, etc.) may return incorrect results for edge cases such as +/- infinity.

Example: gamma(infinity) should be infinity, but returns nan via approximation:

![image](https://user-images.githubusercontent.com/21338071/175292475-3dc05f8c-5e1a-4deb-bd6b-e0468309f04c.png)

https://www.wolframalpha.com/input?i=gamma%28infinity%29

My suggestion would be catching edge cases on the input, so in the case of the gamma approximation:

```fsharp
let gamma z =
match z with
| z when (infinity.Equals(z)) -> infinity
| z when ((-infinity).Equals(z)) -> nan
| _ ->
let lanczosCoefficients = [76.18009172947146;-86.50532032941677;24.01409824083091;-1.231739572450155;0.1208650973866179e-2;-0.5395239384953e-5]
let rec sumCoefficients acc i coefficients =
match coefficients with
| [] -> acc
| h::t -> sumCoefficients (acc + (h/i)) (i+1.0) t
let gamma = 5.0
let x = z - 1.0
Math.Pow(x + gamma + 0.5, x + 0.5) * Math.Exp( -(x + gamma + 0.5) ) * Math.Sqrt( 2.0 * Math.PI ) * sumCoefficients 1.000000000190015 (x + 1.0) lanczosCoefficients

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.