Post Reply 
HP49-50G Statistics: Variance (Pop/Sample) & Mean, with frequencies
03-24-2022, 04:28 PM (This post was last modified: 01-12-2023 01:40 PM by Gil.)
Post: #1
HP49-50G Statistics: Variance (Pop/Sample) & Mean, with frequencies
Here is a very basic, elementary small prog to calculate the pop-variance, sample-variance and mean when dealing with frequencies.

Suppose you want to calculate the variances and mean of
the four values 5,6,7 and 10.

Write the Matrix
[[5]
[6]
[7]
[10]]
and launch —>Var

Suppose now that you have 10 times the value 5, 20 times the value 6, 30 times 7 and 1 time the value 10.

Write then the following Matrix, adding a column for the frequencies:
[[10 5]
[20 6]
[30 7]
[ 1 10]]
and launch again —>Var
Note: For a single argument like here with the Matrix in stack level 1, the program will then always suppose, automatically, that the frequencies are in the first column (and the corresponding values in the second column).

Suppose now it is not the case.
Let's say that you have the following Matrix in the stack:
[[36 10 5]
[26 20 6]
[89 30 7]
[ 45 1 10]]
As the previous frequencies appear now in column 2 and the values in column 3, you will have to "tell that piece of information" to the program, adding the following instruction in stack level 1
{2 3}
Now launch (with the matrix in stack level 2)
—>Var
Note that inside the {} you must have two numbers: the first one being for the frequencies col#, the second one standing for the col# of the corresponding x-values.

The code is the following:
\<< "1 Arg or 2 Arg

If 1 Arg
\[]Matrix [nx2]
1st col: freq
2nd col: val
OR
\[]Matrix [nx1]
col: val
(all freq=1)

If 2 Arg
\[]Matrix [n x k]
\[]{k1 k2}
k1 col: freq
k2 col: val
" DROP DUP TYPE 5 ==
IF
THEN DUP OBJ\-> DROP 4 PICK UNROT
ELSE DUP 1 2
END ROT DUP SIZE OBJ\-> DROP 0 0 0 \-> cf cx M i j \GSf \GSX \GSX2
\<< j 1 ==
IF
THEN M { i 1 } 1 CON 1 COL+ 'M' STO
END 1 i
FOR i M { i cf } GET \-> f
\<< f '\GSf' STO+ M { i cx } GET DUP f * '\GSX' STO+ SQ f * '\GSX2' STO+
\>>
NEXT \GSf "N" \->TAG \GSX "\GSX" \->TAG \GSX \GSf / \->NUM "Mean" \->TAG DUP \GSX2 DUP \->NUM "\GSX2" \->TAG UNROT \GSf / SWAP SQ - \->NUM "Vpop" \->TAG DUP \GSf * \GSf 1 - / \->NUM "Vsam" \->TAG
\>>
\>>

The output will be:

[[36 10 5 ]
[ 26 20 6 ]
[ 89 30 7 ]
[45 1 10 ]]
{2 3}
:N: 61
:SumX: 390
:Mean: 6.39344262295
:SumX2: 2540.
:Vpop: .7632356893
:Vsam: .775956284122

Regards


Attached File(s)
.hp  VAR.01.hp (Size: 753 bytes / Downloads: 1)
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
HP49-50G Statistics: Variance (Pop/Sample) & Mean, with frequencies - Gil - 03-24-2022 04:28 PM



User(s) browsing this thread: 1 Guest(s)