An Introductory Example

An Introductory Example - QuantEcon


In [1]:
# -*- coding: utf-8 -*-
"""
QuantEcon Notebook
Copyright (c) 2016 @myuuuuun

Released under the MIT license.
"""
%matplotlib inline
import math
import numpy as np

In [2]:
from random import normalvariate
import matplotlib.pyplot as plt

ts_length = 100
epsilon_values = []   # An empty list

for i in range(ts_length):
    e = normalvariate(0, 1)
    epsilon_values.append(e)
    
plt.plot(epsilon_values, 'purple')
plt.show()



In [24]:
x = []
x.append("aiueo")
x.append(3)

x


Out[24]:
['aiueo', 3]

In [27]:
array = [2, 3, 4, False, True, "aiueo"]

for i in range(3):
    print(array.pop())
    
print(array)


aiueo
True
False
[2, 3, 4]

In [29]:
array = [2, 3, 4, False, True, "aiueo"]
array.append("aaa")

array


Out[29]:
[2, 3, 4, False, True, 'aiueo', 'aaa']

In [31]:
array = [2, 3, 4, False, True, "aiueo"]

for elem in array:
    print(elem)


2
3
4
False
True
aiueo

In [33]:
array = [2, 3, 4, False, True, "aiueo"]

for i in range(4):
    print(i, "番目の要素は: ", array[i])


0 番目の要素は:  2
1 番目の要素は:  3
2 番目の要素は:  4
3 番目の要素は:  False

In [35]:
animals = ['dog', 'cat', 'fish']
for animal in animals:
    print("The plural of " + animal + " is " + animal + "s")


The plural of dog is dogs
The plural of cat is cats
The plural of fish is fishs

In [42]:
s = 0
i = 1
while i <= 10:
    s += i
    i += 1
    
print(s)


55

In [44]:
s = 0
i = 1
while True:
    s += i
    i += 1
    if i > 10:
        break
    
print(s)


55

In [49]:
def your_favorite_name(argument):
    return argument

In [50]:
print( your_favorite_name("太郎") )


太郎

In [51]:
# 引数の2乗を計算する
def x_2(x):
    s = x * x
    return s

In [53]:
y = x_2(0.3)

print(y)


0.09

In [15]:
from random import normalvariate, uniform

def generate_data(n, generator_type=None):
    print(generator_type)
    epsilon_values = []
    for i in range(n):
        if generator_type == 'U':
            e = uniform(0, 1)
        else:
            e = normalvariate(0, 1)
        epsilon_values.append(e)
    return epsilon_values

data = generate_data(100, "U")
plt.plot(data, 'b.')
plt.show()


U

In [16]:
data = generate_data(100)
plt.plot(data, 'b.')
plt.show()


None

In [17]:
x = -10
s = 'negative' if x < 0 else 'nonnegative'
print(s)


negative

In [18]:
x = 3.3
s = 'negative' if x < 0 else 'nonnegative'
print(s)


nonnegative

In [19]:
x = 20

if x < 0:
    print("negative")
else:
    print("nonnegative")


nonnegative

In [24]:
x = -0.9
s = 'negative' if x < 0 else  'zero'  if x == 0 else 'positive'
print(s)


negative

In [33]:
from random import uniform, normalvariate, weibullvariate
import matplotlib.pyplot as plt


def generate_data(n, generator_type=uniform):
    print(uniform)
    epsilon_values = []
    for i in range(n):
        e = generator_type(0, 1)
        epsilon_values.append(e)
    return epsilon_values

data = generate_data(100)
plt.plot(data, 'b-')
plt.show()


<bound method Random.uniform of <random.Random object at 0x103054818>>

In [34]:
array = [0, -3, 2**10, 1000]

max(array)


Out[34]:
1024

In [35]:
m = max

m(array)


Out[35]:
1024

In [36]:
m


Out[36]:
<function max>

In [37]:
animals = ['dog', 'cat', 'bird']
plurals = [animal + 's' for animal in animals]

print(plurals)


['dogs', 'cats', 'birds']

In [39]:
plurals = []
for animal in animals:
    plurals.append(animal + "s")
    
print(plurals)


['dogs', 'cats', 'birds']

In [41]:
array = list(range(1, 10))

print(array)


[1, 2, 3, 4, 5, 6, 7, 8, 9]

In [47]:
doubles = [pow(x, 3) for x in array]

print(doubles)


[1, 8, 27, 64, 125, 216, 343, 512, 729]

In [50]:
import math
doubles = [x**math.sqrt(2) for x in array]

print(doubles)


[1.0, 2.665144142690225, 4.728804387837416, 7.102993301316016, 9.738517742335421, 12.602945316172725, 15.672890873693548, 18.930500992570288, 22.361590938430393]

In [53]:
epsilon_values = [uniform(0, 1) for i in range(10)]

print(epsilon_values)


[0.5636087413851024, 0.20654498114465702, 0.08086216130328894, 0.9573565874798018, 0.8210680235899843, 0.323124641644538, 0.5733962304172113, 0.6276348035924046, 0.6447262089725652, 0.9969868404834185]

In [ ]: