stats/base/dists/f/cdf loses accuracy for small x and returns 0 for very small x

Aperta Adatta ai principianti
#16,655 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
javascript
Ambito
data

Direzione di ricerca

La issue indica che lib/main.js, lib/factory.js e src/main.c contengono la stessa logica. Leggi l’implementazione di CDF e i relativi test, quindi confronta i branch attuali con gli input stabili della funzione beta descritti nella issue. Verifica i casi segnalati per valori piccoli di x rispetto ai valori attesi ed esegui i test pertinenti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Description

The F distribution CDF is inaccurate for small x and returns exactly 0 once d1*x/d2 is below about 1e-16.

For d1*x <= d2, the implementation evaluates betainc( d2/(d2+(d1*x)), d2/2, d1/2, true, true ). The first argument is 1 - d1*x/(d2+d1*x), which loses most of its significant digits when x is small (and rounds to exactly 1 for tiny x), so the upper regularized incomplete beta is computed from a heavily rounded input.

The branches appear to be swapped relative to the numerically stable choice (also used by Boost's fisher_f CDF): when d1*x <= d2, compute betainc( (d1*x)/(d2+(d1*x)), d1/2, d2/2, true, false ) (small argument, no cancellation), and when d1*x > d2, compute betainc( d2/(d2+(d1*x)), d2/2, d1/2, true, true ).

With the branches swapped, the maximum relative error over a grid of d1, d2 in {0.5, 1, 2, 3, 5, 10, 30} and x from 1e-20 to 1e8 drops from 1.0 to about 1.6e-15 (compared with SciPy and mpmath).

The same logic is in lib/main.js, lib/factory.js, and src/main.c.

Related Issues

None

Questions

No.

Demo

N/A

Reproduction

var cdf = require( '@stdlib/stats/base/dists/f/cdf' );

console.log( cdf( 1.0e-20, 2.0, 3.0 ) );
console.log( cdf( 1.0e-12, 2.0, 3.0 ) );
console.log( cdf( 1.0e-12, 10.0, 30.0 ) );

Expected Results
1.0e-20
9.9999999999916667e-13
4.7851851851586008e-59

(reference values computed with mpmath at 40 digits; SciPy's `f.cdf` agrees)
Actual Results
0
1.0000333894309431e-12
4.789970438279987e-59
Version

@stdlib/stats-base-dists-f-cdf 0.2.3 (the same code is on develop)

Environments

Node.js

Browser Version

N/A

Node.js / npm Version

Node.js v24.16.0, npm 11.13.0

Platform

Windows 11

Checklist
  • Read and understood the Code of Conduct.
  • Searched for existing issues and pull requests.
Lingua principale
JavaScript
Stelle
6k
Fork
1.3k
Merge medio
1g 21h
PR unite (30g)
224

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di stdlib-js/stdlib

Tutte le issue di stdlib-js/stdlib

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.