As stated by David in his Unpublished opinion piece : " A computer can calculate the standard deviation of a million numbers before your finger has had time to let go of the Enter key."
… yet I can find no trace of this useful function in GH or in any of the plugins I researched.
Am I missing something obvious ?
Meanwhile you can use MathNet that has many statistics function (this library is also used in G2)
I also used it a lot on my old work, as all parameters in my models were not a single number but a distribution model (mean + standard deviation for example).
/// <summary>
/// Calcule l'écart type d'un ensemble de valeurs connaissant la moyenne (division par n-1 au lieu de n)
/// </summary>
/// <param name="arg_lst_values">Liste ou tableau de valeurs</param>
/// <param name="arg_mean">Moyenne arithmétique</param>
/// <returns></returns>
public static double StandardDeviationUnbiased(IEnumerable<double> arg_lst_values, double arg_mean)
{
double standardDeviation = 0;
int number = 0;
foreach (double val in arg_lst_values)
{
if (!double.IsNaN(val))
{
standardDeviation += (val - arg_mean) * (val - arg_mean);
number++;
}
}
if (number > 1)
{
standardDeviation = Math.Sqrt(standardDeviation / ((double)number - 1.0));
}
return standardDeviation;
}
Maybe not exactly obvious, but it does the trick natively in Grasshopper 1: The standard Python library has a statistics module, which has a standard deviation function (and other useful functions):
I can’t seem to replicate the progressive slowdown, but @nathanletwory’s implementation of the variance function is still way faster than the standard library version! That’s good to know, thanks.
Yes Grasshopper 2, G2 is the command to launch Grasshopper 2 . I understand it was not very useful but it shows that some parts will be better covered by Grasshopper 2 (Field, statistics, functions …)
with the C# function 250521_StandardDeviation_02.gh (11.9 KB)
Looks like it’s just the IronPython implementation that’s much slower. The standard CPython implementation is on par with @laurent_delrieu’s C# script. But then again the CPython component comes with a potential input cost on large data sets: