Variance (Arraylist function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
mNo edit summary
Line 64: Line 64:
   
   
%al = new
%al = new
%al:add(5)
%al = [[List (Arraylist function)|list]](5, 3, 8)
%al:add(3)
 
%al:add(8)
print %al:variance
print %al:variance
end
end

Revision as of 21:40, 12 July 2012

Compute the variance of the items (Arraylist class)

[Introduced in Sirius Mods 7.8]


This method returns the "mean standard deviation" of the values of the items in the collection. From statistics, this is the average of the squares of the deviations of the value of each item from the mean of all the items.

Syntax

%variance = al:Variance[( [itemFunction])]

Syntax terms

%variance A Float variable to contain the numeric result.
al An Arraylist object.
method A function that operates on the type of the items in the Arraylist. It may be a local method or method variable or a class member (Variable, Property), and it must return an intrinsic (probably Float) value.

The default method value is the special identity function, This, which simply returns the item value.

Usage notes

  • The optional method parameter lets you further manipulate the Arraylist item values before performing the Variance method's operation. If your Arraylist's items are not intrinsic values, you must specify a function that can map the item values to intrinsic values or the method will fail.

    For example, for an Arraylist that is a list of coordinates, you could return the variance of their distance from the origin by first applying a local function as the Variance method's method parameter:

    b class point public constructor new(%x is float, %y is float) variable x is float variable y is float end public constructor new(%x is float, %y is float) %this:x = %x %this:y = %y end constructor end class local function (point):distance is float return (%this:x * %this:x + %this:y * %this:y):squareRoot end function %al is arrayList of object point %al = new %al:add(new(1,1)) %al:add(new(3,4)) %al:add(new(-5,12)) print %al:variance(distance) end

    The result is 23.4542568616187.

Examples

b %al is arrayList of float %al = new %al = list(5, 3, 8) print %al:variance end

The result is:

4.22222222222222

See also