Statistical Summaries
Mean With Count
Summing Active Values
A mean is a sum divided by the number of active observations.
Program
Play the program to change how many values contribute to the mean.
mean_with_count.f90
Replay: real traced execution (multi-file project)
program mean_with_count_demo
implicit none
integer :: values(4)
integer :: active_count
integer :: i
integer :: total
real :: mean
values = [4, 6, 11, 15]
active_count = 3
total = 0
do i = 1, active_count
total = total + values(i)
end do
mean = real(total) / real(active_count)
print '(I0, 1X, F0.1)', active_count, mean
end program mean_with_count_demo
program mean_with_count_demo
implicit none
integer :: values(4)
integer :: active_count
integer :: i
integer :: total
real :: mean
values = [4, 6, 11, 15]
active_count = 2
total = 0
do i = 1, active_count
total = total + values(i)
end do
mean = real(total) / real(active_count)
print '(I0, 1X, F0.1)', active_count, mean
end program mean_with_count_demo
program mean_with_count_demo
implicit none
integer :: values(4)
integer :: active_count
integer :: i
integer :: total
real :: mean
values = [4, 6, 11, 15]
active_count = 4
total = 0
do i = 1, active_count
total = total + values(i)
end do
mean = real(total) / real(active_count)
print '(I0, 1X, F0.1)', active_count, mean
end program mean_with_count_demo
values ← [4, 6, 11, 15]
9values = [4, 6, 11, 15]10active_count = 3values this step[4, 6, 11, 15]valuesactive_count ← 3
9values = [4, 6, 11, 15]10active_count = 311total = 0values this step3active_counttotal ← 0
10active_count = 311total = 012do i = 1, active_countvalues this step0totali ← 1
11total = 012do i = 1, active_count13 total = total + values(i)values this step1itotal ← 4
12do i = 1, active_count13 total = total + values(i)14end dovalues this step0 → 4total4values(1)i ← 2
11total = 012do i = 1, active_count13 total = total + values(i)values this step2itotal ← 10
12do i = 1, active_count13 total = total + values(i)14end dovalues this step4 → 10total6values(2)i ← 3
11total = 012do i = 1, active_count13 total = total + values(i)values this step3itotal ← 21
12do i = 1, active_count13 total = total + values(i)14end dovalues this step10 → 21total11values(3)mean ← 7.0
14end do15mean = real(total) / real(active_count)16print '(I0, 1X, F0.1)', active_count, meanvalues this step7.0mean21total3active_countprint '(I0, 1X, F0.1)', active_count, mean
15 mean = real(total) / real(active_count)16 print '(I0, 1X, F0.1)', active_count, mean17end program mean_with_count_demooutput3 7.0values this step3active_count7.0mean
values ← [4, 6, 11, 15]
9values = [4, 6, 11, 15]10active_count = 2values this step[4, 6, 11, 15]valuesactive_count ← 2
9values = [4, 6, 11, 15]10active_count = 211total = 0values this step2active_counttotal ← 0
10active_count = 211total = 012do i = 1, active_countvalues this step0totali ← 1
11total = 012do i = 1, active_count13 total = total + values(i)values this step1itotal ← 4
12do i = 1, active_count13 total = total + values(i)14end dovalues this step0 → 4total4values(1)i ← 2
11total = 012do i = 1, active_count13 total = total + values(i)values this step2itotal ← 10
12do i = 1, active_count13 total = total + values(i)14end dovalues this step4 → 10total6values(2)mean ← 5.0
14end do15mean = real(total) / real(active_count)16print '(I0, 1X, F0.1)', active_count, meanvalues this step5.0mean10total2active_countprint '(I0, 1X, F0.1)', active_count, mean
15 mean = real(total) / real(active_count)16 print '(I0, 1X, F0.1)', active_count, mean17end program mean_with_count_demooutput2 5.0values this step2active_count5.0mean
values ← [4, 6, 11, 15]
9values = [4, 6, 11, 15]10active_count = 4values this step[4, 6, 11, 15]valuesactive_count ← 4
9values = [4, 6, 11, 15]10active_count = 411total = 0values this step4active_counttotal ← 0
10active_count = 411total = 012do i = 1, active_countvalues this step0totali ← 1
11total = 012do i = 1, active_count13 total = total + values(i)values this step1itotal ← 4
12do i = 1, active_count13 total = total + values(i)14end dovalues this step0 → 4total4values(1)i ← 2
11total = 012do i = 1, active_count13 total = total + values(i)values this step2itotal ← 10
12do i = 1, active_count13 total = total + values(i)14end dovalues this step4 → 10total6values(2)i ← 3
11total = 012do i = 1, active_count13 total = total + values(i)values this step3itotal ← 21
12do i = 1, active_count13 total = total + values(i)14end dovalues this step10 → 21total11values(3)i ← 4
11total = 012do i = 1, active_count13 total = total + values(i)values this step4itotal ← 36
12do i = 1, active_count13 total = total + values(i)14end dovalues this step21 → 36total15values(4)mean ← 9.0
14end do15mean = real(total) / real(active_count)16print '(I0, 1X, F0.1)', active_count, meanvalues this step9.0mean36total4active_countprint '(I0, 1X, F0.1)', active_count, mean
15 mean = real(total) / real(active_count)16 print '(I0, 1X, F0.1)', active_count, mean17end program mean_with_count_demooutput4 9.0values this step4active_count9.0mean
active count
`active_count` controls how much of the fixed array is summarized.
running total
The loop adds each active observation into `total`.
real division
`real(total) / real(active_count)` keeps the mean as a real number.