An Introductory Example - QuantEcon(Julia)


In [4]:
using PyPlot
ts_length = 100
epsilon_values = randn(ts_length)
plot(epsilon_values, "blue")


Out[4]:
1-element Array{Any,1}:
 PyObject <matplotlib.lines.Line2D object at 0x31b64ab38>

In [31]:
#collect(3:10)
# ここの処理は〜〜〜
#plot(collect(1:100), epsilon_values, "blue")

s = linspace(0, 1, 11)


Out[31]:
linspace(0.0,1.0,11)

In [49]:
ts_length = 100
array = zeros(Int64, 2, 2, 2, 2)

#array[:, :, :] = 0
#array[1, 1, 1] = 21

array


Out[49]:
2x2x2x2 Array{Int64,4}:
[:, :, 1, 1] =
 0  0
 0  0

[:, :, 2, 1] =
 0  0
 0  0

[:, :, 1, 2] =
 0  0
 0  0

[:, :, 2, 2] =
 0  0
 0  0

In [51]:
array = [10, "にゃん", false]

array


Out[51]:
3-element Array{Any,1}:
    10     
      "にゃん"
 false     

In [66]:
array = Array{Any}(3)

array[3] = 20
array[2] = "aiueo"
array[1] = 10.2

array[3] = "aaaaa"

array


Out[66]:
3-element Array{Any,1}:
 10.2     
   "aiueo"
   "aaaaa"

In [72]:
array = ["2", 30, "🙅"]

typeof(array[3])


Out[72]:
UTF8String

In [73]:
array = [3, 2, "20", "aiueo", false]

length(array)


Out[73]:
5

In [74]:
array = [2 3; 2 1]
length(array)


Out[74]:
4

In [79]:
array = [3, 2, "20", "aiueo", false]
pop!(array)
array


Out[79]:
4-element Array{Any,1}:
 3       
 2       
  "20"   
  "aiueo"

In [81]:
array = [3, 2, "20", "aiueo", false]
x = "added"
push!(array, x)

array


Out[81]:
6-element Array{Any,1}:
     3       
     2       
      "20"   
      "aiueo"
 false       
      "added"

In [90]:
array = ["aiueo", 3, false, 20.3]

for variable in array
    println(variable)
end


aiueo
3
false
20.3

In [99]:
array = ["aiueo", 3, false, 20.3]

for i in 1:3
    println(i, ": ", array[i])
end


1: aiueo
2: 3
3: false

In [105]:
ts_length = 100
epsilon_values = Array(Float64, ts_length)

for i in 1:ts_length
    epsilon_values[i] = randn()
end

epsilon_values
#plot(epsilon_values, "b-")


Out[105]:
100-element Array{Float64,1}:
 -1.98243 
 -0.289045
  0.548386
  2.68586 
 -0.826671
 -0.27946 
  0.833183
  0.820687
  1.68391 
  0.848353
  1.43303 
  1.02952 
 -0.138658
  ⋮       
  0.193556
  0.376007
 -0.770974
 -0.935151
  0.798305
 -1.63905 
  1.50708 
 -1.1761  
  0.224439
 -0.517274
 -0.944976
  1.77557 

In [107]:
words = ["foo", "bar"]
for word in words
    println("Hello $(word)")
end


Hello foo
Hello bar

In [112]:
xs = [2pi, pi/2, pi/3]
for x in xs
    println("Hello $(cos(x))")
end


Hello 1.0
Hello 6.123233995736766e-17
Hello 0.5000000000000001

In [119]:
s = 0
i = 1

while true
    s += i
    i += 1
end

print(s)


LoadError: InterruptException:
while loading In[119], in expression starting on line 4

In [120]:
s = 0
i = 1

while true
    s += i
    i += 1
    if i > 10
        break
    end
end

print(s)


55

In [121]:
function my_sum(from, to)
    s = 0
    for i = from:to
        s += i
    end
    return s
end


Out[121]:
my_sum (generic function with 1 method)

In [123]:
my_sum(2, 10)


Out[123]:
54

In [1]:
using Distributions

In [12]:
Normal(1, 2)


Out[12]:
Distributions.Normal(μ=1.0, σ=2.0)

In [13]:
function plot_histogram(distribution, n)
    epsilon_values = rand(distribution, n)  # n draws from distribution
    plt[:hist](epsilon_values)
end

lp = Normal(5, 1)
plot_histogram(lp, 5000)


Out[13]:
([5.0,40.0,191.0,710.0,1340.0,1369.0,901.0,346.0,84.0,14.0],[1.079994632701876,1.8331729634194591,2.5863512941370423,3.3395296248546256,4.092707955572209,4.845886286289792,5.599064617007375,6.3522429477249585,7.105421278442542,7.858599609160125,8.611777939877708],Any[PyObject <matplotlib.patches.Rectangle object at 0x31c024470>,PyObject <matplotlib.patches.Rectangle object at 0x31c22a550>,PyObject <matplotlib.patches.Rectangle object at 0x31c239b70>,PyObject <matplotlib.patches.Rectangle object at 0x31c2476d8>,PyObject <matplotlib.patches.Rectangle object at 0x31c247f98>,PyObject <matplotlib.patches.Rectangle object at 0x31c24c208>,PyObject <matplotlib.patches.Rectangle object at 0x31c2409b0>,PyObject <matplotlib.patches.Rectangle object at 0x31c2523c8>,PyObject <matplotlib.patches.Rectangle object at 0x31c24c898>,PyObject <matplotlib.patches.Rectangle object at 0x31c259780>])

In [15]:
rand(100)


Out[15]:
100-element Array{Float64,1}:
 0.404843
 0.68938 
 0.143533
 0.705652
 0.129699
 0.321097
 0.532875
 0.562169
 0.199171
 0.50925 
 0.844162
 0.640836
 0.444534
 ⋮       
 0.45307 
 0.540133
 0.399599
 0.828188
 0.862301
 0.907723
 0.968492
 0.919998
 0.90256 
 0.867195
 0.212816
 0.175562

Exercise1

reduceを使ってみる


In [18]:
my_factorial(n) = reduce(*, collect(1:n))


Out[18]:
my_factorial (generic function with 1 method)

In [19]:
my_factorial(4)


Out[19]:
24

非整数に対応してみる


In [ ]:

Exercise2


In [20]:
my_binomial_rv(n, p) = sum(rand(n) .< p)


Out[20]:
my_binomial_rv (generic function with 1 method)

In [24]:
for i=1:20
    println(my_binomial_rv(10, 0.1))
end


1
1
0
2
1
1
0
0
3
0
1
0
0
1
3
1
2
2
1
1

Exercise3


In [89]:
function montecalro_pi(size)
    sample = rand(size, 2)
    inner = 0.0

    for (s1, s2) in zip(sample[:, 1], sample[:, 2])
        inner += s1^2 + s2^2 <= 1 ? 1 : 0
    end
        
    return inner * 4 / size
end


Out[89]:
montecalro_pi (generic function with 1 method)

In [92]:
print( montecalro_pi(1000000) )


3.141292

Exercise4

n: コインを投げる回数, p: 表が出る確率 のとき, x回以上連続で表が出る確率を求める


In [38]:
function coin(n, x, p)
    total = 2^n
    sucess = 0
    
    for i=x:n
        sucess += factorial(n) / factorial(i) / factorial(n-i)
    end
    
    return sucess / total
end


Out[38]:
coin (generic function with 1 method)

In [39]:
coin(10, 3, 0.5)


Out[39]:
0.9453125

In [ ]: