# stat 579

## Homework #5

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

#### Testing Randomness (Function Writing):

When we are dealing with simulations, we are often faced with the question of whether a sequence of numbers is 'random', and wether it follows a specific pattern.

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).

You are expected to write one functions to solve each of these tasks (they can call each other, you can write 'helper' functions and call those, too).
Make sure to comment your code appropriately. Use indentation for better structuring and ease of reading.

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.

#### To test your functions:

Use the following sequences and report results from your functions:

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)

#### Deliverables:

Submit an R markdown file (that is ***just*** the XXX.Rmd file of all of the files that RStudio creates for you) that contains all of the (commented!) R code for your functions, calls to your functions and results.

## Sample Solution: