HP Forums
Means: geometric, weighted, harmonic - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: HP Prime Software Library (/forum-15.html)
+--- Thread: Means: geometric, weighted, harmonic (/thread-9762.html)



Means: geometric, weighted, harmonic - salvomic - 12-23-2017 11:20 AM

Here there is a simple program to calculate some means, given lists of values.
• gmean Geometric mean of {list1} (all numbers in the list must be >0)
• wmean Weighted mean of {list1} with weights {wlist}
• hmean Harmonic mean of {list1} (all numbers must be >0)

Enjoy!

Salvo M.

Code:

// Geometric mean
// list1 mustn't have 0 and must contain positive numbers
EXPORT gmean(list1)
BEGIN
RETURN surd(ΠLIST(list1),SIZE(list1));
END;

// Weighted mean
// list1 items, wlist weights
EXPORT wmean(list1, wlist)
BEGIN
RETURN ΣLIST(list1*wlist)/ΣLIST(wlist);
END;

// Harmonic mean
// list1 mustn't contain 0
EXPORT hmean(list1)
BEGIN
RETURN (ΣLIST((list1)^(-1)))^(-1)*SIZE(list1);
END;

EDIT:: thanks to Carlos29pz for info: I didn't remember that we can use the already present command mean(list1, list2) to get weighted mean, so please, use my program only for geometric and harmonic means, since those command won't be built-in the Prime.

EDIT 2 :: see also the thread Means API by StephenG1CMZ with some other means