Density of a Two-Piece Normal (or Split Normal) Distribution
Asked Answered
M

1

4

Is there a density function for the two-piece Normal distribution:

enter image description here

on CRAN? Thought I would check before I code one. I have checked the distribution task view. It is not listed there. I have looked in a couple of likely packages, but to no avail.

Update: I have added dsplitnorm, psplitnorm, qsplitnorm and rsplitnorm functions to the fanplot package.

Megdal answered 17/4, 2013 at 15:50 Comment(0)
T
5

If you choose to construct your own version of the distribution, you might be interested in distr. It (and the related packages distrEx, distrSim, distrTEst, distrTeach and distrDoc) have been written to provide a unified interface for constructing new distributions from existing ones. (I constructed this example with the help of the wonderful vignette that accompanies the distrDoc package and which can be gotten by typing vignette("distr").)

This implements the split normal distribution, which may not be exactly what you are after. Using the distr toolset, though, it shouldn't be too hard to adjust this to fit your exact needs.

library(distr)

## Construct the distribution object.
## Here, it's a split normal distribution with mode=0, and lower- and
## upper-half standard deviations of 1 and 2, respectively.
splitNorm <- UnivarMixingDistribution(Truncate(Norm(0,2), upper=0), 
                                      Truncate(Norm(0,1), lower=0), 
                                      mixCoeff=c(0.5, 0.5))
## Construct its density function ...
dsplitNorm <- d(splitNorm)
## ... and a function for sampling random variates from it
rsplitNorm <- r(splitNorm)

## Compare the density it returns to that from rnorm()
dsplitNorm(-1)
# [1] 0.1760327    
dnorm(-1, sd=2)
# [1] 0.1760327    

## Sample and plot a million random variates from the distribution
x <- rsplitNorm(1e6)         
hist(x, breaks=100, col="grey")

## Plot the distribution's continuous density
plot(splitNorm, to.draw.arg="d") 

enter image description here

enter image description here

Terrain answered 17/4, 2013 at 17:45 Comment(2)
Thanks. I think it is what I am looking for (or at least a parametrization of what I am looking for).Megdal
@gjabel -- A simpler approach, which I'd probably use if I just needed the density, is to do something like this: ifelse(x < 0, dnorm(x, mean=0, sd=2), dnorm(x, mean=0, sd=1)) (again for a distribution with mode=0, lower half sd=2, and upper half sd=1).Acciaccatura

© 2022 - 2024 — McMap. All rights reserved.