1
$\begingroup$

I want to calculate a quantile of a specific distribution. Therefore I need the cdf. My distribution is a standardized Student's-t distribution, this can be written as \begin{align*} f(l|\nu) =(\pi (\nu-2))^{-\frac{1}{2}}\Gamma \left(\frac{\nu}{2} \right)^{-1} \Gamma \left(\frac{\nu+1}{2} \right) \left(1+\frac{l^2}{\nu-2} \right)^{-\frac{1+\nu}{2}} \end{align*}

This can be implemented in R with:

probabilityfunction<-function(x)(pinumber*(param-2))^(-1/2)*gamma(param
/2)^(-1)*gamma((param+1)/2)*(1+l^2/(param-2))^(-(1+param)/2)

Where pinumber is the value for pi and param is the $\nu$. Lets say $\nu=5$. Then I can get the probability by just inserting a certain value for my l. But I want to have the cumulative density, since I later want to compute the quantile. I thought about something like

cumsum(probabilityfunction(5))

to give me the cumulative value up to 5.

But obviously this does not work. How can I get the cumulative probability and later on the quantile?

EDIT: OK, I found a first improvement:

integrate(probabilityfunction,-Inf,2) 

would be a good starting point, but how to do the other thing?

$\endgroup$
  • 2
    $\begingroup$ Are you aware that R has the density, cumulative and quantile functions for a t-distribution already implemented (dt, pt and qt)? Or do you simply want to be able to do this for other distributions? $\endgroup$ – Nick Sabbe Apr 25 '13 at 15:10
  • $\begingroup$ @NickSabbe the implemented distribution is NOT the same. I use a different one and later on, I also want to do this for other distributions. (R has the standard Student's-t distribution, but not the standardized Student's-t distribution, this is a difference) $\endgroup$ – Stat Tistician Apr 25 '13 at 15:30
  • $\begingroup$ You don't need a variable to hold the value of $\pi$. In R this is built in as pi. $\endgroup$ – Glen_b -Reinstate Monica Apr 25 '13 at 23:33
3
$\begingroup$

CDF and quantile functions of this (extremely restrictive) distribution are straightforward to obtain in R, using that this is a Student-t distribution with a particular scale parameter:

CDF

pt(x/sqrt((nu-2)/nu),df=nu)

Quantile

qt(p,df=nu)*sqrt((nu-2)/nu)

This looks like a duplicate of your previous question: quantile transformation with t distribution (sorry, you have to use the monotonic transformation ;) )

$\endgroup$
1
$\begingroup$

Extending your own suggestion of using numerical integration, you can invert the function by using nonlinear equation solving. I've not tested this through, so there may be errors, but something like this should work:

dens<-function(x, nu)
{
    (pi*(nu-2))^(-1/2)*gamma(nu/2)^(-1)*gamma((nu+1)/2)*(1+x^2/(nu-2))^(-(1+nu)/2)
}

cum<-function(x, nu)
{
    integrate(dens,-Inf,x, nu=nu)
}

quant<-function(p, nu)
{
    #maybe format the result somewhat better
    nleqslv(0, function(x){cum(x, nu)-p} )
}

cum(0.05, 3)

It should not be too hard to generalize this so you can pass in any density function and parameters, but there are some conditions for the integration and inversion to work. Neither can I say something on the precision (the result shows a standard error, but this does not take the error in integrating into account).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.