The reverse/inverse of the normal distribution function in R
Asked Answered
S

3

17

To plot a normal distribution curve in R we can use:

(x = seq(-4,4, length=100))
y = dnorm(x)
plot(x, y)

enter image description here

If dnorm calculates y as a function of x, does R have a function that calculates x as a function of y? If not what is the best way to approach this?

Sheya answered 25/10, 2013 at 11:56 Comment(6)
Have you read pnorm ??Yb
Perhaps I'm missing something, but pnorm(y) doesn't give x, hence plot(pnorm(y), y) does not give the normal distribution (it's actually a straight line).Sheya
plot(pnorm(y), y) is certainly not a straight line. However, plot(ppoints(y), y) is.Khmer
One problem is that the inverse of a density function is not a function, as it is not one to one, but mrip's answer below gives as close to what you appear to be asking as you can get.Francinafrancine
Not all functons are invertible, and this is an example (it is not strictly increasing nor decreasing). I think that @gung answer is more useful here.Cadet
Yes that's exactly what I was after. Thanks all. @hong-ooi - I meant that plot(pnorm(y), y) where y = dnorm(seq(-4,4, length=100)) is straight. plot(ppoints(y), y) for the same y vector is in fact normal!Sheya
C
8

I'm not sure if the inverse of the density function is built in -- it's not used nearly as often as the inverse of the cumulative distribution function. I can't think offhand of too many situation where the inverse density function is useful. Of course, that doesn't mean there aren't any, so if you are sure this is the function you need, you could just do:

dnorminv<-function(y) sqrt(-2*log(sqrt(2*pi)*y))

plot(x, y)
points(dnorminv(y),y,pch=3)

enter image description here

Calamus answered 25/10, 2013 at 12:38 Comment(9)
Thanks mrip. I'd tried inversing the normal distribution function myself but without success.Sheya
I cannot be built-in, as there is no possible inverse for this density function (see wikipedia).Cadet
@Cadet Well, the positive inverse could be built in as a partial function. The inverse to the CDF is built in and that is technically not a function either, since it is only defined on [0,1].Calamus
You are right, if you divide this function in 2 parts, then both resulting functions comply with the criteria for being invertible (in more general terms: on function to the right of the mean, and another to the left). Also, I don't see the problem for a function to be defined on [0,1], functions don't have to be restricted to a predefined domain.Cadet
You are right about [0,1]. It is a function, just not a real function (I think that's the standard terminology). I guess a better example would be the function x^2, which is technically not invertible, but the sqrt function is the positive branch of the inverse.Calamus
Yep, also in trigonometry all the inverses are defined only for a specific range in the co-domain, in a similar way of what you suggested (e.g.: arcsin goes from [0,1] to [-pi/2, pi/2]).Cadet
In practical terms are you saying that this function only works for inputs in the range [0,1/sqrt(2*pi)]? Because that appears to be the case.Sheya
Yes, @geotheory, that is the case. Looking at the plot above (either his or yours), imagine someone told you the y-value is 0.2, what is the x-value? Well, there are 2 possibilities {~-1.175, ~1.175}. Recall that the definition of a function is that it has only 1 answer. That is why there isn't an inverse of the pdf, although mrip's workaround may be good enough for you in practice.Borax
dnorm(0) yields 0.3989423. However, dnorminv(0.3989423) returns NaN. This is happening because log(sqrt(2*pi)*y) is returning 4.912632e-08 instead of 0. So an improvement to your answer could be dnorminv<-function(y) sqrt(-2* round(log(sqrt(2*pi)*y),6))Manzanilla
B
26

What dnorm() is doing is giving you a probability density function. If you integrate over that, you would have a cumulative distribution function (which is given by pnorm() in R). The inverse of the CDF is given by qnorm(); that is the standard way these things are conceptualized in statistics.

Borax answered 25/10, 2013 at 12:22 Comment(1)
Thanks for clarification gung. I'm no statistician so my terminology is perhaps not so precise.Sheya
C
8

I'm not sure if the inverse of the density function is built in -- it's not used nearly as often as the inverse of the cumulative distribution function. I can't think offhand of too many situation where the inverse density function is useful. Of course, that doesn't mean there aren't any, so if you are sure this is the function you need, you could just do:

dnorminv<-function(y) sqrt(-2*log(sqrt(2*pi)*y))

plot(x, y)
points(dnorminv(y),y,pch=3)

enter image description here

Calamus answered 25/10, 2013 at 12:38 Comment(9)
Thanks mrip. I'd tried inversing the normal distribution function myself but without success.Sheya
I cannot be built-in, as there is no possible inverse for this density function (see wikipedia).Cadet
@Cadet Well, the positive inverse could be built in as a partial function. The inverse to the CDF is built in and that is technically not a function either, since it is only defined on [0,1].Calamus
You are right, if you divide this function in 2 parts, then both resulting functions comply with the criteria for being invertible (in more general terms: on function to the right of the mean, and another to the left). Also, I don't see the problem for a function to be defined on [0,1], functions don't have to be restricted to a predefined domain.Cadet
You are right about [0,1]. It is a function, just not a real function (I think that's the standard terminology). I guess a better example would be the function x^2, which is technically not invertible, but the sqrt function is the positive branch of the inverse.Calamus
Yep, also in trigonometry all the inverses are defined only for a specific range in the co-domain, in a similar way of what you suggested (e.g.: arcsin goes from [0,1] to [-pi/2, pi/2]).Cadet
In practical terms are you saying that this function only works for inputs in the range [0,1/sqrt(2*pi)]? Because that appears to be the case.Sheya
Yes, @geotheory, that is the case. Looking at the plot above (either his or yours), imagine someone told you the y-value is 0.2, what is the x-value? Well, there are 2 possibilities {~-1.175, ~1.175}. Recall that the definition of a function is that it has only 1 answer. That is why there isn't an inverse of the pdf, although mrip's workaround may be good enough for you in practice.Borax
dnorm(0) yields 0.3989423. However, dnorminv(0.3989423) returns NaN. This is happening because log(sqrt(2*pi)*y) is returning 4.912632e-08 instead of 0. So an improvement to your answer could be dnorminv<-function(y) sqrt(-2* round(log(sqrt(2*pi)*y),6))Manzanilla
B
4

The derivation of the inverse of the standard normal pdf is:

enter image description here

Breathe answered 27/1, 2020 at 15:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.