<commit_before>package onlinestats

import "math"

type Stats interface {
	Mean() float64
	Var() float64
	Len() int
}

// From https://github.com/codahale/ministat/blob/master/src/ministat.c

// TTest implements a Student's t-test.
func TTest(rs, ds Stats, confidx Confidence) float64 {

	i := ds.Len() + rs.Len() - 2

	var t float64

	if i > nstudent {
		t = student[0][confidx]
	} else {
		t = student[i][confidx]
	}

	dn := float64(ds.Len())
	rn := float64(rs.Len())

	spool := (dn-1)*ds.Var() + (rn-1)*rs.Var()
	spool /= dn + rn - 2
	spool = math.Sqrt(spool)
	s := spool * math.Sqrt(1/dn+1/rn)
	d := ds.Mean() - rs.Mean()
	e := t * s

	if math.Abs(d) <= e {
		// no difference
		return 0
	}

	return d
}

// Confidence is a confidence level for the Student's t-test
type Confidence int

const (
	Conf80 Confidence = iota
	Conf90
	Conf95
	Conf98
	Conf99
	Conf99p5
)

const nconf = 6

var studentpct = [nconf]float64{80, 90, 95, 98, 99, 99.5}

const nstudent = 100

// +1 because 0 is used for 'more than 100 samples'
var student = [nstudent + 1][nconf]float64{
	/* inf */ {1.282, 1.645, 1.960, 2.326, 2.576, 3.090},<commit_msg>Replace t-test with Welch's<commit_after>package onlinestats

import "math"

type Stats interface {
	Mean() float64
	Var() float64
	Len() int
}

// Welch implements a Welch's t-test.
func Welch(xs, ys Stats, confidx Confidence) float64 {

	xvar := xs.Var()
	yvar := ys.Var()

	xn := float64(xs.Len())
	yn := float64(ys.Len())

	wvar := (xvar/xn + yvar/yn)

	vnum := wvar * wvar
	vdenom := (xvar*xvar)/(xn*xn*(xn-1)) + (yvar*yvar)/(yn*yn*(yn-1))
	v := vnum / vdenom

	var tdist float64

	if v > nstudent {
		tdist = student[0][confidx]
	} else {
		vlower := math.Trunc(v)
		vfract := v - vlower

		vidx := int(vlower)

		tdist = student[vidx][confidx]
		// linear approximation
		tdist += vfract * (student[vidx+1][confidx] - student[vidx][confidx])
	}

	sampleStdErr := math.Sqrt(wvar)
	diff := ys.Mean() - xs.Mean()
	e := tdist * sampleStdErr

	if math.Abs(diff) <= e {
		// no difference
		return 0
	}

	return diff
}

// From https://github.com/codahale/ministat/blob/master/src/ministat.c

// Confidence is a confidence level for the Student's t-test
type Confidence int

const (
	Conf80 Confidence = iota
	Conf90
	Conf95
	Conf98
	Conf99
	Conf99p5
)

const nconf = 6

var studentpct = [nconf]float64{80, 90, 95, 98, 99, 99.5}

const nstudent = 100

// +1 because 0 is used for 'more than 100 degrees of freedom'
var student = [nstudent + 1][nconf]float64{
	/* inf */ {1.282, 1.645, 1.960, 2.326, 2.576, 3.090},
{'fraction_non_alphanumeric': 0.1580188679245283, 'fraction_numerical': 0.06485849056603774, 'mean_word_length': 3.2416666666666667, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4948835', 'n_tokens_mistral': 1204, 'n_tokens_neox': 1036, 'n_words': 277}