'I am initialising the same class, and calling the same function but with a changed parameter, and getting incorrect answers

I am developing a class to calculate the hazard rates from bond yields.

class bond:
    def bondflows(self,t0=0,riskfree=True):
        if riskfree==True: 
            self.y = self.rf
        t=self.delt
        self.delt = np.abs(self.delt-t0)
        while self.delt<self.mat-t0:
            self.flows.append(t*self.c*self.fv*exp(-self.delt*self.y))
            self.delt = self.delt + t
        self.flows.append(self.fv*(1+self.c*t)*exp(-self.y*(self.mat-t0)))
        self.flows = [np.round(i,4) for i in self.flows]
        return self.flows
    
    def __init__(self,fv,c,delt,mat,y,rf):
        self.mat,self.delt=mat,delt
        self.y,self.rf,self.c=y,rf,c
        self.fv=fv
        self.flows=[]

I am getting correct answers when I calculate like this:

for i in [(1,0.065),(2,0.068),(3,0.0695)]:
    m,y=i
    pv_loss = np.round(sum(bond(100,0.08,0.5,m,y,0.05).bondflows())-sum(bond(100,0.08,0.5,m,y,0.05).bondflows(riskfree=False)),4)
    print(pv_loss)

I am getting incorrect answers when I calculate like this:

for i in [(1,0.065),(2,0.068),(3,0.0695)]:
    m,y=i
    bondfn = bond(100,0.08,0.5,m,y,0.05)
    pv_loss = np.round(sum(bondfn.bondflows())-sum(bondfn.bondflows(riskfree=False)),4)
    print(pv_loss)

The incorrect answers are like negative 100s. I would like to be able to use the shorter form so that I can apply more functions to the same bond description



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source