Пример #1
0
def stoch_value():
    #Compute Solution==========================================================
    sigma = .5
    mu = 4*sigma
    K = 7
    Gamma, eps = discretenorm.discretenorm(K,mu,sigma)

    N = 100
    W = np.linspace(0,1,N)
    V = np.zeros((N,K))

    u = lambda c: np.sqrt(c)
    beta = 0.99

    X,Y= np.meshgrid(W,W)
    Wdiff = Y-X
    index = Wdiff < 0
    Wdiff[index] = 0

    util_grid = u(Wdiff)

    util3 = np.tile(util_grid[:,:,np.newaxis],(1,1,K))
    eps_grid = eps[np.newaxis,np.newaxis,:]
    eps_util = eps_grid*util3

    Gamma_grid = Gamma[np.newaxis,:]

    delta = 1
    Vprime = V
    z = 0
    while (delta > 10**-9):
        z= z+1
        V = Vprime
        gamV = Gamma_grid*V
        Expval = np.sum(gamV,1)
        Exp_grid = np.tile(Expval[np.newaxis,:,np.newaxis],(N,1,K))
        arg = eps_util+beta*Exp_grid
        arg[index] = -10^10
        Vprime = np.amax(arg,1)
        psi_ind = np.argmax(arg,1)
        psi = W[psi_ind]
        delta = np.linalg.norm(Vprime - V)

    #============================================================
    #Plot 3D
    x=np.arange(0,N)
    y=np.arange(0,K)
    X,Y=np.meshgrid(x,y)
    fig1 = plt.figure()
    ax1= Axes3D(fig1)
    ax1.set_xlabel(r'$W$')
    ax1.set_ylabel(r'$\varepsilon$')
    ax1.set_zlabel(r'$V$')
    ax1.plot_surface(W[X],Y,np.transpose(Vprime), cmap=cm.coolwarm)
    plt.savefig('stoch_value.pdf')
    plt.clf()
Пример #2
0
def stoch_value():    
    #Compute Solution==========================================================
    sigma = .5
    mu = 4*sigma
    K = 7
    Gamma, eps = discretenorm.discretenorm(K,mu,sigma)
    
    N = 100
    W = np.linspace(0,1,N)
    V = np.zeros((N,K))
    
    u = lambda c: np.sqrt(c)
    beta = 0.99
    
    X,Y= np.meshgrid(W,W)
    Wdiff = Y-X
    index = Wdiff < 0
    Wdiff[index] = 0
    
    util_grid = u(Wdiff)
    
    util3 = np.tile(util_grid[:,:,np.newaxis],(1,1,K))
    eps_grid = eps[np.newaxis,np.newaxis,:]
    eps_util = eps_grid*util3
    
    Gamma_grid = Gamma[np.newaxis,:]
    
    delta = 1
    Vprime = V
    z = 0
    while (delta > 10**-9):
        z= z+1
        V = Vprime
        gamV = Gamma_grid*V
        Expval = np.sum(gamV,1)
        Exp_grid = np.tile(Expval[np.newaxis,:,np.newaxis],(N,1,K))
        arg = eps_util+beta*Exp_grid
        arg[index] = -10^10
        Vprime = np.amax(arg,1)
        psi_ind = np.argmax(arg,1)
        psi = W[psi_ind]
        delta = np.linalg.norm(Vprime - V)
    
    #============================================================    
    #Plot 3D    
    x=np.arange(0,N)
    y=np.arange(0,K)
    X,Y=np.meshgrid(x,y)
    fig1 = plt.figure()
    ax1= Axes3D(fig1)
    ax1.set_xlabel(r'$W$')
    ax1.set_ylabel(r'$\varepsilon$')
    ax1.set_zlabel(r'$V$')
    ax1.plot_surface(W[X],Y,np.transpose(Vprime), cmap=cm.coolwarm)
    plt.savefig('stoch_value.pdf')
    plt.clf()
              value is 1.

    Outputs:
        max_vals: The maximum values found along the dimension.
        arg_maxes: The locaiton of the max_vals along a dimension.
    """
    return np.array([np.max(array, axis=axis), np.argmax(array, axis=axis)])

## --------------------------------Problem 1--------------------------------- ##
sigma = np.sqrt(0.25)
mu = 4 * sigma
nodes = 7
beta = 0.9
tol = 10e-9

eps, gamma = dn.discretenorm(nodes, mu, sigma)

## --------------------------------Problem 2--------------------------------- ##
w = np.array(np.arange(0.01, 1.01, .01))
N = w.size
vtp1 = np.zeros((N,nodes))

# Create probability matrix for epsilon'
E_mat = np.tile(gamma, (N, 1))

# Integrate out epsilon' and get just the E[v(w')] vector.
Evtp1_prime = np.sum(vtp1 * E_mat, axis = 1)

# Flip it so the w' is the third dimension. Then repeat it in w, epsilon dims.
Evtp1_prime = np.reshape(Evtp1_prime, (1,1,N))
Evtp1_mat = np.tile(Evtp1_prime, (N, nodes, 1))