In [4]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import math

from basic_artificial_market import ArtificialMarket

In [ ]:
import numpy as np
import matplotlib.pyplot as plt
import math

class ArtificialMarket():
    def __init__(self, num_player=1000, fdmtl=10000.0, ganma=1, sigma=0.06, P_sigma=30):
        self.num_player = num_player
        self.random_state = np.random.RandomState()
        self.fdmtl = fdmtl
        self.sigma = sigma
        self.ganma2 = 10000
        self.P_sigma = P_sigma
    
    def weight(self, w_1_max=1, w_2_max=10, w_3_max=1):
        num_player = self.num_player
        weight_1 = np.zeros(num_player)
        weight_2 = np.zeros(num_player)
        weight_3 = np.zeros(num_player)
        random_state = self.random_state
        for i in range(num_player):
            weight_1[i] = random_state.uniform()*w_1_max
            weight_2[i] = random_state.uniform()*w_2_max
            weight_3[i] = random_state.uniform()*w_3_max
        weight = [weight_1, weight_2, weight_3]
        return weight
    
    def ganma(self, ganma_max=10000, num_player=None):
        random_state = self.random_state
        if num_player is None:
            num_player = self.num_player
        ganma = np.array([])
        for i in range(num_player):
            ganma = np.append(ganma, int(random_state.uniform(ganma_max)))
        return ganma
    
    def P_t(self, past_data, delta_l, delta_t):
        if np.sum(delta_t) <= delta_l:
            P_t = past_data[-1]
        else: 
            d = 0
            j = 0
            while d <= delta_l:
                d += delta_t[-j]
                j += 1
            P_t = past_data[-j]
        return P_t
    
    def r_t_h(self, past_data, P_t, ganma, w_2):
        if len(past_data) < ganma:
            r_t_h = 0
            w_2 = 0
        else:
            past_data_ganma = past_data[-ganma]
            r_t_h = np.log(past_data_ganma/P_t)
            #print ganma, len(past_data), past_data[-1], past_data[-20], past_data_ganma
        return r_t_h, w_2

    def one_market_model(self, w, delta_l=0, past_data=None, delta_t=None, b_limit=None, s_limit=None, delta=1, ganma_max=10):
        num_player = self.num_player
        sigma = self.sigma
        P_sigma = self.P_sigma
        P_f = self.fdmtl
        
        if past_data is None:
            past_data = [P_f]
        if b_limit is None:
            b_limit = []
        if s_limit is None:
            s_limit = []
        if delta_t is None:
            delta_t = np.array([])
            
        P_t_1 = past_data[-1]
        w_1 = w[0]
        w_2 = w[1]
        w_3 = w[2]
        
        r_t_e = np.zeros(num_player)
        
        ganma = self.ganma()
    
        for i in range(num_player):
            w_2_i = w_2[i]
            
            P_t_1 = self.P_t(past_data, delta_l, delta_t)
            
            #if len(past_data) < ganma[i]:
                #r_t_h = np.log10(P_t_1/past_data[0])
            #else:
                #past_data_ganma = past_data[-ganma[i]]
                #r_t_h = np.log10(P_t_1/past_data_ganma)
            #r_t_h = self.r_t_h(past_data, P_t_1, ganma[i])
            
            r_t_h, w_2_i = self.r_t_h(past_data, P_t_1, ganma[i], w_2_i) 
            
            e_t = np.random.normal(0, sigma)
            r_t_e[i] = (w_1[i]*np.log(P_f/P_t_1) + w_2_i*r_t_h + w_3[i]*e_t)/(w_1[i] + w_2_i + w_3[i])
            print w_2_i
            P_e = P_t_1*math.exp(r_t_e[i])
            P_o = np.random.normal(P_e, P_sigma)
            if P_e > P_o:
                P_o = round(P_o, 1)
                if len(s_limit) > 0 and np.min(s_limit) < P_o:
                    P_t = np.min(s_limit)
                    s_limit = np.delete(s_limit, np.argmin(s_limit))
                else:
                    b_limit = np.append(b_limit, P_o)
                    P_t = P_t_1
            else:
                P_o = round(P_o, 1)+0.1
                if len(b_limit) > 0 and np.max(b_limit) > P_o:
                    P_t = np.max(b_limit)
                    b_limit = np.delete(b_limit, np.argmax(b_limit))
                else:
                    s_limit = np.append(s_limit, P_o)
                    P_t = P_t_1
            past_data = np.append(past_data, P_t)
            delta_t = np.append(delta_t, np.random.exponential(delta))
        return past_data, delta_t, b_limit, s_limit
        
    def one_market_simulation(self, delta_l=0, t_max=100000):
        w = self.weight()
        past_data, delta_t, b_limit, s_limit = self.one_market_model(w, delta_l)
        t = len(delta_t)
        print t_max
        while t < t_max:
            past_data, delta_t, b_limit, s_limit = self.one_market_model(w, delta_l, past_data, delta_t, b_limit, s_limit)
            t = len(delta_t)
        return past_data
    
    def rad(self, past_data, fdmtl=10000.0):
        p = 0
        for i in range(len(past_data)):
            p += math.fabs(float(past_data[i]) - fdmtl)/fdmtl
        m = 1.0/float(len(past_data))*p
        return m

    def agreed_rate(self, past_data, market_order):
        print market_order
        agreed_rate = float(market_order)/float(len(past_data))
        return agreed_rate

In [ ]:
AM = ArtificialMarket()
past_data = AM.one_market_simulation(0, 10000)

In [126]:
plt.plot(past_data)


Out[126]:
[<matplotlib.lines.Line2D at 0x10de14c50>]

In [127]:
AM.rad(past_data)


Out[127]:
0.007234273314533777

AM = ArtificialMarket(ganma=100)

k, t, market_order= AM.one_market_simulation(10, 1) plt.plot(k) AM.rad(k)


In [ ]:


In [73]:
AM.ganma()[1]


Out[73]:
3066.0

In [168]:
np.random.uniform(10000)


Out[168]:
5068.742341229107

In [14]:
math.exp(1)


Out[14]:
2.718281828459045

In [ ]: