卷积
激励函数:g(t)
单位激励下的响应函数:f(t)
绘制时间(t)与痛感(h)的函数关系图。
a = [1 2 3 4 5] 原数组 b = [8 7 6] 卷积核数组 使用b作为卷积核数组对a数组执行卷积运算的过程如下: 44 65 86 有效卷积结果(valid):比原数组短 23 44 65 86 59 同维卷积结果(same):卷积核中心元素 8 23 44 65 86 59 30 完全卷积结果(full) 0 0 1 2 3 4 5 0 0 6 7 8 6 7 8 6 7 8 6 7 8 6 7 8 6 7 8 6 7 8 c = numpy.convolve(a, b, 卷积类型)
5日移动均线序列可以直接使用卷积实现
a = [a, b, c, d, e, f, g, h, i, j]
b = [1/5, 1/5, 1/5, 1/5, 1/5]
使用卷积函数numpy.convolve(a, b, 卷积类型)实现5日均线
sma52 = np.convolve( closing_prices, np.ones(5) / 5, 'valid') mp.plot(dates[4:], sma52, c='limegreen', alpha=0.5, linewidth=6, label='SMA-5(2)')
# 卷积移动平均线 import numpy as np import matplotlib.pyplot as mp import datetime as dt import matplotlib.dates as md def dmy2ymd(dmy): """ 把日月年转年月日 :param day: :return: """ dmy = str(dmy, encoding='utf-8') t = dt.datetime.strptime(dmy, '%d-%m-%Y') s = t.date().strftime('%Y-%m-%d') return s dates, opening_prices, \ highest_prices, lowest_prices, \ closing_prices = \ np.loadtxt('aapl.csv', delimiter=',', usecols=(1, 3, 4, 5, 6), unpack=True, dtype='M8[D],f8,f8,f8,f8', converters={1: dmy2ymd}) # 日月年转年月日 # 绘制收盘价的折现图 mp.figure('APPL', facecolor='lightgray') mp.title('APPL', fontsize=18) mp.xlabel('Date', fontsize=14) mp.ylabel('Price', fontsize=14) mp.grid(linestyle=":") # 设置刻度定位器 # 每周一一个主刻度,一天一个次刻度 ax = mp.gca() ma_loc = md.WeekdayLocator(byweekday=md.MO) ax.xaxis.set_major_locator(ma_loc) ax.xaxis.set_major_formatter(md.DateFormatter('%Y-%m-%d')) ax.xaxis.set_minor_locator(md.DayLocator()) # 修改dates的dtype为md.datetime.datetiem dates = dates.astype(md.datetime.datetime) mp.plot(dates, closing_prices, color='dodgerblue', linewidth=2, linestyle='--', alpha=0.8, label='APPL Closing Price') #绘制5日移动平均线 ma5 = np.zeros(closing_prices.size-4) for i in range(ma5.size): ma5[i] =np.mean(closing_prices[i:i+5]) mp.plot(dates[4:],ma5,color='orangered',label='MA5') #基于卷积绘制5日均线 kernel = np.ones(5)/5 ma52 = np.convolve( closing_prices,kernel,'valid' ) mp.plot(dates[4:],ma52,color='orangered', label='MA-52',linewidth=7,alpha=0.3) #绘制10日移动平均线 ma10 = np.zeros(closing_prices.size-9) for i in range(ma10.size): ma10[i] =np.mean(closing_prices[i:i+10]) mp.plot(dates[9:],ma10,color='green',label='MA10') #基于卷积绘制10日移动平均线 kernel = np.ones(10)/10 ma102 = np.convolve(closing_prices,kernel,'valid') mp.plot(dates[9:],ma102,color='green', label='MA-10',linewidth=7,alpha=0.3) mp.legend() mp.gcf().autofmt_xdate() mp.show()
加权卷积
使用卷积函数numpy.convolve(a, b, 卷积类型)实现加权5日均线
weights = np.exp(np.linspace(-1, 0, 5)) weights /= weights.sum() ema5 = np.convolve(closing_prices, weights[::-1], 'valid') mp.plot(dates[4:], sma52, c='limegreen', alpha=0.5, linewidth=6, label='SMA-5')
# 卷积移动平均线 import numpy as np import matplotlib.pyplot as mp import datetime as dt import matplotlib.dates as md def dmy2ymd(dmy): """ 把日月年转年月日 :param day: :return: """ dmy = str(dmy, encoding='utf-8') t = dt.datetime.strptime(dmy, '%d-%m-%Y') s = t.date().strftime('%Y-%m-%d') return s dates, opening_prices, \ highest_prices, lowest_prices, \ closing_prices = \ np.loadtxt('aapl.csv', delimiter=',', usecols=(1, 3, 4, 5, 6), unpack=True, dtype='M8[D],f8,f8,f8,f8', converters={1: dmy2ymd}) # 日月年转年月日 # 绘制收盘价的折现图 mp.figure('APPL', facecolor='lightgray') mp.title('APPL', fontsize=18) mp.xlabel('Date', fontsize=14) mp.ylabel('Price', fontsize=14) mp.grid(linestyle=":") # 设置刻度定位器 # 每周一一个主刻度,一天一个次刻度 ax = mp.gca() ma_loc = md.WeekdayLocator(byweekday=md.MO) ax.xaxis.set_major_locator(ma_loc) ax.xaxis.set_major_formatter(md.DateFormatter('%Y-%m-%d')) ax.xaxis.set_minor_locator(md.DayLocator()) # 修改dates的dtype为md.datetime.datetiem dates = dates.astype(md.datetime.datetime) mp.plot(dates, closing_prices, color='dodgerblue', linewidth=2, linestyle='--', alpha=0.8, label='APPL Closing Price') #基础卷积实现5日加权平均线 #寻找一组卷积核 kernel = np.exp(np.linspace(-1,0,5)) #卷积核中所有元素之和=1 kernel/=kernel.sum() print(kernel) ema53 = np.convolve(closing_prices,kernel[::-1],'valid') mp.plot(dates[4:],ema53,color='red', label='EMA-53') mp.legend() mp.gcf().autofmt_xdate() mp.show()