__author__ = 'Randall'

## DEMAPP05 Chebychev polynomial and spline approximantion of various functions

# Preliminary tasks

# Demonstrates Chebychev polynomial, cubic spline, and linear spline approximation for the following functions
#   1: y = sqrt(x+1)
#   2: y = exp(-x)
#   3: y = 1./(1+25*x.^2).
#   4: y = sqrt(abs(x))

## Functions to be approximated
funcs = [
    lambda x: 1 + x + 2 * x**2 - 3 * x**3, lambda x: np.exp(-x), lambda x: 1 /
    (1 + 25 * x**2), lambda x: np.sqrt(np.abs(x))
]

# Set degree of approximation and endpoints of approximation interval
n = 7  # degree of approximation
a = -1  # left endpoint
b = 1  # right endpoint

# Construct uniform grid for error ploting
x = nodeunif(2001, a, b)


def subfig(k, x, y, xlim, ylim, title):
    plt.subplot(2, 2, k)
    plt.plot(x, y)
    plt.xlim(xlim)
Esempio n. 2
0
# The function to be solved is $$f(x) = 1.01 - (1- x)^2$$
# where $x \geq 0$. Notice that $f(x)$ has roots $1\pm\sqrt{1.01}$

# * Preliminary tasks

# In[1]:
from demos.setup import np, plt, tic, toc
from compecon import MCP



# * Billup's function roots are

# In[2]:

roots = 1 + np.sqrt(1.01), 1 - np.sqrt(1.01)
print(roots)


# ### Set up the problem
# The class **MCP** is used to represent mixed-complementarity problems. To create one instance, we define the objective function and the boundaries $a$ and $b$ such that for $a \leq x \leq b$

# In[3]:

def billups(x):
    fval = 1.01 - (1 - x) ** 2
    fjac = 2 * (1 - x )
    return fval, fjac


a = 0
Esempio n. 3
0
def g(x):
    return np.sqrt(x)
Esempio n. 4
0
def f(x):
    fval = x - np.sqrt(x)
    fjac = 1-0.5 / np.sqrt(x)
    return fval, fjac
Esempio n. 5
0
def g(x):
    return np.sqrt(x)
Esempio n. 6
0
def f(x):
    fval = x - np.sqrt(x)
    fjac = 1 - 0.5 / np.sqrt(x)
    return fval, fjac
# Solve hard nonlinear complementarity problem on R using semismooth and minmax methods.  Problem involves Billup's function.  Minmax formulation fails semismooth formulation suceeds.
#
# The function to be solved is $$f(x) = 1.01 - (1- x)^2$$
# where $x \geq 0$. Notice that $f(x)$ has roots $1\pm\sqrt{1.01}$

# * Preliminary tasks

# In[1]:
from demos.setup import np, plt, tic, toc
from compecon import MCP

# * Billup's function roots are

# In[2]:

roots = 1 + np.sqrt(1.01), 1 - np.sqrt(1.01)
print(roots)

# ### Set up the problem
# The class **MCP** is used to represent mixed-complementarity problems. To create one instance, we define the objective function and the boundaries $a$ and $b$ such that for $a \leq x \leq b$

# In[3]:


def billups(x):
    fval = 1.01 - (1 - x)**2
    fjac = 2 * (1 - x)
    return fval, fjac


a = 0
Esempio n. 8
0
from demos.setup import np, plt
from scipy.integrate import quad


""" Computing Function Inner Products, Norms & Metrics """

# Compute Inner Product and Angle
a, b = -1, 1
f = lambda x: 2 * x**2 - 1
g = lambda x: 4 * x**3 - 3*x
fg = quad(lambda x: f(x) * g(x), a, b)[0]
ff = quad(lambda x: f(x) * f(x), a, b)[0]
gg = quad(lambda x: g(x) * g(x), a, b)[0]
angle = np.arccos(fg / np.sqrt(ff * gg)) * 180 / np.pi
print('\nCompute inner product and angle')
print('\tfg = {:6.4f},   ff = {:6.4f}, gg = {:6.4f},  angle = {:3.2f}'.format(fg, ff, gg, angle))

# Compute Function Norm
a, b = 0, 2
f = lambda x: x**2 - 1
p1, p2 = 1, 2
q1 = quad(lambda x: np.abs(f(x) - g(x)) ** p1, a, b)[0] ** (1 / p1)
q2 = quad(lambda x: np.abs(f(x) - g(x)) ** p2, a, b)[0] ** (1 / p2)
print('\nCompute function norm')
print('\tnorm 1 = {:6.4f},   norm 2 = {:6.4f}'.format(q1, q2))


# Compute Function Metrics
a, b = 0, 2
f = lambda x: x**3 + x**2 + 1
g = lambda x: x**3 + 2
Esempio n. 9
0
def resid(c):
    Q.c = c
    q = Q(p)
    return p + q / (-3.5 * p**(-4.5)) - np.sqrt(q) - q**2
Esempio n. 10
0
from compecon import DDPmodel

# DEMDDP04 Binomial American put option model

# Model Parameters
T = 0.5                 # years to expiration
sigma = 0.2                 # annual volatility
r = 0.05                # annual interest rate
strike = 2.1                 # option strike price
p0 = 2.0                 # current asset price

# Discretization Parameters
N = 100                 # number of time intervals
tau = T / N              	# length of time intervals
delta = np.exp(-r * tau)   	# discount factor
u = np.exp(sigma * np.sqrt(tau))	# up jump factor
q = 0.5 + np.sqrt(tau) * (r - (sigma**2) / 2) / (2 * sigma) # up jump probability

# State Space
price = p0 * u ** np.arange(-N, N+1)      # asset prices
n = price.size        # number of states

# Action Space (hold=1, exercise=2)
X = ['hold', 'exercise']   	# vector of actions
m = len(X)               	# number of actions

# Reward Function
f = np.zeros((m,n))
f[1] = strike - price

# State Transition Probability Matrix
Esempio n. 11
0
## DEMAPP05 Chebychev polynomial and spline approximantion of various functions

# Preliminary tasks

# Demonstrates Chebychev polynomial, cubic spline, and linear spline approximation for the following functions
#   1: y = sqrt(x+1)
#   2: y = exp(-x)
#   3: y = 1./(1+25*x.^2).
#   4: y = sqrt(abs(x))

## Functions to be approximated
funcs = [lambda x: 1 + x + 2 * x ** 2 - 3 * x ** 3,
         lambda x: np.exp(-x),
         lambda x: 1 / ( 1 + 25 * x ** 2),
         lambda x: np.sqrt(np.abs(x))]

# Set degree of approximation and endpoints of approximation interval
n = 7   # degree of approximation
a = -1  # left endpoint
b = 1   # right endpoint

# Construct uniform grid for error ploting
x = nodeunif(2001, a, b)

def subfig(k, x, y, xlim, ylim, title):
    plt.subplot(2, 2, k)
    plt.plot(x, y)
    plt.xlim(xlim)
    plt.ylim(ylim)
    plt.title(title)