Generating a trinomial tree with arrays - Python -


i have simple problem cann't figure out. have following code:

r = [np.array([100])] in range(2):   r.append(np.concatenate((r[-1][:1]*1.5, r[-1]*0.5))) 

this produces @ each iteration:

[array([100]), array([ 150.,   50.])]  [array([100]), array([ 150.,   50.]), array([ 225.,   75.,   25.])] 

the idea of code generate structure similar binomial tree up=1.5, down=0.5. 100 go 150, down 50. in next time step, 150, go 225 or 75; , 50 75 or 25.

but now.... want use generate trinomial tree. up=1.5, middle=stays same=1, down=0.5.

so iteration should produce

[array([100]), array([ 150., 100.,  50.])]  [array([100]), array([ 150.,  100., 50.]), array([ 225., 150., 75., 150., 100., 75., 75., 50., 25. ])] 

this generates kind-of non recombinant structure. dont know how modify code it. closest got is:

r.append(np.concatenate((r[-1][:1]*1.5,r[-1][:1]*1, r[-1]*0.5))) 

but doesnt make result:

[array([100]), array([ 150.,  100.,   50.]), array([ 225.,  150.,   75.,   50.,   25.])] 

thank !

since first 1 recombining, of down , down of producing same result , hence 1 of them being ignored in code.

what want non-recombining structure, need consider results, changing produce desired results:

for in range(2):   r.append(np.concatenate((r[-1]*1.5, r[-1], r[-1]*0.5)))  >>> r [array([100]), array([ 150.,  100.,   50.]), array([ 225.,  150.,   75.,  150.,  100.,   50.,   75.,   50.,   25.])] 

what take previous array (r[-1]) , multiplies 3 multiplier want , concatenate() combines these 3 together.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -