PopMeanVariance

function PopMeanVariance(int[] calldata x, int[] calldata weights, uint8 precision) returns the the sample mean and biased variance according to the following formulas:

iwixi/(sumiwi)\sum_i w_i * x_i / (sum_i w_i)
iwi(ximean)2/(sumiwi)\sum_i w_i (x_i - mean)^2 / (sum_i w_i)

Lenghts of x and weights have to be equal. Precision represents the number of decimal places and can be set between 0 and 18. It is applied to x, weights and output.

function PopMeanVariance(int[] calldata x, int[] calldata weights, uint8 precision) external returns (int, int)

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

func (con *NeuralMath) PopMeanVariance(gas *big.Int, x, weights []*big.Int, precision uint8) (*big.Int, *big.Int, error, *big.Int) {
	mul, err := validatePrecision(precision)
	if err != nil {
		return nil, nil, err, nil
	}
	if len(x) != len(weights) {
		return nil, nil, errors.New("x must have the same length as weghts"), nil
	}
	xf, err := toFloats(x, mul)
	if err != nil {
		return nil, nil, err, nil
	}
	wf, err := toFloats(weights, mul)
	if err != nil {
		return nil, nil, err, nil
	}
	mean, variance := stat.PopMeanVariance(xf, wf)
	return toBig(mean, mul), toBig(variance, mul), nil, nil
}

Last updated