Saturday, 16 November 2019

quantopian lecture NumPy

import numpy as np
import matplotlib.pyplot as plt

A = np.array([[1, 2], [3, 4]])
print A, type(A)
print A.shape
#first column
print A[:, 0]
#first row
print A[0, :]
#first row
print A[0]
#second row, second column
print A[1, 1]

------------------------
[[1 2]
 [3 4]] <type 'numpy.ndarray'>
(2, 2)
[1 3]
[1 2]
[1 2]
4

-----------------------------
stock_list = [3.5, 5, 2, 8, 4.2]
returns = np.array(stock_list)
print returns, type(returns)
print np.log(returns)
print np.mean(returns)
print np.max(returns)
returns*2 + 5
print "Mean: ", np.mean(returns), "Std Dev: ", np.std(returns)

---------------------------------
[ 3.5  5.   2.   8.   4.2] <type 'numpy.ndarray'>
[ 1.25276297  1.60943791  0.69314718  2.07944154  1.43508453]
4.54
8.0
Mean:  4.54 Std Dev:  1.99158228552

------------------------------------
N = 10
#create matrix with N rows, 20 columns, all values 0
returns = np.zeros((N, 20))

#generate 20 values (1+-0.1) for each of 10 stocks to simulate 10% fluctuation
for i in range(0, N):
    returns[i] = np.random.normal(1, 0.1, 20)

print returns

-------------------
[[ 1.03106776  1.05468042  0.95484129  1.00791592  0.97503239  1.16924021
   1.03094272  0.88663418  0.98184415  1.12563429  1.10268922  1.04089233
   0.89611761  0.96677619  0.96622677  0.91201808  1.00382794  1.1401533
   0.98819255  1.14371201]
 [ 0.78958078  1.05885922  0.86220874  0.89660524  0.98059216  0.87783413
   0.90657333  1.03980406  1.06212268  0.92187714  0.96084574  0.99757358
   0.89130947  0.98494642  0.98927777  1.07842667  0.89442444  1.01049714
   0.84995483  1.0591395 ]
 [ 1.22490492  0.96571626  1.00179064  1.0928795   1.04838166  0.91973272
   0.97177359  0.91569413  1.17500945  1.14197296  1.10279227  1.01789198
   1.0679357   1.02915363  1.0061314   1.06420138  1.0345859   1.03125553
   0.86506542  1.07629615]
 [ 0.98187678  0.87289137  0.89466529  0.95013111  1.02461151  0.99680892
   0.91350208  0.88012537  0.85386131  0.9482019   0.93380868  0.91333927
   0.94250097  1.15446     1.13059652  1.20503639  0.90927665  0.85362986
   1.14138124  1.06879598]
 [ 1.07101098  0.96595767  1.06083827  1.16424979  0.95199777  1.03350921
   0.98425205  1.01869927  0.93129452  1.02147242  0.97701336  0.97043075
   1.17036581  1.06068463  1.15400094  1.01938223  1.08851357  1.13253319
   1.06476737  1.19244557]
 [ 1.05019664  0.97590288  1.0830167   1.00392006  1.07881173  1.07049307
   1.17038075  0.95942305  0.7484157   1.05945289  1.07379404  1.0268822
   1.0217178   0.9949103   1.05066957  0.74864275  1.06412451  0.961283
   0.87042728  1.07378468]
 [ 0.89085125  1.10119192  1.00990661  0.91820266  1.00908679  0.73740874
   1.00485464  0.97427282  1.09424579  0.84876444  1.01691899  1.0188415
   1.15582724  0.90586819  0.87907785  1.05440651  0.92476789  0.87475955
   1.09272491  0.93802951]
 [ 0.97633747  1.21554237  0.95190964  1.0345212   1.0123711   1.03702179
   1.11903298  0.90348583  0.97683806  0.87895557  0.92125309  1.0532206
   0.95497304  1.11988127  1.1355286   1.07307923  0.97733744  1.08540676
   0.97249376  0.89770286]
 [ 1.08621875  1.0198653   1.04726792  0.98290847  1.05640872  0.89579798
   1.09843927  0.83548153  1.02303315  0.90011522  0.96444887  1.26715629
   1.14621686  0.98682604  0.97082908  1.06998541  1.15237376  1.03912552
   0.87601541  0.84186623]
 [ 1.00719248  0.98842068  0.92611714  0.89175453  1.02358354  0.98464005
   1.16993698  0.89091489  1.00820166  1.07533476  1.06151955  0.8967124
   0.93210651  1.02820371  1.01284052  0.94901959  0.85795195  1.05141096
   0.99480577  0.80594927]]
 
 ---------------------------
 #mean percentage returns of stocks
mean_returns = [(np.mean(R) - 1)*100 for R in returns]
print mean_returns

plt.bar(np.arange(N), mean_returns)
plt.xlabel('Stock')
plt.ylabel('Returns')
plt.title('Returns for {0} Random Assets'.format(N));

----------------------------------
[1.8921966974847049, -4.4377348109003512, 3.7658259559407714, -2.1524940254945335, 5.1670968189016619, 0.43124796965616774, -2.7499609644974221, 1.4844633385441064, 1.3018988447207835, -2.2169154058116147]


---------------------
#simulate portfolio of 10 stocks
weights = np.random.uniform(0, 1, N)
weights = weights/np.sum(weights)
print weights

---------------------
[ 0.18746564  0.13005439  0.13909044  0.00896143  0.27201674  0.0756121
  0.10938963  0.02699273  0.03911474  0.01130216]

-----------------------
#calculate portfolio return
#np.dot(a, b) = a1b1 + a2b2 + a3b3 ...
p_returns = np.dot(weights, mean_returns)
print "Expected return of the portfolio: ", p_returns

-------------------------
Expected return of the portfolio:  1.48534036925

--------------------------
v = np.array([1, 2, np.nan, 4, 5])
print np.mean(v)
ix = ~np.isnan(v)
print ix
print v[ix]
print np.mean(v[ix])
#calculate nonnull mean
print np.nanmean(v)

---------------------------
nan
[ True  True False  True  True]
[ 1.  2.  4.  5.]
3.0
3.0

-------------------------------
A = np.array([
        [1, 2, 3, 12, 6],
        [4, 5, 6, 15, 20],
        [7, 8, 9, 10, 10]       
    ])
B = np.array([
        [4, 4, 2],
        [2, 3, 1],
        [6, 5, 8],
        [9, 9, 9]
    ])
print np.dot(B, A)
print np.transpose(A)

-------------------
[[ 34  44  54 128 124]
 [ 21  27  33  79  82]
 [ 82 101 120 227 216]
 [108 135 162 333 324]]
[[ 1  4  7]
 [ 2  5  8]
 [ 3  6  9]
 [12 15 10]
 [ 6 20 10]]

https://www.quantopian.com/lectures/introduction-to-numpy

No comments:

Post a Comment