diff --git "a/Flask Web\345\274\200\345\217\221\357\274\232\345\237\272\344\272\216Python\347\232\204Web\345\272\224\347\224\250\345\274\200\345\217\221\345\256\236\346\210\230.pdf" "b/Flask Web\345\274\200\345\217\221\357\274\232\345\237\272\344\272\216Python\347\232\204Web\345\272\224\347\224\250\345\274\200\345\217\221\345\256\236\346\210\230.pdf" new file mode 100644 index 0000000..50343ab Binary files /dev/null and "b/Flask Web\345\274\200\345\217\221\357\274\232\345\237\272\344\272\216Python\347\232\204Web\345\272\224\347\224\250\345\274\200\345\217\221\345\256\236\346\210\230.pdf" differ diff --git a/NumPy/Code/ch10code/README b/NumPy/Code/ch10code/README new file mode 100644 index 0000000..0360b53 --- /dev/null +++ b/NumPy/Code/ch10code/README @@ -0,0 +1,19 @@ +NumPy Beginner's Guide + +Chapter 10 When Numpy is not enough: Scipy and beyond + +The example code requires SciPy and Matplotlib. + +Run the code examples with + +python + +frequencies.py Demonstrates FFT transforms. +gaussquad.py Demonstrates numerical integration. +images.py Demonstrates image manipulation. +optfit.py Demonstrates optimization. +pair.py Demonstrates two samples statistical tests. +scipyio.py Demonstrates Matlab/Octave interoperability. +sincinterp.py Demonstrates interpolation. +statistics.py Demonstrates statistical functionality. +trend.py Demonstrates the detrend function. diff --git a/NumPy/Code/ch10code/a.mat b/NumPy/Code/ch10code/a.mat new file mode 100644 index 0000000..7d0b84a Binary files /dev/null and b/NumPy/Code/ch10code/a.mat differ diff --git a/NumPy/Code/ch10code/algebra.py b/NumPy/Code/ch10code/algebra.py new file mode 100644 index 0000000..f1ae452 --- /dev/null +++ b/NumPy/Code/ch10code/algebra.py @@ -0,0 +1,61 @@ +from matplotlib.finance import quotes_historical_yahoo +from datetime import date +import numpy as np +import matplotlib.pyplot as plt +from scipy import fftpack +from scipy import signal +from matplotlib.dates import DateFormatter, DayLocator, MonthLocator +from scipy import optimize + + +today = date.today() +start = (today.year - 1, today.month, today.day) + +quotes = quotes_historical_yahoo("QQQ", start, today) +quotes = np.array(quotes) + +dates = quotes.T[0] +qqq = quotes.T[4] + + +y = signal.detrend(qqq) + + +alldays = DayLocator() +months = MonthLocator() +month_formatter = DateFormatter("%b %Y") + +fig = plt.figure() +ax = fig.add_subplot(211) + +ax.xaxis.set_minor_locator(alldays) +ax.xaxis.set_major_locator(months) +ax.xaxis.set_major_formatter(month_formatter) + +amps = np.abs(fftpack.fftshift(fftpack.rfft(y))) +amps[amps < amps.max()] = 0 + +def residuals(p, y, x): + A,k,theta = p + err = y-A * np.sin(2* np.pi* k * x + theta) + + return err + +filtered = -fftpack.irfft(fftpack.ifftshift(amps)) +N = len(qqq) +f = np.linspace(-N/2, N/2, N) +p0 = [filtered.max(), f[amps.argmax()]/N, np.pi/3] +plsq = optimize.leastsq(residuals, p0, args=(filtered, dates)) +p = plsq[0] +print p +plt.plot(dates, y, 'o', label="detrended") +plt.plot(dates, filtered, label="filtered") +plt.plot(dates, p[0] * np.sin(2 * np.pi * dates * p[1] + p[2]), '^', label="fit") +fig.autofmt_xdate() +plt.legend() + +ax2 = fig.add_subplot(212) +plt.plot(f, amps, label="transformed") + +plt.legend() +plt.show() diff --git a/NumPy/Code/ch10code/frequencies.py b/NumPy/Code/ch10code/frequencies.py new file mode 100644 index 0000000..a8cd07e --- /dev/null +++ b/NumPy/Code/ch10code/frequencies.py @@ -0,0 +1,54 @@ +from matplotlib.finance import quotes_historical_yahoo +from datetime import date +import numpy as np +from scipy import signal +import matplotlib.pyplot as plt +from scipy import fftpack +from matplotlib.dates import DateFormatter +from matplotlib.dates import DayLocator +from matplotlib.dates import MonthLocator + + +today = date.today() +start = (today.year - 1, today.month, today.day) + +quotes = quotes_historical_yahoo("QQQ", start, today) +quotes = np.array(quotes) + +dates = quotes.T[0] +qqq = quotes.T[4] + + +y = signal.detrend(qqq) + + +alldays = DayLocator() +months = MonthLocator() +month_formatter = DateFormatter("%b %Y") + +fig = plt.figure() +fig.subplots_adjust(hspace=.3) +ax = fig.add_subplot(211) + +ax.xaxis.set_minor_locator(alldays) +ax.xaxis.set_major_locator(months) +ax.xaxis.set_major_formatter(month_formatter) + +# make font size bigger +ax.tick_params(axis='both', which='major', labelsize='x-large') + +amps = np.abs(fftpack.fftshift(fftpack.rfft(y))) +amps[amps < 0.1 * amps.max()] = 0 + +plt.plot(dates, y, 'o', label="detrended") +plt.plot(dates, -fftpack.irfft(fftpack.ifftshift(amps)), label="filtered") +fig.autofmt_xdate() +plt.legend(prop={'size':'x-large'}) + +ax2 = fig.add_subplot(212) +ax2.tick_params(axis='both', which='major', labelsize='x-large') +N = len(qqq) +plt.plot(np.linspace(-N/2, N/2, N), amps, label="transformed") + +plt.legend(prop={'size':'x-large'}) +plt.show() diff --git a/NumPy/Code/ch10code/gaussquad.py b/NumPy/Code/ch10code/gaussquad.py new file mode 100644 index 0000000..d9ebaff --- /dev/null +++ b/NumPy/Code/ch10code/gaussquad.py @@ -0,0 +1,4 @@ +from scipy import integrate +import numpy as np + +print "Gaussian integral", np.sqrt(np.pi), integrate.quad(lambda x: np.exp(-x**2), -np.inf, np.inf) diff --git a/NumPy/Code/ch10code/images.py b/NumPy/Code/ch10code/images.py new file mode 100644 index 0000000..b5c41e8 --- /dev/null +++ b/NumPy/Code/ch10code/images.py @@ -0,0 +1,31 @@ +from scipy import misc +import numpy as np +import matplotlib.pyplot as plt +from scipy import ndimage + + +image = misc.lena().astype(np.float32) + +plt.subplot(221) +plt.title("Original Image") +img = plt.imshow(image, cmap=plt.cm.gray) +plt.axis("off") + +plt.subplot(222) +plt.title("Median Filter") +filtered = ndimage.median_filter(image, size=(42,42)) +plt.imshow(filtered, cmap=plt.cm.gray) +plt.axis("off") + +plt.subplot(223) +plt.title("Rotated") +rotated = ndimage.rotate(image, 90) +plt.imshow(rotated, cmap=plt.cm.gray) +plt.axis("off") + +plt.subplot(224) +plt.title("Prewitt Filter") +filtered = ndimage.prewitt(image) +plt.imshow(filtered, cmap=plt.cm.gray) +plt.axis("off") +plt.show() diff --git a/NumPy/Code/ch10code/optfit.py b/NumPy/Code/ch10code/optfit.py new file mode 100644 index 0000000..de80aa3 --- /dev/null +++ b/NumPy/Code/ch10code/optfit.py @@ -0,0 +1,67 @@ +from matplotlib.finance import quotes_historical_yahoo +import numpy as np +import matplotlib.pyplot as plt +from scipy import fftpack +from scipy import signal +from matplotlib.dates import DateFormatter +from matplotlib.dates import DayLocator +from matplotlib.dates import MonthLocator +from scipy import optimize + + +start = (2010, 7, 25) +end = (2011, 7, 25) + +quotes = quotes_historical_yahoo("QQQ", start, end) +quotes = np.array(quotes) + +dates = quotes.T[0] +qqq = quotes.T[4] + + +y = signal.detrend(qqq) + + +alldays = DayLocator() +months = MonthLocator() +month_formatter = DateFormatter("%b %Y") + +fig = plt.figure() +fig.subplots_adjust(hspace=.3) +ax = fig.add_subplot(211) + +ax.xaxis.set_minor_locator(alldays) +ax.xaxis.set_major_locator(months) +ax.xaxis.set_major_formatter(month_formatter) +ax.tick_params(axis='both', which='major', labelsize='x-large') + +amps = np.abs(fftpack.fftshift(fftpack.rfft(y))) +amps[amps < amps.max()] = 0 + +def residuals(p, y, x): + A,k,theta,b = p + err = y-A * np.sin(2* np.pi* k * x + theta) + b + + return err + +filtered = -fftpack.irfft(fftpack.ifftshift(amps)) +N = len(qqq) +f = np.linspace(-N/2, N/2, N) +p0 = [filtered.max(), f[amps.argmax()]/(2*N), 0, 0] +print "P0", p0 + +plsq = optimize.leastsq(residuals, p0, args=(filtered, dates)) +p = plsq[0] +print "P", p +plt.plot(dates, y, 'o', label="detrended") +plt.plot(dates, filtered, label="filtered") +plt.plot(dates, p[0] * np.sin(2 * np.pi * dates * p[1] + p[2]) + p[3], '^', label="fit") +fig.autofmt_xdate() +plt.legend(prop={'size':'x-large'}) + +ax2 = fig.add_subplot(212) +ax2.tick_params(axis='both', which='major', labelsize='x-large') +plt.plot(f, amps, label="transformed") + +plt.legend(prop={'size':'x-large'}) +plt.show() diff --git a/NumPy/Code/ch10code/pair.py b/NumPy/Code/ch10code/pair.py new file mode 100644 index 0000000..6b1dde5 --- /dev/null +++ b/NumPy/Code/ch10code/pair.py @@ -0,0 +1,30 @@ +from matplotlib.finance import quotes_historical_yahoo +from datetime import date +import numpy as np +from scipy import stats +from statsmodels.stats.stattools import jarque_bera +import matplotlib.pyplot as plt + + +def get_close(symbol): + today = date.today() + start = (today.year - 1, today.month, today.day) + + quotes = quotes_historical_yahoo(symbol, start, today) + quotes = np.array(quotes) + + return quotes.T[4] + +spy = np.diff(np.log(get_close("SPY"))) +dia = np.diff(np.log(get_close("DIA"))) + +print "Means comparison", stats.ttest_ind(spy, dia) +print "Kolmogorov smirnov test", stats.ks_2samp(spy, dia) + +print "Jarque Bera test", jarque_bera(spy - dia)[1] + +plt.hist(spy, histtype="step", lw=1, label="SPY") +plt.hist(dia, histtype="step", lw=2, label="DIA") +plt.hist(spy - dia, histtype="step", lw=3, label="Delta") +plt.legend() +plt.show() diff --git a/NumPy/Code/ch10code/repeat_audio.py b/NumPy/Code/ch10code/repeat_audio.py new file mode 100644 index 0000000..47899fc --- /dev/null +++ b/NumPy/Code/ch10code/repeat_audio.py @@ -0,0 +1,31 @@ +from scipy.io import wavfile +import matplotlib.pyplot as plt +import urllib2 +import numpy as np +import sys + +response = urllib2.urlopen('http://www.thesoundarchive.com/austinpowers/smashingbaby.wav') +print response.info() +WAV_FILE = 'smashingbaby.wav' +filehandle = open(WAV_FILE, 'w') +filehandle.write(response.read()) +filehandle.close() +sample_rate, data = wavfile.read(WAV_FILE) +print "Data type", data.dtype, "Shape", data.shape + +plt.subplot(2, 1, 1) +plt.title("Original") +plt.plot(data) + +plt.subplot(2, 1, 2) + +# Repeat the audio fragment +repeated = np.tile(data, int(sys.argv[1])) + +# Plot the audio data +plt.title("Repeated") +plt.plot(repeated) +wavfile.write("repeated_yababy.wav", + sample_rate, repeated) + +plt.show() diff --git a/NumPy/Code/ch10code/scipyio.py b/NumPy/Code/ch10code/scipyio.py new file mode 100644 index 0000000..c2ab9f9 --- /dev/null +++ b/NumPy/Code/ch10code/scipyio.py @@ -0,0 +1,6 @@ +import numpy as np +from scipy import io + +a = np.arange(7) + +io.savemat("a.mat", {"array": a}) diff --git a/NumPy/Code/ch10code/sincinterp.py b/NumPy/Code/ch10code/sincinterp.py new file mode 100644 index 0000000..3ef4671 --- /dev/null +++ b/NumPy/Code/ch10code/sincinterp.py @@ -0,0 +1,21 @@ +import numpy as np +from scipy import interpolate +import matplotlib.pyplot as plt + +x = np.linspace(-18, 18, 36) +noise = 0.1 * np.random.random(len(x)) +signal = np.sinc(x) + noise + +interpreted = interpolate.interp1d(x, signal) +x2 = np.linspace(-18, 18, 180) +y = interpreted(x2) + +cubic = interpolate.interp1d(x, signal, kind="cubic") +y2 = cubic(x2) + +plt.plot(x, signal, 'o', label="data") +plt.plot(x2, y, '-', label="linear") +plt.plot(x2, y2, '-', lw=2, label="cubic") + +plt.legend() +plt.show() diff --git a/NumPy/Code/ch10code/statistics.py b/NumPy/Code/ch10code/statistics.py new file mode 100644 index 0000000..8a87d0c --- /dev/null +++ b/NumPy/Code/ch10code/statistics.py @@ -0,0 +1,12 @@ +from scipy import stats +import matplotlib.pyplot as plt + +generated = stats.norm.rvs(size=900) +print "Mean", "Std", stats.norm.fit(generated) +print "Skewtest", "pvalue", stats.skewtest(generated) +print "Kurtosistest", "pvalue", stats.kurtosistest(generated) +print "Normaltest", "pvalue", stats.normaltest(generated) +print "95 percentile", stats.scoreatpercentile(generated, 95) +print "Percentile at 1", stats.percentileofscore(generated, 1) +plt.hist(generated) +plt.show() diff --git a/NumPy/Code/ch10code/trend.py b/NumPy/Code/ch10code/trend.py new file mode 100644 index 0000000..1dc5e19 --- /dev/null +++ b/NumPy/Code/ch10code/trend.py @@ -0,0 +1,35 @@ +from matplotlib.finance import quotes_historical_yahoo +from datetime import date +import numpy as np +from scipy import signal +import matplotlib.pyplot as plt +from matplotlib.dates import DateFormatter +from matplotlib.dates import DayLocator +from matplotlib.dates import MonthLocator + + +today = date.today() +start = (today.year - 1, today.month, today.day) + +quotes = quotes_historical_yahoo("QQQ", start, today) +quotes = np.array(quotes) + +dates = quotes.T[0] +qqq = quotes.T[4] + + +y = signal.detrend(qqq) + +alldays = DayLocator() +months = MonthLocator() +month_formatter = DateFormatter("%b %Y") + +fig = plt.figure() +ax = fig.add_subplot(111) + +plt.plot(dates, qqq, 'o', dates, qqq - y, '-') +ax.xaxis.set_minor_locator(alldays) +ax.xaxis.set_major_locator(months) +ax.xaxis.set_major_formatter(month_formatter) +fig.autofmt_xdate() +plt.show() diff --git a/NumPy/Code/ch11code/.life_demo.py.swp b/NumPy/Code/ch11code/.life_demo.py.swp new file mode 100644 index 0000000..99266a5 Binary files /dev/null and b/NumPy/Code/ch11code/.life_demo.py.swp differ diff --git a/NumPy/Code/ch11code/animation.py b/NumPy/Code/ch11code/animation.py new file mode 100644 index 0000000..d0b6d9a --- /dev/null +++ b/NumPy/Code/ch11code/animation.py @@ -0,0 +1,49 @@ +import pygame, sys +from pygame.locals import * +import numpy as np + +pygame.init() +clock = pygame.time.Clock() +screen = pygame.display.set_mode((400, 400)) + +pygame.display.set_caption('Animating Objects') +img = pygame.image.load('head.jpg') + +steps = np.linspace(20, 360, 40).astype(int) +right = np.zeros((2, len(steps))) +down = np.zeros((2, len(steps))) +left = np.zeros((2, len(steps))) +up = np.zeros((2, len(steps))) + +right[0] = steps +right[1] = 20 + +down[0] = 360 +down[1] = steps + +left[0] = steps[::-1] +left[1] = 360 + +up[0] = 20 +up[1] = steps[::-1] + +pos = np.concatenate((right.T, down.T, left.T, up.T)) +i = 0 + +while True: + # Erase screen + screen.fill((255, 255, 255)) + + if i >= len(pos): + i = 0 + + screen.blit(img, pos[i]) + i += 1 + + for event in pygame.event.get(): + if event.type == QUIT: + pygame.quit() + sys.exit() + + pygame.display.update() + clock.tick(30) diff --git a/NumPy/Code/ch11code/clustering.py b/NumPy/Code/ch11code/clustering.py new file mode 100644 index 0000000..e360bd7 --- /dev/null +++ b/NumPy/Code/ch11code/clustering.py @@ -0,0 +1,38 @@ +import numpy as np +import sklearn.cluster +import pygame, sys +from pygame.locals import * + + +positions = np.random.randint(0, 400, size=(30, 2)) + +positions_norms = np.sum(positions ** 2, axis=1) +S = - positions_norms[:, np.newaxis] - positions_norms[np.newaxis, :] + 2 * np.dot(positions, positions.T) + +aff_pro = sklearn.cluster.AffinityPropagation().fit(S) +labels = aff_pro.labels_ + +polygon_points = [] + +for i in xrange(max(labels) + 1): + polygon_points.append([]) + + +# Sorting points by cluster +for i in xrange(len(labels)): + polygon_points[labels[i]].append(positions[i]) + +pygame.init() +screen = pygame.display.set_mode((400, 400)) + + +while True: + for i in xrange(len(polygon_points)): + pygame.draw.polygon(screen, (255, 0, 0), polygon_points[i]) + + for event in pygame.event.get(): + if event.type == QUIT: + pygame.quit() + sys.exit() + + pygame.display.update() diff --git a/NumPy/Code/ch11code/head.jpg b/NumPy/Code/ch11code/head.jpg new file mode 100644 index 0000000..09e3ea8 Binary files /dev/null and b/NumPy/Code/ch11code/head.jpg differ diff --git a/NumPy/Code/ch11code/life_demo.py b/NumPy/Code/ch11code/life_demo.py new file mode 100644 index 0000000..6a890ef --- /dev/null +++ b/NumPy/Code/ch11code/life_demo.py @@ -0,0 +1,88 @@ +import os, pygame +from pygame.locals import * +import numpy as np +from scipy import ndimage + +def get_pixar(arr, weights): + states = ndimage.convolve(arr, weights, mode='wrap') + + bools = (states == 13) | (states == 12 ) | (states == 3) + + return bools.astype(int) + +def draw_cross(pixar): + (posx, posy) = pygame.mouse.get_pos() + pixar[posx, :] = 1 + pixar[:, posy] = 1 + +def random_init(n): + return np.random.random_integers(0, 1, (n, n)) + +def draw_pattern(pixar, pattern): + print pattern + + if pattern == 'glider': + coords = [(0,1), (1,2), (2,0), (2,1), (2,2)] + elif pattern == 'block': + coords = [(3,3), (3,2), (2,3), (2,2)] + elif pattern == 'exploder': + coords = [(0,1), (1,2), (2,0), (2,1), (2,2), (3,3)] + elif pattern == 'fpentomino': + coords = [(2,3),(3,2),(4,2),(3,3),(3,4)] + + + pos = pygame.mouse.get_pos() + + xs = np.arange(0, pos[0], 10) + ys = np.arange(0, pos[1], 10) + + for x in xs: + for y in ys: + for i, j in coords: + pixar[x + i, y + j] = 1 + + +def main(): + pygame.init () + + N = 400 + pygame.display.set_mode((N, N)) + pygame.display.set_caption("Life Demo") + + screen = pygame.display.get_surface() + + pixar = random_init(N) + weights = np.array([[1,1,1], [1,10,1], [1,1,1]]) + + cross_on = False + + while True: + pixar = get_pixar(pixar, weights) + + if cross_on: + draw_cross(pixar) + + pygame.surfarray.blit_array(screen, pixar * 255 ** 3) + pygame.display.flip() + + for event in pygame.event.get(): + if event.type == QUIT: + return + if event.type == MOUSEBUTTONDOWN: + cross_on = not cross_on + if event.type == KEYDOWN: + if event.key == ord('r'): + pixar = random_init(N) + print "Random init" + if event.key == ord('g'): + draw_pattern(pixar, 'glider') + if event.key == ord('b'): + draw_pattern(pixar, 'block') + if event.key == ord('e'): + draw_pattern(pixar, 'exploder') + if event.key == ord('f'): + draw_pattern(pixar, 'fpentomino') + + +if __name__ == '__main__': + main() diff --git a/NumPy/Code/ch11code/matplotlib_demo.py b/NumPy/Code/ch11code/matplotlib_demo.py new file mode 100644 index 0000000..64289f4 --- /dev/null +++ b/NumPy/Code/ch11code/matplotlib_demo.py @@ -0,0 +1,76 @@ +import pygame, sys +from pygame.locals import * +import numpy as np +import matplotlib as mpl + +mpl.use("Agg") + +import matplotlib.pyplot as plt +import matplotlib.backends.backend_agg as agg + +fig = plt.figure(figsize=[3, 3]) +ax = fig.add_subplot(111) +canvas = agg.FigureCanvasAgg(fig) + +def plot(data): + ax.plot(data) + canvas.draw() + renderer = canvas.get_renderer() + + raw_data = renderer.tostring_rgb() + size = canvas.get_width_height() + + return pygame.image.fromstring(raw_data, size, "RGB") + +pygame.init() +clock = pygame.time.Clock() +screen = pygame.display.set_mode((400, 400)) + +pygame.display.set_caption('Animating Objects') +img = pygame.image.load('head.jpg') + +steps = np.linspace(20, 360, 40).astype(int) +right = np.zeros((2, len(steps))) +down = np.zeros((2, len(steps))) +left = np.zeros((2, len(steps))) +up = np.zeros((2, len(steps))) + +right[0] = steps +right[1] = 20 + +down[0] = 360 +down[1] = steps + +left[0] = steps[::-1] +left[1] = 360 + +up[0] = 20 +up[1] = steps[::-1] + +pos = np.concatenate((right.T, down.T, left.T, up.T)) +i = 0 +history = np.array([]) +surf = plot(history) + +while True: + # Erase screen + screen.fill((255, 255, 255)) + + if i >= len(pos): + i = 0 + surf = plot(history) + + + screen.blit(img, pos[i]) + history = np.append(history, pos[i]) + screen.blit(surf, (100, 100)) + + i += 1 + + for event in pygame.event.get(): + if event.type == QUIT: + pygame.quit() + sys.exit() + + pygame.display.update() + clock.tick(30) diff --git a/NumPy/Code/ch11code/opengl_demo.py b/NumPy/Code/ch11code/opengl_demo.py new file mode 100644 index 0000000..28df416 --- /dev/null +++ b/NumPy/Code/ch11code/opengl_demo.py @@ -0,0 +1,44 @@ +import pygame +from pygame.locals import * +import numpy as np + +from OpenGL.GL import * +from OpenGL.GLU import * + +def display_openGL(w, h): + pygame.display.set_mode((w,h), pygame.OPENGL|pygame.DOUBLEBUF) + + glClearColor(0.0, 0.0, 0.0, 1.0) + glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) + + gluOrtho2D(0, w, 0, h) + + +def main(): + pygame.init() + pygame.display.set_caption('OpenGL Demo') + DIM = 400 + display_openGL(DIM, DIM) + glColor3f(1.0, 0, 0) + vertices = np.array([[0, 0], [DIM/2, DIM], [DIM, 0]]) + NPOINTS = 9000 + indices = np.random.random_integers(0, 2, NPOINTS) + point = [175.0, 150.0] + + for i in xrange(NPOINTS): + glBegin(GL_POINTS) + point = (point + vertices[indices[i]])/2.0 + glVertex2fv(point) + glEnd() + + glFlush() + pygame.display.flip() + + while True: + for event in pygame.event.get(): + if event.type == QUIT: + return + + +if __name__ == '__main__': + main() diff --git a/NumPy/Code/ch11code/simplegame.py b/NumPy/Code/ch11code/simplegame.py new file mode 100644 index 0000000..e1bc280 --- /dev/null +++ b/NumPy/Code/ch11code/simplegame.py @@ -0,0 +1,19 @@ +import pygame, sys +from pygame.locals import * + +pygame.init() +screen = pygame.display.set_mode((400, 300)) + +pygame.display.set_caption('Hello World!') + +while True: + sysFont = pygame.font.SysFont("None", 19) + rendered = sysFont.render('Hello World', 0, (255, 100, 100)) + screen.blit(rendered, (100, 100)) + + for event in pygame.event.get(): + if event.type == QUIT: + pygame.quit() + sys.exit() + + pygame.display.update() diff --git a/NumPy/Code/ch11code/surfarray_demo.py b/NumPy/Code/ch11code/surfarray_demo.py new file mode 100644 index 0000000..6c4aa5a --- /dev/null +++ b/NumPy/Code/ch11code/surfarray_demo.py @@ -0,0 +1,24 @@ +import pygame, sys +from pygame.locals import * +import numpy as np + +pygame.init() +img = pygame.image.load('head.jpg') +pixels = pygame.surfarray.array2d(img) +X = pixels.shape[0] * 7 +Y = pixels.shape[1] * 7 +screen = pygame.display.set_mode((X, Y)) +pygame.display.set_caption('Surfarray Demo') +new_pixels = np.tile(pixels, (7, 7)).astype(int) + + +while True: + screen.fill((255, 255, 255)) + pygame.surfarray.blit_array(screen, new_pixels) + + for event in pygame.event.get(): + if event.type == QUIT: + pygame.quit() + sys.exit() + + pygame.display.update() diff --git a/NumPy/Code/ch1code/README b/NumPy/Code/ch1code/README new file mode 100644 index 0000000..b7f27bd --- /dev/null +++ b/NumPy/Code/ch1code/README @@ -0,0 +1,17 @@ +Chapter 1 of NumPy Beginners Guide. + +Prerequisites: + You should have Python and NumPy installed on your system. + +There is one program in this chapter that demonstrates vector addition the Python way. +Run from the command line as follows + +python vectorsum.py n + +where n is an integer that specifies the size of the vectors. For example, + +python vectorsum.py 1000 + +The first vector to be added contains the squares of 0 up to n. +The second vector contains the cubes of 0 up to n. +The program prints the last 2 elements of the sum and the elapsed time. diff --git a/NumPy/Code/ch1code/vectorsum.py b/NumPy/Code/ch1code/vectorsum.py new file mode 100644 index 0000000..0a99bd7 --- /dev/null +++ b/NumPy/Code/ch1code/vectorsum.py @@ -0,0 +1,53 @@ +#!/usr/bin/env/python + +import sys +from datetime import datetime +import numpy as np + +""" + Chapter 1 of NumPy Beginners Guide. + This program demonstrates vector addition the Python way. + Run from the command line as follows + + python vectorsum.py n + + where n is an integer that specifies the size of the vectors. + + The first vector to be added contains the squares of 0 up to n. + The second vector contains the cubes of 0 up to n. + The program prints the last 2 elements of the sum and the elapsed time. +""" + +def numpysum(n): + a = np.arange(n) ** 2 + b = np.arange(n) ** 3 + c = a + b + + return c + +def pythonsum(n): + a = range(n) + b = range(n) + c = [] + + for i in range(len(a)): + a[i] = i ** 2 + b[i] = i ** 3 + c.append(a[i] + b[i]) + + return c + + +size = int(sys.argv[1]) + +start = datetime.now() +c = pythonsum(size) +delta = datetime.now() - start +print "The last 2 elements of the sum", c[-2:] +print "PythonSum elapsed time in microseconds", delta.microseconds + +start = datetime.now() +c = numpysum(size) +delta = datetime.now() - start +print "The last 2 elements of the sum", c[-2:] +print "NumPySum elapsed time in microseconds", delta.microseconds diff --git a/NumPy/Code/ch2code/README b/NumPy/Code/ch2code/README new file mode 100644 index 0000000..30e666d --- /dev/null +++ b/NumPy/Code/ch2code/README @@ -0,0 +1,27 @@ +Chapter 2 Beginning with NumPy fundamentals + +The scripts below demonstrate various aspects of +NumPy arrays and data types. + +Run from the commandline with + +python + +where name of script is one of the following: + +arrayattributes.py +arrayattributes2.py +arrayconversion.py +charcodes.py +dtypeattributes.py +dtypeattributes2.py +dtypeconstructors.py +elementselection.py +numericaltypes.py +record.py +shapemanipulation.py +slicing.py +slicing1d.py +splitting.py +stacking.py + diff --git a/NumPy/Code/ch2code/arrayattributes.py b/NumPy/Code/ch2code/arrayattributes.py new file mode 100644 index 0000000..03a26d8 --- /dev/null +++ b/NumPy/Code/ch2code/arrayattributes.py @@ -0,0 +1,45 @@ +import numpy as np + +# Chapter 2 Beginning with NumPy fundamentals +# +# Demonstrates the dtype and shape attributes +# of ndarray. +# +# Run from the commandline with +# +# python arrayattributes.py +a = np.arange(5) +print "In: a = arange(5)" + +print "In: a.dtype" +print a.dtype +#Out: dtype('int64') +print + +print "In: a.shape" +print a.shape +#Out: (5,) +print + +print "In: a" +print a +#Out[4]: array([0, 1, 2, 3, 4]) +print + +m = np.array([np.arange(2), np.arange(2)]) + +print "In: m" +print m +#Out: +#array([[0, 1], +# [0, 1]]) +print + +print "In: m.shape" +print m.shape +#Out: (2, 2) + +print "In: m.dtype" +print m.dtype +#Out: dtype('int64') +print diff --git a/NumPy/Code/ch2code/arrayattributes2.py b/NumPy/Code/ch2code/arrayattributes2.py new file mode 100644 index 0000000..e944dbd --- /dev/null +++ b/NumPy/Code/ch2code/arrayattributes2.py @@ -0,0 +1,139 @@ +import numpy as np + +# Chapter 2 Beginning with NumPy fundamentals +# +# Demonstrates ndarray attributes. +# +# Run from the commandline with +# +# python arrayattributes2.py +b = np.arange(24).reshape(2, 12) +print "In: b" +print b +#Out: +#array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], +# [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]]) + +print "In: b.ndim" +print b.ndim +#Out: 2 + +print "In: b.size" +print b.size +#Out: 24 + +print "In: b.itemsize" +print b.itemsize +#Out: 8 + +print "In: b.nbytes" +print b.nbytes +#Out: 192 + +print "In: b.size * b.itemsize" +print b.size * b.itemsize +#Out: 192 + +print "In: b.resize(6,4)" +print b.resize(6,4) + +print "In: b" +print b +#Out: +#array([[ 0, 1, 2, 3], +# [ 4, 5, 6, 7], +# [ 8, 9, 10, 11], +# [12, 13, 14, 15], +# [16, 17, 18, 19], +# [20, 21, 22, 23]]) + +print "In: b.T" +print b.T +#Out: +#array([[ 0, 4, 8, 12, 16, 20], +# [ 1, 5, 9, 13, 17, 21], +# [ 2, 6, 10, 14, 18, 22], +# [ 3, 7, 11, 15, 19, 23]]) + +print "In: b.ndim" +print b.ndim +#Out: 1 + +print "In: b.T" +print b.T +#Out: array([0, 1, 2, 3, 4]) + + +print "In: b = array([1.j + 1, 2.j + 3])" +b = np.array([1.j + 1, 2.j + 3]) + +print "In: b" +print b +#Out: array([ 1.+1.j, 3.+2.j]) + +print "In: b.real" +print b.real +#Out: array([ 1., 3.]) + +print "In: b.imag" +print b.imag +#Out: array([ 1., 2.]) + +print "In: b.dtype" +print b.dtype +#Out: dtype('complex128') + +print "In: b.dtype.str" +print b.dtype.str +#Out: ' + +print "In: for it in f: print it" +for it in f: + print it +#0 +#1 +#2 +#3 + +print "In: b.flat[2]" +print b.flat[2] +#Out: 2 + + +print "In: b.flat[[1,3]]" +print b.flat[[1,3]] +#Out: array([1, 3]) + + +print "In: b" +print b +#Out: +#array([[7, 7], +# [7, 7]]) + +print "In: b.flat[[1,3]] = 1" +b.flat[[1,3]] = 1 + +print "In: b" +print b +#Out: +#array([[7, 1], +# [7, 1]]) + diff --git a/NumPy/Code/ch2code/arrayconversion.py b/NumPy/Code/ch2code/arrayconversion.py new file mode 100644 index 0000000..5b1cd48 --- /dev/null +++ b/NumPy/Code/ch2code/arrayconversion.py @@ -0,0 +1,44 @@ +import numpy as np + +# Chapter 2 Beginning with NumPy fundamentals +# +# Demonstrates the NumPy record data type. +# +# Run from the commandline with +# +# python arrayconversion.py +b = np.array([ 1.+1.j, 3.+2.j]) +print "In: b" +print b +#Out: array([ 1.+1.j, 3.+2.j]) + +print "In: b.tolist()" +print b.tolist() +#Out: [(1+1j), (3+2j)] + +print "In: b.tostring()" +print b.tostring() +#Out: '\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\x00@' + +print "In: fromstring('\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\x00@', dtype=complex)" +print np.fromstring('\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\x00@', dtype=complex) +#Out: array([ 1.+1.j, 3.+2.j] + +print "In: fromstring('20:42:52',sep=':', dtype=int)" +print np.fromstring('20:42:52',sep=':', dtype=int) +#Out: array([20, 42, 52]) + +print "In: b" +print b +#Out: array([ 1.+1.j, 3.+2.j]) + +print "In: b.astype(int)" +print b.astype(int) +#/usr/local/bin/ipython:1: ComplexWarning: Casting complex values to real discards the imaginary part +# #!/usr/bin/python +#Out: array([1, 3]) + +print "In: b.astype('complex')" +print b.astype('complex') +#Out: array([ 1.+1.j, 3.+2.j]) + diff --git a/NumPy/Code/ch2code/charcodes.py b/NumPy/Code/ch2code/charcodes.py new file mode 100644 index 0000000..ce75d71 --- /dev/null +++ b/NumPy/Code/ch2code/charcodes.py @@ -0,0 +1,19 @@ +import numpy as np + +# Chapter 2 Beginning with NumPy fundamentals +# +# Demonstrates the NumPy dtype character codes. +# +# Run from the commandline with +# +# python charcodes.py + +print "In: arange(7, dtype='f')" +print np.arange(7, dtype='f') +#Out: array([ 0., 1., 2., 3., 4., 5., 6.], dtype=float32) + +print "In: arange(7, dtype='D')" +print np.arange(7, dtype='D') +#Out: array([ 0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j, 4.+0.j, 5.+0.j, 6.+0.j]) + + diff --git a/NumPy/Code/ch2code/dtypeattributes.py b/NumPy/Code/ch2code/dtypeattributes.py new file mode 100644 index 0000000..02ee135 --- /dev/null +++ b/NumPy/Code/ch2code/dtypeattributes.py @@ -0,0 +1,24 @@ +import numpy as np + +# Chapter 2 Beginning with NumPy fundamentals +# +# Demonstrates the NumPy +# dtype.byteorder and dtype.itemsize +# +# Run from the commandline with +# +# python dtypeattributes.py + + +print "In: a = array([[1,2],[3,4]])" +a = np.array([[1,2],[3,4]]) + +print "In: a.dtype.byteorder" +print a.dtype.byteorder +#Out: '=' + +print "In: a.dtype.itemsize" +print a.dtype.itemsize +#Out: 8 + + diff --git a/NumPy/Code/ch2code/dtypeattributes2.py b/NumPy/Code/ch2code/dtypeattributes2.py new file mode 100644 index 0000000..81b7329 --- /dev/null +++ b/NumPy/Code/ch2code/dtypeattributes2.py @@ -0,0 +1,27 @@ +import numpy as np + +# Chapter2 Beginning with NumPy fundamentals +# +# Demonstrates the NumPy +# dtype attributes +# +# Run from the commandline with +# +# python dtypeattributes2.py +print "In: t = dtype('Float64')" +t = np.dtype('Float64') + +print "In: t.char" +print t.char +#Out: 'd' + +print "In: t.type" +print t.type +#Out: + +print "In: t.str" +print t.str +#Out: ' lowerBB)) + +print lowerBB[between_bands] +print c[between_bands] +print upperBB[between_bands] +between_bands = len(np.ravel(between_bands)) +print "Ratio between bands", float(between_bands)/len(c_slice) + +t = np.arange(N - 1, C) +plot(t, c_slice, lw=1.0) +plot(t, sma, lw=2.0) +plot(t, upperBB, lw=3.0) +plot(t, lowerBB, lw=4.0) +show() diff --git a/NumPy/Code/ch3code/data.csv b/NumPy/Code/ch3code/data.csv new file mode 100644 index 0000000..a8d1222 --- /dev/null +++ b/NumPy/Code/ch3code/data.csv @@ -0,0 +1,30 @@ +AAPL,28-01-2011, ,344.17,344.4,333.53,336.1,21144800 +AAPL,31-01-2011, ,335.8,340.04,334.3,339.32,13473000 +AAPL,01-02-2011, ,341.3,345.65,340.98,345.03,15236800 +AAPL,02-02-2011, ,344.45,345.25,343.55,344.32,9242600 +AAPL,03-02-2011, ,343.8,344.24,338.55,343.44,14064100 +AAPL,04-02-2011, ,343.61,346.7,343.51,346.5,11494200 +AAPL,07-02-2011, ,347.89,353.25,347.64,351.88,17322100 +AAPL,08-02-2011, ,353.68,355.52,352.15,355.2,13608500 +AAPL,09-02-2011, ,355.19,359,354.87,358.16,17240800 +AAPL,10-02-2011, ,357.39,360,348,354.54,33162400 +AAPL,11-02-2011, ,354.75,357.8,353.54,356.85,13127500 +AAPL,14-02-2011, ,356.79,359.48,356.71,359.18,11086200 +AAPL,15-02-2011, ,359.19,359.97,357.55,359.9,10149000 +AAPL,16-02-2011, ,360.8,364.9,360.5,363.13,17184100 +AAPL,17-02-2011, ,357.1,360.27,356.52,358.3,18949000 +AAPL,18-02-2011, ,358.21,359.5,349.52,350.56,29144500 +AAPL,22-02-2011, ,342.05,345.4,337.72,338.61,31162200 +AAPL,23-02-2011, ,338.77,344.64,338.61,342.62,23994700 +AAPL,24-02-2011, ,344.02,345.15,338.37,342.88,17853500 +AAPL,25-02-2011, ,345.29,348.43,344.8,348.16,13572000 +AAPL,28-02-2011, ,351.21,355.05,351.12,353.21,14395400 +AAPL,01-03-2011, ,355.47,355.72,347.68,349.31,16290300 +AAPL,02-03-2011, ,349.96,354.35,348.4,352.12,21521000 +AAPL,03-03-2011, ,357.2,359.79,355.92,359.56,17885200 +AAPL,04-03-2011, ,360.07,360.29,357.75,360,16188000 +AAPL,07-03-2011, ,361.11,361.67,351.31,355.36,19504300 +AAPL,08-03-2011, ,354.91,357.4,352.25,355.76,12718000 +AAPL,09-03-2011, ,354.69,354.76,350.6,352.47,16192700 +AAPL,10-03-2011, ,349.69,349.77,344.9,346.67,18138800 +AAPL,11-03-2011, ,345.4,352.32,345,351.99,16824200 diff --git a/NumPy/Code/ch3code/ema.py b/NumPy/Code/ch3code/ema.py new file mode 100644 index 0000000..0930a58 --- /dev/null +++ b/NumPy/Code/ch3code/ema.py @@ -0,0 +1,22 @@ +import numpy as np +import sys +from matplotlib.pyplot import plot +from matplotlib.pyplot import show + +x = np.arange(5) +print "Exp", np.exp(x) +print "Linspace", np.linspace(-1, 0, 5) + +N = int(sys.argv[1]) + + +weights = np.exp(np.linspace(-1., 0., N)) +weights /= weights.sum() +print "Weights", weights + +c = np.loadtxt('data.csv', delimiter=',', usecols=(6,), unpack=True) +ema = np.convolve(weights, c)[N-1:-N+1] +t = np.arange(N - 1, len(c)) +plot(t, c[N-1:], lw=1.0) +plot(t, ema, lw=2.0) +show() diff --git a/NumPy/Code/ch3code/eye.txt b/NumPy/Code/ch3code/eye.txt new file mode 100644 index 0000000..0f504d7 --- /dev/null +++ b/NumPy/Code/ch3code/eye.txt @@ -0,0 +1,2 @@ +1.000000000000000000e+00 0.000000000000000000e+00 +0.000000000000000000e+00 1.000000000000000000e+00 diff --git a/NumPy/Code/ch3code/genfromtxt.py b/NumPy/Code/ch3code/genfromtxt.py new file mode 100644 index 0000000..211706b --- /dev/null +++ b/NumPy/Code/ch3code/genfromtxt.py @@ -0,0 +1,5 @@ +import numpy as np + +data = np.eye(2) +np.savetxt("eye.txt", data) +print np.genfromtxt("eye.txt") diff --git a/NumPy/Code/ch3code/linearmodel.py b/NumPy/Code/ch3code/linearmodel.py new file mode 100644 index 0000000..0993857 --- /dev/null +++ b/NumPy/Code/ch3code/linearmodel.py @@ -0,0 +1,27 @@ +import numpy as np +import sys + +N = int(sys.argv[1]) + +c = np.loadtxt('data.csv', delimiter=',', usecols=(6,), unpack=True) + +b = c[-N:] +b = b[::-1] +print "b", b + +A = np.zeros((N, N), float) +print "Zeros N by N", A + +for i in range(N): + A[i, ] = c[-N - 1 - i: - 1 - i] + +print "A", A + + +(x, residuals, rank, s) = np.linalg.lstsq(A, b) + +print x, residuals, rank, s + +print np.dot(b, x) +#352.51614809 +#actual close 353.56 diff --git a/NumPy/Code/ch3code/ndarraymethods.py b/NumPy/Code/ch3code/ndarraymethods.py new file mode 100644 index 0000000..2f0d3c1 --- /dev/null +++ b/NumPy/Code/ch3code/ndarraymethods.py @@ -0,0 +1,15 @@ +import numpy as np + +a = np.arange(5) +print "a =", a +print "Clipped", a.clip(1, 2) + +a = np.arange(4) +print a +print "Compressed", a.compress(a > 2) + +b = np.arange(1, 9) +print "b =", b +print "Factorial", b.prod() + +print "Factorials", b.cumprod() diff --git a/NumPy/Code/ch3code/range.py b/NumPy/Code/ch3code/range.py new file mode 100644 index 0000000..4e282fc --- /dev/null +++ b/NumPy/Code/ch3code/range.py @@ -0,0 +1,9 @@ +import numpy as np + +h,l=np.loadtxt('data.csv', delimiter=',', usecols=(4,5), unpack=True) +print "highest =", np.max(h) +print "lowest =", np.min(l) +print (np.max(h) + np.min(l)) /2 + +print "Spread high price", np.ptp(h) +print "Spread low price", np.ptp(l) diff --git a/NumPy/Code/ch3code/returns.py b/NumPy/Code/ch3code/returns.py new file mode 100644 index 0000000..7732693 --- /dev/null +++ b/NumPy/Code/ch3code/returns.py @@ -0,0 +1,17 @@ +import numpy as np + +c=np.loadtxt('data.csv', delimiter=',', usecols=(6,), unpack=True) + +returns = np.diff( c ) / c[ : -1] +print "Standard deviation =", np.std(returns) + +logreturns = np.diff( np.log(c) ) + +posretindices = np.where(returns > 0) +print "Indices with positive returns", posretindices + +annual_volatility = np.std(logreturns)/np.mean(logreturns) +annual_volatility = annual_volatility / np.sqrt(1./252.) +print "Annual volatility", annual_volatility + +print "Monthly volatility", annual_volatility * np.sqrt(1./12.) diff --git a/NumPy/Code/ch3code/save.py b/NumPy/Code/ch3code/save.py new file mode 100644 index 0000000..7526926 --- /dev/null +++ b/NumPy/Code/ch3code/save.py @@ -0,0 +1,5 @@ +import numpy as np + +i2 = np.eye(2) +print i2 +np.savetxt("eye.txt", i2) diff --git a/NumPy/Code/ch3code/simplestats.py b/NumPy/Code/ch3code/simplestats.py new file mode 100644 index 0000000..1bdb7b3 --- /dev/null +++ b/NumPy/Code/ch3code/simplestats.py @@ -0,0 +1,13 @@ +import numpy as np + +c=np.loadtxt('data.csv', delimiter=',', usecols=(6,), unpack=True) +print "median =", np.median(c) +sorted = np.msort(c) +print "sorted =", sorted + +N = len(c) +print "middle =", sorted[(N - 1)/2] +print "average middle =", (sorted[N /2] + sorted[(N - 1) / 2]) / 2 + +print "variance =", np.var(c) +print "variance from definition =", np.mean((c - c.mean())**2) diff --git a/NumPy/Code/ch3code/sma.py b/NumPy/Code/ch3code/sma.py new file mode 100644 index 0000000..e8092bb --- /dev/null +++ b/NumPy/Code/ch3code/sma.py @@ -0,0 +1,16 @@ +import numpy as np +import sys +from matplotlib.pyplot import plot +from matplotlib.pyplot import show + +N = int(sys.argv[1]) + +weights = np.ones(N) / N +print "Weights", weights + +c = np.loadtxt('data.csv', delimiter=',', usecols=(6,), unpack=True) +sma = np.convolve(weights, c)[N-1:-N+1] +t = np.arange(N - 1, len(c)) +plot(t, c[N-1:], lw=1.0) +plot(t, sma, lw=2.0) +show() diff --git a/NumPy/Code/ch3code/trendline.py b/NumPy/Code/ch3code/trendline.py new file mode 100644 index 0000000..2e9954b --- /dev/null +++ b/NumPy/Code/ch3code/trendline.py @@ -0,0 +1,41 @@ +import numpy as np +from matplotlib.pyplot import plot +from matplotlib.pyplot import show + +def fit_line(t, y): + A = np.vstack([t, np.ones_like(t)]).T + + return np.linalg.lstsq(A, y)[0] + +h, l, c = np.loadtxt('data.csv', delimiter=',', usecols=(4, 5, 6), unpack=True) + +pivots = (h + l + c) / 3 +print "Pivots", pivots + +t = np.arange(len(c)) +sa, sb = fit_line(t, pivots - (h - l)) +ra, rb = fit_line(t, pivots + (h - l)) + +support = sa * t + sb +resistance = ra * t + rb +condition = (c > support) & (c < resistance) +print "Condition", condition +between_bands = np.where(condition) +print support[between_bands] +print c[between_bands] +print resistance[between_bands] +between_bands = len(np.ravel(between_bands)) +print "Number points between bands", between_bands +print "Ratio between bands", float(between_bands)/len(c) + +print "Tomorrows support", sa * (t[-1] + 1) + sb +print "Tomorrows resistance", ra * (t[-1] + 1) + rb + +a1 = c[c > support] +a2 = c[c < resistance] +print "Number of points between bands 2nd approach" ,len(np.intersect1d(a1, a2)) + +plot(t, c) +plot(t, support) +plot(t, resistance) +show() diff --git a/NumPy/Code/ch3code/vwap.py b/NumPy/Code/ch3code/vwap.py new file mode 100644 index 0000000..96ec4ac --- /dev/null +++ b/NumPy/Code/ch3code/vwap.py @@ -0,0 +1,10 @@ +import numpy as np + +c,v=np.loadtxt('data.csv', delimiter=',', usecols=(6,7), unpack=True) +vwap = np.average(c, weights=v) +print "VWAP =", vwap + +print "mean =", np.mean(c) + +t = np.arange(len(c)) +print "twap =", np.average(c, weights=t) diff --git a/NumPy/Code/ch3code/weekdays.py b/NumPy/Code/ch3code/weekdays.py new file mode 100644 index 0000000..bcbfdf0 --- /dev/null +++ b/NumPy/Code/ch3code/weekdays.py @@ -0,0 +1,34 @@ +import numpy as np +from datetime import datetime + +# Monday 0 +# Tuesday 1 +# Wednesday 2 +# Thursday 3 +# Friday 4 +# Saturday 5 +# Sunday 6 +def datestr2num(s): + return datetime.strptime(s, "%d-%m-%Y").date().weekday() + +dates, close=np.loadtxt('data.csv', delimiter=',', usecols=(1,6), converters={1: datestr2num}, unpack=True) +print "Dates =", dates + +averages = np.zeros(5) + +for i in range(5): + indices = np.where(dates == i) + prices = np.take(close, indices) + avg = np.mean(prices) + print "Day", i, "prices", prices, "Average", avg + averages[i] = avg + + +top = np.max(averages) +print "Highest average", top +print "Top day of the week", np.argmax(averages) + +bottom = np.min(averages) +print "Lowest average", bottom +print "Bottom day of the week", np.argmin(averages) + diff --git a/NumPy/Code/ch3code/weeksummary.csv b/NumPy/Code/ch3code/weeksummary.csv new file mode 100644 index 0000000..e3dcf2b --- /dev/null +++ b/NumPy/Code/ch3code/weeksummary.csv @@ -0,0 +1,3 @@ +APPL,335.8,346.7,334.3,346.5 +APPL,347.8,360.0,347.6,356.8 +APPL,356.7,364.9,349.5,350.5 diff --git a/NumPy/Code/ch3code/weeksummary.py b/NumPy/Code/ch3code/weeksummary.py new file mode 100644 index 0000000..3336e95 --- /dev/null +++ b/NumPy/Code/ch3code/weeksummary.py @@ -0,0 +1,43 @@ +import numpy as np +from datetime import datetime + +# Monday 0 +# Tuesday 1 +# Wednesday 2 +# Thursday 3 +# Friday 4 +# Saturday 5 +# Sunday 6 +def datestr2num(s): + return datetime.strptime(s, "%d-%m-%Y").date().weekday() + +dates, open, high, low, close=np.loadtxt('data.csv', delimiter=',', usecols=(1, 3, 4, 5, 6), converters={1: datestr2num}, unpack=True) +close = close[:16] +dates = dates[:16] + +# get first Monday +first_monday = np.ravel(np.where(dates == 0))[0] +print "The first Monday index is", first_monday + +# get last Friday +last_friday = np.ravel(np.where(dates == 4))[-1] +print "The last Friday index is", last_friday + +weeks_indices = np.arange(first_monday, last_friday + 1) +print "Weeks indices initial", weeks_indices + +weeks_indices = np.split(weeks_indices, 3) +print "Weeks indices after split", weeks_indices + +def summarize(a, o, h, l, c): + monday_open = o[a[0]] + week_high = np.max( np.take(h, a) ) + week_low = np.min( np.take(l, a) ) + friday_close = c[a[-1]] + + return("APPL", monday_open, week_high, week_low, friday_close) + +weeksummary = np.apply_along_axis(summarize, 1, weeks_indices, open, high, low, close) +print "Week summary", weeksummary + +np.savetxt("weeksummary.csv", weeksummary, delimiter=",", fmt="%s") diff --git a/NumPy/Code/ch4code/__MACOSX/ch4code/._.DS_Store b/NumPy/Code/ch4code/__MACOSX/ch4code/._.DS_Store new file mode 100644 index 0000000..a9dcab0 Binary files /dev/null and b/NumPy/Code/ch4code/__MACOSX/ch4code/._.DS_Store differ diff --git a/NumPy/Code/ch4code/ch4code/.DS_Store b/NumPy/Code/ch4code/ch4code/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/NumPy/Code/ch4code/ch4code/.DS_Store differ diff --git a/NumPy/Code/ch4code/ch4code/BHP.csv b/NumPy/Code/ch4code/ch4code/BHP.csv new file mode 100644 index 0000000..5207c3c --- /dev/null +++ b/NumPy/Code/ch4code/ch4code/BHP.csv @@ -0,0 +1,30 @@ +BHP,11-02-2011, ,93.11,94.26,92.9,93.72,1741900 +BHP,14-02-2011, ,94.57,96.23,94.39,95.64,2620800 +BHP,15-02-2011, ,94.45,95.47,93.91,94.56,2461300 +BHP,16-02-2011, ,92.67,93.58,92.56,93.3,3270900 +BHP,17-02-2011, ,92.65,93.98,92.58,93.93,2650200 +BHP,18-02-2011, ,92.34,93,92,92.39,4667300 +BHP,22-02-2011, ,93.14,93.98,91.75,92.11,5359800 +BHP,23-02-2011, ,91.93,92.46,91.05,92.36,7768400 +BHP,24-02-2011, ,92.42,92.71,90.93,91.76,4799100 +BHP,25-02-2011, ,93.48,94.04,92.44,93.91,3448300 +BHP,28-02-2011, ,94.81,95.11,94.1,94.6,4719800 +BHP,01-03-2011, ,95.05,95.2,93.13,93.27,3898900 +BHP,02-03-2011, ,93.89,94.89,93.54,94.43,3727700 +BHP,03-03-2011, ,95.9,96.11,95.18,96.02,3379400 +BHP,04-03-2011, ,96.12,96.44,95.08,95.76,2463900 +BHP,07-03-2011, ,96.51,96.66,94.03,94.47,3590900 +BHP,08-03-2011, ,93.72,94.47,92.9,94.34,3805000 +BHP,09-03-2011, ,92.94,93.13,91.86,92.22,3271700 +BHP,10-03-2011, ,89,89.17,87.93,88.31,5507800 +BHP,11-03-2011, ,88.24,89.8,88.16,89.59,2996800 +BHP,14-03-2011, ,88.17,89.06,87.82,89.02,3434800 +BHP,15-03-2011, ,84.58,87.32,84.35,86.95,5008300 +BHP,16-03-2011, ,86.31,87.28,83.85,84.88,7809799 +BHP,17-03-2011, ,87.32,88.29,86.89,87.38,3947100 +BHP,18-03-2011, ,89.53,89.58,88.05,88.56,3809700 +BHP,21-03-2011, ,90.13,90.16,88.88,89.59,3098200 +BHP,22-03-2011, ,89.5,89.59,88.42,88.71,3500200 +BHP,23-03-2011, ,89.57,90.32,88.85,90.02,4285600 +BHP,24-03-2011, ,90.86,91.35,89.7,91.26,3918800 +BHP,25-03-2011, ,90.42,91.09,90.07,90.67,3632200 diff --git a/NumPy/Code/ch4code/ch4code/README b/NumPy/Code/ch4code/ch4code/README new file mode 100644 index 0000000..aa16395 --- /dev/null +++ b/NumPy/Code/ch4code/ch4code/README @@ -0,0 +1,21 @@ +Chapter 4 Convenience functions for your convenience + +The following scripts are part of the chapter 4 code: + +correlation.py + Run with python correlation.py + +mode.py + Run with python mode.py + +obv.py + Run with python obv.py + +polynomials.py + Run with python polynomials.py 3 + +simulation.py + Run with python simulation.py 0.999 + +smoothing.py + Run with python smoothing.py 8 diff --git a/NumPy/Code/ch4code/ch4code/VALE.csv b/NumPy/Code/ch4code/ch4code/VALE.csv new file mode 100644 index 0000000..d0e1d25 --- /dev/null +++ b/NumPy/Code/ch4code/ch4code/VALE.csv @@ -0,0 +1,30 @@ +VALE,11-02-2011, ,33.88,34.54,33.63,34.37,18433500 +VALE,14-02-2011, ,34.53,35.29,34.52,35.13,20780700 +VALE,15-02-2011, ,34.89,35.31,34.82,35.14,17756700 +VALE,16-02-2011, ,35.16,35.4,34.81,35.31,16792800 +VALE,17-02-2011, ,35.18,35.6,35.04,35.57,24088300 +VALE,18-02-2011, ,35.31,35.37,34.89,35.03,21286600 +VALE,22-02-2011, ,33.94,34.57,33.36,33.44,28364700 +VALE,23-02-2011, ,33.43,34.12,33.1,33.94,22559300 +VALE,24-02-2011, ,34.3,34.3,33.56,34.21,20591900 +VALE,25-02-2011, ,34.67,34.95,34.05,34.27,20151500 +VALE,28-02-2011, ,34.34,34.51,33.7,34.23,16126000 +VALE,01-03-2011, ,34.39,34.44,33.68,33.76,17282400 +VALE,02-03-2011, ,33.61,34.5,33.57,34.32,15870900 +VALE,03-03-2011, ,34.77,34.89,34.53,34.87,14648200 +VALE,04-03-2011, ,34.67,34.83,34.04,34.5,15330800 +VALE,07-03-2011, ,34.43,34.53,32.97,33.23,25040500 +VALE,08-03-2011, ,33.22,33.7,32.55,33.29,17093000 +VALE,09-03-2011, ,33.23,33.44,32.68,32.88,20026300 +VALE,10-03-2011, ,32.17,32.4,31.68,31.91,30803900 +VALE,11-03-2011, ,31.53,32.42,31.49,32.17,24429900 +VALE,14-03-2011, ,32.03,32.45,31.74,32.44,15525500 +VALE,15-03-2011, ,30.99,31.93,30.79,31.91,24767700 +VALE,16-03-2011, ,31.99,32.03,30.68,31.04,30394153 +VALE,17-03-2011, ,31.44,31.82,31.32,31.51,24035000 +VALE,18-03-2011, ,32.17,32.39,31.98,32.14,19740600 +VALE,21-03-2011, ,32.81,32.85,32.26,32.42,18923700 +VALE,22-03-2011, ,32.13,32.32,31.74,32.25,18934200 +VALE,23-03-2011, ,32.39,32.91,32.22,32.7,18359900 +VALE,24-03-2011, ,32.82,32.94,32.12,32.36,25894100 +VALE,25-03-2011, ,32.26,32.74,31.93,32.34,16688900 diff --git a/NumPy/Code/ch4code/ch4code/correlation.py b/NumPy/Code/ch4code/ch4code/correlation.py new file mode 100644 index 0000000..a8cb19e --- /dev/null +++ b/NumPy/Code/ch4code/ch4code/correlation.py @@ -0,0 +1,32 @@ +import numpy as np +from matplotlib.pyplot import plot +from matplotlib.pyplot import show + +bhp = np.loadtxt('BHP.csv', delimiter=',', usecols=(6,), unpack=True) + +bhp_returns = np.diff(bhp) / bhp[ : -1] + +vale = np.loadtxt('VALE.csv', delimiter=',', usecols=(6,), unpack=True) + +vale_returns = np.diff(vale) / vale[ : -1] + +covariance = np.cov(bhp_returns, vale_returns) +print "Covariance", covariance + +print "Covariance diagonal", covariance.diagonal() +print "Covariance trace", covariance.trace() + +print covariance/ (bhp_returns.std() * vale_returns.std()) + +print "Correlation coefficient", np.corrcoef(bhp_returns, vale_returns) + +difference = bhp - vale +avg = np.mean(difference) +dev = np.std(difference) + +print "Out of sync", np.abs(difference[-1] - avg) > 2 * dev + +t = np.arange(len(bhp_returns)) +plot(t, bhp_returns, lw=1) +plot(t, vale_returns, lw=2) +show() diff --git a/NumPy/Code/ch4code/ch4code/obv.py b/NumPy/Code/ch4code/ch4code/obv.py new file mode 100644 index 0000000..29de42e --- /dev/null +++ b/NumPy/Code/ch4code/ch4code/obv.py @@ -0,0 +1,17 @@ +import numpy as np + +c, v=np.loadtxt('BHP.csv', delimiter=',', usecols=(6, 7), unpack=True) + +change = np.diff(c) +print "Change", change + +signs = np.sign(change) +print "Signs", signs + +pieces = np.piecewise(change, [change < 0, change > 0], [-1, 1]) +print "Pieces", pieces + +print "Arrays equal?", np.array_equal(signs, pieces) + +print "On balance volume", v[1:] * signs + diff --git a/NumPy/Code/ch4code/ch4code/polynomials.py b/NumPy/Code/ch4code/ch4code/polynomials.py new file mode 100644 index 0000000..7231671 --- /dev/null +++ b/NumPy/Code/ch4code/ch4code/polynomials.py @@ -0,0 +1,28 @@ +import numpy as np +import sys +from matplotlib.pyplot import plot +from matplotlib.pyplot import show + +bhp=np.loadtxt('BHP.csv', delimiter=',', usecols=(6,), unpack=True) + +vale=np.loadtxt('VALE.csv', delimiter=',', usecols=(6,), unpack=True) + +t = np.arange(len(bhp)) +poly = np.polyfit(t, bhp - vale, int(sys.argv[1])) +print "Polynomial fit", poly + +print "Next value", np.polyval(poly, t[-1] + 1) + +print "Roots", np.roots(poly) + +der = np.polyder(poly) +print "Derivative", der + +print "Extremas", np.roots(der) +vals = np.polyval(poly, t) +print np.argmax(vals) +print np.argmin(vals) + +plot(t, bhp - vale) +plot(t, vals) +show() diff --git a/NumPy/Code/ch4code/ch4code/simulation.py b/NumPy/Code/ch4code/ch4code/simulation.py new file mode 100644 index 0000000..a84cd2e --- /dev/null +++ b/NumPy/Code/ch4code/ch4code/simulation.py @@ -0,0 +1,30 @@ +import numpy as np +import sys + +o, h, l, c = np.loadtxt('BHP.csv', delimiter=',', usecols=(3, 4, 5, 6), unpack=True) + +def calc_profit(open, high, low, close): + #buy just below the open + buy = open * float(sys.argv[1]) + + # daily range + if low < buy < high: + return (close - buy)/buy + else: + return 0 + +func = np.vectorize(calc_profit) +profits = func(o, h, l, c) +print "Profits", profits + +real_trades = profits[profits != 0] +print "Number of trades", len(real_trades), round(100.0 * len(real_trades)/len(c), 2), "%" +print "Average profit/loss %", round(np.mean(real_trades) * 100, 2) + +winning_trades = profits[profits > 0] +print "Number of winning trades", len(winning_trades), round(100.0 * len(winning_trades)/len(c), 2), "%" +print "Average profit %", round(np.mean(winning_trades) * 100, 2) + +losing_trades = profits[profits < 0] +print "Number of losing trades", len(losing_trades), round(100.0 * len(losing_trades)/len(c), 2), "%" +print "Average loss %", round(np.mean(losing_trades) * 100, 2) diff --git a/NumPy/Code/ch4code/ch4code/smoothing.py b/NumPy/Code/ch4code/ch4code/smoothing.py new file mode 100644 index 0000000..1d1c24e --- /dev/null +++ b/NumPy/Code/ch4code/ch4code/smoothing.py @@ -0,0 +1,42 @@ +import numpy as np +import sys +from matplotlib.pyplot import plot +from matplotlib.pyplot import show + +N = int(sys.argv[1]) + +weights = np.hanning(N) +print "Weights", weights + +bhp = np.loadtxt('BHP.csv', delimiter=',', usecols=(6,), unpack=True) +bhp_returns = np.diff(bhp) / bhp[ : -1] +smooth_bhp = np.convolve(weights/weights.sum(), bhp_returns)[N-1:-N+1] + +vale = np.loadtxt('VALE.csv', delimiter=',', usecols=(6,), unpack=True) +vale_returns = np.diff(vale) / vale[ : -1] +smooth_vale = np.convolve(weights/weights.sum(), vale_returns)[N-1:-N+1] + +K = int(sys.argv[1]) +t = np.arange(N - 1, len(bhp_returns)) +poly_bhp = np.polyfit(t, smooth_bhp, K) +poly_vale = np.polyfit(t, smooth_vale, K) + +poly_sub = np.polysub(poly_bhp, poly_vale) +xpoints = np.roots(poly_sub) +print "Intersection points", xpoints + +reals = np.isreal(xpoints) +print "Real number?", reals + +xpoints = np.select([reals], [xpoints]) +xpoints = xpoints.real +print "Real intersection points", xpoints + +print "Sans 0s", np.trim_zeros(xpoints) + +plot(t, bhp_returns[N-1:], lw=1.0) +plot(t, smooth_bhp, lw=2.0) + +plot(t, vale_returns[N-1:], lw=1.0) +plot(t, smooth_vale, lw=2.0) +show() diff --git a/NumPy/Code/ch5code/README b/NumPy/Code/ch5code/README new file mode 100644 index 0000000..fdd488b --- /dev/null +++ b/NumPy/Code/ch5code/README @@ -0,0 +1,40 @@ +Chapter 5 Working with matrices and ufuncs + +The accompanying code demonstrates NumPy matrices and universal functions. +These are the Python scripts: + +answer42.py Returns 42. + Run with python answer42.py + +bittwiddling.py Demonstrates bitwise and comparison functions. + Run with python bittwiddling.py + +bmatcreation.py Demonstrates matrix creation with the bmat function. + Run with python bmatcreation.py + +dividing.py Demonstrates the NumPy division functions. + Run with python dividing.py + +fibonacci.py Shows calculation of Fibonacci numbers. + Run with python fibonacci.py + +lissajous.py Draws Lissajous figuresa and requires Matplotlib. + Run with python lissajous.py 9 8 + +matrixcreation.py Demonstrates matrix creation. + Run with python matrixcreation.py + +modulo.py Demonstrates the modulo operation. + Run with python modulo.py + +sawtooth.py Draws a sawtooth wave and requires Matplotlib. + Run with python sawtooth.py 99 + +squarewave.py Draws a square wave and requires Matplotlib. + Run with python squarewave.py 99 + +ufuncattributes.py Demonstrates the attributes of universal functions. + Run with python ufuncattributes.py + +ufuncmethods.py Demonstrates universal functions methods. + Run with python ufuncmethods.py diff --git a/NumPy/Code/ch5code/answer42.py b/NumPy/Code/ch5code/answer42.py new file mode 100644 index 0000000..1ce1a2d --- /dev/null +++ b/NumPy/Code/ch5code/answer42.py @@ -0,0 +1,12 @@ +import numpy as np + +def ultimate_answer(a): + result = np.zeros_like(a) + result.flat = 42 + + return result + +ufunc = np.frompyfunc(ultimate_answer, 1, 1) +print "The answer", ufunc(np.arange(4)) + +print "The answer", ufunc(np.arange(4).reshape(2, 2)) diff --git a/NumPy/Code/ch5code/bittwiddling.py b/NumPy/Code/ch5code/bittwiddling.py new file mode 100644 index 0000000..0ac8c44 --- /dev/null +++ b/NumPy/Code/ch5code/bittwiddling.py @@ -0,0 +1,10 @@ +import numpy as np + +x = np.arange(-9, 9) +y = -x +print "Sign different?", (x ^ y) < 0 +print "Sign different?", np.less(np.bitwise_xor(x, y), 0) +print "Power of 2?\n", x, "\n", (x & (x - 1)) == 0 +print "Power of 2?\n", x, "\n", np.equal(np.bitwise_and(x, (x - 1)), 0) +print "Modulus 4\n", x, "\n", x & ((1 << 2) - 1) +print "Modulus 4\n", x, "\n", np.bitwise_and(x, np.left_shift(1, 2) - 1) diff --git a/NumPy/Code/ch5code/bmatcreation.py b/NumPy/Code/ch5code/bmatcreation.py new file mode 100644 index 0000000..b32b719 --- /dev/null +++ b/NumPy/Code/ch5code/bmatcreation.py @@ -0,0 +1,8 @@ +import numpy as np + +A = np.eye(2) +print "A", A +B = 2 * A +print "B", B +print "Compound matrix\n", np.bmat("A B; A B") + diff --git a/NumPy/Code/ch5code/dividing.py b/NumPy/Code/ch5code/dividing.py new file mode 100644 index 0000000..bd03836 --- /dev/null +++ b/NumPy/Code/ch5code/dividing.py @@ -0,0 +1,14 @@ +from __future__ import division +import numpy as np + +a = np.array([2, 6, 5]) +b = np.array([1, 2, 3]) + +print "Divide", np.divide(a, b), np.divide(b, a) +print "True Divide", np.true_divide(a, b), np.true_divide(b, a) +print "Floor Divide", np.floor_divide(a, b), np.floor_divide(b, a) +c = 3.14 * b +print "Floor Divide 2", np.floor_divide(c, b), np.floor_divide(b, c) +print "/ operator", a/b, b/a +print "// operator", a//b, b//a +print "// operator 2", c//b, b//c diff --git a/NumPy/Code/ch5code/fibonacci.py b/NumPy/Code/ch5code/fibonacci.py new file mode 100644 index 0000000..344474b --- /dev/null +++ b/NumPy/Code/ch5code/fibonacci.py @@ -0,0 +1,13 @@ +import numpy as np + +F = np.matrix([[1, 1], [1, 0]]) +print "F", F +print "8th Fibonacci", (F ** 7)[0, 0] +n = np.arange(1, 9) + +sqrt5 = np.sqrt(5) +phi = (1 + sqrt5)/2 +fibonacci = np.rint((phi**n - (-1/phi)**n)/sqrt5) +print "Fibonacci", fibonacci + + diff --git a/NumPy/Code/ch5code/lissajous.py b/NumPy/Code/ch5code/lissajous.py new file mode 100644 index 0000000..09e69c2 --- /dev/null +++ b/NumPy/Code/ch5code/lissajous.py @@ -0,0 +1,13 @@ +import numpy as np +from matplotlib.pyplot import plot +from matplotlib.pyplot import show +import sys + +a = float(sys.argv[1]) +b = float(sys.argv[2]) +t = np.linspace(-np.pi, np.pi, 201) +x = np.sin(a * t + np.pi/2) +y = np.sin(b * t) +plot(x, y) +show() + diff --git a/NumPy/Code/ch5code/matrixcreation.py b/NumPy/Code/ch5code/matrixcreation.py new file mode 100644 index 0000000..51a3168 --- /dev/null +++ b/NumPy/Code/ch5code/matrixcreation.py @@ -0,0 +1,9 @@ +import numpy as np + +A = np.mat('1 2 3; 4 5 6; 7 8 9') +print "Creation from string", A +print "transpose A", A.T +print "Inverse A", A.I +print "Check Inverse", A * A.I + +print "Creation from array", np.mat(np.arange(9).reshape(3, 3)) diff --git a/NumPy/Code/ch5code/modulo.py b/NumPy/Code/ch5code/modulo.py new file mode 100644 index 0000000..05e09b2 --- /dev/null +++ b/NumPy/Code/ch5code/modulo.py @@ -0,0 +1,8 @@ +import numpy as np + +a = np.arange(-4, 4) + +print "Remainder", np.remainder(a, 2) +print "Mod", np.mod(a, 2) +print "% operator", a % 2 +print "Fmod", np.fmod(a, 2) diff --git a/NumPy/Code/ch5code/sawtooth.py b/NumPy/Code/ch5code/sawtooth.py new file mode 100644 index 0000000..a6c5758 --- /dev/null +++ b/NumPy/Code/ch5code/sawtooth.py @@ -0,0 +1,16 @@ +import numpy as np +from matplotlib.pyplot import plot +from matplotlib.pyplot import show +import sys + +t = np.linspace(-np.pi, np.pi, 201) +k = np.arange(1, float(sys.argv[1])) +f = np.zeros_like(t) + +for i in range(len(t)): + f[i] = np.sum(np.sin(2 * np.pi * k * t[i])/k) + +f = (-2 / np.pi) * f +plot(t, f, lw=1.0) +plot(t, np.abs(f), lw=2.0) +show() diff --git a/NumPy/Code/ch5code/squarewave.py b/NumPy/Code/ch5code/squarewave.py new file mode 100644 index 0000000..0c58e4a --- /dev/null +++ b/NumPy/Code/ch5code/squarewave.py @@ -0,0 +1,16 @@ +import numpy as np +from matplotlib.pyplot import plot +from matplotlib.pyplot import show +import sys + +t = np.linspace(-np.pi, np.pi, 201) +k = np.arange(1, float(sys.argv[1])) +k = 2 * k - 1 +f = np.zeros_like(t) + +for i in range(len(t)): + f[i] = np.sum(np.sin(k * t[i])/k) + +f = (4 / np.pi) * f +plot(t, f) +show() diff --git a/NumPy/Code/ch5code/trigonometry.py b/NumPy/Code/ch5code/trigonometry.py new file mode 100644 index 0000000..f29cf8c --- /dev/null +++ b/NumPy/Code/ch5code/trigonometry.py @@ -0,0 +1,7 @@ +import numpy as np +from pylab import * + +x = np.linspace(-np.pi, np.pi, 2001) +sines = np.sin(x) +hist(np.arctanh(np.clip(sines, -0.999, 0.999)), bins=40) +show() diff --git a/NumPy/Code/ch5code/ufuncattributes.py b/NumPy/Code/ch5code/ufuncattributes.py new file mode 100644 index 0000000..a7489e7 --- /dev/null +++ b/NumPy/Code/ch5code/ufuncattributes.py @@ -0,0 +1,9 @@ +import numpy as np + +print "Docstring", np.add.__doc__ +print "Name", np.add.__name__ +print "Nin", np.add.nin +print "Nout", np.add.nout +print "Nargs", np.add.nargs +print "Ntypes", np.add.ntypes +print "Types", np.add.types diff --git a/NumPy/Code/ch5code/ufuncmethods.py b/NumPy/Code/ch5code/ufuncmethods.py new file mode 100644 index 0000000..e83dcfb --- /dev/null +++ b/NumPy/Code/ch5code/ufuncmethods.py @@ -0,0 +1,12 @@ +import numpy as np + +a = np.arange(9) + +print "Reduce", np.add.reduce(a) +print "Accumulate", np.add.accumulate(a) +print "Reduceat", np.add.reduceat(a, [0, 5, 2, 7]) +print "Reduceat step I", np.add.reduce(a[0:5]) +print "Reduceat step II", a[5] +print "Reduceat step III", np.add.reduce(a[2:7]) +print "Reduceat step IV", np.add.reduce(a[7:]) +print "Outer", np.add.outer(np.arange(3), a) diff --git a/NumPy/Code/ch6code/README b/NumPy/Code/ch6code/README new file mode 100644 index 0000000..9a00df5 --- /dev/null +++ b/NumPy/Code/ch6code/README @@ -0,0 +1,47 @@ +NumPy Beginner's Guide +Chapter 6 Move further with NumPy Modules + +The following scripts below are part of the example code. +Python and NumPy is required for most of the scripts, +for some Matplotlib is also needed. + + +Every script can be run with the command + +python + +decomposition.py + Demonstrates the svd function. + +determinant.py + Demonstrates the det function. + +eigenvalues.py + Demonstrates the eig and eigvals functions. + +fourier.py + Demonstrates the fft and ifft functions. + +fouriershift.py + Demonstrates the fftshift and ifftshift functions. + +headortail.py + Demonstrates the binomial function. + +inversion.py + Demonstrates the inv function. + +lognormaldist.py + Demonstrates the lognormal function. + +normaldist.py + Demonstrates the normal function. + +pseudoinversion.py + Demonstrates the pinv function. + +solution.py + Demonstrates the solve function. + +urn.py + Demonstrates the hypergeometric function. diff --git a/NumPy/Code/ch6code/decomposition.py b/NumPy/Code/ch6code/decomposition.py new file mode 100644 index 0000000..0c5c224 --- /dev/null +++ b/NumPy/Code/ch6code/decomposition.py @@ -0,0 +1,17 @@ +import numpy as np + +A = np.mat("4 11 14;8 7 -2") +print "A\n", A + +U, Sigma, V = np.linalg.svd(A, full_matrices=False) + +print "U" +print U + +print "Sigma" +print Sigma + +print "V" +print V + +print "Product\n", U * np.diag(Sigma) * V diff --git a/NumPy/Code/ch6code/determinant.py b/NumPy/Code/ch6code/determinant.py new file mode 100644 index 0000000..761131b --- /dev/null +++ b/NumPy/Code/ch6code/determinant.py @@ -0,0 +1,6 @@ +import numpy as np + +A = np.mat("3 4;5 6") +print "A\n", A + +print "Determinant", np.linalg.det(A) diff --git a/NumPy/Code/ch6code/dice.py b/NumPy/Code/ch6code/dice.py new file mode 100644 index 0000000..b10ce64 --- /dev/null +++ b/NumPy/Code/ch6code/dice.py @@ -0,0 +1,17 @@ +import numpy as np +from pylab import * + +N = 10000 +outcome = np.random.geometric(1.0/6.0, size=N) +enemy = np.zeros(N) +enemy[0] = outcome[0] + +for i in range(1, len(outcome)): + enemy[i] = outcome[i] + enemy[i - 1] + +us = 6 * np.arange(1, len(enemy) + 1) +t = np.arange(N) + +plot(t, enemy, lw=1) +plot(t, us, lw=2) +show() diff --git a/NumPy/Code/ch6code/eigenvalues.py b/NumPy/Code/ch6code/eigenvalues.py new file mode 100644 index 0000000..f298ab0 --- /dev/null +++ b/NumPy/Code/ch6code/eigenvalues.py @@ -0,0 +1,15 @@ +import numpy as np + +A = np.mat("3 -2;1 0") +print "A\n", A + +print "Eigenvalues", np.linalg.eigvals(A) + +eigenvalues, eigenvectors = np.linalg.eig(A) +print "First tuple of eig", eigenvalues +print "Second tuple of eig\n", eigenvectors + +for i in range(len(eigenvalues)): + print "Left", np.dot(A, eigenvectors[:,i]) + print "Right", eigenvalues[i] * eigenvectors[:,i] + print diff --git a/NumPy/Code/ch6code/fourier.py b/NumPy/Code/ch6code/fourier.py new file mode 100644 index 0000000..1c11813 --- /dev/null +++ b/NumPy/Code/ch6code/fourier.py @@ -0,0 +1,10 @@ +import numpy as np +from matplotlib.pyplot import plot, show + +x = np.linspace(0, 2 * np.pi, 30) +wave = np.cos(x) +transformed = np.fft.fft(wave) +print np.all(np.abs(np.fft.ifft(transformed) - wave) < 10 ** -9) + +plot(transformed) +show() diff --git a/NumPy/Code/ch6code/fouriershift.py b/NumPy/Code/ch6code/fouriershift.py new file mode 100644 index 0000000..4adaef1 --- /dev/null +++ b/NumPy/Code/ch6code/fouriershift.py @@ -0,0 +1,12 @@ +import numpy as np +from matplotlib.pyplot import plot, show + +x = np.linspace(0, 2 * np.pi, 30) +wave = np.cos(x) +transformed = np.fft.fft(wave) +shifted = np.fft.fftshift(transformed) +print np.all(np.abs(np.fft.ifftshift(shifted) - transformed) < 10 ** -9) + +plot(transformed, lw=2) +plot(shifted, lw=3) +show() diff --git a/NumPy/Code/ch6code/headortail.py b/NumPy/Code/ch6code/headortail.py new file mode 100644 index 0000000..4a81017 --- /dev/null +++ b/NumPy/Code/ch6code/headortail.py @@ -0,0 +1,20 @@ +import numpy as np +from matplotlib.pyplot import plot, show + +cash = np.zeros(10000) +cash[0] = 1000 +outcome = np.random.binomial(9, 0.5, size=len(cash)) + +for i in range(1, len(cash)): + + if outcome[i] < 5: + cash[i] = cash[i - 1] - 1 + elif outcome[i] < 10: + cash[i] = cash[i - 1] + 1 + else: + raise AssertionError("Unexpected outcome " + outcome) + +print outcome.min(), outcome.max() + +plot(np.arange(len(cash)), cash) +show() diff --git a/NumPy/Code/ch6code/inversion.py b/NumPy/Code/ch6code/inversion.py new file mode 100644 index 0000000..8b7edc5 --- /dev/null +++ b/NumPy/Code/ch6code/inversion.py @@ -0,0 +1,9 @@ +import numpy as np + +A = np.mat("0 1 2;1 0 3;4 -3 8") +print "A\n", A + +inverse = np.linalg.inv(A) +print "inverse of A\n", inverse + +print "Check\n", A * inverse diff --git a/NumPy/Code/ch6code/lognormaldist.py b/NumPy/Code/ch6code/lognormaldist.py new file mode 100644 index 0000000..b643ee9 --- /dev/null +++ b/NumPy/Code/ch6code/lognormaldist.py @@ -0,0 +1,12 @@ +import numpy as np +import matplotlib.pyplot as plt + +N=10000 +lognormal_values = np.random.lognormal(size=N) +dummy, bins, dummy = plt.hist(lognormal_values, np.sqrt(N), normed=True, lw=1) +sigma = 1 +mu = 0 +x = np.linspace(min(bins), max(bins), len(bins)) +pdf = np.exp(-(np.log(x) - mu)**2 / (2 * sigma**2))/ (x * sigma * np.sqrt(2 * np.pi)) +plt.plot(x, pdf,lw=3) +plt.show() diff --git a/NumPy/Code/ch6code/normaldist.py b/NumPy/Code/ch6code/normaldist.py new file mode 100644 index 0000000..ed5ce32 --- /dev/null +++ b/NumPy/Code/ch6code/normaldist.py @@ -0,0 +1,11 @@ +import numpy as np +import matplotlib.pyplot as plt + +N=10000 + +normal_values = np.random.normal(size=N) +dummy, bins, dummy = plt.hist(normal_values, np.sqrt(N), normed=True, lw=1) +sigma = 1 +mu = 0 +plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) * np.exp( - (bins - mu)**2 / (2 * sigma**2) ),lw=2) +plt.show() diff --git a/NumPy/Code/ch6code/pseudoinversion.py b/NumPy/Code/ch6code/pseudoinversion.py new file mode 100644 index 0000000..1726d25 --- /dev/null +++ b/NumPy/Code/ch6code/pseudoinversion.py @@ -0,0 +1,9 @@ +import numpy as np + +A = np.mat("4 11 14;8 7 -2") +print "A\n", A + +pseudoinv = np.linalg.pinv(A) +print "Pseudo inverse\n", pseudoinv + +print "Check", A * pseudoinv diff --git a/NumPy/Code/ch6code/solution.py b/NumPy/Code/ch6code/solution.py new file mode 100644 index 0000000..c09e443 --- /dev/null +++ b/NumPy/Code/ch6code/solution.py @@ -0,0 +1,12 @@ +import numpy as np + +A = np.mat("1 -2 1;0 2 -8;-4 5 9") +print "A\n", A + +b = np.array([0, 8, -9]) +print "b\n", b + +x = np.linalg.solve(A, b) +print "Solution", x + +print "Check\n", np.dot(A , x) diff --git a/NumPy/Code/ch6code/urn.py b/NumPy/Code/ch6code/urn.py new file mode 100644 index 0000000..e4b4c25 --- /dev/null +++ b/NumPy/Code/ch6code/urn.py @@ -0,0 +1,16 @@ +import numpy as np +from matplotlib.pyplot import plot, show + +points = np.zeros(100) +outcomes = np.random.hypergeometric(25, 1, 3, size=len(points)) + +for i in range(len(points)): + if outcomes[i] == 3: + points[i] = points[i - 1] + 1 + elif outcomes[i] == 2: + points[i] = points[i - 1] - 6 + else: + print outcomes[i] + +plot(np.arange(len(points)), points) +show() diff --git a/NumPy/Code/ch7code/.DS_Store b/NumPy/Code/ch7code/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/NumPy/Code/ch7code/.DS_Store differ diff --git a/NumPy/Code/ch7code/AAPL.csv b/NumPy/Code/ch7code/AAPL.csv new file mode 100644 index 0000000..a8d1222 --- /dev/null +++ b/NumPy/Code/ch7code/AAPL.csv @@ -0,0 +1,30 @@ +AAPL,28-01-2011, ,344.17,344.4,333.53,336.1,21144800 +AAPL,31-01-2011, ,335.8,340.04,334.3,339.32,13473000 +AAPL,01-02-2011, ,341.3,345.65,340.98,345.03,15236800 +AAPL,02-02-2011, ,344.45,345.25,343.55,344.32,9242600 +AAPL,03-02-2011, ,343.8,344.24,338.55,343.44,14064100 +AAPL,04-02-2011, ,343.61,346.7,343.51,346.5,11494200 +AAPL,07-02-2011, ,347.89,353.25,347.64,351.88,17322100 +AAPL,08-02-2011, ,353.68,355.52,352.15,355.2,13608500 +AAPL,09-02-2011, ,355.19,359,354.87,358.16,17240800 +AAPL,10-02-2011, ,357.39,360,348,354.54,33162400 +AAPL,11-02-2011, ,354.75,357.8,353.54,356.85,13127500 +AAPL,14-02-2011, ,356.79,359.48,356.71,359.18,11086200 +AAPL,15-02-2011, ,359.19,359.97,357.55,359.9,10149000 +AAPL,16-02-2011, ,360.8,364.9,360.5,363.13,17184100 +AAPL,17-02-2011, ,357.1,360.27,356.52,358.3,18949000 +AAPL,18-02-2011, ,358.21,359.5,349.52,350.56,29144500 +AAPL,22-02-2011, ,342.05,345.4,337.72,338.61,31162200 +AAPL,23-02-2011, ,338.77,344.64,338.61,342.62,23994700 +AAPL,24-02-2011, ,344.02,345.15,338.37,342.88,17853500 +AAPL,25-02-2011, ,345.29,348.43,344.8,348.16,13572000 +AAPL,28-02-2011, ,351.21,355.05,351.12,353.21,14395400 +AAPL,01-03-2011, ,355.47,355.72,347.68,349.31,16290300 +AAPL,02-03-2011, ,349.96,354.35,348.4,352.12,21521000 +AAPL,03-03-2011, ,357.2,359.79,355.92,359.56,17885200 +AAPL,04-03-2011, ,360.07,360.29,357.75,360,16188000 +AAPL,07-03-2011, ,361.11,361.67,351.31,355.36,19504300 +AAPL,08-03-2011, ,354.91,357.4,352.25,355.76,12718000 +AAPL,09-03-2011, ,354.69,354.76,350.6,352.47,16192700 +AAPL,10-03-2011, ,349.69,349.77,344.9,346.67,18138800 +AAPL,11-03-2011, ,345.4,352.32,345,351.99,16824200 diff --git a/NumPy/Code/ch7code/README b/NumPy/Code/ch7code/README new file mode 100644 index 0000000..e4b7b1a --- /dev/null +++ b/NumPy/Code/ch7code/README @@ -0,0 +1,27 @@ +NumPy Beginner's Guide + +Chapter 7 Peeking into special functions + +Execute the Python example scripts of this chapter +with + + python + +bessel0.py Demonstrates the i0 function. +extracted.py Demonstrates the extract function. +futurevalue.py Demonstrates the fv function. +interestrate.py Demonstrates the rate function. +internalratereturn.py Demonstrates the irr function. +lex.py Demonstrates the lexsort function. +netpresentvalue.py Demonstrates npv function. +numberpayments.py Demonstrates the nper function. +payment.py Demonstrates the pmt function. +plot_bartlett.py Demonstrates the bartlett function. +plot_blackman.py Demonstrates the blackman function. +plot_hamming.py Demonstrates the hamming function. +plot_kaiser.py Demonstrates the kaiser function. +plot_sinc.py Demonstrates the sinc function. +presentvalue.py Demonstrates the pv function. +sinc2d.py Demonstrates the sinc function in 2D. +sortcomplex.py Demonstrates the sort_complex function. +sortedsearch.py Demonstrates the searchsorted function. diff --git a/NumPy/Code/ch7code/bessel0.py b/NumPy/Code/ch7code/bessel0.py new file mode 100644 index 0000000..bbcad3f --- /dev/null +++ b/NumPy/Code/ch7code/bessel0.py @@ -0,0 +1,8 @@ +import numpy as np +from pylab import * + +x = np.linspace(0, 4, 100) +vals = np.i0(x) + +plot(x, vals) +show() diff --git a/NumPy/Code/ch7code/extracted.py b/NumPy/Code/ch7code/extracted.py new file mode 100644 index 0000000..8602e8d --- /dev/null +++ b/NumPy/Code/ch7code/extracted.py @@ -0,0 +1,6 @@ +import numpy as np + +a = np.arange(7) +condition = (a % 2) == 0 +print "Even numbers", np.extract(condition, a) +print "Non zero", np.nonzero(a) diff --git a/NumPy/Code/ch7code/futurevalue.py b/NumPy/Code/ch7code/futurevalue.py new file mode 100644 index 0000000..5470822 --- /dev/null +++ b/NumPy/Code/ch7code/futurevalue.py @@ -0,0 +1,12 @@ +import numpy as np +from matplotlib.pyplot import plot, show + +print "Future value", np.fv(0.03/4, 5 * 4, -10, -1000) + +fvals = [] + +for i in xrange(1, 10): + fvals.append(np.fv(.03/4, i * 4, -10, -1000)) + +plot(fvals, 'bo') +show() diff --git a/NumPy/Code/ch7code/interestrate.py b/NumPy/Code/ch7code/interestrate.py new file mode 100644 index 0000000..4beacc4 --- /dev/null +++ b/NumPy/Code/ch7code/interestrate.py @@ -0,0 +1,3 @@ +import numpy as np + +print "Interest rate", 12 * np.rate(167, -100, 9000, 0) diff --git a/NumPy/Code/ch7code/internalratereturn.py b/NumPy/Code/ch7code/internalratereturn.py new file mode 100644 index 0000000..ebd4725 --- /dev/null +++ b/NumPy/Code/ch7code/internalratereturn.py @@ -0,0 +1,3 @@ +import numpy as np + +print "Internal rate of return", np.irr([-100, 38, 48, 90, 17, 36]) diff --git a/NumPy/Code/ch7code/lex.py b/NumPy/Code/ch7code/lex.py new file mode 100644 index 0000000..07eb56e --- /dev/null +++ b/NumPy/Code/ch7code/lex.py @@ -0,0 +1,11 @@ +import numpy as np +import datetime + +def datestr2num(s): + return datetime.datetime.strptime(s, "%d-%m-%Y").toordinal() + +dates,closes=np.loadtxt('AAPL.csv', delimiter=',', usecols=(1, 6), converters={1:datestr2num}, unpack=True) +indices = np.lexsort((dates, closes)) + +print "Indices", indices +print ["%s %s" % (datetime.date.fromordinal(int(dates[i])), closes[i]) for i in indices] diff --git a/NumPy/Code/ch7code/modifiedratereturn.py b/NumPy/Code/ch7code/modifiedratereturn.py new file mode 100644 index 0000000..9838c9a --- /dev/null +++ b/NumPy/Code/ch7code/modifiedratereturn.py @@ -0,0 +1,3 @@ +import numpy as np + +print "Modified internal rate of return", np.mirr([-100, 38, 48, 90, 17, 36], 0.03, 0.03) diff --git a/NumPy/Code/ch7code/netpresentvalue.py b/NumPy/Code/ch7code/netpresentvalue.py new file mode 100644 index 0000000..b9797ed --- /dev/null +++ b/NumPy/Code/ch7code/netpresentvalue.py @@ -0,0 +1,7 @@ +import numpy as np + +cashflows = np.random.randint(100, size=5) +cashflows = np.insert(cashflows, 0, -100) +print "Cashflows", cashflows + +print "Net present value", np.npv(0.03, cashflows) diff --git a/NumPy/Code/ch7code/numberpayments.py b/NumPy/Code/ch7code/numberpayments.py new file mode 100644 index 0000000..6eadaae --- /dev/null +++ b/NumPy/Code/ch7code/numberpayments.py @@ -0,0 +1,3 @@ +import numpy as np + +print "Number of payments", np.nper(0.10/12, -100, 9000) diff --git a/NumPy/Code/ch7code/payment.py b/NumPy/Code/ch7code/payment.py new file mode 100644 index 0000000..b53f366 --- /dev/null +++ b/NumPy/Code/ch7code/payment.py @@ -0,0 +1,3 @@ +import numpy as np + +print "Payment", np.pmt(0.01/12, 12 * 30, 10000000) diff --git a/NumPy/Code/ch7code/plot_bartlett.py b/NumPy/Code/ch7code/plot_bartlett.py new file mode 100644 index 0000000..badf5ac --- /dev/null +++ b/NumPy/Code/ch7code/plot_bartlett.py @@ -0,0 +1,6 @@ +import numpy as np +from matplotlib.pyplot import plot, show + +window = np.bartlett(42) +plot(window) +show() diff --git a/NumPy/Code/ch7code/plot_blackman.py b/NumPy/Code/ch7code/plot_blackman.py new file mode 100644 index 0000000..11f6004 --- /dev/null +++ b/NumPy/Code/ch7code/plot_blackman.py @@ -0,0 +1,14 @@ +import numpy as np +from matplotlib.pyplot import plot, show, legend +from matplotlib.dates import datestr2num +import sys + + +closes=np.loadtxt('AAPL.csv', delimiter=',', usecols=(6,), converters={1:datestr2num}, unpack=True) +N = int(sys.argv[1]) +window = np.blackman(N) +smoothed = np.convolve(window/window.sum(), closes, mode='same') +plot(smoothed[N:-N], lw=2, label="smoothed") +plot(closes[N:-N], label="closes") +legend(loc='best') +show() diff --git a/NumPy/Code/ch7code/plot_hamming.py b/NumPy/Code/ch7code/plot_hamming.py new file mode 100644 index 0000000..9bc3cdb --- /dev/null +++ b/NumPy/Code/ch7code/plot_hamming.py @@ -0,0 +1,7 @@ +import numpy as np +from pylab import * + +window = np.hamming(42) +plot(window) +show() + diff --git a/NumPy/Code/ch7code/plot_kaiser.py b/NumPy/Code/ch7code/plot_kaiser.py new file mode 100644 index 0000000..130a001 --- /dev/null +++ b/NumPy/Code/ch7code/plot_kaiser.py @@ -0,0 +1,6 @@ +import numpy as np +from pylab import * + +window = np.kaiser(42, 0) +plot(window) +show() diff --git a/NumPy/Code/ch7code/plot_sinc.py b/NumPy/Code/ch7code/plot_sinc.py new file mode 100644 index 0000000..6549777 --- /dev/null +++ b/NumPy/Code/ch7code/plot_sinc.py @@ -0,0 +1,8 @@ +import numpy as np +from matplotlib.pyplot import plot, show + +x = np.linspace(0, 4, 100) +vals = np.sinc(x) + +plot(x, vals) +show() diff --git a/NumPy/Code/ch7code/presentvalue.py b/NumPy/Code/ch7code/presentvalue.py new file mode 100644 index 0000000..b076bf0 --- /dev/null +++ b/NumPy/Code/ch7code/presentvalue.py @@ -0,0 +1,3 @@ +import numpy as np + +print "Present value", np.pv(0.03/4, 5 * 4, -10, 1376.09633204) diff --git a/NumPy/Code/ch7code/sinc2d.py b/NumPy/Code/ch7code/sinc2d.py new file mode 100644 index 0000000..6bc8787 --- /dev/null +++ b/NumPy/Code/ch7code/sinc2d.py @@ -0,0 +1,9 @@ +import numpy as np +from matplotlib.pyplot import imshow, show + +x = np.linspace(0, 4, 100) +xx = np.outer(x, x) +vals = np.sinc(xx) + +imshow(vals) +show() diff --git a/NumPy/Code/ch7code/sortcomplex.py b/NumPy/Code/ch7code/sortcomplex.py new file mode 100644 index 0000000..878a883 --- /dev/null +++ b/NumPy/Code/ch7code/sortcomplex.py @@ -0,0 +1,7 @@ +import numpy as np + +np.random.seed(42) +complex_numbers = np.random.random(5) + 1j * np.random.random(5) +print "Complex numbers\n", complex_numbers + +print "Sorted\n", np.sort_complex(complex_numbers) diff --git a/NumPy/Code/ch7code/sortedsearch.py b/NumPy/Code/ch7code/sortedsearch.py new file mode 100644 index 0000000..fbd3152 --- /dev/null +++ b/NumPy/Code/ch7code/sortedsearch.py @@ -0,0 +1,7 @@ +import numpy as np + +a = np.arange(5) +indices = np.searchsorted(a, [-2, 7]) +print "Indices", indices + +print "The full array", np.insert(a, indices, [-2, 7]) diff --git a/NumPy/Code/ch8code/README b/NumPy/Code/ch8code/README new file mode 100644 index 0000000..3f20b2f --- /dev/null +++ b/NumPy/Code/ch8code/README @@ -0,0 +1,17 @@ +NumPy Beginner's Guide + +Chapter 8 Assuring quality with testing + +NumPy 1.3+ required. Run the scripts with + +python + +almostequal.py Demonstrates the assert_almost_equal function. +approxequal.py Demonstrates the assert_approx_equal function. +arrayalmostequal.py Demonstrates the assert_array_almost_equal function. +arrayequal.py Demonstrates the assert_array_equal function. +arrayless.py Demonstrates the assert_array_less function. +equal.py Demonstrates the assert_equal function. +maxulp.py Demonstrates the assert_array_max_ulp function. +nulp.py Demonstrates the assert_almost_equal_nulp function. +stringequal.py Demonstrates the assert_string_equal function. diff --git a/NumPy/Code/ch8code/almostequal.py b/NumPy/Code/ch8code/almostequal.py new file mode 100644 index 0000000..a47bf81 --- /dev/null +++ b/NumPy/Code/ch8code/almostequal.py @@ -0,0 +1,4 @@ +import numpy as np + +print "Decimal 6", np.testing.assert_almost_equal(0.123456789, 0.123456780, decimal=7) +print "Decimal 7", np.testing.assert_almost_equal(0.123456789, 0.123456780, decimal=8) diff --git a/NumPy/Code/ch8code/approxequal.py b/NumPy/Code/ch8code/approxequal.py new file mode 100644 index 0000000..cad07de --- /dev/null +++ b/NumPy/Code/ch8code/approxequal.py @@ -0,0 +1,4 @@ +import numpy as np + +print "Significance 8", np.testing.assert_approx_equal(0.123456789, 0.123456780, significant=8) +print "Significance 9", np.testing.assert_approx_equal(0.123456789, 0.123456780, significant=9) diff --git a/NumPy/Code/ch8code/arrayalmostequal.py b/NumPy/Code/ch8code/arrayalmostequal.py new file mode 100644 index 0000000..2249624 --- /dev/null +++ b/NumPy/Code/ch8code/arrayalmostequal.py @@ -0,0 +1,4 @@ +import numpy as np + +print "Decimal 8", np.testing.assert_array_almost_equal([0, 0.123456789], [0, 0.123456780], decimal=8) +print "Decimal 9", np.testing.assert_array_almost_equal([0, 0.123456789], [0, 0.123456780], decimal=9) diff --git a/NumPy/Code/ch8code/arrayequal.py b/NumPy/Code/ch8code/arrayequal.py new file mode 100644 index 0000000..1262a02 --- /dev/null +++ b/NumPy/Code/ch8code/arrayequal.py @@ -0,0 +1,4 @@ +import numpy as np + +print "Pass", np.testing.assert_allclose([0, 0.123456789, np.nan], [0, 0.123456780, np.nan], rtol=1e-7, atol=0) +print "Fail", np.testing.assert_array_equal([0, 0.123456789, np.nan], [0, 0.123456780, np.nan]) diff --git a/NumPy/Code/ch8code/arrayless.py b/NumPy/Code/ch8code/arrayless.py new file mode 100644 index 0000000..7e3ea36 --- /dev/null +++ b/NumPy/Code/ch8code/arrayless.py @@ -0,0 +1,4 @@ +import numpy as np + +print "Pass", np.testing.assert_array_less([0, 0.123456789, np.nan], [1, 0.23456780, np.nan]) +print "Fail", np.testing.assert_array_less([0, 0.123456789, np.nan], [0, 0.123456780, np.nan]) diff --git a/NumPy/Code/ch8code/decorator_test.py b/NumPy/Code/ch8code/decorator_test.py new file mode 100644 index 0000000..f8839a4 --- /dev/null +++ b/NumPy/Code/ch8code/decorator_test.py @@ -0,0 +1,34 @@ +from numpy.testing.decorators import setastest +from numpy.testing.decorators import skipif +from numpy.testing.decorators import knownfailureif +from numpy.testing import decorate_methods + + +@setastest(False) +def test_false(): + pass + +@setastest(True) +def test_true(): + pass + +@skipif(True) +def test_skip(): + pass + +@knownfailureif(True) +def test_alwaysfail(): + pass + + +class TestClass(): + def test_true2(self): + pass + + +class TestClass2(): + def test_false2(self): + pass + + +decorate_methods(TestClass2, setastest(False), 'test_false2') diff --git a/NumPy/Code/ch8code/decorator_test.pyc b/NumPy/Code/ch8code/decorator_test.pyc new file mode 100644 index 0000000..2bedb59 Binary files /dev/null and b/NumPy/Code/ch8code/decorator_test.pyc differ diff --git a/NumPy/Code/ch8code/docstringtest.py b/NumPy/Code/ch8code/docstringtest.py new file mode 100644 index 0000000..469c5ff --- /dev/null +++ b/NumPy/Code/ch8code/docstringtest.py @@ -0,0 +1,13 @@ +import numpy as np + +def factorial(n): + """ + Test for the factorial of 3 that should pass. + >>> factorial(3) + 6 + + Test for the factorial of 0 that should fail. + >>> factorial(0) + 1 + """ + return np.arange(1, n+1).cumprod()[-1] diff --git a/NumPy/Code/ch8code/docstringtest.pyc b/NumPy/Code/ch8code/docstringtest.pyc new file mode 100644 index 0000000..60a56c7 Binary files /dev/null and b/NumPy/Code/ch8code/docstringtest.pyc differ diff --git a/NumPy/Code/ch8code/equal.py b/NumPy/Code/ch8code/equal.py new file mode 100644 index 0000000..9ecbfb0 --- /dev/null +++ b/NumPy/Code/ch8code/equal.py @@ -0,0 +1,3 @@ +import numpy as np + +print "Equal?", np.testing.assert_equal((1, 2), (1, 3)) diff --git a/NumPy/Code/ch8code/maxulp.py b/NumPy/Code/ch8code/maxulp.py new file mode 100644 index 0000000..a95f5dd --- /dev/null +++ b/NumPy/Code/ch8code/maxulp.py @@ -0,0 +1,6 @@ +import numpy as np + +eps = np.finfo(float).eps +print "EPS", eps +print "1", np.testing.assert_array_max_ulp(1.0, 1.0 + eps) +print "2", np.testing.assert_array_max_ulp(1.0, 1 + 2 * eps, maxulp=2) diff --git a/NumPy/Code/ch8code/nulp.py b/NumPy/Code/ch8code/nulp.py new file mode 100644 index 0000000..5dc8e4f --- /dev/null +++ b/NumPy/Code/ch8code/nulp.py @@ -0,0 +1,6 @@ +import numpy as np + +eps = np.finfo(float).eps +print "EPS", eps +print "1", np.testing.assert_array_almost_equal_nulp(1.0, 1.0 + eps) +print "2", np.testing.assert_array_almost_equal_nulp(1.0, 1.0 + 2 * eps) diff --git a/NumPy/Code/ch8code/stringequal.py b/NumPy/Code/ch8code/stringequal.py new file mode 100644 index 0000000..c7a1fae --- /dev/null +++ b/NumPy/Code/ch8code/stringequal.py @@ -0,0 +1,4 @@ +import numpy as np + +print "Pass", np.testing.assert_string_equal("NumPy", "NumPy") +print "Fail", np.testing.assert_string_equal("NumPy", "Numpy") diff --git a/NumPy/Code/ch8code/unit_test.py b/NumPy/Code/ch8code/unit_test.py new file mode 100644 index 0000000..2c1653b --- /dev/null +++ b/NumPy/Code/ch8code/unit_test.py @@ -0,0 +1,32 @@ +import numpy as np +import unittest + + +def factorial(n): + if n == 0: + return 1 + + if n < 0: + raise ValueError, "Unexpected negative value" + + return np.arange(1, n+1).cumprod() + + +class FactorialTest(unittest.TestCase): + def test_factorial(self): + #Test for the factorial of 3 that should pass. + self.assertEqual(6, factorial(3)[-1]) + np.testing.assert_equal(np.array([1, 2, 6]), factorial(3)) + + def test_zero(self): + #Test for the factorial of 0 that should pass. + self.assertEqual(1, factorial(0)) + + def test_negative(self): + #Test for the factorial of negative numbers that should fail. + # It should throw a ValueError, but we expect IndexError + self.assertRaises(IndexError, factorial(-10)) + + +if __name__ == '__main__': + unittest.main() diff --git a/NumPy/Code/ch9code/README b/NumPy/Code/ch9code/README new file mode 100644 index 0000000..1a5d699 --- /dev/null +++ b/NumPy/Code/ch9code/README @@ -0,0 +1,31 @@ +NumPy Beginner's Guide + +Chapter 9 Plotting with Matplotlib + + +candlesticks.py Demonstrates candlesticks on stock charts. + Run with python candlesticks.py + +emalegend.py Demonstrates plot legends. + Run with python emalegend.py + +fillbetween.py Demonstrates region shading. + Run with python fillbetween.py + +logy.py Demonstrates logplots. + Run with python logy.py + +polyplot.py Demonstrates simple plots. + Run with python polyplot.py + +polyplot2.py Demonstrates simple plots. + Run with python polyplot2.py + +polyplot3.py Demonstrates simple plots. + Run with python polyplot3.py + +scatterprice.py Demonstrates scatter plots. + Run with python scatterprice.py + +stockhistogram.py Demonstrates histograms. + Run with python stockhistogram.py diff --git a/NumPy/Code/ch9code/animation.py b/NumPy/Code/ch9code/animation.py new file mode 100644 index 0000000..f77264a --- /dev/null +++ b/NumPy/Code/ch9code/animation.py @@ -0,0 +1,24 @@ +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.animation as animation + +fig = plt.figure() +ax = fig.add_subplot(111) +N = 10 +x = np.random.rand(N) +y = np.random.rand(N) +z = np.random.rand(N) +circles, triangles, dots = ax.plot(x, 'ro', y, 'g^', z, 'b.') +ax.set_ylim(0, 1) +plt.axis('off') + +def update(data): + circles.set_ydata(data[0]) + triangles.set_ydata(data[1]) + return circles, triangles + +def generate(): + while True: yield np.random.rand(2, N) + +anim = animation.FuncAnimation(fig, update, generate, interval=150) +plt.show() diff --git a/NumPy/Code/ch9code/candlesticks.py b/NumPy/Code/ch9code/candlesticks.py new file mode 100644 index 0000000..439f44a --- /dev/null +++ b/NumPy/Code/ch9code/candlesticks.py @@ -0,0 +1,34 @@ +from matplotlib.dates import DateFormatter +from matplotlib.dates import DayLocator +from matplotlib.dates import MonthLocator +from matplotlib.finance import quotes_historical_yahoo +from matplotlib.finance import candlestick +import sys +from datetime import date +import matplotlib.pyplot as plt + +today = date.today() +start = (today.year - 1, today.month, today.day) + +alldays = DayLocator() +months = MonthLocator() +month_formatter = DateFormatter("%b %Y") + +symbol = 'DISH' + +if len(sys.argv) == 2: + symbol = sys.argv[1] + +quotes = quotes_historical_yahoo(symbol, start, today) + +fig = plt.figure() +ax = fig.add_subplot(111) +ax.xaxis.set_major_locator(months) +ax.xaxis.set_minor_locator(alldays) +ax.xaxis.set_major_formatter(month_formatter) + +candlestick(ax, quotes) +fig.autofmt_xdate() +plt.show() + + diff --git a/NumPy/Code/ch9code/contour.py b/NumPy/Code/ch9code/contour.py new file mode 100644 index 0000000..bff12ef --- /dev/null +++ b/NumPy/Code/ch9code/contour.py @@ -0,0 +1,14 @@ +import matplotlib.pyplot as plt +import numpy as np +from matplotlib import cm + +fig = plt.figure() +ax = fig.add_subplot(111) + +u = np.linspace(-1, 1, 100) + +x, y = np.meshgrid(u, u) +z = x ** 2 + y ** 2 +ax.contourf(x, y, z) + +plt.show() diff --git a/NumPy/Code/ch9code/emalegend.py b/NumPy/Code/ch9code/emalegend.py new file mode 100644 index 0000000..4c6f0c8 --- /dev/null +++ b/NumPy/Code/ch9code/emalegend.py @@ -0,0 +1,60 @@ +from matplotlib.finance import quotes_historical_yahoo +from matplotlib.dates import DateFormatter +from matplotlib.dates import DayLocator +from matplotlib.dates import MonthLocator +import sys +from datetime import date +import matplotlib.pyplot as plt +import numpy as np + +today = date.today() +start = (today.year - 1, today.month, today.day) + +symbol = 'DISH' + +if len(sys.argv) == 2: + symbol = sys.argv[1] + +quotes = quotes_historical_yahoo(symbol, start, today) +quotes = np.array(quotes) +dates = quotes.T[0] +close = quotes.T[4] + + +fig = plt.figure() +ax = fig.add_subplot(111) + +emas = [] +for i in range(9, 18, 3): + weights = np.exp(np.linspace(-1., 0., i)) + weights /= weights.sum() + + ema = np.convolve(weights, close)[i-1:-i+1] + idx = (i - 6)/3 + ax.plot(dates[i-1:], ema, lw=idx, label="EMA(%s)" % (i)) + data = np.column_stack((dates[i-1:], ema)) + emas.append(np.rec.fromrecords(data, names=["dates", "ema"])) + +first = emas[0]["ema"].flatten() +second = emas[1]["ema"].flatten() +bools = np.abs(first[-len(second):] - second)/second < 0.0001 +xpoints = np.compress(bools, emas[1]) + +for xpoint in xpoints: + ax.annotate('x', xy=xpoint, textcoords='offset points', + xytext=(-50, 30), + arrowprops=dict(arrowstyle="->")) + +leg = ax.legend(loc='best', fancybox=True) +leg.get_frame().set_alpha(0.5) + +alldays = DayLocator() +months = MonthLocator() +month_formatter = DateFormatter("%b %Y") +ax.plot(dates, close, lw=1.0, label="Close") +ax.xaxis.set_major_locator(months) +ax.xaxis.set_minor_locator(alldays) +ax.xaxis.set_major_formatter(month_formatter) +ax.grid(True) +fig.autofmt_xdate() +plt.show() diff --git a/NumPy/Code/ch9code/fillbetween.py b/NumPy/Code/ch9code/fillbetween.py new file mode 100644 index 0000000..81c82f7 --- /dev/null +++ b/NumPy/Code/ch9code/fillbetween.py @@ -0,0 +1,38 @@ +from matplotlib.finance import quotes_historical_yahoo +from matplotlib.dates import DateFormatter +from matplotlib.dates import DayLocator +from matplotlib.dates import MonthLocator +import sys +from datetime import date +import matplotlib.pyplot as plt +import numpy as np + +today = date.today() +start = (today.year - 1, today.month, today.day) + +symbol = 'DISH' + +if len(sys.argv) == 2: + symbol = sys.argv[1] + +quotes = quotes_historical_yahoo(symbol, start, today) +quotes = np.array(quotes) +dates = quotes.T[0] +close = quotes.T[4] + + +alldays = DayLocator() +months = MonthLocator() +month_formatter = DateFormatter("%b %Y") + +fig = plt.figure() +ax = fig.add_subplot(111) +ax.plot(dates, close) +plt.fill_between(dates, close.min(), close, where=close>close.mean(), facecolor="green", alpha=0.4) +plt.fill_between(dates, close.min(), close, where=close