Search code examples
rdistributionconvolution

Summing N normal distributions


I am trying to determine the distribution of the sum of N univariate distributions. Can you suggest a function that allows me to dynamically input any N number of distributions?

This works:

library(distr)
var1 <- Norm(mean=14, sd=1)
var2 <- Norm(mean=10, sd=1)
var3 <- Norm(mean=9, sd=1)
conv <- convpow(var1+var2+var3,1)

This (obviously) doesn't work since pasting the list together creates a messy character string, however this is the framework for my ideal function:

convolution_multi <- function(mean_list = c(14,10,9,10,50)){
  distribution_list <- lapply(X = mean_list, Norm, sd=1)
  conv_out <- convpow(paste(distribution_list,collapse="+"),1)
  return(conv_out)
}

Thanks for your help!


Solution

  • You can use Reduce to repeatedly add each RV to one another. After that you can use convpow

    new_var <- Reduce("+", distribution_list)
    convpow(new_var, 1)
    

    With that being said the call to convpow does absolutely nothing here.

    > identical(convpow(new_var, 1), new_var)
    [1] TRUE