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} count _numbers;
Kexanone marked this conversation as resolved.
Show resolved Hide resolved
_mean = _mean / _count;

private _resSumSqrs = 0;
{
_resSumSqrs = _resSumSqrs + (_x - _mean)^2;
} count _numbers;
Kexanone marked this conversation as resolved.
Show resolved Hide resolved

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
4 changes: 0 additions & 4 deletions addons/arrays/test_findMax.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ TEST_TRUE(isNil "_result",_fn);
_result = [1, "not a number", 3, 4, 5] call CBA_fnc_findMax;
TEST_TRUE(isNil "_result",_fn);

// Test invalid array given
_result = "not an array" call CBA_fnc_findMax;
TEST_TRUE(isNil "_result",_fn);

// Test empty array given
_result = [] call CBA_fnc_findMax;
TEST_TRUE(isNil "_result",_fn);
Expand Down
4 changes: 0 additions & 4 deletions addons/arrays/test_findMin.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ TEST_TRUE(isNil "_result",_fn);
_result = [1, "not a number", 3, 4, 5] call CBA_fnc_findMin;
TEST_TRUE(isNil "_result",_fn);

// Test invalid array given
_result = "not an array" call CBA_fnc_findMin;
TEST_TRUE(isNil "_result",_fn);

// Test empty array given
_result = [] call CBA_fnc_findMin;
TEST_TRUE(isNil "_result",_fn);
Expand Down
5 changes: 0 additions & 5 deletions addons/arrays/test_findNil.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,4 @@ _result = [] call CBA_fnc_findNil;
_expected = -1;
TEST_OP(_result,==,_expected,_fn);

// Return a not found when a non array is given
_result = "not an array" call CBA_fnc_findNil;
_expected = -1;
TEST_OP(_result,==,_expected,_fn);

nil;
5 changes: 0 additions & 5 deletions addons/arrays/test_findNull.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,4 @@ _result = [] call CBA_fnc_findNull;
_expected = -1;
TEST_OP(_result,==,_expected,_fn);

// Return a not found when a non array is given
_result = "not an array" call CBA_fnc_findNull;
_expected = -1;
TEST_OP(_result,==,_expected,_fn);

nil;
5 changes: 0 additions & 5 deletions addons/arrays/test_findTypeName.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,6 @@ _result = [[], "STRING"] call CBA_fnc_findTypeName;
_expected = -1;
TEST_OP(_result,==,_expected,_fn);

// Return not found if non array is given
_result = ["not an array", "STRING"] call CBA_fnc_findTypeName;
_expected = -1;
TEST_OP(_result,==,_expected,_fn);

// Return not found if parameters are nil
_result = [nil, nil] call CBA_fnc_findTypeName;
_expected = -1;
Expand Down
10 changes: 0 additions & 10 deletions addons/arrays/test_findTypeOf.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,6 @@ _result = [[], typeOf player] call CBA_fnc_findTypeOf;
_expected = -1;
TEST_OP(_result,==,_expected,_fn);

// Return not found, when a non array is passed
_result = ["not an array", typeOf player] call CBA_fnc_findTypeOf;
_expected = -1;
TEST_OP(_result,==,_expected,_fn);

// Return not found, when search parameter is not an object or string
_result = [["", player, 5, objNull], 5] call CBA_fnc_findTypeOf;
_expected = -1;
TEST_OP(_result,==,_expected,_fn);

// Return not found, when parameters are nil
_result = [nil, nil] call CBA_fnc_findTypeOf;
_expected = -1;
Expand Down
4 changes: 0 additions & 4 deletions addons/arrays/test_shuffle.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ TEST_OP(count _result,==,count _original,_fn);
TEST_OP(_x,in,_original,_fn);
} forEach _result;

_original = [1, 2, 3];
_result = _original call CBA_fnc_shuffle;
TEST_OP(count _result,==,1,_fn);

_original = [];
_result = [_original] call CBA_fnc_shuffle;
TEST_OP(count _result,==,count _original,_fn);
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;