Chi Square

The chi square function is used to calculate the chi-square statistic which is used for testing the goodness of fit of a distribution or model to observed data. In machine learning, it is used for feature selection, evaluating the independence of categorical variables, and implementing chi-square tests for model evaluation or hypothesis testing. The formal definition is defined below where O is the observed value and E is the expected value under the null hypothesis.

χ2=i=1n(OiEi)2Ei\chi^2 = \sum_{i=1}^n \frac{(O_i - E_i)^2}{E_i}

To implement the ChiSquare function, see the method signature below. Precision represents the number of decimal places and can be set between 0 and 18. It is applied to cals, exps and output.

function ChiSquare(int[] calldata vals, int[] calldata exps, uint8 precision) external returns (int);

Under the hood, the following functions are utilized directly in the Neural client.

func (con *NeuralMath) ChiSquare(gas *big.Int, vals []*big.Int, exps []*big.Int, precision uint8) (*big.Int, error, *big.Int) {
	//TODO: Add errror handling
	mul, err := validatePrecision(precision)
	if err != nil {
		return nil, err, nil
	}

	f, err := toFloats(vals, mul)
	if err != nil {
		return nil, err, nil
	}
	e, err := toFloats(exps, mul)
	if err != nil {
		return nil, err, nil
	}
	n := stat.ChiSquare(f, e)
	if invalidFloat(n) {
		return nil, errors.New("Inf/NaN result"), nil
	}
	x := big.NewInt(0)
	v, _ := big.NewFloat(n * mul).Int(x)
	return v, nil, nil
}

Last updated