4

I am interested to build a R function that I can use to test the limits of the Taylor series approximation. I am aware that there is limits to what I am doing, but it's exactly those limits I wish to investigate.

I have two normally distributed random variables x and y. x has a mean of 7 and a standard deviation (sd) of 1. y has a mean of 5 and a sd of 4.

me.x <- 4; sd.x <- 1
me.y <- 5; sd.y <- 4

I know how to estimate the mean ratio of y/x, like this

# E(y/x) = E(y)/E(x) - Cov(y,x)/E(x)^2 + Var(x)*E(y)/E(x)^3
me.y/me.x - 0/me.x^2 + sd.x*me.y/me.x^3
[1] 1.328125

I am however stuck on how to estimate the Standard Deviation of the ratio? I realize I have to use a Taylor expansion, but not how to use it.

Doing a simple simulation I get

 x <- rnorm(10^4, mean = 4, sd = 1);  y <- rnorm(10^4, mean = 5, sd = 4)
 sd(y/x)
 [1] 2.027593
 mean(y/x)[1]
 1.362142
Eric Fail
  • 8,191
  • 8
  • 72
  • 128
  • 1
    Did you look at http://www.stat.cmu.edu/~hseltman/files/ratio.pdf? – Severin Pappadeux Feb 08 '16 at 22:30
  • Yes, but I honestly was more overwhelmed then anything else. Could you possible _hold my hand a bit_ and show me the first steps? – Eric Fail Feb 08 '16 at 22:31
  • In a few hours? a bit busy now – Severin Pappadeux Feb 08 '16 at 22:37
  • Sure, I'll post it here if I figure it out in the interim. Thanks! – Eric Fail Feb 08 '16 at 22:38
  • I don't think a Taylor series approximation is going to be useful here. (1) The s.d. of the ratio may not exist. Example: ratio of normal (0, 1) variables has a Cauchy distribution, which has no mean or higher moments. (2) Even in cases in which the s.d. exists, a Taylor series may give a poor approximation. What is your larger goal here? Maybe we can suggest a different approach. – Robert Dodier Feb 09 '16 at 00:30
  • @RobertDodier, thank you for your thoughtful comment. My larger goal is to have an R function that can use to test the limits of the Taylor series approximation. Does that make sense? I am aware that there is limits to what I am doing, but it's exactly those limits I wish to investigate. – Eric Fail Feb 09 '16 at 00:51
  • I don't think that makes sense -- a Taylor series or any approximation will yield some number. But the s.d. doesn't exist -- there's nothing to compare the approximation to. – Robert Dodier Feb 09 '16 at 06:57
  • @RobertDodier, thank you for your comment. I am not sure I understand. Can't I compare the s.d. to what I get in my simulation? Like I compare the mean. – Eric Fail Feb 09 '16 at 15:38
  • See comments in my answer below. The point is that *in the cases where the SD doesn't exist* (i.e. the integral of x^2*PDF(x) is divergent), your simulation results will be wonky/all over the map. As long as you recognize that, you can do something reasonable. – Ben Bolker Feb 09 '16 at 15:58
  • Eric, basically based on our analysis, your Taylor expansion won't make any sense. It will produce some finite number, but there is nothing to compare it to - you cannot compare some finite number to infinite number and get sensible conclusions. I propose you play with the code, find set ov values where SD is finite and ask another question about taylor expansion. – Severin Pappadeux Feb 09 '16 at 16:13
  • Update: I've made a mistake in my yesterday code, braces around the constant, please check update. It won't change the result, though (it affects the constant, and normalization), SD is still infinite. – Severin Pappadeux Feb 09 '16 at 16:15

3 Answers3

5

There is an analytical expression for the PDF of the ratio of two gaussians, done by David Hinkley (e.g. see Wikipedia). So we could compute all momentums, means etc. I typed it and apparently it clearly doesn't have finite second momentum, thus it doesn't have finite standard deviation. Note, I've denoted your Y gaussian as my X, and your X as my Y (formulas assume X/Y). I've got mean value of ratio pretty close to the what you've got from simulation, but last integral is infinite, sorry. You could sample more and more values, but from sampling std.dev is growing as well, as noted by @G.Grothendieck

library(ggplot2)

m.x <- 5; s.x <- 4
m.y <- 4; s.y <- 1

a <- function(x) {
    sqrt( (x/s.x)^2 + (1.0/s.y)^2 )
}

b <- function(x) {
    (m.x*x)/s.x^2 + m.y/s.y^2
}

c <- (m.x/s.x)^2 + (m.y/s.y)^2

d <- function(x) {
    u <- b(x)^2 - c*a(x)^2
    l <- 2.0*a(x)^2
    exp( u / l )
}

# PDF for the ratio of the two different gaussians
PDF <- function(x) {
    r <- b(x)/a(x)
    q <- pnorm(r) - pnorm(-r)

    (r*d(x)/a(x)^2) * (1.0/(sqrt(2.0*pi)*s.x*s.y)) * q + exp(-0.5*c)/(pi*s.x*s.y*a(x)^2)
}

# normalization
nn <- integrate(PDF, -Inf, Inf)
nn <- nn[["value"]]

# plot PDF
p <- ggplot(data = data.frame(x = 0), mapping = aes(x = x))
p <- p + stat_function(fun = function(x) PDF(x)/nn) + xlim(-2.0, 6.0)
print(p)

# first momentum
m1 <- integrate(function(x) x*PDF(x), -Inf, Inf)
m1 <- m1[["value"]]

# mean
print(m1/nn)

# some sampling
set.seed(32345)
n <- 10^7L
x <- rnorm(n, mean = m.x, sd = s.x); y <- rnorm(n, mean = m.y, sd = s.y)
print(mean(x/y))
print(sd(x/y))

# second momentum - Infinite!
m2 <- integrate(function(x) x*x*PDF(x), -Inf, Inf)

Thus, it is impossible to test any Taylor expansion for std.dev.

Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64
  • Since this is an integral for a particular case, do you know if the second moment always diverges or only for certain parameter ranges? – Ben Bolker Feb 09 '16 at 14:12
  • see my update. I strongly suspect that your statement ("doesn't have a SD") is only true sometimes (e.g. for the values given by the OP). – Ben Bolker Feb 09 '16 at 15:59
  • @BenBolker yes, I played with values as well, and could confirm that for some values result/SD is infinite, and for some it is not. – Severin Pappadeux Feb 09 '16 at 16:11
  • @SeverinPappadeux, thank you for your thorough response. I have a question, in line 50 and 51 of your code you write `print(mean(x/y))` and `print(sd(x/y))`, I can't really tell if `x/y` is assumed elsewhere in your code, but what I am trying to do in my example is `y/x`. It could easily be that I am not getting what you are trying to say, but could you explain the `x/y` in your code vs. the `y/x` in my question? Thanks! – Eric Fail Feb 10 '16 at 17:28
  • @EricFail I exchanged X and Y, (X <-> Y), because formulas for gaussian ratio PDF is written for X/Y. As you could note, you have N(5,4)/N(4,1) denoted as Y/X, and I have absolutely THE SAME N(5,4)/N(4,1) only denoted as X/Y. Compare me.x with m.y and se.x with s.y and vice versa. Sorry for the confusion, but changing D.Hinkley formulas would be a bit too much. – Severin Pappadeux Feb 10 '16 at 17:33
  • Anyway, for your parameters SD is infinite, we're all pretty sure about it. You might play with parameters as prof.Bolker and myself did, and find ones where second momentum is finite. For that set of values you might want to ask again question wrt Taylor expansion, and we could proceed further – Severin Pappadeux Feb 10 '16 at 17:35
3

With the cautions suggested by @G.Grothendieck in mind: a useful mnemonic for products and quotients of independent X and Y variables is

CV^2(X/Y) = CV^2(X*Y) = CV^2(X) + CV^2(Y)

where CV is the coefficient of variation (sd(X)/mean(X)), so CV^2 is Var/mean^2. In other words

Var(Y/X)/(m(Y/X))^2 = Var(X)/m(X)^2 + Var(Y)/m(Y)^2

or rearranging

sd(Y/X) = sqrt[ Var(X)*m(Y/X)^2/m(X)^2 + Var(Y)*m(Y/X)^2/m(Y)^2 ]

