def test_mov_average_expw(self):
        "Test mov_average_expw"
        ser_a = ma.array(range(150), dtype=np.float32)
        ser_b = ser_a.copy()
        # set extreme value to ensure masked values are not impacting result
        ser_b[0] = 5e34
        ser_b[0] = ma.masked
        #
        res_a = mf.mov_average_expw(ser_a, 10)
        res_b = mf.mov_average_expw(ser_b, 10, tol=1e-6)
        assert_almost_equal(res_a[-1], 144.5)
        assert_almost_equal(res_a, res_b)

        # with specified tolerance value, should be 69 masked values in res_b
        assert_equal(np.sum(res_b.mask), 69)
    def test_mov_average_expw(self):
        "Test mov_average_expw"
        ser_a = ma.array(range(150), dtype=np.float32)
        ser_b = ser_a.copy()
        # set extreme value to ensure masked values are not impacting result
        ser_b[0] = 5e34
        ser_b[0] = ma.masked
        #
        res_a = mf.mov_average_expw(ser_a, 10)
        res_b = mf.mov_average_expw(ser_b, 10, tol=1e-6)
        assert_almost_equal(res_a[-1], 144.5)
        assert_almost_equal(res_a, res_b)

        # with specified tolerance value, should be 69 masked values in res_b
        assert_equal(np.sum(res_b.mask), 69)
 def test_mov_average_expw_mask(self):
     "Make sure that mov_average_expw doesn't modify the initial mask"
     N = 256
     series = ts.time_series(np.random.rand(N), start_date=ts.Date("D", "2008-01-01"))
     series[96:128] = ma.masked
     controlmask = np.zeros(N, dtype=bool)
     controlmask[96:128] = True
     #
     test = mf.mov_average_expw(series, 16)
     assert_not_equal(test.mask, series.mask)
     assert_equal(series.mask, controlmask)
 def test_mov_average_expw_mask(self):
     "Make sure that mov_average_expw doesn't modify the initial mask"
     N = 256
     series = ts.time_series(np.random.rand(N),
                             start_date=ts.Date('D', '2008-01-01'))
     series[96:128] = ma.masked
     controlmask = np.zeros(N, dtype=bool)
     controlmask[96:128] = True
     #
     test = mf.mov_average_expw(series, 16)
     assert_not_equal(test.mask, series.mask)
     assert_equal(series.mask, controlmask)
Exemple #5
0
def make_memory_graph(ma=False):
	clf()
	values = load("memory.average*.json")
	for test in values:
		instance_count = len(test['instances'])
		t = map(lambda x: x['used'], test['instances'])

		def sum_s(*args):
			return reduce(lambda x, y: x+int(y), args, 0)

		m = min(map(len, t))
		for i in t:
			del i[m:]

		test['results'] = map(sum_s, *t)
		print len(test['instances'][0]['used'])

		test['x'] = range(0, instance_count*len(test['results'])*1000, instance_count*1000)
		
		linestyles = ['-','--','-.',':']	


	for i in range(0, len(values)):
		x = values[i]['x']
		y = values[i]['results']

		if ma:
			y = mov_average_expw(y, 2000)

		gb = 1.0 * 1024**3
		y = map(lambda x: x/gb, y)

		x = map(lambda x: 1.0*x/1000000, x)


		r = plot(x, y, label="%s instances" % (len(values[i]['instances'])), color='k', linestyle=linestyles[i])
		
	legend(loc='upper left')
import numpy as np
import matplotlib.pyplot as plt
import scikits.timeseries as ts
import scikits.timeseries.lib.plotlib as tpl
from scikits.timeseries.lib.moving_funcs import mov_average_expw

# generate some random data
data = np.cumprod(1 + np.random.normal(0, 1, 300) / 100)
series = ts.time_series(data, start_date=ts.Date(freq='M', year=1982, month=1))
fig = tpl.tsfigure()
fsp = fig.add_tsplot(111)
fsp.tsplot(series, '-', mov_average_expw(series, 40), 'r--')
plt.show()
Exemple #7
0
import numpy as np
from scikits.timeseries import *
from scikits.timeseries.lib.moving_funcs import mov_mean, mov_average_expw
import utilfuncs.finutils as fn; reload(fn)
from matplotlib.pyplot import plot

data = np.arange(0, 100)
dates = np.arange(0, 100)
win = 20

### compare movavg calc to timeseries movmean:
# ts mov_mean
t = time_series(data=data, dates=dates)
tMM = mov_mean(t, win)

# finutils movingaverage (with leading zeros, like timeseries)
fMA = np.array([0 if i < win-1 else np.mean(data[i-win+1:i+1]) for i in dates])

print 'moving average errors: %f' % (sum(tMM!=fMA))

### compare exp movavg calc to timeseries expw movavg
# ts expw
tEXP = mov_average_expw(t, win)

# finutils
fEXP = fn.movingema(t, win, 0.0952381)

print 'exp moving average errors: %f' % (sum(tEXP!=fEXP))

plot(tEXP-fEXP, '-g')
import numpy as np
import matplotlib.pyplot as plt
import scikits.timeseries as ts
import scikits.timeseries.lib.plotlib as tpl
from scikits.timeseries.lib.moving_funcs import mov_average_expw

# generate some random data
data = np.cumprod(1 + np.random.normal(0, 1, 300) / 100)
series = ts.time_series(data, start_date=ts.Date(freq="M", year=1982, month=1))
fig = tpl.tsfigure()
fsp = fig.add_tsplot(111)
fsp.tsplot(series, "-", mov_average_expw(series, 40), "r--")
plt.show()
Exemple #9
0
from scikits.timeseries.lib.moving_funcs import mov_mean, mov_average_expw
import utilfuncs.finutils as fn

reload(fn)
from matplotlib.pyplot import plot

data = np.arange(0, 100)
dates = np.arange(0, 100)
win = 20

### compare movavg calc to timeseries movmean:
# ts mov_mean
t = time_series(data=data, dates=dates)
tMM = mov_mean(t, win)

# finutils movingaverage (with leading zeros, like timeseries)
fMA = np.array(
    [0 if i < win - 1 else np.mean(data[i - win + 1:i + 1]) for i in dates])

print 'moving average errors: %f' % (sum(tMM != fMA))

### compare exp movavg calc to timeseries expw movavg
# ts expw
tEXP = mov_average_expw(t, win)

# finutils
fEXP = fn.movingema(t, win, 0.0952381)

print 'exp moving average errors: %f' % (sum(tEXP != fEXP))

plot(tEXP - fEXP, '-g')