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:
_numbers - 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 [["_numbers", [], [[]]], ["_ddof", 0, [0]]];

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

private _mean = 0;
{_mean = _mean + _x} forEach _numbers;
_mean = _mean / _count;

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

sqrt (_resSumSqrs / (_count - _ddof)) // return
2 changes: 1 addition & 1 deletion addons/arrays/test.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#define DEBUG_MODE_FULL
#include "script_component.hpp"

#define TESTS ["filter", "inject", "join", "shuffle", "findNil", "findNull", "findTypeName", "findTypeOf", "findMax", "findMin", "insert"]
#define TESTS ["filter", "inject", "join", "shuffle", "findNil", "findNull", "findTypeName", "findTypeOf", "findMax", "findMin", "insert", "standardDeviation"]
SCRIPT(test-arrays);

// ----------------------------------------------------------------------------
Expand Down
32 changes: 32 additions & 0 deletions addons/arrays/test_standardDeviation.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// ----------------------------------------------------------------------------
#define DEBUG_MODE_FULL
#define DEBUG_SYNCHRONOUS
#include "script_component.hpp"

SCRIPT(test_standardDeviation);

// ----------------------------------------------------------------------------

private ["_expected", "_result", "_fn"];

_fn = "CBA_fnc_standardDeviation";
LOG("Testing " + _fn);

TEST_DEFINED("CBA_fnc_standardDeviation","");

// Test population standard deviation
_result = [[1, 2, 3, 4, 5, 6, 7]] call CBA_fnc_standardDeviation;
_expected = 2;
TEST_OP(_result,isEqualTo,_expected,_fn);

// Test sample standard deviation
_result = [[1, 2, 3], 1] call CBA_fnc_standardDeviation;
_expected = 1;
TEST_OP(_result,isEqualTo,_expected,_fn);

// Test empty array
_result = [[]] call CBA_fnc_standardDeviation;
_expected = 0;
TEST_OP(_result,isEqualTo,_expected,_fn);

nil;