patchPSPP - Patches: patch #8052, Harmonic means for descriptive...


You are not allowed to post comments on this tracker with your current authentication level.

patch #8052: Harmonic means for descriptive statistics

Submitter:  Teppo Jussmäki <jssmk>
Submitted:  Mon 20 May 2013 09:51:49 AM UTC
Category:  None Item Group:  None
Status:  Need Info Assigned to:  None
Open/Closed:  Open

Sun 26 May 2013 09:27:05 AM UTC, comment #5: 

Thanks for making sure of the definition! I did not have any reliable good sources at hand.

I am going to review the code and do changes for accepting negative values and cheking for any zeros in the dataset.

Teppo Jussmäki <jssmk>
Sun 26 May 2013 08:16:52 AM UTC, comment #4: 

The SPSS documentation doesn't mention any limits on the domain of the input.
(it says the weights must be positive, but does not mention the values).

In the special case, where a value is zero. It can be shown algebraically that the harmonic mean is also zero:

h = a.b / (a+b)
  = a.0  / a

So I retract my previous statement.  I think now that if the sum-of-reciprocals method is used, there should be a special case when one or more values are zero.  But I still don't see why negative values should be treated specially.

I cannot find in the academic literature any indication that harmonic means are defined only for positive values.
This would also fit with the parallel resistor analogy (yes negative resistors do exist - kindof - see Horowitz and Hill).

John Darrington <jmd>
Group administrator
Sat 25 May 2013 08:50:13 AM UTC, comment #3: 

I must admit I was a bit confused about the definition of harmonic mean of sets with non-positive values. Wikipedia gives the definition of harmonic mean for positive real numbers. OpenOffice also produces errors with sets including zeros and negatives.

WolframAlpha does calculations for sets such as {2, -2, 3} -> 9, and also {2, -2, 0} -> 0, which should be handled as an exeption. In the Wikipedia definition, harmonic mean should always be less or equal with arithmetic mean. The zero handling in the Wolfram version is quite intuitive if you consider the resistance of parallel resistors: if even one resistor is zero, the circuit is equivalent of all-zero resistors in parallel.

There seems to be different types of implementations in applications. Which one to follow?

Teppo Jussmäki <jssmk>
Sat 25 May 2013 05:33:36 AM UTC, comment #2: 

+      /* Sum of reciprocals only needed for variables with only positive values */
+      m->sumreci = (value <= 0. || m->sumreci == SYSMIS) ? SYSMIS : m->sumreci + 1 / value * weight;

I don't understand why there is this exception for values less than or equal to zero.

The harmonic mean is well defined for negative values, so we don't need to make a special case there.  Where a value is exactly equal to zero, the reciprocal of course is infinite.  However since we are dealing with floating point numbers, this will be recognised by the floating point library and the end result will be (correctly) NaN.  So as far as I can see, we don't need to special case zero either.

John Darrington <jmd>
Group administrator
Tue 21 May 2013 03:13:33 AM UTC, comment #1: 

This looks nice to me.  To apply this, we'd have to get the GNU copyright assignment process started.  Are you willing to do that?  I can give you instructions here, if you like, or you can email me at

Ben Pfaff <blp>
Group administrator
Mon 20 May 2013 09:51:49 AM UTC, original submission:  

Harmonic means for descriptive statistics (descriptives and frequencies). Calculated in moments.c. I separated counting of harmonic means in a new function void moments_calc_additional.


GET FILE="Speed_and_distance.sav".

WEIGHT BY Distance_km.

    /VARIABLES= Speed_kmph

Files modified:

Teppo Jussmäki <jssmk>


(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attached Files
file #28127:  harmonic_fixed.patch added by jssmk (7KiB - application/octet-stream - Previous patch file had some issues with truncated lines. Use with --ignore-whitespace. The patch does not include any changes in PSPP documentation files.)
file #28115:  harmonic.diff added by jssmk (7KiB - application/octet-stream)
file #28116:  Speed_and_distance.sav added by jssmk (469B - application/octet-stream)


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -email is unavailable- added by jmd (Posted a comment)
  • -email is unavailable- added by blp (Posted a comment)
  • -email is unavailable- added by jssmk (Submitted the item)
  • -email is unavailable- added by jssmk (New patch, harmonic mean)

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.


    Follow 5 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2013-05-21 jssmk Attached File- Added harmonic_fixed.patch, #28127
    2013-05-21 blp StatusNone Need Info
    2013-05-20 jssmk Attached File- Added harmonic.diff, #28115
        Attached File- Added Speed_and_distance.sav, #28116
        Carbon-Copy- Added jmd

    Back to the top

    Powered by Savane 3.13-758e.
    Corresponding source code