Numpy Exercise 3

Imports


In [26]:
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns

In [27]:
import antipackage
import github.ellisonbg.misc.vizarray as va

Geometric Brownian motion

Here is a function that produces standard Brownian motion using NumPy. This is also known as a Wiener Process.


In [28]:
def brownian(maxt, n):
    """Return one realization of a Brownian (Wiener) process with n steps and a max time of t."""
    t = np.linspace(0.0,maxt,n)
    h = t[1]-t[0]
    Z = np.random.normal(0.0,1.0,n-1)
    dW = np.sqrt(h)*Z
    W = np.zeros(n)
    W[1:] = dW.cumsum()
    return t, W

Call the brownian function to simulate a Wiener process with 1000 steps and max time of 1.0. Save the results as two arrays t and W.


In [30]:
#t, w = numpy.empty(1000)
t, W = brownian(1.0, 1000)

In [31]:
assert isinstance(t, np.ndarray)
assert isinstance(W, np.ndarray)
assert t.dtype==np.dtype(float)
assert W.dtype==np.dtype(float)
assert len(t)==len(W)==1000

Visualize the process using plt.plot with t on the x-axis and W(t) on the y-axis. Label your x and y axes.


In [32]:
plt.plot(t, W, "bo")
plt.ylabel('Position')
plt.xlabel('Time')


Out[32]:
<matplotlib.text.Text at 0x7f600c239710>

In [33]:
assert True # this is for grading

Use np.diff to compute the changes at each step of the motion, dW, and then compute the mean and standard deviation of those differences.


In [35]:
dW = np.diff(W)
mean = np.mean(dW)
stdev = np.std(dW)
print mean
print stdev


0.00111997057131
0.0305419444717

In [ ]:


In [ ]:


In [36]:
assert len(dW)==len(W)-1
assert dW.dtype==np.dtype(float)

Write a function that takes $W(t)$ and converts it to geometric Brownian motion using the equation:

$$ X(t) = X_0 e^{((\mu - \sigma^2/2)t + \sigma W(t))} $$

Use Numpy ufuncs and no loops in your function.


In [37]:
def geo_brownian(t, W, X0, mu, sigma):
    return X0*np.exp((mu-(sigma**2)/2)*t+sigma*W)
print geo_brownian(t,W,2,mean,stdev)


[ 2.          1.99866074  1.99816816  2.00002283  1.99885445  2.00037311
  1.99879607  2.00055225  2.00401481  2.00352973  2.00123225  1.99723389
  1.99499379  1.99274747  1.99371349  1.99141482  1.98875019  1.98896124
  1.99069129  1.99144048  1.99187357  1.99080615  1.99210767  1.99565986
  1.99505934  1.99343335  1.9980209   1.99769574  1.9984267   1.9938482
  1.99555098  1.99519797  1.99403286  1.9927199   1.99213952  1.99343273
  1.99729111  1.99405591  1.99456784  1.99662007  1.99730353  1.99739715
  1.99925586  1.99892253  1.99944043  1.99925612  1.99838933  1.9978252
  1.99779378  1.9971553   1.99737625  1.99557252  1.99364038  1.9911027
  1.99100071  1.9937244   1.99008981  1.98947306  1.99190689  1.99201388
  1.99140932  1.99164098  1.99046065  1.99075193  1.98913523  1.98838987
  1.98847206  1.99092598  1.98854077  1.99051731  1.99080979  1.98933073
  1.98569952  1.98543707  1.98542988  1.9846784   1.98623026  1.98389235
  1.98333164  1.98449742  1.98431631  1.98450829  1.98377215  1.98217758
  1.98118881  1.98333535  1.98275077  1.97921031  1.98288465  1.98151127
  1.98150682  1.98419664  1.99057965  1.98750731  1.98673165  1.9860552
  1.98692644  1.98902568  1.99019042  1.99057224  1.98944209  1.98534967
  1.98714421  1.98805949  1.98994272  1.99184066  1.99290779  1.99387479
  1.99333912  1.99573332  1.99400498  1.98810109  1.98621225  1.98503949
  1.98307941  1.98117856  1.97960668  1.97754954  1.97629666  1.9754516
  1.9715575   1.97051865  1.97076964  1.96879591  1.96930567  1.9677715
  1.96904124  1.97153117  1.97061717  1.97034045  1.96939212  1.96988783
  1.96953098  1.96840783  1.97074967  1.9690172   1.96652538  1.96876444
  1.97089066  1.96966241  1.97155387  1.96769236  1.96770249  1.96668379
  1.96769614  1.96894092  1.96728993  1.96876099  1.96919756  1.968718
  1.96631889  1.96777884  1.96807322  1.96823535  1.96983864  1.96920266
  1.9698791   1.97054959  1.97190161  1.9740882   1.97511227  1.97847425
  1.97658203  1.97658529  1.97668307  1.97645745  1.97358385  1.97344673
  1.97292085  1.9735265   1.97224279  1.96864757  1.96805538  1.96466493
  1.96245522  1.96663394  1.97007813  1.96988117  1.96923043  1.97363006
  1.97571201  1.97677335  1.97267684  1.97340203  1.97583887  1.97739654
  1.98056482  1.98358825  1.98299206  1.98353274  1.9813264   1.98172715
  1.98050991  1.98201218  1.97842203  1.98026713  1.98210877  1.98121978
  1.9796347   1.97814117  1.97644604  1.97599605  1.97416379  1.9726409
  1.97203713  1.9772398   1.98114077  1.98146226  1.98052059  1.97801256
  1.97801725  1.97811013  1.97553156  1.97236703  1.97165085  1.97343653
  1.97516202  1.97371067  1.97381216  1.97135985  1.97191567  1.97374546
  1.97192806  1.97232566  1.97045219  1.97246214  1.97338004  1.97266516
  1.97458632  1.97615648  1.97856444  1.97635603  1.97745624  1.97737362
  1.97810848  1.97602197  1.97546536  1.97520472  1.97632927  1.97917102
  1.98014232  1.97730808  1.97550676  1.97668221  1.97419674  1.96685856
  1.96683731  1.96672478  1.9684675   1.96661682  1.9694258   1.97025181
  1.96950475  1.97030239  1.97154411  1.96940284  1.97085508  1.9695935
  1.96872579  1.96773607  1.96659128  1.9676875   1.96562621  1.96530285
  1.96349463  1.96710962  1.96435419  1.9648686   1.96292705  1.96190819
  1.96329067  1.9621464   1.96311058  1.96361647  1.96442186  1.96340818
  1.96041494  1.96130385  1.96353132  1.95984157  1.96068368  1.95816586
  1.95533053  1.95249639  1.95289936  1.95226559  1.95258852  1.95442115
  1.95605298  1.95778791  1.96016147  1.95980725  1.95893958  1.9584543
  1.95804016  1.95959412  1.96026671  1.96098193  1.96289804  1.96058691
  1.95992396  1.96160158  1.96411054  1.96566557  1.96706087  1.96659343
  1.96437584  1.96543593  1.9664425   1.96772868  1.96828306  1.96685301
  1.96743875  1.96991273  1.97290371  1.97189655  1.97027265  1.96932338
  1.97279844  1.97279288  1.9713812   1.97004768  1.97129834  1.97337603
  1.96879809  1.96760584  1.96986487  1.97042894  1.9691735   1.9705744
  1.97194653  1.97246064  1.97299149  1.97173582  1.97256827  1.97011629
  1.97289617  1.97263072  1.97249679  1.97194569  1.97126273  1.9737936
  1.97472404  1.97380385  1.96966895  1.9699348   1.96977665  1.96694978
  1.96731356  1.97028908  1.96928912  1.9712538   1.97145587  1.97503222
  1.97434831  1.97531842  1.97553411  1.97742358  1.97478008  1.9734449
  1.97427963  1.97520423  1.97469384  1.97378013  1.97434375  1.9734257
  1.97322553  1.97163638  1.97118199  1.97166548  1.97281324  1.97316059
  1.97306069  1.97184112  1.97237213  1.97002988  1.97157822  1.97054452
  1.97065281  1.97301503  1.97348314  1.97523394  1.97730805  1.98047928
  1.983153    1.9822697   1.98651974  1.98257021  1.98350296  1.98063473
  1.98411729  1.98371123  1.98600791  1.98410247  1.98386532  1.9860068
  1.98815692  1.98919824  1.99071719  1.9914303   1.99356578  1.99057266
  1.98939472  1.98724583  1.9838099   1.98318385  1.98081196  1.98248715
  1.98342341  1.98076781  1.98331254  1.98693419  1.98689318  1.98890409
  1.98725027  1.98465519  1.98407209  1.98277409  1.98452345  1.98299529
  1.98229862  1.98231224  1.98470511  1.98683944  1.9885776   1.98680115
  1.98730311  1.98695592  1.98634491  1.98566513  1.98664104  1.98910338
  1.9877994   1.98851937  1.99034489  1.98989072  1.98838557  1.98661756
  1.98884518  1.98744613  1.98824506  1.98779361  1.98887727  1.9882495
  1.98829043  1.989551    1.99163577  1.99039777  1.99186817  1.99007524
  1.98810853  1.98848226  1.98976659  1.99171737  1.99254566  1.99178253
  1.99138376  1.9935464   1.99295517  1.99177018  1.9924692   1.99306063
  1.99658205  1.99555088  1.99435698  1.9940632   1.99576258  1.99381891
  1.9936987   1.99550025  1.99198766  1.99065063  1.99167766  1.99190983
  1.99813724  1.9997645   1.99737834  2.00192441  2.00218877  2.00114553
  1.99977445  1.99983228  1.99730778  1.99632521  1.99758201  1.99833262
  1.99845682  1.99742644  1.99787901  1.99787099  1.99925135  2.00156292
  1.99795675  1.99695298  1.99644395  2.000105    1.998959    1.99960871
  1.99970825  1.99925802  2.001378    1.99857534  2.00108233  2.0025122
  2.00386177  2.00188746  2.00196359  1.99968891  1.99849488  2.00003948
  1.99767607  1.99949545  1.99724717  1.99967881  1.9994534   2.00545657
  2.00516109  2.00495919  2.00817653  2.01112003  2.00938945  2.00972427
  2.00898136  2.00497017  2.00229123  2.00028125  1.99752893  1.99842423
  1.99687745  1.99510138  1.99571885  1.99587518  1.99562552  1.99637309
  1.99670606  1.99935723  2.00216191  2.00215355  2.00310934  2.00441257
  2.00416451  2.00522059  2.00682187  2.00753751  2.01027542  2.01107494
  2.01220117  2.01223971  2.01331639  2.0177649   2.0160232   2.01426983
  2.01497069  2.01670128  2.01681877  2.01453582  2.01093885  2.00816399
  2.00731028  2.01023471  2.01201944  2.01098379  2.01007238  2.0095216
  2.01250369  2.01172632  2.00973908  2.01068015  2.01064929  2.0119402
  2.01248337  2.01320423  2.01123715  2.01199009  2.01339835  2.01488998
  2.01536792  2.01617881  2.01372379  2.01452981  2.01230117  2.0107821
  2.01366457  2.01571525  2.01225288  2.01333512  2.01569406  2.01484391
  2.01062562  2.010744    2.01040981  2.0100977   2.010377    2.01016578
  2.00969301  2.01088372  2.01083084  2.01250268  2.01281476  2.01253727
  2.01499771  2.01512522  2.01805657  2.01753658  2.01731019  2.01971597
  2.01793777  2.01846769  2.01734962  2.01905331  2.01932486  2.01853532
  2.01681236  2.01847136  2.01857159  2.01951191  2.02023686  2.01788166
  2.01838051  2.01749493  2.0178704   2.01898821  2.02057478  2.02174118
  2.02430389  2.02585105  2.0226006   2.02227971  2.02020328  2.01821321
  2.01765427  2.01828717  2.0175023   2.017173    2.01846264  2.0145807
  2.01522273  2.0180531   2.01853657  2.01983272  2.02029733  2.02281853
  2.02008509  2.02316111  2.01843695  2.01922698  2.01832283  2.01808582
  2.01816596  2.01899778  2.01605947  2.01556679  2.0141055   2.01382848
  2.01469823  2.01410085  2.01397737  2.0140775   2.01336716  2.01405961
  2.0144247   2.01361339  2.01278154  2.01211208  2.01434339  2.01335989
  2.01111042  2.01145945  2.00853256  2.00797611  2.00684551  2.00853109
  2.00847802  2.01022812  2.01065104  2.01098883  2.0126192   2.01465407
  2.01080049  2.01296891  2.00979075  2.00964801  2.01005022  2.01018219
  2.0102915   2.0108443   2.01040407  2.00876476  2.01118683  2.0103433
  2.01297095  2.01473806  2.01761065  2.01990349  2.01796114  2.02026699
  2.01746887  2.01485214  2.01627132  2.01446058  2.01352443  2.01393983
  2.01645962  2.01759546  2.01858461  2.0202857   2.02140346  2.02085801
  2.01920317  2.02128369  2.02105269  2.02251333  2.0215818   2.0206951
  2.01956983  2.01615196  2.01600357  2.01369285  2.01514262  2.01481514
  2.01459235  2.0175326   2.01764834  2.02049178  2.0183921   2.01615126
  2.0158096   2.0132834   2.0161897   2.01642083  2.01434114  2.01567624
  2.01479986  2.01329858  2.01221154  2.01151422  2.01318027  2.01123939
  2.00936201  2.00927421  2.01141169  2.00916838  2.00962272  2.00693269
  2.00478133  2.00454352  2.00227407  2.00378097  2.00716319  2.00620169
  2.00734024  2.0116554   2.01092479  2.01103639  2.0085398   2.00843168
  2.01011674  2.0077054   2.00754413  2.00696204  2.00771911  2.00686564
  2.00419109  2.00815294  2.00814353  2.010493    2.01031379  2.00890562
  2.00693829  2.0058992   2.00477049  2.00758217  2.00788137  2.00639979
  2.0073734   2.00725598  2.00936788  2.00826324  2.01227543  2.01369901
  2.01212501  2.01199624  2.01031951  2.00854609  2.0096621   2.01070021
  2.01070304  2.0121309   2.00935745  2.00786788  2.00960723  2.00972761
  2.008143    2.01039205  2.01006293  2.01144571  2.01039472  2.00955103
  2.01070735  2.01290318  2.01266867  2.01634099  2.01863576  2.02076367
  2.02020923  2.02159574  2.02428378  2.02356783  2.02798044  2.03167485
  2.03154768  2.03387376  2.03549689  2.03700883  2.03655522  2.03747562
  2.04060753  2.0414739   2.04157626  2.03990876  2.04290393  2.04276183
  2.04193188  2.0424418   2.04324996  2.04215587  2.04111089  2.041598
  2.04077824  2.04114947  2.04122186  2.03985356  2.03913906  2.03982233
  2.04384915  2.04402741  2.04458954  2.04457252  2.04247462  2.04413715
  2.04756853  2.04441951  2.04438931  2.0429723   2.04222962  2.04226384
  2.04371469  2.04441955  2.04597917  2.04838588  2.05013918  2.04704148
  2.04638361  2.04793201  2.04829376  2.04645999  2.04903846  2.04818429
  2.0472176   2.04735323  2.04928469  2.04919347  2.04672113  2.04515787
  2.04466404  2.04263545  2.04152169  2.0437865   2.04460241  2.0450469
  2.04485244  2.04655482  2.04366608  2.04148633  2.0433361   2.04393463
  2.04287192  2.04460427  2.04379165  2.04428639  2.04432708  2.04440037
  2.04198468  2.04456448  2.04350616  2.04283246  2.0426113   2.04194426
  2.04121977  2.04022751  2.0438087   2.04169042  2.04422576  2.04369044
  2.04393945  2.04359261  2.04264195  2.04297874  2.04295949  2.0455354
  2.04325113  2.0442252   2.04534257  2.04251878  2.04327387  2.0434044
  2.0469052   2.04337986  2.04374532  2.04272903  2.04590808  2.04428271
  2.04277379  2.04531132  2.04402528  2.04454685  2.04597133  2.04823165
  2.04492878  2.04371041  2.0485844   2.04868416  2.04728643  2.04829679
  2.05083485  2.04883567  2.051135    2.05129987  2.05304688  2.05503516
  2.05138281  2.05260498  2.05017826  2.04890803  2.05020839  2.04803896
  2.05021032  2.04764846  2.04715745  2.04948204  2.04918266  2.04959632
  2.05228133  2.0519187   2.05272877  2.05248597  2.05259809  2.0559483
  2.05451284  2.05929262  2.05801265  2.06215794  2.06157359  2.06186965
  2.06489189  2.06698348  2.07076686  2.07150017  2.07470359  2.07584244
  2.07705628  2.0744603   2.07234679  2.07326105  2.07230626  2.07505218
  2.07753161  2.07717163  2.07773697  2.0794113   2.07800739  2.07517058
  2.07442357  2.07225953  2.06879302  2.06853802  2.07110332  2.07353568
  2.07571728  2.07376876  2.07622587  2.07539068  2.07724369  2.08043635
  2.0766467   2.07531286  2.07271142  2.07459829  2.07513326  2.07671251
  2.07026679  2.06703861  2.06684019  2.06970474  2.07088128  2.06921609
  2.06846436  2.07202687  2.07156268  2.07087789]

In [23]:
assert True # leave this for grading

Use your function to simulate geometric brownian motion, $X(t)$ for $X_0=1.0$, $\mu=0.5$ and $\sigma=0.3$ with the Wiener process you computed above.

Visualize the process using plt.plot with t on the x-axis and X(t) on the y-axis. Label your x and y axes.


In [38]:
X = geo_brownian(t,W,1.0,0.5,0.3)
plt.plot(t,X, 'ro')
plt.ylabel('X(t)')
plt.xlabel('time')


Out[38]:
<matplotlib.text.Text at 0x7f600c257310>

In [25]:
assert True # leave this for grading

In [ ]: