vendredi 16 septembre 2022

generating quasi-random sequences of events in R

I am trying to write a function in R that will generate random events over time following these criteria:

  1. There are N initial events (say 10)
  2. All events have roughly the same probability at the beginning (obviously summing to 1)
  3. Randomly one of the events has an increase in probability (say twice the currently most probable event)
  4. Over time the probability of all events changes with the one that has been experienced the most decreasing at a higher rate (say 0.3)
  5. Randomly a new event is introduced and its probability is far higher than all the others (say 0.5)

So far I managed to write part of the code but I am very far from my goal and I don't even think what I have tried so far is really correct.

 
#install.packages("randtoolbox")
library(randtoolbox)

quasi_random_events<-function(n_events=10,rateIncrease=2,rateDecrease=0.3,newEvent=0.5, iter=5){
  #1) There are N initial events (say 10)
  events<-LETTERS[1:n_events]
  # 2)All events have roughly the same probability at the beginning (sum to 1)
  n <-length(events)
  #Sobol sequence
  x <- sobol(n = n)
  probs <- x / sum(x)
  sum(probs) == 1
  

  res<-lapply(1:iter,function(x){
  # 3)Randomly one of the events has an increase in probability (say twice the currently most probable event)
  up=sample(events,1)
  probUp=which(events==up)
  probs[probUp]*rateIncrease

  #------------- here I should update all the other probabilities to sum to 1
  #???????
    
  # 4)Over time the probability of all events changes with the one that has been experienced the most decreasing at a higher rate (say 0.3)
  #?????
  # 5)Randomly a new event is introduced and its probability is far higher then all the others (say 0.5)
  #????
  
  
  })
  return(res)
}

The final results should be a function that at each iteration updates the previous probability as described from points 3 to 5.




Aucun commentaire:

Enregistrer un commentaire