Skip to content

Instantly share code, notes, and snippets.

@myrddian
Last active September 29, 2018 06:05
Show Gist options
  • Save myrddian/df5fa6255ff4d002a994df07405ad73a to your computer and use it in GitHub Desktop.
Save myrddian/df5fa6255ff4d002a994df07405ad73a to your computer and use it in GitHub Desktop.
Generates a random dirichlet with n-attributes
#returns a randomly generated dirichlet for psuedo counts
#Alpha is passed as Shape to the RGamma Function
#Beta is passed as scale to the RGamma Function
#NVar is the number of variables you are going to psuedocount
random_dirichlet <- function(n_var, alpha=0, beta=1) {
return_val <- 0
#Prevent alpha being less than n_var other wise
#n_var will have empty psuedo-counts, which is what
#this is trying to prevent.
if(alpha == 0)
{
alpha <- n_var + 1 #Trying to possibly fill all vars
}
else if(alpha < n_var)
{
alpha <- n_var+alpha
}
for(i in 1:n_var) {
return_val[i] <- rgamma(1,shape=alpha,scale = beta)
}
#Normalise and round to a nice set of values
return_val <- round(return_val/sum(return_val) * 100,1)/100
return(return_val)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment