How to generate random values from Horseshoe prior

Hi,Guys:
I have been trying to generate random values from horseshoe prior for a plotting purpose…

Yet they all seems a little bit off compared to the original plots in Carvalho et al. (2009)Handling Sparsity via the Horseshoe.

Some of the functions that I have used are rhs(n,lambda,tau)and dhs(x,lambda,tau) from library(LaplacesDemon) where I provided the parameter values for local shrinkage and global shrinkage…

Could someone provide some insights why this is happening?
Any helps will be appreciated.
Thanks!

I believe that is because Carvalho et al. are showing a composite probability density across many draws of the local lambda parameter. Each individual component of the horseshoe (i.e. for each term it is applied to) is a normal distribution. Only when you stack them up do you get the spike and slab appearance.

Here’s one way to generate that look.

library(dplyr, warn.conflicts = FALSE)
library(LaplacesDemon)
library(ggplot2)

# plot over this range
xseq <- seq(-5, 5, length.out = 500)

# how many terms for the horseshoe
N <- 100 

# generate N densities, drawing the lambda parameter from a half-cauchy 
denslist <- purrr::map(abs(rcauchy(N, 0, 1)), ~dhs(xseq, lambda=.x, tau=0.5))

# add them all together and divide by the number of draws
dens <- Reduce(`+`, denslist)/N

data.frame(x=xseq, density=dens) %>% 
  ggplot(aes(x, density)) +
  geom_line()