%mathpiper,def="WeightedMean"


WeightedMean(list) :=
[

    Check(IsList(list), "Argument", "Argument must be a list.");
    
    Local( values, lastWeight, weights );
    
    values := {};
    
    weights := {};
    
    
    ForEach(element,list)
    [        
        Check(IsList(element), "Argument", "Values and their associated weights must be in a list.");
        
        Check(Length(element) = 2, "Argument", "Each value and its associated weight must be in a two element list.");
        
        values := values : element[1];

        weights := weights : element[2];
    ];
    
    Sum(values * weights)/Sum(weights);

];

%/mathpiper

    %output,preserve="false"
      Result: True
.   %/output



%mathpiper_docs,name="WeightedMean",categories="User Functions;Statistics & Probability",access="experimental"
*CMD WeightedMean --- weighted mean
*STD
*CALL 
	WeightedMean({{value, weight},...})

*PARMS

{value} -- a value.

{weight} -- the weight to associate with the value.

*DESC 
This function allows more weight to be associated with certain values and
less weight to others when calculating their mean.

*E.G.
In>  WeightedMean({{92,50}, {87,40}, {76,10}})
Result: 442/5

In>  N(WeightedMean({{92,50}, {87,40}, {76,10}}))
Result: 88.4

*SEE Mean, Median, Mode, GeometricMean
%/mathpiper_docs

    %output,preserve="false"
      
.   %/output


