Skip to content

Instantly share code, notes, and snippets.

@igorcoding
Created April 18, 2015 19:00
Show Gist options
  • Save igorcoding/9f4eb7d9b24a9241cb3c to your computer and use it in GitHub Desktop.
Save igorcoding/9f4eb7d9b24a9241cb3c to your computer and use it in GitHub Desktop.
Some time series analysing
from pprint import pprint
import math
from scipy.optimize import curve_fit
import numpy as np
import matplotlib.pyplot as plt
import pylab
import scipy.stats as stats
import sklearn.linear_model
def read_file(filename):
arr = []
with open(filename) as f:
lines = f.readlines()
for l in lines:
arr.append(float(l))
return np.asarray(arr)
def prepare_powers(ds, max_power=1):
new_ds = []
for x in ds:
t = []
for p in xrange(1, max_power + 1):
t.append(x ** p)
new_ds.append(tuple(t))
return new_ds
def main():
files = ['data/time series 1.txt',
'data/time series 2.txt',
'data/time series 3.txt']
series = [read_file(f) for f in files]
covs = []
for i in xrange(0, len(series)):
for j in xrange(0, i):
covs.append((i, j, np.cov(series[i], series[j])))
print 'covs: ',
pprint(covs)
angles = []
for i in xrange(0, len(series)):
for j in xrange(0, i):
angle = math.acos(np.dot(series[i], series[j]) / (np.linalg.norm(series[i]) * np.linalg.norm(series[j])))
angles.append((i, j, math.degrees(angle)))
print 'angles: ',
pprint(angles)
dists = []
for i in xrange(0, len(series)):
for j in xrange(0, i):
dist = np.linalg.norm(series[i] - series[j])
dists.append((i, j, dist))
print 'dists: ',
pprint(dists)
momenta = []
for i in xrange(0, len(series)):
momenta.append((i, np.mean(series[i]), np.var(series[i])))
print 'momenta: ',
pprint(momenta)
normaltest = [stats.normaltest(s) for s in series]
print 'normaltest: ',
pprint(normaltest)
def garmonic(x, a, b, c, d):
return a * np.exp(-b * x) * np.sin(c * x + d)
xdata = range(0, len(series[0] + 1))
garmonic_fit = [curve_fit(garmonic, xdata, s)[0] for s in series]
# 4437.42 * exp(-8.22 x) * sin(x)
# 26337.27 * exp(-10 x) * sin(x)
# 4966.55 * exp(-8.34 x) * sin(x)
pprint(garmonic_fit)
# plt.figure(1)
# plt.plot(xrange(1, len(series[0]) + 1), series[0])
# plt.figure(2)
# plt.plot(xrange(1, len(series[0]) + 1), series[1])
# plt.figure(3)
# plt.plot(xrange(1, len(series[0]) + 1), series[2])
# plt.show()
fig = plt.figure(1)
ax1 = fig.add_subplot(111)
ax1.scatter(series[0], series[1], s=10, c='b', marker="s", label='0-1')
fig = plt.figure(2)
ax1 = fig.add_subplot(111)
ax1.scatter(series[0], series[2], s=10, c='r', marker="o", label='0-2')
fig = plt.figure(3)
ax1 = fig.add_subplot(111)
ax1.scatter(series[1], series[2], s=10, c='g', marker="o", label='1-2')
plt.legend(loc='upper left')
# fig = plt.figure(4)
# ax1 = fig.add_subplot(111)
# ax1.scatter(series[0], series[0], s=10, c='g', marker="o", label='1-2')
#
# fig = plt.figure(5)
# ax1 = fig.add_subplot(111)
# ax1.scatter(series[1], series[1], s=10, c='g', marker="o", label='1-2')
#
# fig = plt.figure(6)
# ax1 = fig.add_subplot(111)
# ax1.scatter(series[2], series[2], s=10, c='g', marker="o", label='1-2')
# plt.figure(7)
# pylab.hist(series[0], 70, normed=1, histtype='stepfilled')
# plt.figure(8)
# pylab.hist(series[1], 70, normed=1, histtype='stepfilled')
# plt.figure(9)
# pylab.hist(series[2], 70, normed=1, histtype='stepfilled')
# pylab.show()
if __name__ == '__main__':
main()
1
1
-0.3
-1.47
-0.222
1.9503
0.37743
-1.907892
-0.5692677
1.32398613
1.217259978
-0.7482525957
-2.09591716317
0.557573306148
2.5332029763063
-0.41299510656447
-2.270211935411382
-0.284849048720261
1.813855029420052
1.503844335063899
-1.734315867964514
-2.495961689357519
1.824348645042067
2.721695197162804
-1.320921971046312
-2.554994045146764
-0.026746295309852
2.742314668139452
1.497740486669035
-3.285297273635806
-2.267211999018524
3.307960879142266
2.481849649542121
-2.199392730293348
-3.022820554500713
0.515063537655649
4.182942696576022
0.677224045130494
-5.068781540279115
-1.210584208237912
4.666692006377914
2.032811147249325
-3.207732694067529
-3.806735938004257
1.908067730383172
5.767529947020673
-1.314845689590112
-6.516084416638196
0.510672001891907
5.803836548227688
1.597790858701936
-4.85211715157803
-4.567757574706758
4.626049039611042
6.714616232442648
-4.367843118547606
-7.190315770183886
2.534063633510611
7.042300703456615
0.978327009209852
-7.655971122604015
-4.395117564288755
8.597407562738605
6.217578228517496
1
1
-1.6
1.129999999999999
-0.287
-1.2932
2.357329999999999
-0.837276999999999
-1.811736200000001
2.26821253
-0.683703306999999
-0.685394729200001
1.64687919673
-2.144111234536999
0.464551127607798
2.51388276614393
-2.845148086057965
0.095332824532872
1.964230401379026
-1.993707511977766
1.423528422339479
0.216331945300207
-2.952210023896337
3.124836170080762
0.566219772460884
-3.384067929198876
2.331886937719843
-0.188718498201921
-1.290754228028658
3.009640118278857
-2.857080949996001
-1.246895760079954
4.66558052612907
-2.614857712669613
-1.443014195150902
2.779810761412477
-2.645085823597589
1.80369409717413
1.938702929500588
-5.447604861119421
2.71461317928761
3.226901385416291
-4.577043895698987
1.913074825377032
0.177013515625012
-2.695091506413372
5.359775595292189
-2.400833930708086
-4.828053910310977
6.392874898657372
-0.94904340304199
-3.033534592928097
3.612987026692878
-4.128012256988793
1.358115121164959
5.90047369320695
-7.734651618844785
-0.086339417212656
6.441383278497989
-4.770411215894764
1.689298494375667
0.739959788625714
-6.192576846794354
7.951819427062465
1
1
-1
-1.2
0.98
1.418
-0.9362
-1.653420000000001
0.864478
1.905209800000001
-0.76040482
-2.171771262000001
0.6192681758
2.450875205780001
-0.436107472802
-2.739573473638201
0.20576087271838
3.034106908273859
0.077073730837168
-3.329810226017529
-0.417762126522637
3.621015035967018
0.821639842771603
-3.90095255528656
-1.293899082577419
4.161657902557475
1.839454781090909
-4.393878214704133
-2.462788080670413
4.586987228107505
3.167765611548205
-4.728909389763436
-3.95743311167937
4.806057017571843
4.833782124604491
-4.803284506868578
-5.797488787751798
4.703864078780256
6.847624074405004
-4.489488079217782
-7.981335289767284
4.140303358162833
9.193499154560296
-3.634983778523087
-10.476347447868635
2.950847411588533
11.819066933814351
-2.064025459365951
-13.207376173132381
0.949690387989308
14.623082829244552
0.417648856136217
-16.04362622655539
-2.063776364405378
17.44161121277039
4.014315122122955
-18.784340821835137
-6.294180716518765
20.033356832366774
8.92693447140732
-21.14399906846272
-11.934027825394324
22.06499619276956
15.333930227210713
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment