Due in class, Thursday/Monday Oct 16/20.

Assume, that

`x`

is a vector consisting of values `TRUE`

and `FALSE`

. The claim is that the sequence is created by independent trials with probability of `TRUE`

of 0.5.
There are different statistical routines that help us test for randomness (or rather test for non-randomness). To implement these, we need to be able to easily extract information from each sequence.
Implement the following functions that will help you do that. Note, that some tasks might benefit from being broken down into smaller pieces (several functions):

- Marginal distribution: return a table of number of trues and falses in sequence
`x`

. - Frequency of M-bit blocks: return a table of frequencies of all 2^M binary patterns of length M. Show that for M=1 you get the same results as in (1).
- Frequency of Runs: a run is an uninterrupted sequence of identical values in
`x`

. Return a frequency breakdown of number of runs by their length. - Pattern Matching: given a pattern
`y`

, find the number of times this pattern occurs in sequence`x`

.

Note: this assignment is not particularly easy - it expects you to take a complex problem and break it down into pieces that you can solve. There will be a lot of different approaches to the problem: one might be to work with text (which we haven't done much yet in class), one might be to think of the connection between binary and decimal numbers, e.g. you could make use of the following function

bintodec <- function(y) { # find the decimal number corresponding to binary sequence y if (! (all(y %in% c(0,1)))) stop("not a binary sequence") res <- sum(y*2^((length(y):1) - 1)) return(res) }Don't give up after the first attempt proves to be a dead end! This is our first programming exercise - this homework is meant to help you develop strategies for programming in R.

The homework is designed such that you can work it out with the functions that you already know, but there are other functions that will help you solve the tasks more directly - use them if you are able to identify them. Particularly elegant solutions will give extra points for the homework.

x1 <- c(0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1) x2 <- c(1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0) x3 <- c(0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0)

- Coming up