-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1407 from Kexanone/achilles-port
Add function to get standard deviation (CBA_fnc_standardDeviation)
- Loading branch information
Showing
4 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |