S<-as.vector(0)
S[1]<-11.956
for (t in 2 : 253 ){
S[t]=S[t-1] + rnorm(n=1, mean = 0, sd = 1)}
plot.ts(S, main = "Precio de la acción para un año",
xlab = "tiempo en días", ylab="precio de la acción",
lwd=2)
trayectorias<-1000
S<-matrix(0, ncol=trayectorias, nrow = 253)
S[1,]<-11.956
for(i in 1:trayectorias){
for(t in 2 : 253){ S[t,i]=S[t-1,i]+ rnorm(n=1, mean = 0, sd = 1) }}
matplot(S , type="l", main="precio simulado con 1000 trayectorias",
xlab="tiempo en días", ylab="precio", las=1, font.axis=4)
n=252
aleatorios<-rbinom(n=n, size=1, p=0.5)
x<-ifelse(aleatorios < 1, -1, 1)
posicion<-c(0,cumsum(x))
cbind(aleatorios, x, posicion)
## Warning in cbind(aleatorios, x, posicion): number of rows of result is not a
## multiple of vector length (arg 1)
## aleatorios x posicion
## [1,] 0 -1 0
## [2,] 1 1 -1
## [3,] 1 1 0
## [4,] 0 -1 1
## [5,] 1 1 0
## [6,] 1 1 1
## [7,] 1 1 2
## [8,] 1 1 3
## [9,] 1 1 4
## [10,] 1 1 5
## [11,] 0 -1 6
## [12,] 1 1 5
## [13,] 1 1 6
## [14,] 0 -1 7
## [15,] 1 1 6
## [16,] 1 1 7
## [17,] 1 1 8
## [18,] 1 1 9
## [19,] 1 1 10
## [20,] 0 -1 11
## [21,] 0 -1 10
## [22,] 1 1 9
## [23,] 1 1 10
## [24,] 1 1 11
## [25,] 1 1 12
## [26,] 0 -1 13
## [27,] 0 -1 12
## [28,] 0 -1 11
## [29,] 0 -1 10
## [30,] 0 -1 9
## [31,] 0 -1 8
## [32,] 1 1 7
## [33,] 0 -1 8
## [34,] 0 -1 7
## [35,] 1 1 6
## [36,] 0 -1 7
## [37,] 1 1 6
## [38,] 0 -1 7
## [39,] 0 -1 6
## [40,] 0 -1 5
## [41,] 0 -1 4
## [42,] 0 -1 3
## [43,] 1 1 2
## [44,] 1 1 3
## [45,] 0 -1 4
## [46,] 0 -1 3
## [47,] 1 1 2
## [48,] 1 1 3
## [49,] 0 -1 4
## [50,] 1 1 3
## [51,] 0 -1 4
## [52,] 0 -1 3
## [53,] 0 -1 2
## [54,] 1 1 1
## [55,] 0 -1 2
## [56,] 0 -1 1
## [57,] 0 -1 0
## [58,] 0 -1 -1
## [59,] 0 -1 -2
## [60,] 0 -1 -3
## [61,] 0 -1 -4
## [62,] 0 -1 -5
## [63,] 0 -1 -6
## [64,] 0 -1 -7
## [65,] 0 -1 -8
## [66,] 1 1 -9
## [67,] 1 1 -8
## [68,] 0 -1 -7
## [69,] 0 -1 -8
## [70,] 1 1 -9
## [71,] 0 -1 -8
## [72,] 1 1 -9
## [73,] 0 -1 -8
## [74,] 1 1 -9
## [75,] 1 1 -8
## [76,] 1 1 -7
## [77,] 0 -1 -6
## [78,] 0 -1 -7
## [79,] 1 1 -8
## [80,] 0 -1 -7
## [81,] 0 -1 -8
## [82,] 1 1 -9
## [83,] 0 -1 -8
## [84,] 0 -1 -9
## [85,] 0 -1 -10
## [86,] 0 -1 -11
## [87,] 0 -1 -12
## [88,] 1 1 -13
## [89,] 1 1 -12
## [90,] 0 -1 -11
## [91,] 0 -1 -12
## [92,] 0 -1 -13
## [93,] 1 1 -14
## [94,] 0 -1 -13
## [95,] 1 1 -14
## [96,] 1 1 -13
## [97,] 0 -1 -12
## [98,] 1 1 -13
## [99,] 1 1 -12
## [100,] 0 -1 -11
## [101,] 1 1 -12
## [102,] 1 1 -11
## [103,] 1 1 -10
## [104,] 0 -1 -9
## [105,] 1 1 -10
## [106,] 0 -1 -9
## [107,] 0 -1 -10
## [108,] 0 -1 -11
## [109,] 0 -1 -12
## [110,] 1 1 -13
## [111,] 1 1 -12
## [112,] 0 -1 -11
## [113,] 1 1 -12
## [114,] 0 -1 -11
## [115,] 0 -1 -12
## [116,] 1 1 -13
## [117,] 1 1 -12
## [118,] 0 -1 -11
## [119,] 0 -1 -12
## [120,] 0 -1 -13
## [121,] 1 1 -14
## [122,] 1 1 -13
## [123,] 1 1 -12
## [124,] 1 1 -11
## [125,] 1 1 -10
## [126,] 1 1 -9
## [127,] 1 1 -8
## [128,] 0 -1 -7
## [129,] 1 1 -8
## [130,] 1 1 -7
## [131,] 0 -1 -6
## [132,] 0 -1 -7
## [133,] 1 1 -8
## [134,] 1 1 -7
## [135,] 0 -1 -6
## [136,] 1 1 -7
## [137,] 1 1 -6
## [138,] 1 1 -5
## [139,] 1 1 -4
## [140,] 0 -1 -3
## [141,] 1 1 -4
## [142,] 0 -1 -3
## [143,] 0 -1 -4
## [144,] 0 -1 -5
## [145,] 1 1 -6
## [146,] 0 -1 -5
## [147,] 1 1 -6
## [148,] 1 1 -5
## [149,] 1 1 -4
## [150,] 0 -1 -3
## [151,] 1 1 -4
## [152,] 0 -1 -3
## [153,] 0 -1 -4
## [154,] 1 1 -5
## [155,] 1 1 -4
## [156,] 0 -1 -3
## [157,] 0 -1 -4
## [158,] 1 1 -5
## [159,] 1 1 -4
## [160,] 1 1 -3
## [161,] 1 1 -2
## [162,] 1 1 -1
## [163,] 1 1 0
## [164,] 0 -1 1
## [165,] 1 1 0
## [166,] 1 1 1
## [167,] 0 -1 2
## [168,] 1 1 1
## [169,] 1 1 2
## [170,] 0 -1 3
## [171,] 0 -1 2
## [172,] 0 -1 1
## [173,] 1 1 0
## [174,] 1 1 1
## [175,] 0 -1 2
## [176,] 0 -1 1
## [177,] 0 -1 0
## [178,] 0 -1 -1
## [179,] 1 1 -2
## [180,] 1 1 -1
## [181,] 1 1 0
## [182,] 0 -1 1
## [183,] 0 -1 0
## [184,] 1 1 -1
## [185,] 0 -1 0
## [186,] 1 1 -1
## [187,] 1 1 0
## [188,] 1 1 1
## [189,] 0 -1 2
## [190,] 0 -1 1
## [191,] 1 1 0
## [192,] 0 -1 1
## [193,] 1 1 0
## [194,] 0 -1 1
## [195,] 0 -1 0
## [196,] 1 1 -1
## [197,] 1 1 0
## [198,] 0 -1 1
## [199,] 1 1 0
## [200,] 0 -1 1
## [201,] 1 1 0
## [202,] 0 -1 1
## [203,] 0 -1 0
## [204,] 0 -1 -1
## [205,] 1 1 -2
## [206,] 1 1 -1
## [207,] 1 1 0
## [208,] 1 1 1
## [209,] 1 1 2
## [210,] 1 1 3
## [211,] 1 1 4
## [212,] 1 1 5
## [213,] 0 -1 6
## [214,] 0 -1 5
## [215,] 0 -1 4
## [216,] 1 1 3
## [217,] 1 1 4
## [218,] 1 1 5
## [219,] 0 -1 6
## [220,] 0 -1 5
## [221,] 1 1 4
## [222,] 0 -1 5
## [223,] 1 1 4
## [224,] 1 1 5
## [225,] 0 -1 6
## [226,] 1 1 5
## [227,] 0 -1 6
## [228,] 1 1 5
## [229,] 0 -1 6
## [230,] 0 -1 5
## [231,] 0 -1 4
## [232,] 1 1 3
## [233,] 0 -1 4
## [234,] 0 -1 3
## [235,] 1 1 2
## [236,] 0 -1 3
## [237,] 0 -1 2
## [238,] 0 -1 1
## [239,] 0 -1 0
## [240,] 1 1 -1
## [241,] 0 -1 0
## [242,] 0 -1 -1
## [243,] 1 1 -2
## [244,] 1 1 -1
## [245,] 0 -1 0
## [246,] 1 1 -1
## [247,] 1 1 0
## [248,] 1 1 1
## [249,] 0 -1 2
## [250,] 0 -1 1
## [251,] 0 -1 0
## [252,] 0 -1 -1
## [253,] 0 -1 -2
par(mfrow=c(1,1))
plot(x=1:(n+1),y=posicion,type='o',pch=16,cex=1.2,
main='Caminata Aleatoria Simple', ylab='Espacio de Estados de X', xlab='Espacio Parametral Z')
abline(h=0,v=0,col='darkblue', lwd=4)
mean(posicion)
## [1] -1.620553
sd(posicion)
## [1] 6.489788
library(ggplot2)
hist(posicion, geom="histogram", binwidth=2)
## Warning in plot.window(xlim, ylim, "", ...): "geom" is not a graphical parameter
## Warning in plot.window(xlim, ylim, "", ...): "binwidth" is not a graphical
## parameter
## Warning in title(main = main, sub = sub, xlab = xlab, ylab = ylab, ...): "geom"
## is not a graphical parameter
## Warning in title(main = main, sub = sub, xlab = xlab, ylab = ylab, ...):
## "binwidth" is not a graphical parameter
## Warning in axis(1, ...): "geom" is not a graphical parameter
## Warning in axis(1, ...): "binwidth" is not a graphical parameter
## Warning in axis(2, ...): "geom" is not a graphical parameter
## Warning in axis(2, ...): "binwidth" is not a graphical parameter
n=1000
trayectorias<-500
aleatorios<-matrix(0, ncol=trayectorias, nrow = (n+1))
X<-matrix(0, ncol=trayectorias, nrow = (n+1))
posicion<-matrix(0, ncol=trayectorias, nrow = (n+1))
for(i in 2 :(n+1)){ for(j in 1:trayectorias){
aleatorios[i,j]=rbinom(n=1, size=1, p=0.5)
X[i,j]=ifelse(aleatorios[i,j] < 1, -1, 1)
posicion[,j]=cumsum(X[,j]) }}
matplot (posicion, type="l",
main="precio simulado con 500 trayectorias", xlab="pasos", ylab="posición", las=1,
font.axis=4,pch=16,cex=1.2)
medias<-as.vector(0)
varianzas<-as.vector(0)
for(k in 1:trayectorias){ medias[k]<-mean(posicion[,k])
varianzas[k]<-var(posicion[,k])}
###. i. la gran media y la gran varianza
media<-mean(medias)
varianza<-mean(varianzas)
hist(medias, col="paleturquoise4", freq=FALSE,
main="Histograma de las medias \nde las 500 trayectoria", las=1,
ylab="frecuencia", xlab="valores rendimientos",
font.axis=4)
abline(v=media,col='darkblue', lwd=4)
hist(varianzas, col="royalblue3", freq=FALSE,
main="Histograma de las varianzas \nde las 500 trayectoria", las=1,
ylab="frecuencia", xlab="valores varianzas",
font.axis=4)
abline(v=varianza,col='darkblue', lwd=4)
ultima_trayectoria<-as.vector(posicion[nrow(posicion),])
Frecuencias<-as.data.frame(table(sort(ultima_trayectoria)))
Pro_frecuentista<-as.data.frame((table(sort(ultima_trayectoria)))/length(ultima_trayectoria))
hist(ultima_trayectoria, col="darkgrey", freq=FALSE,
main="Histograma de los estados visitados", las=1,
ylab="frecuencia realtiva", xlab="estados visitados",
font.axis=4)
View(Pro_frecuentista)
nrow(posicion)*ncol(posicion)
## [1] 500500
estados_visitados<-matrix(posicion,
nrow=nrow(posicion)*ncol(posicion), ncol=1)
length(unique(estados_visitados))
## [1] 190
unique(estados_visitados)
## [,1]
## [1,] 0
## [2,] -1
## [3,] 1
## [4,] -2
## [5,] -3
## [6,] 2
## [7,] 3
## [8,] 4
## [9,] 5
## [10,] 6
## [11,] 7
## [12,] 8
## [13,] 9
## [14,] 10
## [15,] 11
## [16,] 12
## [17,] 13
## [18,] 14
## [19,] 15
## [20,] 16
## [21,] 17
## [22,] 18
## [23,] 19
## [24,] 20
## [25,] 21
## [26,] 22
## [27,] 23
## [28,] -4
## [29,] -5
## [30,] -6
## [31,] -7
## [32,] -8
## [33,] -9
## [34,] -10
## [35,] -11
## [36,] -12
## [37,] -13
## [38,] -14
## [39,] -15
## [40,] -16
## [41,] -17
## [42,] -18
## [43,] -19
## [44,] -20
## [45,] -21
## [46,] -22
## [47,] -23
## [48,] -24
## [49,] -25
## [50,] -26
## [51,] -27
## [52,] -28
## [53,] -29
## [54,] -30
## [55,] -31
## [56,] -32
## [57,] -33
## [58,] -34
## [59,] -35
## [60,] -36
## [61,] -37
## [62,] -38
## [63,] -39
## [64,] -40
## [65,] -41
## [66,] -42
## [67,] -43
## [68,] -44
## [69,] -45
## [70,] -46
## [71,] -47
## [72,] -48
## [73,] -49
## [74,] -50
## [75,] -51
## [76,] -52
## [77,] -53
## [78,] -54
## [79,] -55
## [80,] -56
## [81,] -57
## [82,] -58
## [83,] -59
## [84,] -60
## [85,] -61
## [86,] -62
## [87,] -63
## [88,] -64
## [89,] -65
## [90,] -66
## [91,] -67
## [92,] -68
## [93,] -69
## [94,] -70
## [95,] -71
## [96,] -72
## [97,] -73
## [98,] 24
## [99,] 25
## [100,] 26
## [101,] 27
## [102,] 28
## [103,] 29
## [104,] 30
## [105,] 31
## [106,] 32
## [107,] 33
## [108,] 34
## [109,] 35
## [110,] 36
## [111,] 37
## [112,] 38
## [113,] 39
## [114,] 40
## [115,] 41
## [116,] 42
## [117,] 43
## [118,] 44
## [119,] 45
## [120,] 46
## [121,] 47
## [122,] 48
## [123,] 49
## [124,] 50
## [125,] 51
## [126,] 52
## [127,] 53
## [128,] 54
## [129,] 55
## [130,] 56
## [131,] 57
## [132,] 58
## [133,] 59
## [134,] 60
## [135,] 61
## [136,] 62
## [137,] 63
## [138,] 64
## [139,] 65
## [140,] 66
## [141,] 67
## [142,] 68
## [143,] -74
## [144,] -75
## [145,] -76
## [146,] -77
## [147,] -78
## [148,] -79
## [149,] -80
## [150,] -81
## [151,] -82
## [152,] -83
## [153,] -84
## [154,] -85
## [155,] -86
## [156,] -87
## [157,] -88
## [158,] -89
## [159,] -90
## [160,] -91
## [161,] -92
## [162,] -93
## [163,] -94
## [164,] -95
## [165,] -96
## [166,] -97
## [167,] -98
## [168,] -99
## [169,] 69
## [170,] 70
## [171,] 71
## [172,] 72
## [173,] 73
## [174,] 74
## [175,] 75
## [176,] 76
## [177,] 77
## [178,] 78
## [179,] 79
## [180,] 80
## [181,] 81
## [182,] 82
## [183,] 83
## [184,] 84
## [185,] 85
## [186,] 86
## [187,] 87
## [188,] 88
## [189,] 89
## [190,] 90
table(estados_visitados)
## estados_visitados
## -99 -98 -97 -96 -95 -94 -93 -92 -91 -90 -89 -88 -87
## 3 10 13 29 54 67 66 58 56 66 72 52 34
## -86 -85 -84 -83 -82 -81 -80 -79 -78 -77 -76 -75 -74
## 36 35 34 38 45 59 81 84 73 78 84 84 96
## -73 -72 -71 -70 -69 -68 -67 -66 -65 -64 -63 -62 -61
## 110 124 167 245 276 260 269 292 313 341 372 422 461
## -60 -59 -58 -57 -56 -55 -54 -53 -52 -51 -50 -49 -48
## 460 461 527 575 592 665 751 789 786 781 826 908 996
## -47 -46 -45 -44 -43 -42 -41 -40 -39 -38 -37 -36 -35
## 1020 1027 1024 1012 1053 1108 1065 1019 1094 1248 1373 1534 1675
## -34 -33 -32 -31 -30 -29 -28 -27 -26 -25 -24 -23 -22
## 1867 2006 2115 2292 2595 2960 3193 3377 3575 3745 3862 4076 4400
## -21 -20 -19 -18 -17 -16 -15 -14 -13 -12 -11 -10 -9
## 4642 4955 5472 5890 6091 6406 6799 7079 7289 7586 7919 8284 8635
## -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4
## 8938 9413 10000 10424 11015 11494 11774 12022 12289 11707 11176 10683 10317
## 5 6 7 8 9 10 11 12 13 14 15 16 17
## 9934 9661 9368 9057 8602 8062 7613 7240 6815 6422 6198 5853 5472
## 18 19 20 21 22 23 24 25 26 27 28 29 30
## 5257 4983 4743 4558 4520 4466 4246 4043 3961 3813 3684 3601 3320
## 31 32 33 34 35 36 37 38 39 40 41 42 43
## 3076 2916 2731 2605 2349 2131 2105 2074 1922 1786 1707 1692 1639
## 44 45 46 47 48 49 50 51 52 53 54 55 56
## 1519 1378 1282 1218 1117 957 865 846 870 831 782 761 665
## 57 58 59 60 61 62 63 64 65 66 67 68 69
## 593 554 489 451 413 342 260 229 238 228 201 175 149
## 70 71 72 73 74 75 76 77 78 79 80 81 82
## 139 128 107 82 73 75 79 81 84 73 44 20 16
## 83 84 85 86 87 88 89 90
## 15 14 11 5 7 9 6 4
library(car)
## Warning: package 'car' was built under R version 4.1.3
## Loading required package: carData
n_visitados<-as.data.frame(table(estados_visitados))
Pro_frecuentista<-round(n_visitados$Freq/(n-1), digits = 3)
Pro_frecuentista
## [1] 0.003 0.010 0.013 0.029 0.054 0.067 0.066 0.058 0.056 0.066
## [11] 0.072 0.052 0.034 0.036 0.035 0.034 0.038 0.045 0.059 0.081
## [21] 0.084 0.073 0.078 0.084 0.084 0.096 0.110 0.124 0.167 0.245
## [31] 0.276 0.260 0.269 0.292 0.313 0.341 0.372 0.422 0.461 0.460
## [41] 0.461 0.528 0.576 0.593 0.666 0.752 0.790 0.787 0.782 0.827
## [51] 0.909 0.997 1.021 1.028 1.025 1.013 1.054 1.109 1.066 1.020
## [61] 1.095 1.249 1.374 1.536 1.677 1.869 2.008 2.117 2.294 2.598
## [71] 2.963 3.196 3.380 3.579 3.749 3.866 4.080 4.404 4.647 4.960
## [81] 5.477 5.896 6.097 6.412 6.806 7.086 7.296 7.594 7.927 8.292
## [91] 8.644 8.947 9.422 10.010 10.434 11.026 11.506 11.786 12.034 12.301
## [101] 11.719 11.187 10.694 10.327 9.944 9.671 9.377 9.066 8.611 8.070
## [111] 7.621 7.247 6.822 6.428 6.204 5.859 5.477 5.262 4.988 4.748
## [121] 4.563 4.525 4.470 4.250 4.047 3.965 3.817 3.688 3.605 3.323
## [131] 3.079 2.919 2.734 2.608 2.351 2.133 2.107 2.076 1.924 1.788
## [141] 1.709 1.694 1.641 1.521 1.379 1.283 1.219 1.118 0.958 0.866
## [151] 0.847 0.871 0.832 0.783 0.762 0.666 0.594 0.555 0.489 0.451
## [161] 0.413 0.342 0.260 0.229 0.238 0.228 0.201 0.175 0.149 0.139
## [171] 0.128 0.107 0.082 0.073 0.075 0.079 0.081 0.084 0.073 0.044
## [181] 0.020 0.016 0.015 0.014 0.011 0.005 0.007 0.009 0.006 0.004