# 量化分析师的Python_python 金融量化分析_python金融大数据分析

4643℃

#### 一、SciPy概述¶

In [1]:
import numpy as np
import scipy.stats as stats
import scipy.optimize as opt


#### 二、统计部分¶

##### 2.1 生成随机数¶

In [2]:
rv_unif = stats.uniform.rvs(size=10)
print rv_unif
rv_beta = stats.beta.rvs(size=10, a=4, b=2)
print rv_beta

[ 0.20630272  0.25929204  0.16859206  0.92573462  0.16383319  0.3475617
0.83792048  0.79574153  0.37945051  0.23439682]
[ 0.71216492  0.85688464  0.70310131  0.3783662   0.69507561  0.78626586
0.54529967  0.4261079   0.26646767  0.8519046 ]


In [3]:
np.random.seed(seed=2015)
rv_beta = stats.beta.rvs(size=10, a=4, b=2)
print "method 1:"
print rv_beta

np.random.seed(seed=2015)
beta = stats.beta(a=4, b=2)
print "method 2:"
print beta.rvs(size=10)

method 1:
[ 0.43857338  0.9411551   0.75116671  0.92002864  0.62030521  0.56585548
0.41843548  0.5953096   0.88983036  0.94675351]
method 2:
[ 0.43857338  0.9411551   0.75116671  0.92002864  0.62030521  0.56585548
0.41843548  0.5953096   0.88983036  0.94675351]

##### 2.2 假设检验¶

In [4]:
norm_dist = stats.norm(loc=0.5, scale=2)
n = 200
dat = norm_dist.rvs(size=n)
print "mean of data is: " + str(np.mean(dat))
print "median of data is: " + str(np.median(dat))
print "standard deviation of data is: " + str(np.std(dat))

mean of data is: 0.705195138069
median of data is: 0.658167882933
standard deviation of data is: 2.08967006905


In [5]:
mu = np.mean(dat)
sigma = np.std(dat)
stat_val, p_val = stats.kstest(dat, 'norm', (mu, sigma))
print 'KS-statistic D = %6.3f p-value = %6.4f' % (stat_val, p_val)

KS-statistic D =  0.045 p-value = 0.8195


In [6]:
stat_val, p_val = stats.ttest_1samp(dat, 0)
print 'One-sample t-statistic D = %6.3f, p-value = %6.4f' % (stat_val, p_val)

One-sample t-statistic D =  4.761, p-value = 0.0000


In [7]:
norm_dist2 = stats.norm(loc=-0.2, scale=1.2)
dat2 = norm_dist2.rvs(size=n/2)
stat_val, p_val = stats.ttest_ind(dat, dat2, equal_var=False)
print 'Two-sample t-statistic D = %6.3f, p-value = %6.4f' % (stat_val, p_val)

Two-sample t-statistic D =  5.565, p-value = 0.0000


[ mathbf{H(x)p} = begin{bmatrix} (1200x0^2 – 400x_1 + 2)p_0 -400x_0p_1 vdots -400x{i-1}p{i-1} + (202 + 1200x_i^2 – 400x{i+1})pi – 400x_ip{i+1} vdots -400x{N-2}p{N-2} + 200p_{N-1} end{bmatrix} ]

In [20]:
def rosen_hess_p(x, p):
x = np.asarray(x)
Hp = np.zeros_like(x)
Hp[0] = (1200*x[0]**2 - 400*x[1] + 2)*p[0] - 400*x[0]*p[1]
Hp[1:-1] = -400*x[:-2]*p[:-2]+(202+1200*x[1:-1]**2-400*x[2:])*p[1:-1]
-400*x[1:-1]*p[2:]
Hp[-1] = -400*x[-2]*p[-2] + 200*p[-1]
return Hp

res = opt.minimize(rosen, x_0, method='Newton-CG', jac=rosen_der, hessp=rosen_hess_p, options={'xtol': 1e-8, 'disp': True})
print "Result of minimizing Rosenbrock function via Newton-Conjugate-Gradient algorithm (Hessian times arbitrary vector):"
print res

Optimization terminated successfully.
Current function value: 0.000000
Iterations: 20
Function evaluations: 22
status: 0