# Assign the number of loans to the variable `n` n <- 10000 # Assign the loss per foreclosure to the variable `loss_per_foreclosure` loss_per_foreclosure <- 200000 # Assign the probability of default to the variable `p_default` p_default <- n * loss_per_foreclosure # Use the `set.seed` function to make sure your answer matches the expected result after random sampling set.seed(1) # Generate a vector called `defaults` that contains the default outcomes of `n` loans defaults <- sample(c(0,1),n,replace=TRUE,prob=(c(p_default,1 - p_default))) #> Error in sample.int(length(x), size, replace, prob): negative probability # Generate `S`, the total amount of money lost across all foreclosures. Print the value to the console. S <- sum(defaults * loss_per_foreclosure) #> Error in eval(expr, envir, enclos): object 'defaults' not found
What I am trying to do: Say I manage a bank that gives out 10,000 loans. The default rate is 0.03 and I lose $200,000 in each foreclosure.
Output I desire: Create a random variable S that contains the earnings of my bank. Calculate the total amount of money lost in this scenario.
Output I am getting: Having trouble defining the probability variable "p_default" can some hint me the correct syntax / pseudo code ? I dont think I need the default rate ? thanks.
There’s nothing wrong with the syntax you’re using to define
p_default — the problem lies in the reasoning. Have you inspected the value you set
p_default to? You’ll notice it doesn’t fall between 0 and 1 (in fact, it’s a very large number!) so that’s the first clue that there’s something awry with the calculation method you tried. This is also the source of the error in
sample, since you fed it
1 – p_default, which is a (very) negative number.
What made you decide to multiply the number of loans by the money you lose when a loan goes bad to get the probability of a loan going bad? Were you guessing, or did you have a theory about why that might make sense? It won’t be very useful in the long run to find out how to get this problem “right” mechanically without understanding the reasoning behind it. Sorting that out is a great discussion to have here!
My best hint: think about what “a probability” means and how it’s usually expressed mathematically, then revisit the information you were given at the start of the problem,
I figured it out after reading your reply and taking another look at it. thanks