Skip to content

Instantly share code, notes, and snippets.

@renyuanL
Last active May 11, 2016 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renyuanL/dc14110bd474858f6e00f1ac75713b28 to your computer and use it in GitHub Desktop.
Save renyuanL/dc14110bd474858f6e00f1ac75713b28 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
Created on Tue May 10 14:15:38 2016
@author: csie3
financial data is here:
https://www.franklin.com.tw/Fund/NavHistory/101
===> f101.csv
"""
import pandas as pd
import pylab as pl
import thinkdsp as td
import numpy as np
def serial_corr(wave, lag=1):
N = len(wave)
y1 = wave.ys[lag:]
y2 = wave.ys[:N-lag]
corr = np.corrcoef(y1, y2, ddof=0)[0, 1]
return corr
def autocorr(wave):
"""Computes and plots the autocorrelation function.
wave: Wave
"""
lags = range(len(wave.ys))#//2)
corrs = [serial_corr(wave, lag) for lag in lags]
return lags, corrs
import numpy as np
def movingAverage(x, length):
y= np.convolve(x, np.ones(length)/length)
y= y[:len(x)]
return y
def ryAutoCorr(fn):
df= pd.read_csv(fn) #'f101.csv')#, parse_dates=[0])
ys= df.淨值.values
ys= ys[-1::-1]
wv= td.Wave(ys)
corr= serial_corr(wv)
R= autocorr(wv)
pl.subplot(3,1,1)
pl.plot(ys)
pl.title(fn)
pl.grid()
pl.subplot(3,1,2)
pl.plot(R[0], R[1])
pl.grid()
ma100= movingAverage(wv.ys, 100)
pl.subplot(3,1,3)
pl.plot(ma100)
fL= ['f101.csv']#,'f102.csv','f103.csv']
for fn in fL:
pl.figure()
ryAutoCorr(fn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment