You are on page 1of 28

1

Basic Commands:

# combine

x <- c(2, 3, 5, 10)


y <- 1:10
x[3]

2^x

z <- seq(from = 5, to = 20, by = 0.5)

x <- 1:100
x = x^2
y <- c(5, 12, 17)
x[y]

a <- x > 12

matrix(1:12, nrow = 3, ncol = 4)

m <- matrix(1:12, nrow = 3)


rbind(m, m)
cbind(m, m)

e <- c(19, 12, 21, 44)


rep(e, times = 3)
2

Basic Functions:

myavg <- function(x) {


a = sum(x)
b = length(x)
return(a/b)
}

mysum <- function(x, y) {


return(1 + x + 2 * y)
}
3

Variance, Random Numbers:

myvar <- function(x, f) {


a <- sum(x * f)
b <- sum(x^2 * f)
return(b - a^2)
}

myvar2 <- function(x) {


n <- length(x)
a <- sum(x)/n
b <- sum((x - a)^2)
return(b/(n - 1))
}

x <- round(runif(50, 0, 100))


myvar2(x)
var(x)

n <- length(x)
f <- rep(1/n, n)
myvar(x, f)

y <- runif(50, 0, 8)
f <- y/sum(y)
myvar(x, f)
4

Standard Deviation:
mysd <- function(v) {
n <- length(v)
m <- mean(v)
S <- 0
for (i in 1:n) {
S = S + (v[i] - m)^2
}
result <- sqrt(S/(n - 1))
}

# v1 is a random vector v2 is a random vector of integers


v1 <- runif(100, min = 0, max = 101)
v2 <- floor(v1)
s1 <- mysd(v2)

# Note that approximately s2=s1/2 and s3=s1/5

g4 <- c()
for (i in 1:500) {
group4 <- sample(v2, 4, replace = T)
m <- mean(group4)
g4 <- c(g4, m)
}

s2 <- mysd(g4)

g25 <- c()


for (i in 1:500) {
group25 <- sample(v2, 25, replace = T)
5

m <- mean(group25)
g25 <- c(g25, m)
}

s3 <- mysd(g25)
6

Standard Deviation of Uniform Distribution:

# 0 < x < 240


# mu = 120

n <- 1e+06
x <- c()
S <- 0
for (i in 1:n) {
x <- 240 * runif(1)
S <- S + (x - 120)^2
}
sigma <- sqrt(S/n)

# Alternative
n <- 1e+06
x <- runif(n, 0, 240)
sigma <- sqrt(sum((x - 120)^2)/n)
7

Combination:

mycomb <- function(n, r) {


S <- 1
if (n - r >= r) {
for (i in 1:r) {
S <- S * (n - r + i)/i
}
} else {
for (i in 1:(n - r)) {
S <- S * (r + i)/i
}
}
print(S)
result <- S
}
8

Negative Binomial Expected Value:

NegBinom <- function(p, k) {


trials <- 0
S <- 0
while (S < k) {
r <- runif(1)
if (r < p) {
S <- S + 1
}
trials <- trials + 1
}
return(trials)
}
9

Binomial Distribution, Plot:

# 27.10.2022 Binomial Distribution plot


# where p denotes the probability of
# success and n denotes the number of trials

n <- 50
p <- 0.7
x <- 0:n
y <- choose(n, x) * p^x * (1 - p)^(n - x)
plot(x, y, "h", lwd = 4, col = "red")
10

Hypergeometric Distribution and Binomial Approximation


Plots:

# 03.11.2022

hyperg <- function(N, n, k, x) {


a <- choose(k, x)
b <- choose((N - k), (n - x))
c <- choose(N, n)
return(a * b/c)
}

binom <- function(n, p, x) {


a <- choose(n, x)
return(a * p^x * (1 - p)^(n - x))
}

N <- 500
k <- 100
n <- 100

x <- 0:n
p <- k/N

y <- hyperg(N, n, k, x)
plot(x, y, "l", col = "red", lwd = 4)

y2 <- binom(n, p, x)
lines(x, y2, "l", col = "blue", lwd = 4)
11

Poisson - Two Functions:

pois <- function(m, x) {


return(exp(-m) * m^x/factorial(x))
}

pois_cum <- function(m, x) {


S <- 0
for (i in 0:x) {
S = S + pois(m, i)
}
return(S)
}
12

Poisson Table - for:

rows <- 16
M <- c()
for (k in (2:10)) {
lambda <- k/2
p <- exp(-lambda)
v = c(p)
for (i in (1:rows)) {
p = p + exp(-lambda) * lambda^i/factorial(i)
v = c(v, p)
}
v <- round(10000 * v)/10000
M <- cbind(M, matrix(v, ncol = 1))
}
M <- cbind(0:rows, M)
M <- rbind(seq(0.5, 5, 0.5), M)
M
13

Poisson Table - while:

n = 7.5
v = c()
p = 0
i = 0
while (p < 0.99995) {
p = p + exp(-n) * n^i/factorial(i)
v = c(v, p)
i = i + 1
}
v = round(10000 * v)/10000
M = cbind(0:i, v)
M
14

Normal Distribution Probability (Numerical Integral):

normaldist <- function(z) {


N <- 1000
delta <- z/N
x <- seq(delta/2, z - delta/2, delta)
y <- exp(-x^2/2)
prob <- 0.5 + sum(y) * delta/sqrt(2 * pi)
prob <- round(10000 * prob)/10000
return(prob)
}
15

Normal Distribution - Graph:

mu <- 50
sigma <- 10
x <- seq(0, 100, 0.1)
y = exp(-(x - mu)^2/(2 * sigma^2))/(sigma * sqrt(2 * pi))
plot(x, y, "l")

# Normal Distribution - Numerical Integral


z <- 0.75
N <- 100
S <- 0
d <- z/N
for (i in 1:N) {
x = i * d
S = S + exp(-x^2/2)
}
S = S + (1/2) - exp(-z^2/2)/2
0.5 + (z * S/N)/(sqrt(2 * pi))
16

Normal Approximation to Binomial Distribution:

binomapprox <- function(n, p) {


x <- 0:n
y <- choose(n, x) * p^x * (1 - p)^(n - x)
plot(x, y, "h", lwd = 10, col = "red")

x2 <- seq(0, n, 0.01)


mu <- n * p
s <- (n * p * (1 - p))^(1/2)
y2 <- exp(-(x2 - mu)^2/(2 * s^2))/((2 * pi)^(1/2) * s)
lines(x2, y2, "l", lwd = 2, col = "blue")
}

Gamma Distribution:

gammadist <- function(x, a, b) {

x^(a - 1) * exp(-x/b)/(b^a * gamma(a))

}
17

Sort a Vector v:

mysort <- function(v) {


n <- length(v)
s <- c()
for (i in 1:n) {
mymin <- v[1]
index <- 1
for (j in 1:length(v)) {
if (v[j] < mymin) {
mymin = v[j]
index = j
}
}
v <- v[-index]
s <- c(s, mymin)
}
return(s)
}
18

List of Primes:

myprime <- function(n) {


print(2)
for (i in 3:n) {
flag = 1
for (j in 2:ceiling(sqrt(i))) {
if (i %% j == 0)
flag = 0
}
if (flag == 1)
print(i)
}
}
19

Correlation:

var223 <- function(x) {


n <- length(x)
mu <- mean(x)
S <- 0
for (i in 1:n) {
S <- S + (x[i] - mu)^2
}
return(S/n)
}

var224 <- function(x) {


return(mean((x - mean(x))^2))
}

cov223 <- function(x, y) {


n <- length(x)
mu_x <- mean(x)
mu_y <- mean(y)
S <- 0
for (i in 1:n) {
S <- S + (x[i] - mu_x) * (y[i] - mu_y)
}
return(S/n)
}

cor223 <- function(x, y) {


return(cov223(x, y)/sqrt(var223(x) * var223(y)))
}
20

Discrete Math Problem:

# In how many different ways can we distribute


#n identical balls to 3 different kids?
balls3kids <- function(n) {
S <- 0
for (i in 0:n) {
for (j in 0:(n - i)) {
S = S + 1
}
}
print(S)
Result = S
}
21

Discrete - Number of Onto Functions:

myonto <- function(n, k) {

S <- 0

for (i in 0:(k - 1)) {


S <- S + (-1)^i * choose(k, i) * (k - i)^n
}
print(S)
result <- S
}
22

Count Number of Primes up to n:

CountPrime <- function(n) {

S <- 1
for (i in 3:n) {
flag <- 1
for (j in 2:(i - 1)) {
if (i %% j == 0) {
flag = 0
break
}
}
S = S + flag
}
return(S)

}
23

Combination:

mycombin <- function(n, r) {


S <- 1
if (n - r >= r) {
for (i in 1:r) {
S <- S * (n - r + i)/i
}
} else {
for (i in 1:(n - r)) {
S <- S * (r + i)/i
}
}
print(S)
result <- S
}
24

Binary Palindrome Test:

palindrome <- function(v) {


S <- TRUE
n <- length(v)
if (n < 1) {
S <- FALSE
}
for (i in 1:n) {
if (v[i] != 0 && v[i] != 1) {
S <- FALSE
}
}
for (i in 1:ceiling(n/2)) {
if (v[i] != v[n - i + 1]) {
S <- FALSE
}
}
result <- S
}
25

Relatively Prime, Pythagoras Triples:

RelPrime <- function(n,m){

rp <- 1
k <- min(n,m)
for (i in 2:k){
if(n %% i == 0 & m %% i == 0){
rp <- 0
}
}
return(rp)
}

PTriples <- function(n){

A <- c()
for (i in 1:n){
for (j in i:n){
k <- sqrt(i^2 + j^2)
if (floor(k) == ceiling(k) & RelPrime(i,j)){
v <- c(i,j,k)
A <- rbind(A,v)
}
}
}
return(A)
}
26

Sampling Distribution:
n <- 100000
x <- 0:100
y <- rep(0,101)
tot <- c()

for (i in 1:n){
S <- sum(round(runif(100)))
y[S] = y[S] + 1
tot <- c(tot,S)
}

m <- 4
z4 <- rep(0,101)
for (i in 1:n){
v <- round(runif(m,1,n))
S <- sum(tot[v]) / m
z4[S] = z4[S] + 1}

m <- 9
z9 <- rep(0,101)
for (i in 1:n){
v <- round(runif(m,1,n))
S <- sum(tot[v]) / m
z9[S] = z9[S] + 1}

plot(x[30:70],z9[30:70],’b’,pch = 16, col = "green")


lines(x[30:70],z4[30:70],’b’,pch = 16, col = "blue")
lines(x[30:70],y[30:70],’b’,pch = 16, col = "red")
27

Confidence Interval:

n <- 10000
#mu: 120
#sigma: 120/sqrt(3)
#conf: 95%
#z:1.96
$sample size: 25
#120 - 1.96 * 120/(5 * sqrt(3)) = 92.84
#120 + 1.96 * 120/(5 * sqrt(3)) = 147.16
success <- 0
for (i in 1:n) {
x <- runif(25, 0, 240)
xbar <- mean(x)
if (92.84 <= xbar & xbar <= 147.16) {
success <- success + 1
}
}
success/n
28

Gambler’s Ruin:

# Start with n dollars


# Bet one dollar each turn
# Win probability of a single game=0.5
# End when reach target (N) or ruin (0)
# What is the probability of reaching target?

N <- 100
n <- 58
S <- 0

for (i in 1:1000) {
x <- n
while (x < N && x > 0) {
if (runif(1) < 0.5) {
x = x - 1
} else {
x = x + 1
}
}
if (x == N) {
S <- S + 1
}
}
print(S/1000)

You might also like