Issue 90759

Summary: ZTEST not same as Excel
Product: Calc Reporter: drking <openoffice>
Component: programmingAssignee: oc
Status: CLOSED FIXED QA Contact: issues@sc <issues>
Severity: Trivial    
Priority: P3 CC: discoleo, don.troodon, issues, ooo, rb.henschel
Version: OOo 2.4.0Keywords: oooqa
Target Milestone: ---   
Hardware: All   
OS: All   
Issue Type: PATCH Latest Confirmation in: ---
Developer Difficulty: ---
Issue Depends on:    
Issue Blocks: 18704    
Attachments:
Description Flags
Description of the ZTEST problem
none
ZTEST: one-sided or one-tailed; proposed bug fix
none
patch as proposed
none
testcase none

Description drking 2008-06-15 21:43:49 UTC
ZTEST produces different results to Excel. Attached file has a review.
Comment 1 drking 2008-06-15 21:48:35 UTC
Created attachment 54497 [details]
Description of the ZTEST problem
Comment 2 discoleo 2008-06-16 09:46:58 UTC
I have tested this some time ago. The results are wrong both in Calc and in
Excel (although each program computes something different). I do not have the
slightest idea what these programs compute.

The issue is mentioned also on the draft OASIS document, and I commented on this
issue on the OASIS mailing list, see section 5 in this message:
http://lists.oasis-open.org/archives/office-comment/200805/msg00002.html
Comment 3 discoleo 2008-06-16 10:02:32 UTC
Just one notice, forgot to mention:
I tested this with MS Office XP on Win2k and again with OOo-dev m19.

Both results are wrong, as exemplified by comparison to R:
> x <- c(90,95,95,100)
> z.test(x,100,5)

        One Sample z-test

data:  x 
z = -2, n = 4.0, Std. Dev. = 5.0, Std. Dev. of the sample mean = 2.5,
p-value = 0.0455
alternative hypothesis: true mean is not equal to 100 
95 percent confidence interval:
 90.10009 99.89991 
sample estimates:
mean of x 
       95
Comment 4 drking 2008-06-16 11:13:55 UTC
Thanks, Leonard
It's difficult to see exactly what R is calculating. I've tried to go back to 
basics to show that there is some logic/validity to Excel's method, and indeed 
that there is a clear bug with Calc, with the 3 parameter version.

I've tried to get z.test to work in R but failed after a brief struggle. What 
happens if you set 
alternative (Direction of the alternative hypothesis) to 'greater', or 'less' 
or even 'two.sided'? I think Excel produces a 'one-sided' (not 'one-tailed') 
test which might correspond to 'greater'.
Comment 5 drking 2008-06-16 12:55:29 UTC
Ah, I figured how to use z.test in R (over lunch ;). Yes, it does give the same 
results as Excel:
z.test(x,100,5,'greater')

Even with the mean on the other side 
y<-c(110,105,105,100)
z.test(y,100,5,'greater')

I'm now convinced that 'one-sided' is the correct description for what Excel 
does - not one- or two- tailed.

Comment 6 Regina Henschel 2008-06-16 12:56:02 UTC
adding me to cc.
Comment 7 drking 2008-06-16 13:09:00 UTC
z.test does the two-tailed test by default:-
=====
> y<-c(110,105,105,100)
> z.test(y,100,5)
        One Sample z-test
data:  y 
z = 2, n = 4.0, Std. Dev. = 5.0, Std. Dev. of the sample mean = 2.5,
p-value = 0.0455
alternative hypothesis: true mean is not equal to 100 
95 percent confidence interval:
 100.1001 109.8999 
sample estimates:
mean of y 
      105 
======
p-value of 0.0455 is exactly twice what Excel gives - which is right, because 
when the sample mean is above the population mean Excel does the one-tailed 
test.

In summary this all hangs together.
Comment 8 discoleo 2008-06-16 16:25:49 UTC
Well, I am glad you figured out the R-syntax. You can access the help in R using:
> help('z.test')
[or whatever command you wish to know about]

Just to remove some confusion:

1.) one sided and single-tailed are the same
   [similarly 2-sided / 2-tailed are the same]

2.) statistical tests should be almost always 2-sided
   [one sided tests are necessary only in rare instances]

3.) R-syntax (with default values / possible options)
z.test(x, mu = 0, stdev, alternative = c("two.sided", "less", "greater"),
       sd = stdev, conf.level = 0.95, ...)

e.g. (you can copy / paste these lines in R)
 x <- c(90,95,95,100)
 z.test(x, mu = 100, sd = 5,'greater')  # one-sided greater
 z.test(x, mu = 100, sd = 5,'less')     # one sided less
 z.test(x, mu = 100, sd = 5,'two.sided')# two sided
 z.test(x, mu = 100, sd = 5 )     # two sided by default
 z.test(x, mu = 100, sd = sd(x) ) # compute the SD automatically

Just some words on using the z-test for people who did NOT figure out yet how to
do it in R:
 - you need first to install the TeachingDemos:
   go to the Menu: Packages -> Install packages ->
     -> select the mirror server -> select TeachingDemos
 - you need now to Load the package:
   go to the Menu: Packages -> Load package -> select TeachingDemos
 - Done.


> I'm now convinced that 'one-sided' is the correct description for
> what Excel does

Good to know, though I'll stick with R for serious statistics. ;-)
Comment 9 discoleo 2008-06-16 16:32:49 UTC
Small corrections:
[parameter 'alternative' needs to be explicitly stated, when naming all the
other parameters]

 x <- c(90,95,95,100)
 z.test(x, mu = 100, sd = 5, alternative = 'greater')  # one-sided greater
 z.test(x, mu = 100, sd = 5, alternative = 'less')     # one sided less
 z.test(x, mu = 100, sd = 5, alternative = 'two.sided')# two sided
 z.test(x, mu = 100, sd = 5 )     # two sided by default
 z.test(x, mu = 100, sd = sd(x) ) # compute the SD automatically
Comment 10 drking 2008-06-16 21:33:53 UTC
Created attachment 54518 [details]
ZTEST: one-sided or one-tailed; proposed bug fix
Comment 11 drking 2008-06-16 21:37:04 UTC
I'm on a roll on this one... ;)

Just attached a defence of my view that 'one-sided' is the correct term, plus a 
proposed bug fix.

Comment 12 discoleo 2008-06-16 22:29:04 UTC
Sneaking into the code for computing the variance/SD/SE, I noticed that Calc
uses an unstable algorithm. Calc should implement either a 2 pass code, or
alternatively a stable one pass algorithm.
Comment 13 discoleo 2008-06-16 23:22:22 UTC
My previous comment refers of course to the ztest code, the true SD-code is
corrected as afr as I know. ;)
Comment 14 drking 2008-06-17 08:30:37 UTC
You have a good point.

http://www.coventry.ac.uk/ec/~nhunt/pottel.pdf highlights a case where Excel's 
STDEV falls down. In fact Calc performs correctly with that data, so it might 
be better to simply call STDEV within ZTEST?

Comment 15 Regina Henschel 2008-06-17 15:33:38 UTC
Improving the statistical functions is an ongoing process. The function ZTEST
should depend on NORMSDIST. Therefore NORMSDIST has to be controlled and
improved first.

Excel is not good for comparison because EXCEL has only 7 digit accuracy very
often. Calc should give better accuracy if possible. It would be nice to get a
test document, which has correct results up to 15 digits for to see how good a
new implementation of ZTEST is.

You will find information on Excel's ZTEST on
http://office.microsoft.com/en-us/excel/HP052042111033.aspx?pid=CH062528191033
or the corresponding one for EXCEL 2007.

A better place to discuss how to implement ZTEST is not this issue but the
mailing list dev@sc.openoffice.org.
Comment 16 drking 2008-06-17 20:32:55 UTC
Hi Regina

Discussion moved to dev@sc.openoffice.org

Comment 17 Regina Henschel 2008-06-17 23:22:39 UTC
If third parameter is given, result is wrong.
Comment 18 Regina Henschel 2008-06-18 12:51:44 UTC
Created attachment 54565 [details]
patch as proposed
Comment 19 Regina Henschel 2008-06-18 12:52:40 UTC
Created attachment 54566 [details]
testcase
Comment 20 Regina Henschel 2008-06-18 12:58:21 UTC
I've added a patch which changes as described by drking.

I thing, that further work on ZTEST should be postponed, until the specification
in ODF1.2 is finished.
Comment 21 Regina Henschel 2009-07-17 22:07:55 UTC
Please apply the patch. The problem here is independent from the other problems
with ZTEST. OOo calculates wrong and that should be corrected. 
Comment 22 ooo 2009-07-20 11:10:18 UTC
Yes, sure, sorry this patch went unnoticed.
Comment 23 ooo 2009-08-20 22:12:01 UTC
In cws odff06:

revision 275201
sc/source/core/tool/interpr3.cxx

Just to be sure: would it be correct to speak of a one-sided test in the online
help, and ODFF? As in

"Returns the one-sided P value of a z test with standard distribution."?
Comment 24 drking 2009-08-21 08:59:41 UTC
I'm not sure that's exactly right. The p-value can refer to the value on the
vertical axis, whereas it's the area (cumulative probability) that is returned.

Because Excel has got their description wrong in the past (and may well still
have it wrong), and because the function returns an unconventional result  it
might be better to be *really* clear about this, with diagrams. The stuff I
wrote in 2008 is here:

http://wiki.services.openoffice.org/wiki/Documentation/How_Tos/Calc:_ZTEST_function

and you'd be very welcome to use it in ODFF of course

Comment 25 Regina Henschel 2009-08-21 14:49:54 UTC
I'm no statistician, but what about:

Calculates the probability of observing a standard normal value greater than the
z-core (M-µ)/(sigma/sqrt(N)), where M is the sample mean, N the sample size, and
µ and sigma the known mean and standard deviation of the population.

Or as short variant: Calculates the probability of observing a value greater
than the z-core.
Comment 26 drking 2009-08-22 22:27:20 UTC
Hi Regina

I think you mean 'z-score' rather than 'z-core'.

For your short form version, we really should make clear that this is a sample,
not the entire population.

There's a real problem understanding the function, because Excel has described
it incorrectly, and it also doesn't work as you might expect.

So my inclination is to use more words, rather than try to be succinct. I do
find the diagrams helpful for this, to highlight the difference between
one-sided and one/two-tailed. I'd suggest that it's helpful to be very clear
that this is not a one- or two-sided test - although I'd also be happy if we
simutaneously avoided the 'one-sided' label.

D
Comment 27 Regina Henschel 2009-08-22 23:33:36 UTC
"I think you mean 'z-score' rather than 'z-core'."
Yes.

"I do find the diagrams helpful for this, to highlight the difference between
one-sided and one/two-tailed."
The diagrams in the Wiki are indeed very useful, but the function description
cannot contain pictures. And the proposed 'one sided' is not exact enough,
because it does not say whether P(Z<z) or P(Z>z) is returned.

This issue is about wrong calculation. That is fixed and therefore the issue is
closed. But the current help "Returns the two-tailed P value of a z test with
standard distribution." is wrong. We need a description, that is right in
content and short enough to be shown in the function wizard. Can you make a
suggestion in a new issue? It should not exceed two lines (see IPMT for a
description which is too long). 
Comment 28 ooo 2009-08-24 12:35:48 UTC
@drking: if you come up with a new wording for the function wizard _within the
next 2-3 days_, please don't create a new issue but add to this issue instead, I
can easily change the resource in CWS odff06 as well where this patch was added.

However, we'd need a separate issue for the online help.
Comment 29 drking 2009-08-24 13:50:11 UTC
Regina and I are discussing this by private email and will come up with something

Comment 30 drking 2009-08-25 08:20:40 UTC
Our proposal for the function wizard:-

function description:
calculates the probability of a sample mean greater than the mean of
the given sample.

first parameter
The given sample, drawn from a normally distributed population

second parameter
The known mean of the population

third parameter
The known standard deviation of the population. If omitted, the standard
deviation of the given sample is used. 
Comment 31 ooo 2009-08-25 11:28:11 UTC
So, no mention of one-sided at all? Ok.
We probably should change the name of the 2nd parameter from "Number" to "mu" as
well. The 3rd is already named "sigma".
Comment 32 Regina Henschel 2009-08-25 12:05:37 UTC
Yes, "number" is too unspecific. For the application help I have suggested
"mean", but "mu" is fine too and shorter.
Comment 33 ooo 2009-08-25 12:29:16 UTC
In cws odff06:

revision 275349
sc/source/ui/src/scfuncs.src
Comment 34 ooo 2009-08-26 14:06:09 UTC
In http://sc.openoffice.org/servlets/ReadMsg?list=dev&msgNo=3673 Leonard Mada
suggested the following description:

"calculates the probability of observing a value as large or larger for the
z-statistic"

Would that do?
Comment 35 Regina Henschel 2009-08-26 17:06:37 UTC
No. The comparison "larger" is incomplete, missing a "than" part.
Comment 36 ooo 2009-09-03 13:34:43 UTC
I used this one now:

Calculates the probability of observing a z-statistic greater than the one
computed based on a sample.

revision 275752
sc/source/ui/src/scfuncs.src
Comment 37 ooo 2009-09-03 21:16:04 UTC
Reassigning to QA for verification.
Comment 38 oc 2009-09-09 16:15:59 UTC
verified in internal build cws_odff06