set.seed(110951)
urn <- sample(10:100, 16, replace = TRUE)
urn
## [1] 11 75 48 41 76 35 32 63 27 77 59 89 90 35 15 73
slips <- sample(1:16, 1000, replace = TRUE, prob = urn)
library(MASS)
truehist(slips, h = 1)

barplot(urn)

a <- sample(c(1, 2), size = 1000, replace = TRUE)
head(a)
## [1] 2 2 2 1 1 2
b <- sample(c(1, 2), size = 1000, replace = TRUE)
head(b)
## [1] 1 2 1 2 1 2
library(R.utils)
## Loading required package: R.oo
## Loading required package: R.methodsS3
## R.methodsS3 v1.8.1 (2020-08-26 16:20:06 UTC) successfully loaded. See ?R.methodsS3 for help.
## R.oo v1.24.0 (2020-08-26 16:11:58 UTC) successfully loaded. See ?R.oo for help.
##
## Attaching package: 'R.oo'
## The following object is masked from 'package:R.methodsS3':
##
## throw
## The following objects are masked from 'package:methods':
##
## getClasses, getMethods
## The following objects are masked from 'package:base':
##
## attach, detach, load, save
## R.utils v2.10.1 (2020-08-26 22:50:31 UTC) successfully loaded. See ?R.utils for help.
##
## Attaching package: 'R.utils'
## The following object is masked from 'package:utils':
##
## timestamp
## The following objects are masked from 'package:base':
##
## cat, commandArgs, getOption, inherits, isOpen, nullfile, parse,
## warnings
slips.string <- intToBin(slips - 1)
head(slips.string)
## [1] "1111" "0001" "0100" "1001" "1011" "1000"
x <- rep(0, 1000)
y <- rep(0, 1000)
for (i in 1:1000) {x[i] <- as.numeric(substr(slips.string[i], a[i], a[i]));
y[i] <- as.numeric(substr(slips.string[i], b[i]+2, b[i]+2))}
head(x)
## [1] 1 0 1 1 1 0
head(y)
## [1] 1 1 0 1 1 0
x <- 2 * x - 1
y <- 2 * y - 1
mean((x*y)[a == 1 & b == 1])
## [1] 0.06463878
mean((x*y)[a == 1 & b == 2])
## [1] 0.1111111
mean((x*y)[a == 2 & b == 1])
## [1] -0.05791506
mean((x*y)[a == 2 & b == 2])
## [1] -0.2252964