Bhattacharyya

Bhattacharyya(int[] calldata p, int[] calldata q, uint8 precision) returns the distance between the probability distributions p and q given by

ln(ipiqi)-\ln ( \sum_i \sqrt{p_i q_i} )

The lengths of p and q must be equal. It is assumed that p and q sum to 1. Precision representes the number of decimal places between 0 and 18. It is applied to p,q and the output.

function Bhattacharyya(int[] calldata p, int[] calldata q, uint8 precision) external returns (int)

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

func (con *NeuralMath) Bhattacharyya(gas *big.Int, p []*big.Int, q []*big.Int, precision uint8) (*big.Int, error, *big.Int) {
	mul, err := validatePrecision(precision)
	if err != nil {
		return nil, err, nil
	}
	if len(p) != len(q) {
		return nil, errors.New("p must have the same length as q"), nil
	}

	pf, err := toFloats(p, mul)
	if err != nil {
		return nil, err, nil
	}
	qf, err := toFloats(q, mul)
	if err != nil {
		return nil, err, nil
	}
	distance := stat.Bhattacharyya(pf, qf)
	return toBig(distance, mul), nil, nil
}

Last updated