eco-detectives / eco_detective

Reading/coding group for The Ecological Detective
1 stars 3 forks source link

1st (real) issue #2

Closed stapial closed 6 years ago

stapial commented 6 years ago

Hi guys.

So, I was trying to make pseudocode 3.1. I get an output but it's not what I would expect, some probabilities are 612 which is instant nope.

Here's my code

probKN <- function (N,p,k){
  p=0.2
  N=50
  k <- seq(1,N,by=1)

  for (i in k) {
  prob = ((N-k+1)/k)*((p)/(1-p))*k[i-1]

  }
  return(prob)
}

probKN

I'm using the last equation in equation 3.48. As I understand it, the last term is the probability of k-1 (the probability previously calculated). I think that's where my code has a bug since I'm not 100% sure how to do that.

I sort of tried to do it with the second formula with the additive previous values (! terms). I didn't know how to do that in R either.

Any insight is appreciated.

Thanks!

stapial commented 6 years ago

I think I fixed it. I wasn't understanding the problem correctly. I had to calculate p(o,N) as a starting point and then calculate the probability for each following k in the loop using the equation.

Did anyone do something similar?? At least now I get results that make sense (0<p<1)

PSEUDOCODE 3.1

Step 1

p <- 0.2
N <- 50

Step 2

Po <- (1-p)^N

Step 3

probKN <- function (N,p,k){
  p=0.2
  N=50
  k <- seq(1,N,by=1)
  p.vec <- vector(length=length(k), "numeric")
  p.vec[1] <- ((N-1+1)/1)*((p)/(1-p))*Po

  for (i in k) {
  p.vec[i+1] = ((N-k[i])/k[i])*((p)/(1-p))*p.vec[i]

  }
  return(p.vec)
}
oharac commented 6 years ago

Glad you got it working! were you able to make a plot? bring ‘em tomorrow to compare with everyone.

I think tomorrow we can chat about loops (the way the pseudocode is set up, and often more intuitive) and vectorization (more “modern” coding in R and Matlab, faster and more efficient), also some of the probability short cuts in R.

On Mon, Jun 25, 2018 at 9:28 PM Sebastian Tapia notifications@github.com wrote:

Closed #2 https://github.com/eco-detectives/eco_detective/issues/2.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/eco-detectives/eco_detective/issues/2#event-1700433316, or mute the thread https://github.com/notifications/unsubscribe-auth/AKN816r5Tlj9mJMb08xnN2nH-kaU_eRDks5uAbhQgaJpZM4U3Emh .

stapial commented 6 years ago

Thanks! I was able to do a plot.. I just uploaded em, im working on 3.2 and 3.3 now