Data smoothing

Data smoothing

Smoothing processing generally comprises a noise data fitting and other operations. Intended for purposes of noise reduction features remove additional factors, intended to fit the mathematical modeling, mathematical methods can be more identifying characteristic curve.

Case: Draw two stocks yield curve. Yield = (the day after the closing price - closing price of the previous day) / closing price of the previous day

 

  Complete data using convolution noise.

# 数据平滑
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, bhp_closing_prices = \
  np.loadtxt ( ' bhp.csv ' , 
             DELIMITER = ' , ' , 
             usecols = (. 1,. 6 ), 
             the unpack = True, 
             DTYPE = ' M8 [D], F8 ' , 
             Converters = {. 1: dmy2ymd})   # sun and the moon in turn date 
vale_closing_prices = \ 
  np.loadtxt ( ' vale.csv ' , 
             DELIMITER = ' , ' , 
             usecols = (6 ,),
             unpack = True)   # because, like the date, the date is not read here 
# Print (a dates) 
# drawn closing price discount FIG 
mp.figure ( ' APPL ' , facecolor = ' LightGray ' ) 
mp.title ( ' APPL ' , = 18 is fontSize ) 
mp.xlabel ( ' a Date ' , fontSize = 14 ) 
mp.ylabel ( ' Price ' , fontSize = 14 ) 
mp.grid (lineStyle = " : " ) 

# set scale locator 
# weekly one main scale, one day a time scale 

ax= Mp.gca () 
ma_locMd.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 ()) 
# modification dates of the dtype md.datetime.datetiem 
dates = dates.astype (md.datetime.datetime) 

# calculation yields two stocks, and plotted 
bhp_returns = np.diff (bhp_closing_prices) / bhp_closing_prices [: -1 ] 
vale_returns = np.diff (vale_closing_prices) / vale_closing_prices [: -. 1 ] 
mp.plot (a dates [ . 1:], bhp_returns, Color = ' Red ' , Alpha = 0.1, label = ' BHP Returns')
mp.plot(dates[1:], vale_returns, color='blue',alpha=0.1, label='vale returns')

#卷积降噪
kernel = np.hanning(8)
kernel/=kernel.sum()
bhp_convalved = np.convolve(bhp_returns,kernel,'valid')
vale_convalved = np.convolve(vale_returns,kernel,'valid')
mp.plot(dates[8:],bhp_convalved,color='dodgerblue',alpha=0.8,label='bhp convalved')
mp.plot(dates[8:],vale_convalved,color='orangered',alpha=0.8,label='vale convalved')

mp.legend()
mp.gcf().autofmt_xdate()
mp.show()

 

  The treated stock returns do polynomial fit.

# 数据平滑
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, bhp_closing_prices = \
  np.loadtxt (  = (6 ,),' Bhp.csv ' , 
             DELIMITER = ' , ' , 
             usecols = (. 1,. 6 ), 
             the unpack = True, 
             DTYPE = ' M8 [D], F8 ' , 
             Converters = {. 1: dmy2ymd})   # DDMMYY turn years day 
vale_closing_prices = \ 
  np.loadtxt ( ' vale.csv ' , 
             DELIMITER = ' , ' , 
             usecols 
             unpack = True)   # because, like the date, the date is not read here 
# Print (a dates) 
# drawn closing price discount FIG 
mp.figure ( ' APPL ' , facecolor = ' LightGray ' ) 
mp.title ( ' APPL ' , = 18 is fontSize ) 
mp.xlabel ( ' a Date ' , fontSize = 14 ) 
mp.ylabel ( ' Price ' , fontSize = 14 ) 
mp.grid (lineStyle = " : " ) 

# set scale locator 
# weekly one main scale, one day a time scale 

ax= Mp.gca () 
ma_locMd.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 ()) 
# modification dates of the dtype md.datetime.datetiem 
dates = dates.astype (md.datetime.datetime) 

# calculation yields two stocks, and plotted 
bhp_returns = np.diff (bhp_closing_prices) / bhp_closing_prices [: -1 ] 
vale_returns = np.diff (vale_closing_prices) / vale_closing_prices [: -. 1 ] 
mp.plot (a dates [ . 1:], bhp_returns, Color = ' Red ' , Alpha = 0.1, label = ' BHP Returns')
mp.plot(dates[1:], vale_returns, color='blue',alpha=0.1, label='vale returns')

#卷积降噪
kernel = np.hanning(8)
kernel/=kernel.sum()
bhp_convalved = np.convolve(bhp_returns,kernel,'valid')
vale_convalved = np.convolve(vale_returns,kernel,'valid')
mp.plot(dates[8:],bhp_convalved,color='dodgerblue',alpha=0.1,label='bhp convalved')
mp.plot(dates[8:],vale_convalved,color='orangered',alpha=0.1,label='vale convalved')

#多项式拟合
days = dates[8:].astype('M8[D]').astype('i4')
bhp_p = np.polyfit(days,bhp_convalved,3)
bhp_val = np.polyval(bhp_p,days)
vale_p = np.polyfit(days,vale_convalved,3)
vale_val = np.polyval(vale_p,days)
mp.plot(dates[8:],bhp_val,color='orangered',label='bhp polyval')
mp.plot(dates[8:],vale_val,color='blue',label='vale polyval')

mp.legend()
mp.gcf().autofmt_xdate()
mp.show()

  Investment income can be analyzed than two stocks by taking the focus of the two functions.

# 数据平滑
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, bhp_closing_prices = \
  np.loadtxt('bhp.csv',
             delimiter=',',
             usecols=(1, 6),
             unpack=True,
             dtype='M8[D],f8',
             converters={1: dmy2ymd})  # 日月年转年月日
vale_closing_prices = \
  np.loadtxt('vale.csv',
             delimiter=',',
             usecols=(6,),
             unpack=True)  # 因为日期一样,所以此处不读日期
# print(dates)
# 绘制收盘价的折现图
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)

# 计算两只股票的收益率,并绘制曲线
bhp_returns = np.diff(bhp_closing_prices) / bhp_closing_prices[:-1]
vale_returns = np.diff(vale_closing_prices) / vale_closing_prices[:-1]
mp.plot(dates[1:], bhp_returns, color='red', alpha=0.1,label='bhp returns')
mp.plot(dates[1:], vale_returns, color='blue',alpha=0.1, label='vale returns')

#卷积降噪
kernel = np.hanning(8)
kernel/=kernel.sum()
bhp_convalved = np.convolve(bhp_returns,kernel,'valid')
vale_convalved = np.convolve(vale_returns,kernel,'valid')
mp.plot(dates[8:],bhp_convalved,color='dodgerblue',alpha=0.1,label='bhp convalved')
mp.plot(dates[8:],vale_convalved,color='orangered',alpha=0.1,label='vale convalved')

#多项式拟合
days = dates[8:].astype('M8[D]').astype('i4')

bhp_p = np.polyfit(days,bhp_convalved,3)
bhp_val = np.polyval(bhp_p,days)
vale_p = np.polyfit(days,vale_convalved,3)
vale_val = np.polyval(vale_p,days)
mp.plot(dates[8:],bhp_val,color='orangered',label='bhp polyval')
mp.plot(dates[8:],vale_val,color='blue',label='vale polyval')


#求两个多项式函数的焦点
diff_p = np.polysub(bhp_p,vale_p)
xs = np.roots(diff_p)
print(xs.astype('M8[D]'))
#['2011-03-23' '2011-03-11' '2011-02-21']


mp.legend()
mp.gcf().autofmt_xdate()
mp.show()

 

Guess you like

Origin www.cnblogs.com/maplethefox/p/11468970.html