Variance

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

iwi(ximean)2/(sumiwi1)\sum_i w_i (x_i - mean)^2 / (sum_i w_i - 1)

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, mean and output.

function Variance(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) Variance(gas *big.Int, x, weights []*big.Int, precision uint8) (*big.Int, error, *big.Int) {
	mul, err := validatePrecision(precision)
	if err != nil {
		return nil, err, nil
	}
	if len(x) != len(weights) {
		return nil, errors.New("x must have the same length as weghts"), nil
	}
	xf, err := toFloats(x, mul)
	if err != nil {
		return nil, err, nil
	}
	wf, err := toFloats(weights, mul)
	if err != nil {
		return nil, err, nil
	}
	variance := stat.Variance(xf, wf)
	return toBig(variance, mul), nil, nil
}

Last updated