Circular Mean

The circular mean function is used for analyzing directional data. It can be used to model periodic data or dealing with angular variables. The formal defintion is provided below.

CircMean(θ1,θ2,,θn)=atan2(1ni=1nsinθi,1ni=1ncosθi)\operatorname{CircMean}(\theta_1, \theta_2, \ldots, \theta_n) = \operatorname{atan2}\left(\frac{1}{n}\sum_{i=1}^n\sin\theta_i, \frac{1}{n}\sum_{i=1}^n\cos\theta_i\right)

where atan2 is a two-sided argument arctangent function

CircularMean(int[] calldata nums, uint[] calldata weights, uint8 precision) returns the Circular Mean of nums with weights. Precision represents the number of decimal places and can be set between 0 and 18. It is applied to nums, weights and the output.

function CircularMean(int256[] calldata nums, uint256[] calldata weights, uint8 precision) external returns (int256);

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

func (con *NeuralMath) CircularMean(gas *big.Int, nums []*big.Int, weights []*big.Int, precision uint8) (*big.Int, error, *big.Int) {
	mul, err := validatePrecision(precision)
	if err != nil {
		return nil, err, nil
	}
	if len(nums) != len(weights) {
		return nil, errors.New("len(nums) != len(weights)"), nil
	}
	w, err := toFloats(weights, mul)
	if err != nil {
		return nil, err, nil
	}
	floats, err := toFloats(nums, mul)
	if err != nil {
		return nil, err, nil
	}
	var mean big.Int
	big.NewFloat(stat.CircularMean(floats, w) * mul).Int(&mean)
	return &mean, nil, nil
}

Last updated