Last active
May 11, 2016 03:16
-
-
Save renyuanL/dc14110bd474858f6e00f1ac75713b28 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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