Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to get standard deviation (CBA_fnc_standardDeviation) #1407

Merged
merged 12 commits into from
Jan 20, 2021
1 change: 1 addition & 0 deletions addons/arrays/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class CfgFunctions {
PATHTO_FNC(selectRandomArray);
PATHTO_FNC(shuffle);
PATHTO_FNC(sortNestedArray);
PATHTO_FNC(standardDeviation);
};
};
};
44 changes: 44 additions & 0 deletions addons/arrays/fnc_standardDeviation.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "script_component.hpp"
/* ----------------------------------------------------------------------------
Function: CBA_fnc_standardDeviation

Description:
Returns the standard deviation, a measure of the spread of a distribution,
of the array elements.

Parameters:
_array - The array from which the standard deviation is computed <ARRAY>
_ddof - The delta degrees of freedom [optional] <SCALAR> (default: 0)
_ddof = 0 - Population standard deviation
_ddof = 1 - Sample standard deviation

Returns:
_stdDev - The standard deviation <SCALAR>

Examples:
(begin example)
// returns roughly 5.564
[[1,4,16,4,1]] call CBA_fnc_standardDeviation;
// returns roughly 6.221
[[1,4,16,4,1], 1] call CBA_fnc_standardDeviation;
(end)

Author:
Kex
---------------------------------------------------------------------------- */

params [["_array", [], [[]]], ["_ddof", 0, [0]]];
Kexanone marked this conversation as resolved.
Show resolved Hide resolved

private _count = count _array;
if (_count <= _ddof) exitWith {0};

private _mean = 0;
{_mean = _mean + _x} forEach _array;
commy2 marked this conversation as resolved.
Show resolved Hide resolved
_mean = _mean / _count;

private _resSumSqrs = 0;
{
_resSumSqrs = _resSumSqrs + (_x - _mean)^2;
} forEach _array;

sqrt (_resSumSqrs / (_count - _ddof)) // return