PopVariance

function PopVariance(int[] calldata x, int[] calldata weights, uint8 precision) returns the unbiased weighted sample variance according to the following formula

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 PopVariance(int[] calldata x, int[] calldata weights, uint8 precision) external returns (int)

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

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

Last updated