Plotting an hyperbolic density using ggplot

Hi guys!
I just started learning R and i’ve run into something that I can’t solve.
My problem is the following: I have a time series of daily return on a stock. I have used ghyp package to fit my daily returns to a hyperbolic distribution. Now I want to plot density of that hyperbolic distribution (eg. NIG).

I want to do my plot using ggplot but the ggplot can’t read ghyp package.
Would appreciate any advice.

Hi Denis, Welcome!

Could you ask this with a minimal REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

If you've never heard of a reprex before, you might want to start by reading this FAQ:

Could you say more about what you mean by this (or, better yet, as suggested, include a small example)? From a quick look at the ghyp documentation, it seems like several base plotting methods are in there. You should be able to recreate these using ggplot2, it's a matter of having your data in a certain format (e.g. a data frame).

I fully endorse @andresrcs and @mara reprex suggestion.

But, without having any experience of ghyp, I would generally plot a distribution by:

  • defining the range of x values I want to cover;
  • calculate the y values using the relevant density function; and
  • put x and y into a data.frame and then plot it with ggplot and geom_line.

For a Normal distribution that might be:

library(ggplot2)
x <- seq(-3,3,0.01)
y <- dnorm(x, mean = 0, sd = 1)
DF <- data.frame(x = x, y = y)
ggplot(DF,aes(x = x, y = y)) + geom_line()

Created on 2019-04-03 by the reprex package (v0.2.1)

So, assuming you there is a dghyp function in the ghyp package and you obtain parameters for your hyperbolic distribution from your fitting process, I think you should be able to pretty much follow that pattern.

Hope this helps.

Ron.

1 Like

@mara sorry for not clarifying good enough.

I have a time series of daily returns on S&P 500 (sample size 3051). I plotted a histogram of daily returns and plotted a normal distribution using parameters (sigma and mean) of my sample to visualise "goodness of fit". To do that I run the following code:

ggplot(snp.srdf, aes(x=snp.srdf$SNP.Adjusted))+geom_histogram(aes(y= stat(density)), binwidth = 0.008, color="azure4" ) + stat_function(fun=dnorm, args = list(mean=mean(snp.srdf$SNP.Adjusted), sd=sd(snp.srdf$SNP.Adjusted)), lwd=1, col="darkgray")

Which looks like this:
42

From the plot it is visible that a gaussian distribution is not a proper fit so I use StepAIC.ghyp (snp.srdf$SNP.Adjusted) to find the appropriate distribution. The best fit turns to be a symmetric NIG with certain parameters. I fitted my data into that distribution using fit.NIGuv() and saved the results.

So now I am trying to plot the same histogram from the beginning (histogram of my sample) with a NIG density. I have tried to use the stat_function with dghyp but it doesn't work.

Do you know of any way plotting that distribution using ggplot or is there a function in which I can add the NIG parameters (sigma, mu, lambda, chi) to get a density plot.

Thank you and regards!

Hmm, well the normal inverse gaussian (if that's what NIG stands for), isn't one of the built-in kernels for ggplot2's geom_density()

See available density kernels here.

EDIT: turns out these helpers only take the stats package distributions as well.
You might take a look at the post here: Plotting Probability Distributions with ggplot2 and ggfortify, I haven't tried myself yet, but could be helpful.

1 Like

Looking at the documentation of ghyp, the fit.NIGuv() function returns an object that can be fed to the dghyp() function, which returns the density of the distribution.

dghyp(x, object = ghyp(), logvalue = FALSE

You can make a data frame with

MySeq <- seq(-0.15, 0.15, length.out = 100)
fitDF <- data.frame(x = MySeq,
                    y =  dghyp(MySeq, Your_fit.NIGuvObject))

You should be able to add that to your plot with

+ geom_line(mapping = aes(x = x, y = y), data = fitDF)

I hope I didn't make too many mistakes writing that code with nothing to test it on.

2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.