For random variables with the mean well away from zero, this is a reasonable approximation.

set.seed(101)
y <- rnorm(1000,mean=5)
x <- rnorm(1000,mean=10)
myx <- mean(y/x)
sqrt(var(x)*myx^2/mean(x)^2 + var(y)*myx^2/mean(y)^2)  ## 0.110412
sd(y/x)  ## 0.1122373

Using your example is considerably worse because the CV of Y is close to 1 -- I initially thought it looked OK, but now I see that it's biased as well as not capturing the variability very well (I'm also plugging in the expected values of the mean and SD rather than their simulated values, but for such a large sample that should be a minor part of the error.)

me.x <- 4; sd.x <- 1
me.y <- 5; sd.y <- 4
myx <- me.y/me.x - 0/me.x^2 + sd.x*me.y/me.x^3
x <- rnorm(1e4,me.x,sd.x); y <- rnorm(1e4,me.y,sd.y)
c(myx,mean(y/x))
sdyx <- sqrt(sd.x^2*myx^2/me.x^2 + sd.y^2*myx^2/me.y^2)
c(sdyx,sd(y/x))    
## 1.113172 1.197855

rvals <- replicate(1000,
    sd(rnorm(1e4,me.y,sd.y)/rnorm(1e4,me.x,sd.x)))
hist(log(rvals),col="gray",breaks=100)
abline(v=log(sdyx),col="red",lwd=2)
min(rvals)  ## 1.182698

enter image description here

All the canned delta-method approaches to computing the variance of Y/X use the point estimate for Y/X (i.e. m(Y/X) = mY/mX), rather than the second-order approximation you used above. Constructing higher-order forms for both the mean and the variance should be straightforward if possibly tedious (a computer algebra system might help ...)

mvec <- c(x = me.x, y = me.y)
V <- diag(c(sd.x, sd.y)^2)
car::deltaMethod(mvec, "y/x", V)
##     Estimate       SE
## y/x     1.25 1.047691

library(emdbook)
sqrt(deltavar(y/x,meanval=mvec,Sigma=V)) ## 1.047691

sqrt(sd.x^2*(me.y/me.x)^2/me.x^2 + sd.y^2*(me.y/me.x)^2/me.y^2)  ## 1.047691

For what it's worth, I took the code in @SeverinPappadeux's answer and made it into a function gratio(mx,my,sx,sy). For the Cauchy case (gratio(0,0,1,1)) it gets confused and reports a mean of 0 (which should be NA/divergent) but correctly reports the variance/std dev as divergent. For the parameters specified by the OP (gratio(5,4,4,1)) it gives mean=1.352176, sd=NA as above. For the first parameters I tried above (gratio(10,5,1,1)) it gives mean=0.5051581, sd=0.1141726.

These numerical experiments strongly suggest to me that the ratio of Gaussians sometimes has a well-defined variance, but I don't know when (time for another question on Math StackOverflow or CrossValidated?)

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • I've posted the answer with exact PDF, second momentum is infinite – Severin Pappadeux Feb 09 '16 at 04:36
  • Since the mean and s.d. don't exist, the approximations don't seem meaningful, unless one qualifies them differently: they are, perhaps, the mean and s.d. of distributions which are "close" in some sense (cross entropy? I don't know) to the distribution of the ratio. – Robert Dodier Feb 09 '16 at 07:01
  • As commented above; is the second moment *always* infinite or just in some cases (including the one given by the OP)? – Ben Bolker Feb 09 '16 at 14:13
  • see addition to my answer. I think the mean and SD *do* exist under some cases. – Ben Bolker Feb 09 '16 at 15:57
  • @BenBolker Update: I've made a mistake in my yesterday code, braces around the constant, please check update. It won't change the result, though (it affects the constant, and normalization), SD is still infinite. – Severin Pappadeux Feb 09 '16 at 16:17
  • @RobertDodier why do you think mean does not exist? Mean, I believe, exist, well defined and both direct integration and simulation give the value around 1.35 – Severin Pappadeux Feb 09 '16 at 16:21
  • @BenBolker if you post the question on SO, CV, ..., could you please provide link? I would be very interested to follow up – Severin Pappadeux Feb 09 '16 at 16:25
  • 1
    @BenBolker `it gets confused and reports a mean of 0`. I don't think it gets confused, it honestly integrates symmetric over 0 term `1/(1+x^2)` multiplied by antisymmetric value of `x`. I would guess any reasonable integration package will return `0` in this case. After all, we declare mean of Cauchy undefined, but not divergent – Severin Pappadeux Feb 09 '16 at 17:09
  • Having skimmed some of the papers on the Gaussian ratio distribution, I'm now *really* confused. Most of them seem to state (unconditionally) that the moments don't exist for these distributions, yet there are reasonably good analytical approximations under certain cases/within certain bounds. So (perhaps) the moments *never* exist but the heavy tails/divergence gets moved out to really extreme values when the coefficients of variation are small ... ?? – Ben Bolker Feb 09 '16 at 20:33
  • @BenBolker About the discrepancy (moments don't exist but there are published approximations), my guess is that the approximations are actually the moments of distributions which are in some sense "close to" the ratio distribution. E.g. if you truncate the tails away from zero, then there is no problematic 1/(1 + x^2) term, moments exist, everybody's happy. But if you look at samples from this happy distribution, it probably looks almost exactly the same as the "interesting" (I won't say "unhappy") ratio distribution. – Robert Dodier Feb 09 '16 at 20:48
  • @BenBolker `moments don't exist for these distributions` might be true in the same sense that we consider Cauchy mean to be undefined, though principal value is 0 and any reasonable integration package will compute 0. Thus, **R** integrates it to something reasonable, but from theoretical POV there is no moments. – Severin Pappadeux Feb 10 '16 at 17:51
2

Such approximations are unlikely to be useful since the distribution may not have a finite standard deviation. Look at how unstable it is:

set.seed(123)
n <- 10^6
X <- rnorm(n, me.x, sd.x)
Y <- rnorm(n, me.y, sd.y)

sd(head(Y/X, 10^3))
## [1] 1.151261

sd(head(Y/X, 10^4))
## [1] 1.298028

sd(head(Y/X, 10^5))
## [1] 1.527188

sd(Y/X)
## [1] 1.863168

Contrast that with what happens when we try the same thing with a normal random variable:

sd(head(Y, 10^3))
## [1] 3.928038

sd(head(Y, 10^4))
## [1] 3.986802

sd(head(Y, 10^5))
## [1] 3.984113

sd(Y)
## [1] 3.999024

Note: If you were in a different situation, e.g. the denominator has compact support, then you could do this:

library(car)

m <- c(x = me.x, y = me.y)
v <- diag(c(sd.x, sd.y)^2)
deltaMethod(m, "y/x", v)
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • Thank you for your input. I recognize that there's certain assumptions that needs to be met, I am however still interested to approximate this assuming that the distributions are normally distributed, unimodal, and roughly symmetric. – Eric Fail Feb 09 '16 at 00:48
  • X and Y being unimodal, normal and symmetric is not sufficient. If X and Y are standard normal then their ratio has no mean and variance. – G. Grothendieck Feb 09 '16 at 01:11
  • Good point. However, I am interested to construct an R function that can use to test the limits of the Taylor series approximation (as I said in a comment above). – Eric Fail Feb 09 '16 at 01:16
  • 1
    OK. I have added a note. This won't give a meaningful answer in the situation posed in the question but might be OK in certain situations. – G. Grothendieck Feb 09 '16 at 01:19
  • `Look at how unstable it is` yes, it doesn't have a finite standard deviation, please take a look at my answer – Severin Pappadeux Feb 09 '16 at 04:40
  • @G.Grothendieck I speculate that moments don't exist whenever the denominator variable (X in this case) has positive density in a neighborhood of zero. What do you think? I suppose this has probably been investigated but I am too lazy to look it up at the moment. – Robert Dodier Feb 09 '16 at 22:11
  • 1
    The t distribution is a counterexample. – G. Grothendieck Feb 09 '16 at 22:44
  • I'm not sure (but this is *definitely* turning into a conversation for somewhere else); it depends on what you mean, precisely, by "in a neighborhood". What does the square-root-inverse-chisq(d) look like for d>1? – Ben Bolker Feb 10 '16 at 18:42