-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtratamientoest.tex
More file actions
1891 lines (1516 loc) · 140 KB
/
tratamientoest.tex
File metadata and controls
1891 lines (1516 loc) · 140 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\chapter{Tratamiento estadístico de datos\\ Statistical data analysis}
\chaptermark{Tratamiento de datos \textreferencemark\ Data analysis}
\epigraph{Dicen que las buenas mozas en Madrid han decidido, el gastar en vez de lengua una espada de dos filos.
Y si hay guerra en este invierno , los Valones y los Suizos llevarán en vez de espada guardapiés y rebocillo.}{El barberillo de Lavapiés. Luis Mariano de Larra}
\begin{flalign*}
&\mathwitch*_{i=0}^{\infty}\Biggl \{&
\end{flalign*}
\begin{paracol}{2}
\section{Secuencias de números aleatorios}
\sectionmark{Secuencias de números aleatorios. \textreferencemark\ Random number sequences.}
Se entiende por secuencia de número aleatorios, aquella en la que no es posible encontrar relación alguna entre un número de la secuencia y los que le preceden.
No es posible generar secuencias de números aleatorios con un computador. La razón es que un computador es una máquina completamente determinista; la salidas que puede producir cualquier programa son completamente predecibles. Solo los procesos físicos pueden ser realmente ---intrínsecamente--- aleatorios.
Sin embargo, es posible con un ordenador generar secuencias de números que simulan ser aleatorios. Estas secuencias reciben el nombre de secuencias de números \emph{pseudoaleatorios}.\index{Números pseudoaleatorios}
El primer algoritmo para obtener números pseudoaleatorios, lo desarrolló John Von Newman en 1946. Se conoce con el nombre de \emph{Middle Square}.\index{Middle Square} La idea consiste en elegir un número inicial, formado por $n$ cifras, que recibe el nombre de semilla.\index{Números aleatorios! Semilla} A continuación se eleva el número al cuadrado. Por último, se extraen las $n$ cifras centrales del número así generado, que constituye el segundo número en la secuencia de números aleatorios. Este número se vuelve a elevar al cuadrado y se extraen de el las $n$ cifras centrales que constituirán el tercer número aleatorio. Este proceso se repite tantas veces como números aleatorios se deseen generar.
Veamos un ejemplo con una semilla formada por $n=8$ dígitos,
\switchcolumn
\section{Random number sequences}
\sectionmark{Secuencias de números aleatorios. \textreferencemark\ Random number sequences.}
A random number sequence is such that it is not possible to find any relationship among a number of the sequence and the previous ones.
It is impossible to generate random numbers using a computer, because a computer is a deterministic machine and then, the outputs of any computer program are always thoroughly predictable. Only physical process could be actually --intrinsically-- unpredictable.
Nevertheless, it is possible to use the computer for generating sequences of number that seem to be random. Such sequences are called \emph{pseudo-random} number sequences.\index[eng]{Pseudo-random numbers}
The first algorithm for obtaining pseudo-random numbers was developed by John Von Newman in 1946. Its is known as the \emph{Middle Square} method.\index[eng]{Middle Square} It starts from an initial number made up of n digits which is called the seed.\index[eng]{Random numbers! Seed} Then, the square of the number is calculated and the n central digits of the result are taken as a new number that will be the second one of the random number sequence. This new number is squared and the n central digit of the result are taken to form the three number of the random sequence. The process is repeated so many times as random numbers you wish to generate.
Let see an example, using a n=8 digits seed,
\end{paracol}
\begin{align*}
semilla/seed=&87289689 \rightarrow
87289689^2={\color{red}7619}\ 48980571\ {\color{red}6721}\\
&48980571\rightarrow
48980571^2={\color{red}2399}\ 09633548\ {\color{red}6041}\\
&09633548 \rightarrow
09633548^2={\color{red}0092}\ 80524706\ {\color{red}8304}\\
&80524706 \rightarrow
80524706^2={\color{red}6484}\ 22827638\ {\color{red}6436}\\
&22827638\\
&\ \ \ \ \ \vdots
\end{align*}
\begin{paracol}{2}
El siguiente código de Python, permite obtener una lista de números pseudoaleatorios mediante el método \emph{Middle Square}
\switchcolumn
The next Python code allows us to get a list a pseudo-random number using the Middle Square method,
\end{paracol}
\inputminted[
frame=lines,
framesep=2mm,
baselinestretch=1.2,
%bgcolor=LightGray,
label=middlesquare.py,
fontsize=\footnotesize,
linenos
]{python}{./codigos/tratamiento_datos/middlesquare.py}
\begin{paracol}{2}
Las secuencias generadas mediante el método \emph{middle square} no son aleatorias, cada número viene completamente determinado por el número anterior. Sin embargo, dichas secuencias parecen aleatorias. La figura \ref{fig:middlesquare} muestra una secuencia de $100$ valores pseudoaleatorios generados mediante \emph{middle square}. Aparentemente, cada valor parece no guardar ninguna relación con los anteriores.
El método descrito, presenta sin embargo serios inconvenientes: Si la semilla contiene ceros o unos a la derecha, estos tienden a repetirse indefinidamente. Además todos los métodos inducen ciclos que hacen que los números generados comiencen a repetirse periódicamente. Por ejemplo, si empezamos con la semilla de cuatro dígitos $3708$, La secuencia cae, tras generar los primeros cuatro números aleatorios, en un ciclo en el que los cuatro siguientes números generados se repiten indefinidamente.
\switchcolumn
Sequences generated using the middle square method are not random, each number is thoroughly determined by the previous one. Nevertheless, these sequences seems random. Figure \ref{fig:middlesquare} shows a sequence of 100 pseudo-random values generated using the middle square. Apparently, each value doesn't seem to have any relationship with the previous ones.
However, this method has significant drawbacks: if the seed contains zeros or ones on the right side, these values tend to repeat indefinitely. Besides, all methods induce cycles, which make the generated numbers begin to repeat periodically. For instance, If we begin with the four-digit seed $3708$, The sequence gets into a cycle after it generates the first four random numbers. The four subsequent numbers repeat indefinitely.
\end{paracol}
\begin{figure}[h]
\centering
\includegraphics[width=12cm]{middlesquare.eps}
\bicaption{Secuencia de $100$ números pseudoaleatorios generada mediante el método \emph{middle square}.}{$100$ pseudo-random numbers sequence generates using the \emph{middle square} method}
\label{fig:middlesquare}
\end{figure}
\begin{align*}
semilla/seed=3708 \rightarrow
&3708^2={\color{red}13}\ 7492\ {\color{red}64}\\
&7492^2={\color{red}56}\ 1300\ {\color{red}64}\\
&1300^2={\color{red}01}\ 6900\ {\color{red}00}\\
&6900^2={\color{red}47}\ 6100\ {\color{red}00}\\
&6100^2={\color{red}37}\ 2100\ {\color{red}00}\\
&2100^2={\color{red}04}\ 4100\ {\color{red}00}\\
&4100^2={\color{red}16}\ 8100\ {\color{red}00}\\
&8100^2={\color{red}65}\ 6100\ {\color{red}00}\\
&6100^2={\color{red}37}\ 2100\ {\color{red}00}\\
&2100^2={\color{red}04}\ 4100\ {\color{red}00}\\
&4100^2={\color{red}16}\ 8100\ {\color{red}00}\\
&\ \ \ \vdots
\end{align*}
\begin{paracol}{2}
Además, en la secuencia generada puede observarse cómo los ceros situados a las derecha una vez que aparecen en la secuencia, se conservan siempre.
En los algoritmos de generación de números aleatorios, la idea es obtener números en el intervalo $[0,1]$. Para ello, se generan números entre $0$ y un número natural $m$ y luego se dividen los números generados, $X_n$ entre $m$,
\switchcolumn
Besides, it is possible to see how the the zeros locate at the right size, once they turn up, remain for ever.
Algorithms for random number generation are usually designed to generate numbers in the interval $[0,1]$. This is carried out generating a number between $0$ an a natural number $m$ and, after, dividing the generated numbers $X_n$ by $m$,
\end{paracol}
\begin{equation*}
U_n=\frac{X_n}{m}
\end{equation*}
\begin{paracol}{2}
Usualmente, se elige para $m$ un valor tan grande como sea posible.
En 1948 Lehmer propuso un algoritmo conocido con el nombre de \emph{linear congruential}. El algoritmo emplea cuatro elementos numéricos,
\begin{itemize}
\item[-] $I_0$, Semilla. Análoga a la del método \emph{middle square}.
\item[-] $m > I_0$, Módulo. Se elige tan grande como sea posible. Cuanto mayor es el módulo, mayor es el periodo del ciclo inducido.
\item[-] $a$, Multiplicador. Debe elegirse de modo que $0\le a < m$
\item[-] $c$, Incremento. Debe elegirse de modo que $0\le c < m$
\end{itemize}
El algoritmo de obtención de la secuencia de números pseudoaleatorios se define en este caso como,
\switchcolumn
Usually, a value as large as possible is chosen for $m$. In 1948 Lehmmer proposed an algorithm known as \emph{linear congruential}. This algorithm uses four numerical items,
\begin{itemize}
\item[-] $I_0$, Seed. Similar to \emph{middle square} method.
\item[-] $m > I_0$, modulus. it is chossen as large as posible. The larger the modulus the lager the period of the induced cycle.
\item[-] $a$, Multiplier. Should be chosen to meet that $0\le a < m$
\item[-] $c$, Increment. Should be chosen to meet that $0\le c < m$
\end{itemize}
Then, the algorithm to obtain the sequence of random number is defined as,
\end{paracol}
\begin{equation*}
I_{n+1}=\text{rem}(a\cdot I_n+c, m)
\end{equation*}
\begin{paracol}{2}
Es decir, cada número se obtiene como el resto de la división entera del producto del multiplicador por el número aleatorio anterior más el incremento, divido entre el módulo. Por tanto, se trata de un número comprendido entre $0$ y $m-1$.
El siguiente código permite calcular una secuencia de números aleatorios por el método \emph{linear congruential}
\switchcolumn
Each number is calculated as the remainder of the product of the multiplier and the previous random number, added to the increment, all divided by the modulus. This results in a number between 0 and m-1.
The following code calculates a sequence of random numbers using the \emph{linear congruential} method.
\end{paracol}
\inputminted[
frame=lines,
framesep=2mm,
baselinestretch=1.2,
%bgcolor=LightGray,
label=linearcong.py,
fontsize=\footnotesize,
linenos
]{python}{./codigos/tratamiento_datos/linearcong.py}
\begin{paracol}{2}
Una variante del algoritmo descrito, frecuentemente empleada, se obtiene haciendo el incremento igual a cero, $c=0$. Dicha variante se conoce con el nombre de \emph{multiplicative congruential}, su expresión general sería,
\switchcolumn
We can get a frequently used variation of the linear congruential algorithm taking the increment equal to zero, $c=0$. Such variation is known as the \emph{multiplicative congruential} algorithm; a general expression for it would be,
\end{paracol}
\begin{equation*}
I_{n+1}=\text{rem}(a\cdot I_n)
\end{equation*}
\begin{paracol}{2}
Dado que el resto de la división entera de un número cualquiera entre el módulo $m$ solo puede tomar valores enteros comprendidos entre $0$ y $m-1$, es fácil deducir que, en el mejor de los casos, podríamos obtener un ciclo de longitud $m$, antes de que secuencia de números aleatorios comenzara a repetirse. De ahí la importancia de elegir $m$ lo más grande posible. Por otra parte, los valores de $I_0$ e $c$, también influyen en la longitud de los ciclos inducidos.
Si ahora dividimos los numero aleatorios obtenidos por el modulo,
\switchcolumn
Since the integer division of whatever number by the modulus $m$ can yield results between $0$ and $m-1$, it becomes clear that, in the best-case scenario, we can achieve a cycle of length $m$ before the sequence of random numbers starts to repeat. Hence, the importance of choosing a large as possible value for $m$. On the other hand, the values taken by $I_0$ and $c$ also influence the length of the induced cycles.
If we now divide the random number achieved by the modulus,
\end{paracol}
\begin{equation*}
x_n=\frac{I_n}{m} \Rightarrow x_n \in [0,1]
\end{equation*}
\begin{paracol}{2}
Los números así obtenidos pertenecen al intervalo $[0,1]$ y están regularmente distribuidos.
En la década de los 60 del siglo pasado, La compañía IBM, desarrolló una función, conocida como \emph{Randu}, que utilizaba el algoritmo \emph{multiplicative congruential}. \emph{Randu} tenía definido un multiplicador $c=2^{16}+3$ y un módulo $m=2^{31}$.
Se han desarrollado diferentes variante de \emph{Randu}, mejorando progresivamente su rendimiento. Una de las mejoras introducidas fue aumentar el número de semillas empleadas,
\switchcolumn
The number obtained belong to the interval $[0,1]$ and are regularly distributed.
In the last century sixties, IBM company developed a function known as \emph{Randu} which employed the \emph{multiplication congruential} algorithm. \emph{Randu} had defined a multiplier $c=2^{16}+3$ and modulus $m=2^{31}$.
Several variations of \emph{Randu} have been developed, gradually improving its performance. One of these improvements was to increase the number of seeds the algorithm uses.
\end{paracol}
\begin{equation*}
I_{n+1}=\text{rem}(a\cdot I_n+b\cdot I_{n-1}+\cdots)
\end{equation*}
\begin{paracol}{2}
Este método permite obtener periodos mayores.
\switchcolumn
This method leads to get longer periods
\end{paracol}
\begin{paracol}{2}
\subsection{El generador de números aleatorios de Numpy.}\label{rand} Se trata de un generador mucho mas evolucionado que los ejemplo descritos hasta ahora. Es muy versátil y se puede emplear de muchas maneras. Aquí vamos a ver solo una introducción. Para una visión detallada, lo mejor es leer la documentación de numpy, (\emph{random sampling}).
Numpy contiene un módulo \mintinline{python}|numpy.random|, que contiene todas las funciones necesarias para generar secuencias de números aleatorios. Lo primero que debemos hacer, es crear un ``generador'' (\emph{generator}) de numeros aleatorios. La manera más sencilla de hacerlo es emplear la función \mintinline{python}|default_rng()|,
\switchcolumn
\subsection{Numpy's random numbers generator.} The numpy's random number generator is fairly more evolved than the example generators described so far. It is highly versatile and can be use in many different ways. Here, we are going to offer only an introduction. For a detailed knowledge it is advisable to read numpy documentation, (\emph{random sampling}).
Numpy has a module \mintinline{python}|numpy.random|, which has all the functions needed to generate sequences of random numbers. The first step is to create a \emph{generator} of random numbers. The easiest way to do it is by using the function \mintinline{python}|default_rng()|,
\end{paracol}
\begin{minted}{python}
import numpy as np
#lo primero de todo es crear un generador de números aleatorios
#si no le indicamos semilla, el la toma del sistema operativo de modo no
#determinista, es decir la secuencia de números generada no podrá volver
#a reproducirse.
gen_num = np.random.default_rng()
\end{minted}
\begin{paracol}{2}
El \emph{objeto} creado \mintinline{python}|gen_num| es un generador de números aleatorios. Lo hemos creado de la forma más sencilla posible. Como no le hemos indicado ningún valor para la semilla, el programa la toma directamente del sistema operativo de un modo aleatorio. Para obtener un número aleatorio en el intervalo $[0,1)$, llamamos directamente al generador que hemos creado.
\switchcolumn
The \emph{object} so created \mintinline{python}|gen_num| is a random number generator. We have create it in the most simple way. A we haven't supplied any value for the seed, the program takes it directly from the operative system in aleatory mode. To get a random number un the interval $[0,1)$, we call the generator we have created directly,
\end{paracol}
\begin{center}
\begin{minipage}{0.5\textwidth}
\begin{minted}{python}
In [2]: n1 = gen_num.random()
...: print('n1=',n1)
n1= 0.15431941570206298
\end{minted}
\end{minipage}
\end{center}
\begin{paracol}{2}
Si llamamos al generador con un valor entero como variable de entrada, nos generará un array de numpy con tantos elementos como indique el valor entero,
\switchcolumn
If we call the generator with an integer number as input variable, it will generate a numpy array with the a number of elements equal to the integer supplied.
\end{paracol}
\begin{center}
\begin{minipage}{0.5\textwidth}
\begin{minted}{python}
In [7]: v = gen_num.random(3)
In [8]: v
Out[8]: array([0.07971535, 0.38433405, 0.03229333])
\end{minted}
\end{minipage}
\end{center}
\begin{paracol}{2}
\noindent y si introducimos una tupla o una lista con dos elementos $[m,n]$, devolverá una matriz de números aleatorios de dimensión $n\times m$,
\switchcolumn
\noindent and if we use a tuple or a list with two elements $[m,n]$, the generator will cast a matrix of random number with dimensions $n\times m$.
\end{paracol}
\begin{center}
\begin{minipage}{0.8\textwidth}
\begin{minted}{python}
In [9]: v = gen_num.random((3,4))
In [10]: v
Out[10]:
array([[0.06821989, 0.6214247 , 0.07793149, 0.99665013],
[0.11869072, 0.6999295 , 0.8089612 , 0.02035995],
[0.67626972, 0.48921183, 0.38843264, 0.71577686]])
\end{minted}
\end{minipage}
\end{center}
\begin{paracol}{2}
Cada vez, que ejecutamos, el generador, éste avanza en la secuencia de números aleatorios generados, a partir de la semilla de inicialización. El método empleado por el generador para obtener la secuencia de números aleatorios, se conoce con el nombre de \emph{Permuted Congruential Generator}. Es el método por defecto, y el único que podemos emplear si creamos nuestro generador empleando la función \mintinline{python}|default_rng()|. Podemos comprobar cual es método empleado por un generador empleando la función print de python,
\switchcolumn
Any time we run the generator, it goes ahead in the sequence of random numbers generated from the initial seed. The method used by the generator to get the sequence of random numbers in known as \emph{Permuted Congruential Generator}. It is the default method and the only one we can use if we create a generator using the \mintinline{python}|default_rng()|. We can determine which method a generator is using by utilizing Python's print function.
\end{paracol}
\begin{center}
\begin{minipage}{0.3\textwidth}
\begin{minted}{python}
In [12]: print(gen_num)
Generator(PCG64)
\end{minted}
\end{minipage}
\end{center}
\begin{paracol}{2}
Nos devuelve las iniciales del método y el número 64 que hace referencia al tamaño en bits de los números enteros empleados en el proceso de generación. EL periodo de este método es de $2^{128}$.
Podemos controlar la secuencia de números aleatorios suministrando una semilla a la función \mintinline{python}|default_rng|, de este modo, podemos reproducir cualquier secuencia de numeros aleatorios, siempre que creemos un nuevo generador con la misma semilla,
\switchcolumn
It retrieves the acronym of the method and the number 64 which refers to the bit size of the number used in the random generation process. The period of this method is $2^{128}$.
We can control the random number sequence supplying a seed to function \mintinline{python}|default_rng|; in this way, we can reproduce any sequence of random numbers, provided we create a new generator with the same seed.
\end{paracol}
\begin{minted}{python}
In [48]:
...: gen_num_1 = np.random.default_rng(100)
...: #generamos una secuencia de números aleatorios
...: for i in range(10):
...: print(gen_num_1.random())
...: #volvemos a inicializar el generador con la misma semilla
...: gen_num_1 = np.random.default_rng(100)
...: #y nos reproduce exactamente la misma secuencia
...: print('\n Secuencia repetida. Repited Sequence \n')
...: for i in range(10):
...: print(gen_num_1.random())
0.8349816305020089
0.5965540269678873
0.2888632416912036
0.042951570694211405
0.9736543951062142
0.5964717040646884
0.7902631644187212
0.9103393812954528
0.6881544475917452
0.18999147338772315
Secuencia repetida. Repited sequence
0.8349816305020089
0.5965540269678873
0.2888632416912036
0.042951570694211405
0.9736543951062142
0.5964717040646884
0.7902631644187212
0.9103393812954528
0.6881544475917452
0.18999147338772315
\end{minted}
\begin{paracol}{2}
Las posibilidades del generador de números aleatorios de numpy son muy amplias. Puede generar números aleatorios empleando distintos tipos de distribuciones de probabilidad (ver sección siguiente), permutar aleatoriamente arrays de números, seleccionar números de modo aleatorio de entre una secuencia de enteros, etc. Para conocer a fondo el proceso, se aconseja consultar la ayuda de numpy.
\section{Probabilidad y distribuciones de probabilidad}
\sectionmark{Probabilidad y distribuciones. \textreferencemark\ Probability and distributions.}
La probabilidad es una propiedad asociada a los sucesos aleatorios. Decimos que un suceso es aleatorio cuando no depende de una causa determinista que nos permite predecir cuándo va a suceder. \index{Suceso Aleatorio}
\subsection{Sucesos aleatorios discretos.}
Obtener \emph{cara} o \emph{cruz} al lanzar una moneda al aire es un ejemplo sencillo de proceso aleatorio, ya que no es posible conocer de antemano cuál será el resultado del lanzamiento. La probabilidad da una medida de las posibilidades de que un determinado suceso aleatorio tenga lugar.
Volviendo al ejemplo de la moneda lanzada al aire, las posibilidades son dos:
\begin{enumerate}
\item Que salga cara.
\item Que salga cruz.
\end{enumerate}
Ambos sucesos son igualmente probables tras un lanzamiento, uno de los dos debe darse necesariamente y no hay, en principio, ningún otro suceso posible. Podemos entonces asociar valores numéricos a la probabilidad con la que se dan las distintas posibilidades:
\begin{itemize}
\item La probabilidad de que salga cara o cruz ---cualquiera de las dos--- debe tener asociado el valor máximo ya que \emph{necesariamente} ha de salir cara o cruz. Habitualmente se toma como valor máximo de un suceso aleatorio el valor $1$, que estaría asociado con el suceso necesario o cierto,
\begin{equation*}
P(\text{cara o cruz}) = 1
\end{equation*}
\item La probabilidad de que no salga ni cara ni cruz. Puesto que hemos establecido que ha de salir necesariamente cara o cruz, estamos ante un suceso imposible. A los sucesos imposibles se les asigna probabilidad $0$,
\begin{equation*}
P(\emptyset) = 0
\end{equation*}
\item La probabilidad de que salga cara. Como en una moneda cara y cruz son igualmente probables, la probabilidad de sacar cara, sera la \emph{mitad} de la probabilidad de sacar cara o cruz,
\begin{equation*}
P(\text{cara})=\frac{1}{2}=0.5
\end{equation*}
\item La probabilidad de sacar cruz. De modo análogo al caso anterior será la mitad de la probabilidad de sacar cara o cruz,
\begin{equation*}
P(\text{cruz})=\frac{1}{2}=0.5
\end{equation*}
\end{itemize}
\switchcolumn
Numpy's random number generator has an extensive range of possibilities. It can generate random numbers using different kinds of probability distributions (see the next section), permuting random number arrays, randomly choosing a number from a sequence of integers, etc. For a complete description of Numpy's random number generator, it is advisable to check Numpy's help.
\section{Probability and probability distributions.}\index[eng]{Random event}
\sectionmark{Probabilidad y distribuciones. \textreferencemark\ Probability and distributions.}
The probability is a property associated with random events. We say that an event is random whenever it does not depend on a deterministic cause, allowing us to forecast when the event will occur.
\subsection{Discrete random events.}
Tossing a coin to obtain either a \emph{head} or a \emph{tail} is a simple example of a random process, as it is impossible to know the result beforehand. Probability measures the odds of a specific event occurring.
Comming back to the coin tossing, there are two possibilities:
\begin{enumerate}
\item that you get head
\item that you get tail
\end{enumerate}
Both events can occur with the same probability after the tossing, but, necessary, one of them has to occur, and there is not, in principle, any other possible event. We can then associate numerical values with the probability that any possible event will occur:
\begin{itemize}
\item The probability of getting head or tail --whatever of them-- should have associated the maximum value because \emph{necessarily} we have to get head or tail. Usually, we take as the maximum value for the probability of a random event the value 1, which would be associated with the necessary or \emph{true} event,
\begin{equation*}
P(\text{head or tail}) = 1
\end{equation*}
\item The probability of not getting either heads or tails in a coin flip represents an impossible event, as we know that we must obtain either heads or tails. Since this event cannot occur, we assign a probability value of 0 to it.
\begin{equation*}
P(\emptyset) = 0
\end{equation*}
\item The probability of getting head. In a \emph{fair} coin, the probability of getting a head or tail is the same, so the probability of getting a head would be half the probability of getting a head or tail,
\begin{equation*}
P(\text{head})=\frac{1}{2}=0.5
\end{equation*}
\item The probability of getting a tail, following the same argument as in the previous case, should also be half the probability of getting a head or tail
\begin{equation*}
P(\text{tail})=\frac{1}{2}=0.5
\end{equation*}
\end{itemize}
\end{paracol}
\begin{figure}
\centering
\includegraphics[width=10cm]{probcaracruz.eps}
\bicaption{Distribución de probabilidad y probabilidad acumulada de los resultados de lanzar una moneda al aire.}{Probability distribution and cumulated probability of the results of tossing a coin.}
\label{fig:moneda}
\end{figure}
\begin{paracol}{2}
Lanzar una moneda o un dado, elegir una carta al azar, rellenar una quiniela, son ejemplos de fenómenos aleatorios discretos. Reciben este nombre, porque los sucesos posibles asociados al fenómeno son numerables, es decir, se pueden contar. Así por ejemplo en el lanzamiento de una moneda solo hay dos sucesos posibles (cara o cruz), en el de un dado hay seis (cada una de sus caras), en el de la elección de una carta hay cuarenta (si se trata de una baraja española) etc. El conjunto de sucesos posibles recibe el nombre de variable aleatoria. \index{Variable! aleatoria}
Una distribución de probabilidad discreta, es una función que asocia cada caso posible de un fenómeno aleatorio con su probabilidad. Si vamos sumando sucesivamente las probabilidades de todos los fenómenos posibles, lo que obtenemos es una nueva función que recibe el nombre de probabilidad acumulada (Más adelante volveremos sobre esta idea). La figura \ref{fig:moneda} muestra la distribución de probabilidad y la probabilidad acumulada asociadas al lanzamiento de una moneda al aire.
Si todos los sucesos posibles dentro de un fenómeno aleatorio discreto tienen la misma probabilidad de suceder, es posible determinar directamente la probabilidad de cada uno de ellos sin más que dividir los casos favorables entre los casos posibles,
\begin{equation*}
P(a)=\frac{\text{casos en que se da} \ a}{\text{numero total de casos posibles}}
\end{equation*}
Así por ejemplo la probabilidad de sacar un $6$ cuando se laza un dado será,
\begin{equation*}
P(a)=\frac{\text{casos en que se da}\ a=1}{\text{numero total de casos posibles}=6}=\frac{1}{6}
\end{equation*}
\switchcolumn
Tossing a coin, rolling a dice, drawing a card at random, or filling in a pool coupon are examples of random discrete phenomena. They are called \emph{discrete} because the outcomes of these activities are numerable, i.e., they can be counted. For instance, there are only to events associated to tossing a coin (head or tail), in the case of rolling a dice we have six events (any one of the dice faces), drawing a card has 40 events associated (well, it depends on the deck, 52 for a french playing card). The set of possible events of a random discrete phenomena are called random variable. \index[eng]{Variable! random}
A discrete probability distribution, us a functions which associates each possible event of a random phenomena with its probability. If we add on the probabilities of every possible event, we obtain a new function called the cumulated probability (More on this later). Figure \ref{fig:moneda} shows the probability distribution and the cumulated probability distribution associated with tossing a coin.
If all possible events belonging to a specific random discrete phenomenon have the same probability of occurring, the probability of any of them can be determined by dividing the number of favorable cases by the number of possible cases,
\begin{equation*}
P(a)=\frac{\text{cases where} \ a \ \text{occurs}}{\text{total number of possible cases}}
\end{equation*}
So, for instance, the probability of getting a $6$ when rolling a dice would be,
\begin{equation*}
P(a)=\frac{\text{cases where} \ a \ \text{occurs} = 1}{\text{total number of possible cases}=6}=\frac{1}{6}
\end{equation*}
\end{paracol}
\begin{figure}
\centering
\includegraphics[width=10cm]{dado.eps}
\bicaption{Distribución de probabilidad y probabilidad acumulada de los resultados de lanzar un dado al aire.}{Probability distribution and cumulated probability of the resutls of rolling a dice.}
\label{fig:dado}
\end{figure}
\begin{paracol}{2}
Una propiedad que deben cumplir los sucesos aleatorios es que la suma de las probabilidades de todos los sucesos posibles debe ser igual a la unidad,
\switchcolumn
A property that all aleatory events should fulfill is that the sum of the probability of all possible events must equal one.
\end{paracol}
\begin{equation*}
\sum_{i=1}^n P(i) = 1
\end{equation*}
\begin{paracol}{2}
Es decir, \emph{necesariamente} debe darse alguno de los casos posibles.
Esto nos lleva al carácter aditivo o acumulativo de la probabilidad. Si elegimos un conjunto de casos posibles de un fenómeno aleatorio cualquiera, la probabilidad de que se de alguno de ellos será la suma de las probabilidades de los casos individuales. Por ejemplo la probabilidad al lanzar un dado de obtener un $2$, un $3$ ó un $6$ será,
\switchcolumn
That is, we have to get \emph{necessarily} one of the possible cases.
This brings us to an important aspect of probability: its additive or accumulative nature. If we select a set of potential events from any random phenomenon, the probability of any one of those events occurring is equal to the sum of the probabilities of each individual case. For example, the probability of rolling a $2$, a $3$, or a $6$ on a dice is,
\end{paracol}
\begin{equation*}
P(2,3,6) = P(2) + P(3) + P(6) = \frac{1}{6}+\frac{1}{6}+\frac{1}{6}=\frac{3}{6} = \frac{1}{2}
\end{equation*}
\begin{paracol}{2}
lo que coincide con lo que intuitivamente cabía esperar, que la probabilidad de sacar uno cualquiera de los tres números fuera $0.5$.
La figura \ref{fig:dado} muestra la distribución de probabilidad y la probabilidad acumulada para el lanzamiento de un dado. La probabilidad acumulada guarda relación con el carácter aditivo de la probabilidad que acabamos de describir. Si consideramos el conjunto de valores posibles en orden: $1 < 2 < 3 < 4 < 5 < 6$, la probabilidad acumulada para cada valor es la probabilidad de sacar un número al lanzar el dado menor o igual que dicho valor. Así por ejemplo la probabilidad de sacar un número menor o igual que tres sería,
\switchcolumn
This result coincides with what we may intuitively expect; the probability of getting any one of the three numbers would be $0.5$.
Figure \ref{fig:dado} shows the probability distribution and the cumulate probability for the dice-rolling example. The cumulated probability is related to the additive nature of probability we have already discussed. If we consider the set of possible outcomes in order: $1 < 2 < 3 < 4 < 5 < 6$, the cumulated probability for each value represents the likelihood of rolling a number less than or equal to this value. For instance, the probability of getting a number less than or equal to three will be,
\end{paracol}
\begin{equation*}
P(n\leqslant 3) = P(1) + P(2) + P(3) = \frac{1}{6}+\frac{1}{6}+\frac{1}{6}=\frac{3}{6} = \frac{1}{2}
\end{equation*}
\begin{paracol}{2}
Hasta ahora, todas las distribuciones discretas de probabilidad que hemos visto ---moneda, dado--- asignan la misma probabilidad a todos los sucesos posibles. En general esto no tiene porqué ser así. Supongamos un dado trucado en el que la probabilidad de sacar un $6$ valga $0.5$, es decir en promedio tiende a salir un $6$ la mitad de las veces que se lanza el dado. Supongamos además que todos los demás números salen con la misma probabilidad,
\switchcolumn
All discrete probability distributions we have encountered so far, ---such as coins and dice--, assign equal probability to their possible outcomes. In general, a probability distribution do not have to do so. Consider a biased die where the probability of rolling a 6 is 0.5. This means we will likely obtain a 6 half the times we roll the die. Moreover, suppose that the remaining numbers roll with equal probability.
\end{paracol}
\begin{figure}
\centering
\includegraphics[width=10cm]{dadot.eps}
\bicaption{Distribución de probabilidad y probabilidad acumulada de los resultados de lanzar un dado trucado al aire.}{Probability distribution and cumulated probability of the results of rolling a biased dice.}
\label{fig:dadot}
\end{figure}
\begin{align*}
P(1)& = P(2) = P(3) = P(4) = P(5) = \frac{1}{10}\\
P(6)& = \frac{1}{2}\\
\sum_i P(i)& = 1
\end{align*}
\begin{paracol}{2}
La figura \ref{fig:dadot} muestra la distribución de probabilidad y la probabilidad asociada al lanzamiento del dato trucado del ejemplo.
\switchcolumn
Figure \ref{fig:dadot} shows the probability distribution associated the example of the biased dice rolling.
\end{paracol}
\begin{paracol}{2}
\subsection{Distribuciones de probabilidad continuas}\label{pdfc}
Hasta ahora, hemos considerado fenómenos en los que los casos posibles ---la variable aleatoria--- son finitos y numerables. Supongamos ahora un fenómeno aleatorio en que su variable aleatoria, $x$, es continua. Esto es: puede tomar todos los valores reales comprendidos en cierto intervalo, $x \in [a,b]$. Supongamos que todos los números contenidos en dicho intervalo pueden ser elegidos con igual probabilidad. Podríamos representar la distribución de probabilidad como una línea horizontal continua $f(x) = c$ que cubriera todo el intervalo $[a,b]$. Para obtener el valor constante $c$ de dicha distribución de probabilidad bastaría \emph{sumar} la probabilidad asociada por la distribución a cada valor del intervalo, igualar el resultado a uno y despejar $c$.
En realidad, no podemos sumar los infinitos valores que contiene el intervalo. De hecho lo que hacemos es sustituir la suma por una integral,
\switchcolumn
\subsection{Continuous probability distributions}
So far, we have considered phenomena in which the possible cases-- the random variable--- are finite and enumerable. Let us suppose now a random phenomenon for which the random variable, $x$, is continuous. It can take every real value in an interval $[a,b]$. Suppose every number in this interval can be chosen with equal probability. We could represent the probability as a continuous horizontal line $f(x) =c$ which would cover the whole interval $[a,b]$. To obtain the constant value $c$ for such probability distribution would be enough to sum up the probability assigned by the distribution to each value in the interval, equal the result to one, and find $c$.
We can not sum up the infinite values contained in the interval. In fact, we have to replace the sum with an integral.
Actually we can not sum up the infinite values contained in the interval. In fact, what we have to do is replace the sum for a in integral.
\end{paracol}
\begin{equation*}
\int_a^b c\cdot dx = 1 \Rightarrow c = \frac{1}{b-a}
\end{equation*}
\begin{paracol}{2}
La figura \ref{fig:puniform} muestra la distribución de probabilidad resultante
Es importante darse cuenta de la diferencia con el caso discreto. El intervalo $[a,b]$ contiene infinitos números. Si tratáramos de asignar un valor a la probabilidad de obtener un número dividiendo casos favorables entre casos posibles, como hacíamos en el caso discreto, obtendríamos un valor $0$ para todos los números.
La distribución de probabilidad $f(x)\geqslant 0$ representa en el caso continuo una \emph{densidad} de probabilidad. Así, dado un número cualquiera $x_0 \in [a,b]$, el valor que toma la distribución de probabilidad $f(x_0)$ representa la probabilidad de obtener un número al azar en un intervalo $dx$ en torno a $x_0$,
\switchcolumn
Figure \ref{fig:puniform} shows the resulting probability distribution.
It is impointronumpy.texrtant to realize that the difference when we compare with the discrete case. The interval $[a,b]$ contains infinite numbers. If we try to assign a value to the probability dividing the favorable cases by the possible cases, as we did in the discrete case, we would obtain a value $0$ for every number.
The probability distribution $f(x)\geqslant 0$ represents in the continuous case the a \emph{density} of probability. $f(x_0)$ represents the probability of obtaining a random number in an interval $dx$ around $x_0$,
\end{paracol}
\begin{equation*}
P\left(x_0-\frac{dx}{2}\leqslant x_0 \leqslant x_0+\frac{dx}{2}\right) = f(x_0)
\end{equation*}
\begin{figure}
\centering
\includegraphics[width=10cm]{puniform.eps}
\bicaption{Distribución de probabilidad uniforme para un intervalo $[a,b]$ y probabilidad acumulada correspondiente.}{Uniform probability distribution and cumulated probability.}
\label{fig:puniform}
\end{figure}
\begin{paracol}{2}
La probabilidad, vendrá siempre asociada a un intervalo, y se obtendrá integrando la distribución de probabilidad en dicho intervalo. Así por ejemplo,
\switchcolumn
The probability will be always associated to an interval, and it will be obtained integrating the probability distribution on this interval. So, for instance,
\end{paracol}
\begin{equation*}
P(x_1 \leqslant x \leqslant x_2) = \int_{x_1}^{x_2}f(x)dx
\end{equation*}
\begin{paracol}{2}
\noindent representa la probabilidad de obtener un número al azar en intervalo $[x_1,x_2]$.
La probabilidad acumulada, se obtiene también mediante integración,
\switchcolumn
\noindent represents the probability of get a random number in the interval $[x_1,x_2]$. The cumulated probability can also be obtained by integration,
\end{paracol}
\begin{equation*}
P(x) = \int_a^xf(x)dx
\end{equation*}
\begin{paracol}{2}
Para el caso de la distribución de probabilidad constante descrita más arriba obtendríamos una línea recta que corta el eje de abcisas en $a$ y el valor de ordenada $1$ en b,
\switchcolumn
For the constant probability distribution described above, we will obtain a straight line that cuts the abscissa axis on $a$ and takes the ordinate value $1$ on b.
\end{paracol}
\begin{equation*}
P(x) = \int_a^xf(x)dx = \int_a^x \frac{1}{b-a}dx = \frac{x-a}{b-a}, \ x \in[a,b]
\end{equation*}
\begin{paracol}{2}
La figura \ref{fig:puniform} muestra la probabilidad acumulada para el ejemplo de la distribución uniforme.
Si integramos la densidad de probabilidad sobre todo el intervalo de definición de la variable aleatoria, el resultado debe ser la unidad, puesto que dicha integral representa la probabilidad de obtener un número cualquiera entre todos los disponibles,
\switchcolumn
Figure \ref{fig:puniform} shows the cumulated probability for the case of an uniform distribution.
If we integrate the probability density over the whole definition interval of the random variable, the result should be one due to such integral represents the probability of getting whatever value among the available numbers,
\end{paracol}
\begin{equation*}
\int_a^b f(x)dx =1, \ a\leqslant x \leqslant b
\end{equation*}
\begin{paracol}{2}
En probabilidad se emplean muchos tipos de distribuciones para representar el modo en que se dan los suceso aleatorios en la naturaleza. A continuación presentamos dos ejemplos muy conocidos:
\paragraph{Distribución exponencial} \index{Distribución exponencial}
La distribución exponencial está definida para sucesos que pueden tomar cualquier valor real positivo ó 0, $x \in [0,\infty)$. se representa mediante una función exponencial decreciente,
\switchcolumn
We use many different kind of distributions to represent how random events take place in nature. Let see now two very well known examples:
\paragraph{Exponential distribution} \index[eng]{Exponential distribution}
Exponetial distribution are define for events that can take a real positive value or $0$, $x\in [0,\infty)$. It is represent by a decreasing exponetial function,
\end{paracol}
\begin{equation*}
f(x)=\frac{1}{\mu}e^{-\frac{x}{\mu}}
\end{equation*}
\begin{figure}
\centering
\includegraphics[width=10cm]{distexp.eps}
\bicaption{Distribución de probabilidad exponencial}{Exponential probability distribution}
\label{fig:distexp}
\end{figure}
\begin{paracol}{2}
La figura \ref{fig:distexp} muestra un ejemplo de distribución exponencial. Es interesante observar como su probabilidad acumulada tiende a $1$ a medida que $x$ tiende a $\infty$.
\switchcolumn
Figure \ref{fig:distexp} shows an example of an exponential distribution. It is interesting to notice how its cumulative probability tends to $1$ as $x$ tends to $\infty$.
\end{paracol}
\begin{equation*}
P(0\leqslant x < \infty) = \int_0^{\infty}\frac{1}{\mu}e^{-\frac{x}{\mu}}dx = 1
\end{equation*}
\begin{paracol}{2}
\paragraph{Distribución Normal}\index{Distribución normal} Aparece con gran frecuencia en la naturaleza. En particular, como veremos más adelante, está relacionada con la incertidumbre inevitable en la medidas experimentales. Esta definida para sucesos aleatorios que pueden tomar cualquier valor real, $-\infty < x < \infty$. Se representa mediante la función de Gauss,
\switchcolumn
\paragraph{Normal distribution}\index[eng]{Normal distribution} It is very frequent to find this distribution in nature. It is related, as we shall see later, with the uncertainty always present in experimental measurements. It is defined for random events that can take any real value, $-\infty < x < \infty$. It is represented by Gauss' function.
\end{paracol}
\begin{equation*}
f(x)=\frac{1}{\sqrt{2\pi\sigma^2}}e^{-\frac{(x-\mu)^2}{2\sigma^2}}
\end{equation*}
\begin{paracol}{2}
\noindent La figura \ref{fig:normal} muestra un ejemplo de distribución normal.
\switchcolumn
\noindent Figure \ref{fig:normal} shows an example of normal distribution.
\end{paracol}
\begin{figure}
\centering
\includegraphics[width=10cm]{normal.eps}
\bicaption{Distribución de probabilidad normal}{Normal probability distribution}
\label{fig:normal}
\end{figure}
\begin{paracol}{2}
\paragraph{Parámetros característicos de una distribución}
Los parámetros característicos permiten definir propiedades importantes de las distribuciones de probabilidad. Nos limitaremos a definir los dos más importantes:
\begin{itemize}\index{Media de una distribución}
\item \textbf{Media ó valor esperado.} El valor esperado de una distribución se obtiene integrando el producto de cada uno de los valores aleatorios sobre los que está definida la distribución, por su densidad de probabilidad,
\end{itemize}
\switchcolumn
\paragraph{Characteristic parameters of Distributions}
The characteristic parameter allow us to define important properties of probability distributions. We will define only the two main distributions parameters:
\begin{itemize}\index[eng]{Mean (Distributions)}
\item \textbf{Distribution mean or expected value} We get the expected value of a distribution integrating the product of the ramdom variable the distribution is defined on, by its probability density,
\end{itemize}
\end{paracol}
\begin{equation*}
\mu = \int_a^b xf(x)dx, \ a\leqslant x \leqslant b
\end{equation*}
\begin{paracol}{2}
Así, para una distribución uniforme definida en un intervalo $[a,b]$,
\switchcolumn
So, for an uniform distribution defined in the interval $[a,b]$
\end{paracol}
\begin{equation*}
\mu = \int_a^b x\frac{1}{b-a}dx = \frac{a+b}{2}
\end{equation*}
\begin{paracol}{2}
para una distribución exponencial,
\switchcolumn
for an exponential distribution
\end{paracol}
\begin{equation*}
\mu = \int_0^{\infty}x\frac{1}{\mu}e^{-\frac{x}{\mu}}dx = \mu
\end{equation*}
\begin{paracol}{2}
y para la distribución normal,
\switchcolumn
and for a normal distribution,
\end{paracol}
\begin{equation*}
\mu = \int_{-\infty}^{\infty}x \frac{1}{\sqrt{2\pi\sigma^2}}e^{-\frac{(x-\mu)^2}{2\sigma^2}}= \mu
\end{equation*}
\begin{paracol}{2}
Es interesante observar como en el caso de las distribuciones exponencial y normal la media es un parámetro que forma parte de la definición de la función de distribución.
\begin{itemize}
\item \textbf{Varianza}\index{Varianza de una distribución} La varianza da una medida de la dispersión de los valores de
la distribución en torno a la media. Se define como,
\end{itemize}
\switchcolumn
It is interesting to notice how for the cases of the exponential and normal distribution the mean is a parameter that is included in the definitions of the distribution function,
\begin{itemize}
\item \textbf{Variance} \index[eng]{Variance (Distribution)} The variance supplies an measurement of the distribution values dispersion around the mean. It is defined as,
\end{itemize}
\end{paracol}
\begin{equation*}
\sigma^2 = \int_a^b (x-\mu)^2f(x)dx, \ a\leqslant x \leqslant b
\end{equation*}
\begin{paracol}{2}
Para una distribución uniforme definida en el intervalo $[a,b]$, tomará el valor,
\switchcolumn
For an uniforme distribution defined in the interval $[a,b]$ it will take the value,
\end{paracol}
\begin{equation*}
\sigma^2 = \int_a^b (x-\mu)^2\frac{1}{b-a}dx = \frac{(b-a)^2}{12}
\end{equation*}
\begin{paracol}{2}
para una distribución exponencial,
\switchcolumn
for an exponential distribution,
\end{paracol}
\begin{equation*}
\sigma^2 = \int_0^{\infty}(x-\mu)^2\frac{1}{\mu}e^{-\frac{x}{\mu}}dx = \mu^2
\end{equation*}
\begin{paracol}{2}
y para la distribución normal,
\switchcolumn
and for a normal distribution,
\end{paracol}
\begin{equation*}
\sigma^2 = \int_{-\infty}^{\infty}x \frac{1}{\sqrt{2\pi\sigma^2}}e^{-\frac{(x-\mu)^2}{2\sigma^2}}= \sigma^2
\end{equation*}
\begin{paracol}{2}
\begin{itemize}
\item \textbf{Desviación estándar.} \index{Desviación estándar de una distribución} Es la raíz cuadrada de la varianza: $\sigma =\sqrt{\sigma^2}$
\end{itemize}
\switchcolumn
\begin{itemize}
\item \textbf{Standard deviation} \index[eng]{Standard deviation (distribution)} It is the variance square root : $\sigma =\sqrt{\sigma^2}$
\end{itemize}
\end{paracol}
\begin{paracol}{2}
\section{El teorema del límite central}\label{tlc}\index{Teorema del ĺimite central}
\sectionmark{El teorema del límite central. \textreferencemark\ Central Limit Theorem.}
El teorema del límite central juega un papel muy importante a la hora de analizar y evaluar resultados experimentales. Nos limitaremos a enunciarlo, pero no daremos una demostración.
Supongamos que tenemos un fenómeno aleatorio continuo que viene caracterizado por una variable aleatoria $x$. Además el fenómeno aleatorio viene descrito por una distribución de probabilidad arbitraria $f(x)$ de media $\mu$ y varianza $\sigma^2$.
Supongamos que realizamos un experimento consistente en obtener $n$ valores de x al azar. Los valores así obtenidos, deberán seguir la distribución de probabilidad $f(x)$. Para caracterizar nuestro experimento, calcularemos la media de los $n$ valores obtenidos,
\switchcolumn
\section{The Central Limit Theorem} \index[eng]{Central Limit Theorem}
\sectionmark{El teorema del límite central. \textreferencemark\ Central Limit Theorem.}
The central limit theorem plays a very important role in experimental data analysis. We are going to present it without demonstration.
Suppose we have a continuous random phenomenon with is defined by a random variable $x$. Besides, the phenomenon is described by an arbitrary probability distribution $f(x)$, with mean $\mu$ and variance $\sigma^2$.
Suppose we carry out an experiment and obtain $n$ random samples of $x$. The values so obtained should follow the probability distribution $f(x)$. Tu characterize our experiment we will calculate the mean of the $n$ samples.
\end{paracol}
\begin{equation*}
\bar{x}_n=\frac{1}{n}\sum_{i=1}^n x_i
\end{equation*}
\begin{paracol}{2}
Si repetimos el experimento muchas veces, obtendremos al final una colección de medias; tantas, como veces hemos repetido el experimento,
$\left\{\bar{x}_{n1}, \bar{x}_{n2} \cdots \bar{x}_{nj} \cdots \right\}$. El teorema del límite central, establece que las medias así obtenidas constituyen a su vez valores de una variable aleatoria que sigue una distribución normal, cuya media coincide con la media $\mu$ de la distribución original $f(x)$ y cuya varianza coincide con el valor de la varianza de la distribución original $\sigma^2$ dividida por el número $n$ de valores de $x$ obtenidos al azar en cada uno de los experimentos. (En todos los experimentos se obtiene siempre el mismo número de valores de la variable aleatoria $x$).
\switchcolumn
If we repeat the experiment many times, we eventually, will obtain a collection of mean values; so many as times we have repeated the experiment, $\left\{\bar{x}_{n1}, \bar{x}_{n2} \cdots \bar{x}_{nj} \cdots \right\}$. The central limit theorem establishes that the set of means so obtained are in turn values of a random variable that follows a normal distribution. The mean of such normal distributions meets the mean $\mu$ of the original distribution $f(x)$ and the its variance is equal to the variance $\sigma^2$ of the original distribution divided by the number $n$ of values of $x$ randomly generated in each one of the experiments. (You should obtain the same number of samples of the random variable $x$ in every experiment).
\end{paracol}
\begin{equation*}
x, \ f(x), \mu, \sigma^2 \Rightarrow \bar{x}_n,\ f(\bar{x}_n)=\frac{1}{\sqrt{2\pi\sigma^2/n}}e^{-\frac{(x-\mu)^2}{2\sigma^2/n}}
\end{equation*}
\begin{paracol}{2}
Veamos un ejemplo para ilustrar el teorema. Hemos visto en la sección \ref{rand} que podemos construir un generador que genera números aleatorio uniformemente distribuidos en el intervalo $[0,1)$. Podemos considerar que el ordenador genera número aleatorios que siguen aproximadamente una distribución continua en dicho intervalo. Podemos alterar dicho intervalo multiplicando por un número cualquiera el resultado de \texttt{gen\_num.random}. Así, por ejemplo,
\switchcolumn
Let see an example to show the theorem's result. We have seen in section \ref{rand} that we can build generator to obtain random numbers uniformly distributed in the interval $[0,1)$. We can consider the computer to generate number with approximately follows a continuous distribution in this interval. We can expand the interval multiplying by any number the result of \texttt{gen\_num.random} . So, for instance,
\end{paracol}
\begin{center}
\begin{minipage}{\textwidth}
\begin{minted}{python}
In [53]: l = 5*gen_num.random(100)
\end{minted}
\end{minipage}
\end{center}
\begin{paracol}{2}
genera un array de 100 números aleatorios en el intervalo $(0,5)$.
Además podemos desplazar el centro del intervalo sumando o restando al resultado anterior un segundo número. Así,
\switchcolumn
generates a 100 random number array in the interval $(0,5)$.
Moreover, we can shift the center of the interval adding or subtracting a secon number. So,
\end{paracol}
\begin{center}
\begin{minipage}{\textwidth}
\begin{minted}{python}
In [53]: l = 5*gen_num.random(100)-2
\end{minted}
\end{minipage}
\end{center}
\begin{paracol}{2}
genera un vector de 100 números aleatorios en el intervalo $(-2,3)$.
Para comprobar el teorema del límite central, podemos construir un bucle que genere un vector de $n$ números aleatorios en un determinado intervalo, calcule su media y guarde el resultado en un vector de medias. El siguiente código de Python, realiza dicho cálculo primero generando un vector de $10$ números aleatorios ($n = 10$) y después generando un vector de $100$, ($n=100$). El programa genera en cada caso un millón de vectores distintos.
\switchcolumn
generates a 100 random numbers array in the interval $(-2,3)$.
We can now check the central limit theorem, building a loop that generates a vector of $n$ random number in an specific interval, calculates their mean value and saves the result in a vector of 'means'. The following python code, makes this calculation generating firs a vector of $10$ random numbers ($n=10$) and then generating a vector of $100$ random numbers, ($n=100$). IN both cases, the program generates one million of different vectors.
\end{paracol}
\inputminted[
frame=lines,
framesep=2mm,
baselinestretch=1.2,
%bgcolor=LightGray,
label=central\_limit,
fontsize=\footnotesize,
linenos
]{python}{./codigos/tratamiento_datos/central_limit.py}
\begin{paracol}{2}
De acuerdo con el teorema del límite central, los dos vectores de medias obtenidos,\\ \texttt{media10} y \texttt{media100}, deben seguir distribuciones normales tales que,
\begin{enumerate}
\item La media de la distribución normal debe coincidir con la media de la distribución a la que pertenecían los datos originales. Como hemos tomado datos distribuidos uniformemente en el intervalo $[-1,1]$, la media de dicha distribución es,
\end{enumerate}
\switchcolumn
According with the central limit theorem, both means vectors obtained by this procedure, \texttt{media10} y \texttt{media100}, should follow normal distributions such that,
\begin{enumerate}
\item The mean of the normal distribution sho\-uld meet the mean of the original distribution the data belong to. as we have taken data uniformly distributed in the interval $[-1,1]$, The mean value of this distribution is,
\end{enumerate}
\end{paracol}
\begin{equation*}
\mu = \frac{(b+a)}{2}=\frac{1+(-1)}{2}=0
\end{equation*}
\begin{paracol}{2}
\begin{enumerate}
\setcounter{enumi}{1}
\item La varianza de las distribuciones normales a las que pertenecen las medias obtenidas será en cada caso el resultado de dividir la varianza de la distribución uniforme original entre el número de datos empleados para calcular dichas medias,
\end{enumerate}
\switchcolumn
\begin{enumerate}
\setcounter{enumi}{1}
\item The variance of the normal distributions the means obtained belongs to, will be in each case the result of dividing the variance of the original uniform distribution by the number of data used to calculate the means,
\end{enumerate}
\end{paracol}
\begin{align*}
\sigma_{10}^2 = \frac{\sigma^2}{10}=\frac{\frac{(b-a)^2}{12}}{10}=\frac{\frac{(1-(-1))^2}{12}}{10}=\frac{1}{30}\\
\sigma_{100}^2 = \frac{\sigma^2}{100}=\frac{\frac{(b-a)^2}{12}}{100}\frac{\frac{(1-(-1))^2}{12}}{10}=\frac{1}{300}
\end{align*}
\begin{figure}
\centering
\includegraphics[width=\textwidth]{tlc.eps}
\bicaption{Teorema del límite central: Comparación entre histogramas normalizados para un millón de medias y la distribución normal a que pertenecen. Izquierda medias de 10 muestras. Derecha medias de 100 muestras}{Central Limit Theorem: Comparison between the normalised histograms for a million of means and the normal distribution they belong to. Left, mean of 10 random samples. Right, means of 100 random samples }\label{fig:ctrli}
\end{figure}
\begin{paracol}{2}
Por tanto, las distribuciones normales a las que pertenecen las medias calculadas son,
\switchcolumn
Thus, the normal distributions the means calculated belong to are,
\end{paracol}
\begin{equation*}
f(\bar{x}_{10})=\frac{1}{\sqrt{2\pi\frac{1}{30}}}e^{-\frac{(x-\mu)^2}{2\frac{1}{30}}},\ f(\bar{x}_{100})=\frac{1}{\sqrt{2\pi\frac{1}{300}}}e^{-\frac{(x-\mu)^2}{2\frac{1}{300}}}
\end{equation*}
\begin{paracol}{2}
La figura \ref{fig:ctrli}, muestra un histograma normalizado de las medias obtenidas con el código que acabamos de describir \footnote{El histograma normalizado se obtiene dividiendo el número de puntos que pertenecen a cada barra del histograma entre el número total de puntos y el ancho de la barra. De esta manera, si sumamos los valores representados en cada barra multiplicados por su ancho, el resultado es la unidad. Solo normalizando el histograma es posible compararlo con la distribución a que pertenecen los datos, que cumple por definición tener área unidad.}. Sobre dicho histograma se ha trazado mediante una línea roja, la función de Gauss que representa en cada caso la distribución normal a que pertenecen las medias. Como puede observarse en ambos casos el histograma normalizado y la distribución coinciden bastante bien.
De las figuras, es fácil deducir que cuantos más datos empleemos en el cálculo de las medias, más centrados son los resultados obtenidos en torno a la media de la distribución original. Por otro lado, esto es una consecuencia lógica del hecho de que la varianza de la distribución normal que siguen las medias, se obtenga dividiendo la varianza de la distribución original, por el número de datos $n$; cuantos mayor es $n$ más pequeña resulta la varianza de la distribución normal resultante.
\switchcolumn
Figure \ref{fig:ctrli} shows a normalized histogram of the means obtained with the code described above\footnote{We can obtain a normalized histogram dividing the number of values belonging to a bar by the width of the bar and the total number of value that compose the histogram. IN this way if we sum up the values represented in each bar multiplied by the bar width the result is one. Only if we normalize the histogram can we compare it with the distribution the data belong two which, by definition should have unit area.}. On top of the histogram we have represented, using a orange line, The Gauss function which represent in each case the normal distribution the means belong to. As can be seen, the normalize histograms and the normal distributions feet well.
From the figures it is easy to observe that as many data are uses to calculate the means, more center are the results around the mean of the original distribution. On the other hand, this is a logical consequence of the fact that the variance of the normal distribution the means follow, are obtained dividing the variance of the original distribution by the number of data $n$; as larger is $n$ smaller is the variance of the resulting normal distribution.
\end{paracol}
\begin{paracol}{2}
\section{Incertidumbre en las medidas experimentales}\index{Incertidumbre}
\sectionmark{Incertidumbre. \textreferencemark\ Uncertainty.}
Siempre que realizamos una medida de una cantidad física dicha medida estará afectada por un cierto error. Dado que no conocemos cual es el valor verdadero que toma la magnitud que estamos midiendo, no podemos tampoco conocer el error que cometemos al medirla. Decimos entonces que toda medida está afectada por un cierto grado de incertidumbre.
Lo que sí es posible hacer, es acotar el grado de incertidumbre de una medida, es decir estimar unos límites con respecto a la medida realizada dentro de los cuales debe estar contenido el verdadero valor que toma la magnitud medida.
Cualquier medida experimental debe incluir junto al resultado de la medida el valor de su incertidumbre y, por supuesto, las unidades empleadas en la medida. La figura \ref{fig:incertidumbre} muestra un modo correcto de expresar una medida experimental, aunque no es el único. Como puede observarse, la medida y su incertidumbre se separan en la representación mediante el símbolo $\pm$. La incertidumbre se representa como un entorno alrededor del valor medido, dentro del cual estaría incluido el valor real de la medida\footnote{Como se verá más adelante se debe indicar también el \emph{grado de confianza} con el que esperamos que la medida caiga dentro del intervalo indicado por la incertidumbre}.
\switchcolumn
\section{Experimental measurement uncertainty}\index[eng]{Uncertainty}
\sectionmark{Incertidumbre. \textreferencemark\ Uncertainty.}
Whenever we measure a physical magnitude, such measurement is affected by some error. As we do not know the actual value taken by the magnitude we are measuring, we cannot know the error we make when we take the measure. We say then, that every measurement is affected by some degree of uncertainty.
But we can, a least, calculate bounds on the degree of measurement uncertainty. This means we can estimate limits around the measurement that contain the actual value of the measured quantity.
Any experimental measurement should include, besides the measure's result, the value of its uncertainty and, of course, the units used in the measurement. Figure \ref{fig:incertidumbre} shows the right way of expressing an experimental measurement, although it is not the only one. As seen, the measurement and its uncertainty are separated in the representation by the symbol $\pm$. So, the uncertainty is represented as a contour around the value of the measurement; inside this contour, the actual value of the measured magnitude should be. \footnote{As we shall later, we should also indicate the \emph{degree of confidence} when we expect the actual value to fall inside the interval indicated by the uncertainty.}
\end{paracol}
\begin{figure}
\centering
\begin{tikzpicture}
%\usetikzlibrary{shapes.geometric}
\path (3,0) node(a) [rectangle,draw=blue, thick,rounded corners,align=center,font=\large]{Medida/Measurement: $125.2$}
(7,-3) node(b)[rectangle,draw=red, thick,rounded corners,align=center,font=\large]{Incertidumbre/Uncertainty: $0.5$}
(9,0) node(c)[rectangle,draw=green, thick,rounded corners,align=center, font=\large]{Unidades/Units: kg}
(6,-1.5) node(d)[font= \huge]{$125.2 \pm 0.5 \text{kg}$};
\draw[blue,-latex](a.south)--(4.5,-1.1);
\draw[red,-latex](b.north)--(7,-1.8);
\draw[green,-latex](c.south)--(8.0,-1.2);
\end{tikzpicture}
\bicaption{Modo correcto de expresar una medida experimental.}{A right way to display an experimental measurement.}
\label{fig:incertidumbre}
\end{figure}
\begin{paracol}{2}
\subsection{Fuentes de incertidumbre.}\label{fdi}
Antes de entrar en el estudio de las fuentes de incertidumbre, es importante destacar que el estudio de la medición constituye por sí mismo una ciencia, conocida con el nombre de metrología. Lo que vamos a describir a continuación tanto referido a las fuentes de incertidumbre como al modo de estimarla, es incompleto y representa tan solo una primera aproximación al problema.
La incertidumbre de una medida tiene fundamentalmente dos causas:
\paragraph{Incertidumbre sistemática de precisión.}\index{Medidas experimentales! precisión}
La primera es la precisión limitada de los aparatos de medida. Cualquier aparato de medida tiene una precisión que viene determinada por la unidad o fracción de unidad más pequeña que es capaz de resolver. Por ejemplo, una regla normal, es capaz de resolver (distinguir) dos longitudes que se diferencien en $0.5 mm$, un reloj digital sencillo es capaz de resolver tiempos con una diferencia de $1s.$ etc.
La incertidumbre debida a la precisión finita de los aparatos de medida recibe el nombre de \emph{Incertidumbre sistemática de precisión.}
Se llama así porque depende exclusivamente de las características del aparato de medida que, en principio, son siempre las mismas.
La incertidumbre sistemática de precisión suele expresarse añadiendo a la medida la mitad de la división mínima de la escala del aparato de medida empleado. Así, por ejemplo, si medimos $123 mm$ con una regla graduada en milímetros, podríamos expresar la medida como $123 \pm 0.5 mm$ o también como $12,3\pm0.05cm$.
La incertidumbre sistemática de precisión representa una distribución uniforme de media el valor de la medida realizada y anchura la división mínima de la escala. Es decir, se considera que el valor real de la medida está dentro de dicho intervalo intervalo con una probabilidad del $100\%$. Volviendo al ejemplo anterior de la regla, el valor real de nuestra medida estaría comprendido en el intervalo $[122.5,\ 123.5 ]$, y podría tomar cualquier valor de dicho intervalo con igual probabilidad.
\paragraph{Incertidumbre estadística.}\index{Medidas experimentales! Incertidumbre estadística} La segunda fuente de incertidumbre se debe a factores ambientales, que modifican de una vez para otra las condiciones en que se realiza una medida experimental. Estos factores hacen que la medidas realizadas sean sucesos aleatorios. En principio podría describirse mediante una distribución de probabilidad $f(x)$, pero en la práctica desconocemos de qué distribución se trata y ni siquiera sabemos cual es su valor esperado $\mu$ o su varianza $\sigma$. \index{Medidas experimentales! Incertidumbre estadística}
Afortunadamente, el teorema del límite central, que describimos en al sección \ref{tlc} proporciona un sistema de estimar la incertidumbre estadística.
Supongamos que repetimos $n$ veces la misma medida experimental. Por ejemplo: tenemos un recipiente con agua, lo calentamos y tomamos la temperatura del agua cuando ésta rompe a hervir. Obtenemos un conjunto\\ $\{x_1,x_2,\cdots x_n \}$ de medidas de la temperatura de ebullición del agua. Cabe esperar que, debido a la incertidumbre estadística, los valores obtenidos sean distintos y, a la vez próximos entre sí\footnote{De hecho, si aparecen algunos valores claramente alejados del resto, lo habitual es considerar que están afectados por algún error desconocido y desecharlos. Dichos valores suelen recibir el nombre de valores o datos aberrantes.}.
Sabemos que dichos valores deben seguir una cierta distribución de probabilidad. Podría\-mos estimar su valor esperado $\mu$ mediante el cálculo de la media aritmética de las medidas tomadas.
\switchcolumn
\subsection{Sources of Uncertainty}\label{fdii}
Before beginning to study the sources of uncertainty, it is important to notice that the study of the 'measure' is science, known as metrology. What we are to present in the following paragraphs, in reference to the sources of uncertainty and the means to estimate this last, is incomplete and only represents a first approach to the problem. The uncertainty of a measurement has mainly two causes:
\paragraph{Systematic accuracy uncertainty.}\index[eng]{Experimental measurements! accuracy}
The\\ first one is the limited accuracy of the sensors. Any sensor's accuracy is determined by the least unit or fraction of the units the device is able to resolve. For instance, a standard rule can resolve (distinguish) two lengths that are different only by $0.5mm$. A simple digital watch can display times with a resolution of one second, etc.
The uncertainty due to the finite accuracy of the sensors is known as \emph{Systematic accuracy uncertainty}. It is named so because it depends only on the features of the sensor device, which are always the same, at least in principle.
Systematic accuracy uncertainty is usually shown by adding half the least division of the Sensor device measurement scale to the measurement. So, for instance, if we are taking a measurement of $123 mm$ with a rule graduated in millimeters, we can present the measurement as $123 \pm 0.5 mm$ or $12,3\pm0.05cm$.
The systematic accuracy uncertainty represents an uniform probability density distribution. The mean is the value measured and the width is the minimum division of the sensor measurement scale. This means, that we consider that the real value of the magnitude is inside this interval (defined by the minimum division value) with a probability of $100\%$. Coming back to the example of the rule, The real value of our measurement will be inside the interval $[122.5,\ 123.5 ]$ and it could take whatever value, inside this interval, with the same probability.
\paragraph{Statistical uncertainty}\index[eng]{Experimental measurements! Statistical Uncertainty}
The second source of uncertainty arises from environmental factors that constantly change the conditions under which a measurement is taken. These factors render the measurement subject to random variations. While we could theoretically describe this variation using a probability distribution $f(x)$, in practice, the specific distribution is unknown. Consequently, we do not know its mean value $\mu$ or its variance $\sigma$.
But, fortunately, the central limit theorem, that we described in section \ref{tlc}, supplies a method to estimate the statical uncertainty.
Let's suppose we repeat the same experimental measurement $n$ times. For example, we have a bowl of water; we heat it and measure the water temperature when it begins to boil. We obtain a set $\{x_1,x_2,\cdots x_n\}$ of boiling water temperature measurements. Due to statistical uncertainty, it is likely that, the values we obtain will differ from one to another but they will be close together\footnote{In fact, when we obtain data points that are significantly different from the rest, we often consider them to be influenced by some unknown error and will discard them. Such data are known as outlayers}.
We know that this data should follow some probability distribution. We can estimate the expected value, $\mu$, by calculating the arithmetic mean of the collected data.
\end{paracol}
\begin{equation*}
\mu \approx \bar{x}_n = \frac{1}{n}\sum_{i=1}^n x_i
\end{equation*}
\begin{paracol}{2}
La media así calculada se toma como el valor resultante de la medida realizada.
De acuerdo con el teorema del límite central, $\bar{x}_n$ es una variable aleatoria que debe seguir una distribución normal de media $\mu$ y de varianza $\sigma^2/n$. Como tampoco conocemos el valor de la varianza $\sigma^2$ de la distribución de probabilidad que siguen los datos medidos, la estimamos a partir de los propios datos medidos, de acuerdo con la siguiente ecuación,
\switchcolumn
We take the mean so calculated as the resulting value of the performed measure.
According to the central limit theorem, the sample mean $\bar{x}_n$ is a random variable that follows a normal distribution with a mean of $\mu$ and a variance of $\sigma^2/n$. Since we do not know the variance $\sigma^2$ of the underlying probability distribution from which the measured data is drawn, we estimate it using the measured data itself, following this equation,
\end{paracol}
\begin{equation*}
\sigma^2 \approx s^2 = \frac{1}{n-1}\sum_{i=1}^n(x_i-\bar{x})^2
\end{equation*}
\begin{paracol}{2}
Dividiendo $s^2$ por el número de muestras empleado se puede aproximar la varianza de la distribución normal que debe seguir $\bar{x}_n$,
\switchcolumn
Dividing $s^2$ by the number of samples, we can approximate the variance of the normal distribution that $\bar{x}_n$ follows,
\end{paracol}
\begin{equation*}
\sigma^2_{xn} \approx s^2_{xn} = \frac{s^2}{n}
\end{equation*}
\begin{paracol}{2}
La incertidumbre, se puede asociar con la raíz cuadrada la varianza que acabamos de estimar,
\switchcolumn
We can associate the uncertainty with the square root of the variance just estimated.,
\end{paracol}
\begin{equation*}
s_{xn}=\sqrt{s^2_{xn}}=\sqrt{\frac{\frac{1}{n-1}\sum_{i=1}^n(x_i-\bar{x})^2}{n}}