[R] A strange question on probability

Spencer Graves spencer.graves at pdf.com
Tue Jun 29 12:19:09 CEST 2004


      Does the following do what you want: 

rseq <- function(n=1, length.=2){
  s1 <- sample(x=length., size=n, replace=TRUE)
  s2 <- sample(x=length., size=n, replace=TRUE)
  ranseq <- array(0, dim=c(n, length.))
  for(i in 1:n)
    ranseq[i, s1[i]:s2[i]] <- 1
  ranseq
}
set.seed(1)
rseq(9, 5)

 > set.seed(1)
 > rseq(9, 5)
      [,1] [,2] [,3] [,4] [,5]
 [1,]    1    1    0    0    0
 [2,]    0    1    0    0    0
 [3,]    1    1    1    0    0
 [4,]    0    0    0    1    1
 [5,]    0    1    0    0    0
 [6,]    0    0    0    1    1
 [7,]    0    0    1    1    1
 [8,]    0    0    0    1    0
 [9,]    0    0    0    1    1
 >
      hope this helps.  spencer graves

Jim Lemon wrote:

>On Tuesday 29 June 2004 01:48 pm, Steve S wrote:
>  
>
>>Dear All,
>>
>>I wonder if there is a probability distribution where you can specify when
>>a certain event start and finish within a fixed period? For example I might
>>specify the number of period to be 5, and a random vector from this
>>distribution might give me:
>>0 1 1 1 0
>>
>>where 1 is always adjacent to each other?
>>
>>This can never happen: 0 0 1 0 1 for example.
>>
>>    
>>
>Well, I'll have a go. Let's call it the start-finish distribution. We have a  
>p (period) and d (duration). As there must be an "off" observation (otherwise 
>we don't know the duration), It's fairly easy to enumerate the outcomes for a 
>given period:
>
>d	start(s)	finish(f)	count
>1	1:n-1	2:n	n-1
>2	1:n-2	3:n	n-2
>...
>n-1	1	n-1	1
>
>Assuming that all outcomes are equally likely, the total number of outcomes 
>is:
>
>n(n-1)/2
>
>thus the probability of a given d occurring is:
>
>P[d|n] = 2(n-d)/n(n-1)
>
>The probabilities of s and f over all d are inverse over the values k in 1:n
>
>P[s=k|n] = (n-k-1)/(n-1)
>P[f=k|n] = (k-1)/(n-1)
>
>giving, I think, a monotonic function for s and f.
>
>  
>
>>My apology for this strange question!
>>
>>    
>>
>My apology if this is no use at all.
>
>Jim
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>  
>




More information about the R-help mailing list