0

I am working currently on generating some random data for a school project.

I have created a variable in R using a binomial distribution to determine if an observation had a loss yes=1 or not=0. Afterwards I am trying to generate the loss amount using a random distribution for all observations which already had a loss (=1).

As my loss amount is a percentage it can be anywhere between 0

What Is The Intuition Behind Beta Distribution @ stats.stackexchange

In a third step I am looking for an if statement, which combines my two variables.

Please find below my code (which is only working for the Loss_Y_N variable):

Loss_Y_N = rbinom(1000000,1,0.01)
Loss_Amount = dbeta(x, 10, 990, ncp = 0, log = FALSE)

ideally I can combine the two into something like

if(Loss_Y_N=1 then Loss_Amount=dbeta(...) #... is meant to be a random variable with mean=0.15 and should be 0<x=<1
else Loss_Amount=0) 

Any input highly appreciated!

Community
  • 1
  • 1
Alex_T_86
  • 5
  • 4
  • So, to clarify, you need to generate a random number between 0 and 1 with mean a specific mean? You could probably edit your question to specify that as the problem and avoid all the extra fluff – D. Ben Knoble Mar 15 '16 at 12:17
  • Loss_Amount = dbeta(...) * Loss_Y_N . Wherever, Loss_Y_N is 0, Loss amount will be 0. – Vikram Venkat Mar 15 '16 at 12:17
  • Thank you both for your comments. Much appreciated. Can you please explain to me what I should put in the parenthesis to have a loss distribution around 15% but allow it to be between 0 – Alex_T_86 Mar 15 '16 at 12:20
  • @Ben Knoble yes maybe you are right. Actually I only need to find out how to best simulate a loss distribution between 0 and 100% with a mean of 15%. – Alex_T_86 Mar 15 '16 at 12:23
  • I edited your comment in Alex, but it has to reviewed as I am still low-ish rep. Hopefully it clarifies the problem for others – D. Ben Knoble Mar 15 '16 at 12:35
  • thank you for your help! – Alex_T_86 Mar 15 '16 at 13:32

1 Answers1

0

Create a vector for your loss proportion. Fill up the elements corresponding to losses with draws from the beta. Tweak the parameters for the beta until you get the desired result.

N <- 100000
loss_indicator <- rbinom(N, 1, 0.1)
loss_prop <- numeric(N)
loss_prop[loss_indicator > 0] <- rbeta(sum(loss_indicator), 10, 990)
Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
  • thank you for your comment - much appreciated. What I don't understand is that when I follow your code I don't get any observations which have a total loss (meaning that my loss_prop variable should be around 1? Am I interpreting the results wrong? – Alex_T_86 Mar 15 '16 at 13:24
  • @Alex_T_86 oops, left out the `> 0`. Try it now. – Hong Ooi Mar 15 '16 at 13:26
  • thank you! Now I just have to play around with the parameter - thank you! – Alex_T_86 Mar 15 '16 at 13:30