Si tecleo algo:
In [1]:
3
Out[1]:
Me devuelve el valor de la expresión.
Si tecleo 2 cosas:
In [2]:
3
4
Out[2]:
Me devuelve la última únicamente.
In [3]:
3
4;
; implica que no devuelva nada (o al menos, que no lo imprima).
In [5]:
1:6
Out[5]:
In [6]:
typeof(ans) # dame el tipo de la última respuesta
Out[6]:
El objeto 1:6 es de tipo UnitRange, "rellenado" con enteros con 64 bits.
No es un arreglo. Para hacer un arreglo:
In [9]:
[1:6]
Out[9]:
Darle un nombre:
In [12]:
a = [1:6]
Out[12]:
In [13]:
a
Out[13]:
Un solo = quiere decir asignarle un valor a la variable del lado izquierdo.
Un rango ocupa (mucho) menos memoria que un arreglo y es más rápido. No almacena nada, sino va generando valores en el vuelo.
In [14]:
rand
Out[14]:
Si sólo pongo el nombre de una función, me dice que es una función.
Para llamarla, uso paréntesis:
In [15]:
rand()
Out[15]:
Regresa un número ((p)seudo-)aleatorio uniformemente entre $0$ y $1$.
In [16]:
rand()
Out[16]:
En Julia, para averiguar cuál función se está llamando, usamos @which y ponemos la llamada completa de la función:
In [17]:
@which rand()
Out[17]:
In [18]:
rand(1:6)
Out[18]:
[Si uso Ctrl-Enter, se queda en la misma celda.]
In [30]:
6 * rand()
Out[30]:
Quiero enteros:
In [55]:
int(6 * rand())
Out[55]:
In [56]:
int(3.6)
Out[56]:
¡int redondea!
In [58]:
floor(3.6)
Out[58]:
In [59]:
floor(-3.6)
Out[59]:
In [60]:
floor(6 * rand())
Out[60]:
In [80]:
ceil(6 * rand())
Out[80]:
In [81]:
1 + floor(6 * rand())
Out[81]:
In [85]:
function dado()
int( 1 + floor(6 * rand()) )
end
Out[85]:
In [86]:
function dado2()
rand(1:6)
end
Out[86]:
In [88]:
function dado(num_caras)
int( 1 + floor(num_caras * rand()) )
end
Out[88]:
In [89]:
dado
Out[89]:
In [102]:
dado()
Out[102]:
In [137]:
dado(2) # una moneda
Out[137]:
In [138]:
dado
Out[138]:
In [139]:
methods(dado)
Out[139]:
In [140]:
function dado(num_caras=6)
int( 1 + floor(num_caras * rand()) )
end
Out[140]:
In [141]:
methods(dado)
Out[141]:
In [142]:
dado()
Out[142]:
In [143]:
dado(10)
Out[143]:
El valor por default (num_caras=6) hace dos métodos distintos de la función.
In [144]:
methods(rand)
Out[144]:
In [145]:
rand(1:6, 10)
Out[145]:
In [146]:
@which rand(1:6, 10)
Out[146]:
In [148]:
@which rand(6, 10)
Out[148]:
In [149]:
rand(6,10)
Out[149]:
In [151]:
rand(1:6, 3,4)
Out[151]:
In [152]:
@which rand(1:6, 3,4)
Out[152]:
In [154]:
rand(1:2, 2, 2, 2)
Out[154]:
In [155]:
rand(1:2, 2, 2, 2, 2)
Out[155]:
Esto se llama multiple dispatch (despacho múltiple)
In [156]:
function dados(N)
rand(1:6, N)
end
Out[156]:
In [157]:
dados(N) = rand(1:6, N)
Out[157]:
In [167]:
N = 10
tiros = dados(N)
Out[167]:
In [168]:
cara1 = 0
for i in 1:length(tiros)
if tiros[i] == 1
cara1 = cara1 + 1
end
end
In [169]:
cara1
Out[169]:
In [174]:
cara1 = 0
for i in 1:length(tiros)
if tiros[i] == 1 cara1 = cara1 + 1 end
end
In [171]:
cara1
Out[171]:
In [ ]:
c = 0
for i in 1:length(t)
if t[i] == 1
c = c + 1
end
end
Unas versiones más:
In [175]:
caras = zeros(6)
Out[175]:
In [177]:
caras = zeros(Int, 6)
Out[177]:
In [178]:
@which zeros(Int, 6)
Out[178]:
In [179]:
#caras = [0, 0, 0, 0, 0, 0]
caras = zeros(Int, 6)
for i in 1:length(tiros)
if tiros[i] == 1
caras[1] = caras[1] + 1
end
if tiros[i] == 2
caras[2] = caras[2] + 1
end
end
In [180]:
caras
Out[180]:
In [182]:
caras[1] # el valor de la primera entrada de caras
Out[182]:
In [183]:
hola = 10
Out[183]:
In [187]:
hola[2]
In [190]:
Out[190]:
Teorema: Si te encuentras haciendo algo más de 2 veces, ¡hay que automatizarlo!
In [194]:
a = 3
b = 3.0
Out[194]:
In [195]:
a == b
Out[195]:
In [196]:
a === b
Out[196]:
In [199]:
c = int32(10)
d = int64(10)
c == d, c === d, is(c, d)
Out[199]:
In [200]:
===
Out[200]:
In [201]:
is
Out[201]:
In [202]:
typeof(ans)
Out[202]:
In [203]:
is(is, is)
Out[203]:
In [204]:
is === is
Out[204]:
Automatizar:
In [207]:
caras = zeros(Int, 6)
for i in 1:length(tiros)
for valor in 1:6
if tiros[i] == valor
caras[valor] += 1
end
end
end
[El editor de IJulia es CodeMirror]
In [208]:
caras
Out[208]:
Construimos un histograma (en la interpretación estadística de la palabra. Ejercicio: Checa si realmente se llame histograma).
Normalizar: dividir entre el número de tiradas:
In [211]:
sum(caras)
Out[211]:
In [213]:
?sum
In [212]:
methods(sum)
Out[212]:
In [210]:
caras / suma(caras)
sum actúa sobre iterables, por ejemplo rangos:
In [214]:
sum(1:6)
Out[214]:
In [215]:
sum(1:.1:6)
Out[215]:
In [216]:
1:.1:6
Out[216]:
In [217]:
typeof(ans)
Out[217]:
In [218]:
[1:.1:6]
Out[218]:
In [219]:
probabilidades = caras / sum(caras)
Out[219]:
In [220]:
sum(probabilidades)
Out[220]:
In [221]:
big(0.2)
Out[221]:
In [222]:
widen(0.2)
Out[222]:
In [224]:
BigFloat("0.2")
Out[224]:
In [225]:
big(10)
Out[225]:
In [226]:
BigFloat(0.2)
Out[226]:
Necesitamos importar un paquete de gráficos. Usaremos PyPlot (wrapper de matplotlib)
In [227]:
using PyPlot
In [ ]:
In [240]:
figure(figsize=(5,3))
plot(probabilidades, "ro-")
ylim(0, 0.4)
xlim(-1, 6)
xlabel("cara")
ylabel("probabilidad")
Out[240]:
In [241]:
plt.hist(tiros)
Out[241]:
Mi gráfica está más bonita que el histograma horrible de PyPlot -- c.f. Edward Tufte
In [248]:
N = 100
tiros = rand(1:6, N);
In [250]:
function histograma(datos, num_cajas)
@show datos
conteos = zeros(Int, num_cajas)
for i in 1:length(datos)
for caja in 1:num_cajas
if datos[i] == caja
conteos[caja] += 1
end
end
end
@show conteos
probabilidades = conteos / length(datos)
end
probabilidades = histograma(tiros, 6)
plot(probabilidades)
Out[250]:
In [251]:
function plot_probas(probabilidades)
figure(figsize=(5,3))
plot(probabilidades, "ro-")
ylim(0, 0.4)
xlim(-1, 6)
xlabel("cara")
ylabel("probabilidad")
end
Out[251]:
In [252]:
plot_probas(probabilidades)
Out[252]:
In [253]:
pwd()
Out[253]:
In [254]:
;ls
In [255]:
include("histograma.jl")
Out[255]:
In [ ]: