《NumPy Beginner's Guide》笔记Chapter4

# -*- coding: utf-8 -*-

__author__ = 'ZengDong'
#日期 = 10:58

import numpy as np
import  matplotlib.pyplot as plt


""" 1. Correlation (A high correlation implies a relationship of some sort) function: cov diagonal trace std corrcoef mean """




bhp = np.loadtxt("BHP.csv", delimiter=",", usecols=(6,), unpack=True)
bhp_returans = np.diff(bhp) / bhp[: -1]
print("len(bhp_returans) = ", len(bhp_returans))   #len 29

vale = np.loadtxt("VALE.csv", delimiter=",", usecols=(6,), unpack=True)
vale_returns = np.diff(vale) / vale[: -1]


#协方差公式: sum(x-x均值)(y-y均值) / (n-1)
covariance = np.cov(bhp_returans, vale_returns)
print("Covariance", covariance)
""" 输出: ('Covariance', array([[ 0.00028179, 0.00019766], [ 0.00019766, 0.00030123]])) """

print("Covariance diagonal", covariance.diagonal())
print("Covariance trace", covariance.trace())


#corrcoef : cov(a,b)/ sigma(a) sigma(b)
print("Correlation coefficient", np.corrcoef(bhp_returans, vale_returns))
""" 输出; ('Correlation coefficient', array([[ 1. , 0.67841747], [ 0.67841747, 1. ]])) """


difference = bhp - vale
avg = np.mean(difference)   #输出:58.3683333333
dev = np.std(difference)   #输出:1.88922926672

print("Out of sync", np.abs(difference[-1] - avg) > 2 * dev)

t = np.arange(len(bhp_returans))
plt.plot(t, bhp_returans, lw = 1.0)
plt.plot(t, vale_returns, lw = 2.0)
#plt.show()






print("2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222")
""" 2. Polynomials function: polyfit: fit a set of data points to a polynomial even if the underlying function is not continuous polyval: computer the values of a polynomial roots: returns the roots of the polynomial polyder: gives back the derivative of a polynomial """

import sys

bhp = np.loadtxt("BHP.csv", delimiter=",", usecols=(6,), unpack=True)
vale = np.loadtxt("VALE.csv", delimiter=",", usecols=(6,), unpack=True)

N_order = 100
t = np.arange(len(bhp))
poly = np.polyfit(t, bhp - vale, N_order)
#N_order + 1项系数
print("Polynomial fit", poly)    #输出:('Polynomial fit', array([ 1.11655581e-03, -5.28581762e-02, 5.80684638e-01, 5.79791202e+01]))

print("Next value", np.polyval(poly, t[-1] + 1))   #输出:('Next value', 57.974307608100695)

#Find out when our polynomial fit reaches zero with the roots function
print("Roots", np.roots(poly))   #输出:('Roots', array([ 35.48624287+30.62717062j, 35.48624287-30.62717062j, -23.63210575 +0.j ]))

#Differentiate the polynomial fit with the polyder function
der = np.polyder(poly)
print("Derivative", der)   #输出:('Derivative', array([ 0.00334967, -0.10571635, 0.58068464]))

#Get the roots of the derivative and find the extrema:
print("Extremas", np.roots(der))   #输出:('Extremas', array([ 24.47820054, 7.08205278]))

#判断7,24是否取到极值?
vals = np.polyval(poly, t)
print("np.argmax(vals)", np.argmax(vals))
print("np.argmin(vals)", np.argmin(vals))


plt.plot(t, bhp - vale)
plt.plot(t, vals)
#plt.show()





print("3333333333333333333333333333333333333333333333333333333333333333333333333333333333333")
""" 3. On-balance volume function: diff: computes the difference between two sequential array elements and returns an array containing these differences: sign: The NumPy sign function returns the signs for each element in an array. -1 is returned for a negative number, 1 for a positive number, and 0, otherwise. piecewise: as its name suggests, evaluates a function piece-by-piece. """
c, v = np.loadtxt("BHP.csv", delimiter=",", usecols=(6, 7), unpack=True)
change = np.diff(c)
print("Change", change, "len(Change)", len(change))

signs = np.sign(change)
print("Signs", signs)

#Alternatively, we can calculate the signs with the piecewise function
pieces = np.piecewise(change, [change < 0, change > 0], [-1, 1])
print("Pieces", pieces)   #输出与上面相同


#Check that the outcome is the same: sign函数与pieceswise函数结果是否相同
print("Arrays equal?", np.array_equal(signs, pieces))   #输出:('Arrays equal?', True)

print("On balance volume", v[1:] * signs)





print("44444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444")
""" 4. Simulation function: vectorize: The vectorize function is the NumPy equivalent of the Python map function another way to avoid using loops """
#First, load the data:
o, h, l, c = np.loadtxt("BHP.csv", delimiter=",", usecols=(3, 4, 5, 6), unpack=True)
N = 1;
def calc_profit(open, high, low, close):
    #buy just below the open
    buy = open * float(N)

    #daily range
    if low < buy < high:
        return (close - buy) / buy
    else:
        return 0


func = np.vectorize(calc_profit)
profits = func(o, h, l, c)
print("profits", profits)

real_trades = profits[profits != 0]
print("Number of trades", len(real_trades), round(100.0 * len(real_trades) / len(c), 2), "%")
print("Average profit/loss %", round(np.mean(real_trades) * 100, 2))

winning_trades = profits[profits > 0]
print("Number of winning trades", len(winning_trades), round(100.0 * len(winning_trades) / len(c), 2), "%")
print("Average profit %", round(np.mean(winning_trades) * 100, 2))

losing_trads = profits[profits < 0]
print("Number of losing trades", len(losing_trads), round(100.0 * len(losing_trads) / len(c), 2), "%")
print("Average loss %", round(np.mean(losing_trads) * 100, 2))





print("5555555555555555555555555555555555555555555555555555555555555555555555555555555")
""" 5. Smoothing function: hanning: a windowing function formed by a weighted cosine """

N = 8


#Call the hanning function to compute weights
weights = np.hanning(N)
print("Weights", weights)   #输出:('Weights', array([ 0. , 0.1882551 , 0.61126047, 0.95048443, 0.95048443, 0.61126047, 0.1882551 , 0. ]))
print("sum(weights)", np.sum(weights))   #3.5
""" t = np.arange(len(weights)) plt.clf() plt.plot(t, weights) plt.show() """

#Calculate the stock returns for the BHP and VALE quotes using convolve with normalized weights
bhp = np.loadtxt("BHP.csv", delimiter=",", usecols=(6,), unpack=True)
bhp_returans = np.diff(bhp) / bhp[: -1]
smooth_bhp = np.convolve(weights / weights.sum(), bhp_returans)[N - 1 : -N + 1]
vale = np.loadtxt("VALE.csv", delimiter=",", usecols=(6,), unpack=True)
vale_returns = np.diff(vale) /vale[: -1]
smooth_vale = np.convolve(weights/weights.sum(), vale_returns)[N - 1 : -N + 1]


#Fit the result of the smoothing step to polynomials
K = 30;
t = np.arange(N - 1, len(bhp_returans))
poly_bhp = np.polyfit(t, smooth_bhp, K)
poly_vale = np.polyfit(t, smooth_vale, K)


#Now, we need to compute for a situation where the polynomials we found in the previous step are equal to each other
poly_sub = np.polysub(poly_bhp, poly_vale)
xpoints = np.roots(poly_sub)
print("Intersection points", xpoints)

reals = np.isreal(xpoints)
print("Real number?", reals);

xpoints = np.select([reals], [xpoints])
xpoints = xpoints.real
print("Real intersection points", xpoints)

print("Sans 0s", np.trim_zeros(xpoints))


plt.clf()
plt.plot(t, bhp_returans[N-1:], lw = 1.0)
plt.plot(t, smooth_bhp, lw = 2.0)
plt.show()









你可能感兴趣的:(numpy)