Example #1
0
def square_root_diffusion_exact(initial_val=0.05,
                                kappa=3.0,
                                theta=0.02,
                                sigma=0.1,
                                time_year=2,
                                num_samples=10000,
                                num_time_interval_discretization=50):
    x = np.zeros((num_time_interval_discretization + 1, num_samples))
    x[0] = initial_val
    dt = time_year / num_time_interval_discretization

    for t in range(1, num_time_interval_discretization + 1):
        df = 4 * theta * kappa / sigma**2
        c = (sigma**2 * (1 - np.exp(-kappa * dt))) / (4 * kappa)
        nc = np.exp(-kappa * dt) / c * x[t - 1]
        x[t] = c * npr.noncentral_chisquare(df, nc, size=num_samples)

    plt.figure(figsize=(10, 6))
    plt.hist(x[-1], bins=50)
    plt.title("Square root diffusion Exact")
    plt.xlabel('value')
    plt.ylabel('frequency')
    plt.show()

    plt.figure(figsize=(10, 6))
    plt.plot(x[:, :10], lw=1.5)
    plt.xlabel('time')
    plt.ylabel('index level')
    plt.title('Sample Path SRD Exact')
    plt.show()

    return x
Example #2
0
 def CIR_trans(ctime, cstate, ntime):
     dt = ntime - ctime
     r_t = cstate
     c = 2 * a / ((1 - np.exp(-a * dt)) * (sigma**2))
     degree_of_freedom = 4 * a * b / sigma**2
     non_centrality = 2 * c * r_t * np.exp(-a * dt)
     return noncentral_chisquare(degree_of_freedom,
                                 non_centrality) / (2 * c)
Example #3
0
def srd_exact():
    x2 = np.zeros((M + 1, I))
    x2[0] = x0
    for t in range(1, M + 1):
        df = 4 * theta * kappa / sigma ** 2
        c = (sigma ** 2 * (1 - np.exp(-kappa * dt))) / (4 * kappa)
        nc = np.exp(-kappa * dt) / c * x2[t - 1] 
        x2[t] = c * npr.noncentral_chisquare(df, nc, size=I)
    return x2
Example #4
0
def srd_exact():
    x2=np.zeros((M+1,I))
    x2[0]=x0
    for t in range(1,M+1):
        df=4*theta*kappa/sigma**2
        c=(sigma**2*(1-np.exp(-kappa*dt)))/(4*kappa)
        nc=np.exp(-kappa*dt)/c*x2[t-1]
        x2[t]=c*npr.noncentral_chisquare(df,nc,size=I)
    return x2
Example #5
0
def cir_exact(T, M, I, x0, kappa, theta, sigma_cir):
    dt = T / M
    xh = np.zeros((M + 1, I))
    xh[0] = x0
    for t in range(1, M + 1):
        df = 4 * theta * kappa / sigma_cir ** 2
        c = sigma_cir ** 2 * (1 - np.exp(-kappa * dt)) / (4 * kappa)
        nc = np.exp(-kappa * dt) / c * xh[t - 1]
        xh[t - 1] = c * npr.noncentral_chisquare(df, nc, size=I)
    return xh
 def time_to_mutation_rate(tree):
     if not hasattr(GC,"NUMPY_SEEDED"):
         from numpy.random import seed as numpy_seed
         numpy_seed(seed=GC.random_number_seed)
         GC.random_number_seed += 1
         GC.NUMPY_SEEDED = True
     t = read_tree_newick(tree)
     for node in t.traverse_preorder():
         if node.edge_length is not None:
             node.edge_length *= noncentral_chisquare(df=GC.tree_rate_df,nonc=GC.tree_rate_lambda)
     return str(t)
def y_cir_path(y0, k, mu, sigma, n_simul, M, T):
    df = (4 * mu * k) / (sigma**2)
    dt = T / M
    l_path = []
    for i in range(n_simul):
        path = [y0]
        for t in range(1, M + 1):
            C = (sigma**2 * (1 - math.exp(-k * dt))) / (4 * k)
            arg = (4 * k * math.exp(-k * dt) *
                   path[-1]) / (sigma**2 * (1 - math.exp(-k * dt)))
            chi2 = npr.noncentral_chisquare(df, arg)
            S = C * chi2
            path.append(S)
        l_path.append(path)
    return l_path
 def time_to_mutation_rate(tree):
     if not hasattr(GC, "NUMPY_SEEDED"):
         from numpy.random import seed as numpy_seed
         numpy_seed(seed=GC.random_number_seed)
         GC.random_number_seed += 1
         GC.NUMPY_SEEDED = True
     t = read_tree_newick(tree)
     for node in t.traverse_preorder():
         if node.edge_length is None:
             if node.is_root():
                 node.rate = GC.tree_rate_R0
             else:
                 node.rate = node.parent.rate
         else:
             if node.is_root():
                 parent_rate = GC.tree_rate_R0
             else:
                 parent_rate = node.parent.rate
             node.rate = noncentral_chisquare(
                 df=GC.tree_rate_df,
                 nonc=nonc(GC.tree_rate_b, GC.tree_rate_sigma, parent_rate,
                           node.edge_length))
             node.edge_length *= node.rate
     return str(t)
Example #9
0
x0 = 0.05
kappa = 3.0
theta = 0.02
sigma = 0.1

M = 50
T = 2.0
I = 10000
dt = T / M
y = np.zeros((M + 1, I))
y[0] = x0
for t in range(1, M + 1):
    df = 4 * theta * kappa / sigma**2
    c = (sigma**2 * (1 - np.exp(-kappa * dt))) / (4 * kappa)
    nc = np.exp(-kappa * dt) * y[t - 1] / c
    y[t] = c * npr.noncentral_chisquare(df, nc, size=I)

plt.plot(y[:, :10], lw=1.5)
plt.title("模拟平方根扩散路径(精确格式)")
plt.xlabel("time")
plt.ylabel("index level")
plt.grid(True)
plt.show()

# 比较欧拉格式与精确格式
print_statistics(x[-1], y[-1])

# 随机波动率
r = 0.05
v0 = 0.1
kappa = 3.0
Example #10
0
	return np.sqrt(kappa**2-2*xi**2*i*a)

def f(z):
	return (0.5*z*h)/np.sinh(0.5*z*h)

def g(z):
	return z/(np.tanh(0.5*h))

def h(z,vt,vu):
	return scipy.special.yv(d,4*np.sqrt(vt*vu)*f(z)/xi**2)

def char_func(a,vt,vu):
	r1=f(gamma(a))/f(kappa)
	r2=np.exp(((vu+vt)/xi**2))*(-g(gamma(a))/np.exp(((vu+vt)/xi**2))*(-g(kappa)))
	r3=h(gamma(a),vt,vu)/h(kappa,vt,vu)
	return r1*r2*r3

def dist_func(x,vt,vu,a,b):
	vec=np.linspace(a,b,n,endpoint=True)
	y=((2/math.pi)*np.sin(vec*x)/vec)*(char_func(vec,vt,vu)).real
	return integrate.simps(y,vec)



vol=[vol_init]
for i in range(n):
	val=vol[-1]
	(vol.append(xi**2(1-np.exp(-kappa*h)))/4*kappa*npr.noncentral_chisquare(d,4*kappa*np.exp(-kappa*h)*val/(xi**2*(1-np.exp(-kappa*h)))))


Example #11
0
def noncentral_chisquare(size, params):
    try:
        return random.noncentral_chisquare(params['df'], params['nonc'], size)
    except ValueError as e:
        exit(e)