Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic Question
#1
Hello Guys,

Coming from the matlab world, I am learning Python, so having some difficulties on very simple stuff.  I want to know how to store each of these values in an array, inside the loop.
RightNow, when I execute that, it just gives the last value value calculated when I type NelsonSiegel. 

from datetime import date
import numpy as np
SettleDate = date(2017,07,14)
Bond1MaturityDate = date(2018,7,13)
Bond3MaturityDate = date(2020,2,17)
Bond5MaturityDate = date(2022,7,21)
Bond10MaturityDate = date(2027,1,20)
Bond15MaturityDate = date(2031,9,16)
Bond20MaturityDate = date(2037,3,17)
Yearfraction = [float((Bond1MaturityDate-SettleDate).days)/365,float((Bond3MaturityDate-SettleDate).days)/365, float((Bond3MaturityDate-SettleDate).days)/365, float((Bond5MaturityDate-SettleDate).days)/365, float((Bond10MaturityDate-SettleDate).days)/365, float((Bond15MaturityDate-SettleDate).days)/365, float((Bond20MaturityDate-SettleDate).days)/365]
CouponRate = [0,0.0290,0.0321,0.0494,0.0585,0.0624]

Beta0=0.01
Beta1=0.01 
Beta2=0.01
Beta3=0.01
Lambda1=1
Lambda2=1
   
for i in range(len(Yearfraction)):
    NelsonSiegel = Beta0 + (Beta1 * ((1-np.exp(-Yearfraction[i]/Lambda1)/Yearfraction[i]*Lambda1))) +  (Beta2 * ((((1-np.exp(-Yearfraction[i]/Lambda1))/(Yearfraction[i]*Lambda1))) - (np.exp(-Yearfraction[i]/Lambda1)))) + (Beta3 * ((((1-np.exp(-Yearfraction[i]/Lambda2))/(Yearfraction[i]*Lambda2))) - (np.exp(-Yearfraction[i]/Lambda2))))
    print(NelsonSiegel)
if i type NelsonSiegel in my ipython i just get the last value when i want to have a vector. I know its a very basic question. Confused

Thank you

SB[/i][/i][/i][/i][/i][/i][/i][/i]
Reply
#2
You want to make NelsonSiegel a list, and append to it:

squares = []
for x in range(8):
    squares.append(x ** 2)
print(squares)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thank it was quick and very simple.

Thanks mate

SB
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python & Dataframe basic question georgialtr 0 3,833 Apr-06-2020, 03:59 PM
Last Post: georgialtr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020