Variance (Arraylist function): Difference between revisions
m (Automatically generated page update) |
mNo edit summary |
||
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
{{Template:Arraylist:Variance subtitle}} | {{Template:Arraylist:Variance subtitle}} | ||
This | 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== | ==Syntax== | ||
{{Template:Arraylist:Variance syntax}} | {{Template:Arraylist:Variance syntax}} | ||
===Syntax terms=== | ===Syntax terms=== | ||
<table class="syntaxTable"> | <table class="syntaxTable"> | ||
<tr><th>%variance</th><td> | <tr><th>%variance</th> | ||
<td>A <var>Float</var> variable to contain the numeric result.</td></tr> | |||
<tr><th>al</th> | <tr><th>al</th> | ||
<td>Arraylist object</td></tr> | <td>An <var>Arraylist</var> object.</td></tr> | ||
<tr><th> | |||
<td><var> | <tr><th>itemFunction</th> | ||
<td>A function that operates on the type of the items in the <var>Arraylist</var>. It may be a [[Local and Common entities#Defining and invoking a local method|local method]] or [[Method variables|method variable]] or a class member (<var>Variable</var>, <var>Property</var>), and it must return an [[Intrinsic classes|intrinsic]] (probably <var>Float</var>) value. | |||
<p> | |||
The default <var class="term">itemFunction</var> value is the special identity function, <var>[[Collections#Using the This function as the Maximum parameter|This]]</var>, which simply returns the item value.</p></td></tr> | |||
</table> | </table> | ||
==Usage notes== | ==Usage notes== | ||
<ul> | |||
<li>The optional <var class="term">itemFunction</var> parameter lets you further manipulate the <var>Arraylist</var> item values before calculating the variance. The <var class="term">itemFunction</var> default (identity function <var>This</var>) fails against non-intrinsic values, however. If your collection items are <i>not</i> intrinsic values, you must specify an <var class="term">itemFunction</var> function that can map the item values to intrinsic values. | |||
<p> | |||
For example, for an <var>Arraylist</var> 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 <var>Variance</var> method's <var class="term">itemFunction</var> parameter: </p> | |||
<p class="code">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 | |||
</p> | |||
The result is <code>23.4542568616187</code>. </ul> | |||
==Examples== | ==Examples== | ||
<p class="code">b | |||
%al is arrayList of float | |||
%al = new | |||
%al = [[List (Arraylist function)|list]](5, 3, 8) | |||
print %al:variance | |||
end | |||
</p> | |||
The result is: | |||
<p class="output">4.22222222222222 | |||
</p> | |||
==See also== | ==See also== | ||
{{Template:Arraylist:Variance footer}} | {{Template:Arraylist:Variance footer}} |
Latest revision as of 16:51, 27 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. |
itemFunction | 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 itemFunction value is the special identity function, This, which simply returns the item value. |
Usage notes
- The optional itemFunction parameter lets you further manipulate the Arraylist item values before calculating the variance. The itemFunction default (identity function This) fails against non-intrinsic values, however. If your collection items are not intrinsic values, you must specify an itemFunction function that can map the item values to intrinsic values.
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 itemFunction 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 is23.4542568616187
.
Examples
b %al is arrayList of float %al = new %al = list(5, 3, 8) print %al:variance end
The result is:
4.22222222222222