1.- Dibuje un Diagrama donde indique la Tendencia Aleatoria y Tendencia Determinista no Aleatoria

Diagrama Tendencia Aleatoria

x <- 1:100
y <- rnorm(100)
plot(x, y, main = "Gráfica con Tendencia Aleatoria", xlab = "X", ylab = "Y")

Diagrama Tendencia Determinista no aleatoria

x <- 1:100
y <- 2*x + 3
plot(x, y, main = "Gráfica con Tendencia Determinista", xlab = "X", ylab = "Y")

2. Señale V o F

3.. Complete:

4.Desarrolle el Método de Cuadrado Medio con su proceso a mano (10) y en R Studio (10 y 100), Xo: 5735 (0.40)

addig <- function(x){
  dig <- unlist(strsplit(as.character(x), ""))
  if(length(dig) < 8){c(rep(0, 8 - length(dig)), dig)} else{dig}
}

midsquare <- function(n, seed){
  for(i in 1:n) {
    seed[i + 1] <- as.numeric(paste(addig(seed[i]^2)[3:6], collapse = ""))
  }
  (seed / 10000)[-1]
}

midsquare(10,5735)
 [1] 0.8902 0.2456 0.0319 0.1017 0.0342 0.1169 0.3665 0.4322 0.6796 0.1856
midsquare(100,5735)
  [1] 0.8902 0.2456 0.0319 0.1017 0.0342 0.1169 0.3665 0.4322 0.6796 0.1856
 [11] 0.4447 0.7758 0.1865 0.4782 0.8675 0.2556 0.5331 0.4195 0.5980 0.7604
 [21] 0.8208 0.3712 0.7789 0.6685 0.6892 0.4996 0.9600 0.1600 0.5600 0.3600
 [31] 0.9600 0.1600 0.5600 0.3600 0.9600 0.1600 0.5600 0.3600 0.9600 0.1600
 [41] 0.5600 0.3600 0.9600 0.1600 0.5600 0.3600 0.9600 0.1600 0.5600 0.3600
 [51] 0.9600 0.1600 0.5600 0.3600 0.9600 0.1600 0.5600 0.3600 0.9600 0.1600
 [61] 0.5600 0.3600 0.9600 0.1600 0.5600 0.3600 0.9600 0.1600 0.5600 0.3600
 [71] 0.9600 0.1600 0.5600 0.3600 0.9600 0.1600 0.5600 0.3600 0.9600 0.1600
 [81] 0.5600 0.3600 0.9600 0.1600 0.5600 0.3600 0.9600 0.1600 0.5600 0.3600
 [91] 0.9600 0.1600 0.5600 0.3600 0.9600 0.1600 0.5600 0.3600 0.9600 0.1600

5.-Desarrolle un ejemplo de Generador Congruencia Combinado manual (5) y R Studio (10 y 100).

congcomb <- function(n, a, m, seed){
  k <- length(a)
  
  ri <- NULL # Para que se vayan almacenando
  for (i in 1:n) {
    seed <- (a * seed) %% m
    xi <- sum((-1)^(1:k-1) * seed) %% m[1]
    ri[i] <- ifelse(xi == 0, (m[1] - 1) / m[1], xi / m[1])
  }
  ri
}

congcomb(n = 10, a = c(2,11,5), m = c(16,32,128), seed = c(12,7,120))
 [1] 0.1875 0.5625 0.1875 0.0625 0.6875 0.5625 0.1875 0.0625 0.6875 0.5625
congcomb(n = 100, a = c(2,11,5), m = c(16,32,128), seed = c(12,7,120))
  [1] 0.1875 0.5625 0.1875 0.0625 0.6875 0.5625 0.1875 0.0625 0.6875 0.5625
 [11] 0.1875 0.0625 0.6875 0.5625 0.1875 0.0625 0.6875 0.5625 0.1875 0.0625
 [21] 0.6875 0.5625 0.1875 0.0625 0.6875 0.5625 0.1875 0.0625 0.6875 0.5625
 [31] 0.1875 0.0625 0.6875 0.5625 0.1875 0.0625 0.6875 0.5625 0.1875 0.0625
 [41] 0.6875 0.5625 0.1875 0.0625 0.6875 0.5625 0.1875 0.0625 0.6875 0.5625
 [51] 0.1875 0.0625 0.6875 0.5625 0.1875 0.0625 0.6875 0.5625 0.1875 0.0625
 [61] 0.6875 0.5625 0.1875 0.0625 0.6875 0.5625 0.1875 0.0625 0.6875 0.5625
 [71] 0.1875 0.0625 0.6875 0.5625 0.1875 0.0625 0.6875 0.5625 0.1875 0.0625
 [81] 0.6875 0.5625 0.1875 0.0625 0.6875 0.5625 0.1875 0.0625 0.6875 0.5625
 [91] 0.1875 0.0625 0.6875 0.5625 0.1875 0.0625 0.6875 0.5625 0.1875 0.0625

6.- Test de Uniformidad , aplicando en R Studio con una semilla aleatoria de 5735 con 10 y con 100 aplicando los dos métodos: Ch Cuadrado y Kolmogorrov- Smirnof

# Ch-Cuadrado ------------------------------------------------------------------
# 10

s1 <- midsquare(10,5735)
s1
 [1] 0.8902 0.2456 0.0319 0.1017 0.0342 0.1169 0.3665 0.4322 0.6796 0.1856
mean(s1)
[1] 0.30844
var(s1)
[1] 0.08278238
hist(s1, probability = T)
curve(dunif(x, min = 0, max = 1), col = "red", add = T)

#install.packages("fdth")
library(fdth)

Attaching package: 'fdth'
The following objects are masked from 'package:stats':

    sd, var
trf <- fdt(s1) 
trf
   Class limits f  rf rf(%) cf cf(%)
 [0.0316,0.205) 5 0.5    50  5    50
  [0.205,0.379) 2 0.2    20  7    70
  [0.379,0.552) 1 0.1    10  8    80
  [0.552,0.726) 1 0.1    10  9    90
  [0.726,0.899) 1 0.1    10 10   100
trf$table
trf$breaks
    start       end         h     right 
0.0315810 0.8991020 0.1735042 0.0000000 
trf <- fdt(s1, start = 0, end = 1, h = 0.1)
trf <- trf$table
trf
fa <- trf$f
fa
 [1] 2 3 1 1 1 0 1 0 1 0
pr_teor <- rep(0.1, 10)
chisq.test(x = fa, p = pr_teor)
Warning in chisq.test(x = fa, p = pr_teor): Chi-squared approximation may be
incorrect

    Chi-squared test for given probabilities

data:  fa
X-squared = 8, df = 9, p-value = 0.5341
#100

s2 <- midsquare(100,5735)
s2
  [1] 0.8902 0.2456 0.0319 0.1017 0.0342 0.1169 0.3665 0.4322 0.6796 0.1856
 [11] 0.4447 0.7758 0.1865 0.4782 0.8675 0.2556 0.5331 0.4195 0.5980 0.7604
 [21] 0.8208 0.3712 0.7789 0.6685 0.6892 0.4996 0.9600 0.1600 0.5600 0.3600
 [31] 0.9600 0.1600 0.5600 0.3600 0.9600 0.1600 0.5600 0.3600 0.9600 0.1600
 [41] 0.5600 0.3600 0.9600 0.1600 0.5600 0.3600 0.9600 0.1600 0.5600 0.3600
 [51] 0.9600 0.1600 0.5600 0.3600 0.9600 0.1600 0.5600 0.3600 0.9600 0.1600
 [61] 0.5600 0.3600 0.9600 0.1600 0.5600 0.3600 0.9600 0.1600 0.5600 0.3600
 [71] 0.9600 0.1600 0.5600 0.3600 0.9600 0.1600 0.5600 0.3600 0.9600 0.1600
 [81] 0.5600 0.3600 0.9600 0.1600 0.5600 0.3600 0.9600 0.1600 0.5600 0.3600
 [91] 0.9600 0.1600 0.5600 0.3600 0.9600 0.1600 0.5600 0.3600 0.9600 0.1600
mean(s2)
[1] 0.500719
var(s2)
[1] 0.08550985
hist(s2, probability = T)
curve(dunif(x, min = 0, max = 1), col = "red", add = T)

library(fdth)
trf <- fdt(s2) 
trf
     Class limits  f   rf rf(%)  cf cf(%)
 [0.03158,0.1488)  4 0.04     4   4     4
  [0.1488,0.2661) 23 0.23    23  27    27
  [0.2661,0.3833) 20 0.20    20  47    47
  [0.3833,0.5006)  5 0.05     5  52    52
  [0.5006,0.6178) 20 0.20    20  72    72
  [0.6178,0.7351)  3 0.03     3  75    75
  [0.7351,0.8523)  4 0.04     4  79    79
  [0.8523,0.9696) 21 0.21    21 100   100
trf$table
trf$breaks
    start       end         h     right 
0.0315810 0.9696000 0.1172524 0.0000000 
trf <- fdt(s2, start = 0, end = 1, h = 0.1)
trf <- trf$table
trf
fa <- trf$f
fa
 [1]  2 23  2 20  5 20  3  3  3 19
pr_teor <- rep(0.1, 10)
chisq.test(x = fa, p = pr_teor)

    Chi-squared test for given probabilities

data:  fa
X-squared = 75, df = 9, p-value = 1.58e-12
# Kolmogorrov- Smirnof ---------------------------------------------------------
  
#n=10
ks.test(s1, "punif", 0, 1)

    Exact one-sample Kolmogorov-Smirnov test

data:  s1
D = 0.3678, p-value = 0.1014
alternative hypothesis: two-sided
#n=100
ks.test(s2, "punif", 0, 1)
Warning in ks.test.default(s2, "punif", 0, 1): ties should not be present for
the Kolmogorov-Smirnov test

    Asymptotic one-sample Kolmogorov-Smirnov test

data:  s2
D = 0.15, p-value = 0.02222
alternative hypothesis: two-sided

• El valor p es 1.58e-12 en la prueba chi - cuadrada, que es menor que 0,05. Por lo tanto, hay suficiente evidencia estadística para rechazar la hipótesis nula, por lo tanto los Ri siguen una distribucion uniforme.

• El valor p es 0.02222 en la prueba kolgomorov smirnov, que es menor que 0,05. Por lo tanto, hay suficiente evidencia para rechazar la hipótesis nula, por lo tanto los Ri siguen una distribucion uniforme.