Python class combination (composition) and aggregation (aggregation)

Source: Internet
Author: User
Tags instance method

The following section of the code began to say, know that someone asked the question, said the code can not understand.

#coding: Utf-8
#书中一个例子简单的短期利率类, Folding is one of the most basic concepts in finance, in the continuous discounted world of fixed short-term interest rates, the discount factor between future cash flow and current date t=0 is t>0 for Do (t) =e (-RT).

Import NumPy as NP

Class Short_rate (object):
def __init__ (self,name,rate):
Self.name=name
Self.rate=rate
def get_discount_factors (self,time_list):
Time_list=np.array (Time_list)
Return Np.exp (-self.rate*time_list)


Class Cash_flow_series (object):
def __init__ (self,name,time_list,cash_flows,short_rate):
Self.name=name
Self.time_list=time_list
Self.cash_flows=cash_flows
Self.short_rate=short_rate
def present_value_list (self):
DF = self.short_rate.get_discount_factors (self.time_list) #想问这一行中为什么可以这样子不继承第一个类可以调用第一个类的函数呢
Return Np.array (self.cash_flows) *df
def net_present_value (self):
Return Np.sum (Self.present_value_list ())

Class Cfs_sensitivity (cash_flow_series):
def npv_sensitivity (self,short_rates):
Npvs=[]
For rate in Short_rates:
Sr.rate=rate #这个实例化rate的意思是?
Npvs.append (Self.net_present_value ())
Return Np.array (NPVs)

short_rates=[0.01,0.025,0.05,0.075,0.1,0.125,0.15,0.2]
Cash_flows=np.array ([ -100,50,75])
TIME_LIST=[0.0,1.0,2.0]

Sr=short_rate (' R ', 0.05)


Print Sr.get_discount_factors (time_list)

sr.rate=0.05
Cfs=cash_flow_series (' Cfs0 ', TIME_LIST,CASH_FLOWS,SR)
Print Cfs.present_value_list ()

Cfs_sens=cfs_sensitivity (' CFS ', TIME_LIST,CASH_FLOWS,SR)

Npvs=cfs_sens.npv_sensitivity (Short_rates)

Print NPVs
The code above is poorly named, and the questioner says the code on the P350 page is the book Python Financial Data Analysis (watercress). Slightly modified

# Coding:utf-8
# An example of a simple short term interest rate class, discounted is one of the most basic concepts in finance, in the continuous discounted fixed short-term interest rate world, the t>0 factor between future cash flow and current date t=0 for Do (t) =e (-RT) Secondary

Import NumPy as NP


Class Shortrate (object):

def __init__ (self, name, rate):
Self.name = Name
Self.rate = Rate

def get_discount_factors (self, time_list):
Time_list = Np.array (time_list)
Return Np.exp (-self.rate * time_list)


Class Cashflowseries (object):

def __init__ (self, name, time_list, Cash_flows, short_rate):
Self.name = Name
Self.time_list = Time_list
Self.cash_flows = Cash_flows
Self.short_rate = Short_rate

def present_value_list (self):
DF = self.short_rate.get_discount_factors (
self.time_list) # Want to ask why this line can not inherit the first class can call a function of the first class.
Return Np.array (self.cash_flows) * DF

def net_present_value (self):
Return Np.sum (Self.present_value_list ())


Class Cfssensitivity (cashflowseries):

def npv_sensitivity (self, shortrates):
NPVs = []
For rate in Shortrates:
Sr.rate = rate # This instantiation of rate means?
Npvs.append (Self.net_present_value ())
Return Np.array (NPVs)

Shortrates = [0.01, 0.025, 0.05, 0.075, 0.1, 0.125, 0.15, 0.2]
Cash_flows = Np.array ([-100, 50, 75])
Time_list = [0.0, 1.0, 2.0]

sr = shortrate (' R ', 0.05)


Print Sr.get_discount_factors (time_list)

Sr.rate = 0.05
CFS = cashflowseries (' Cfs0 ', Time_list, Cash_flows, SR)
Print Cfs.present_value_list ()


Cfs_sens = cfssensitivity (' cfs ', Time_list, Cash_flows, SR)

NPVs = cfs_sens.npv_sensitivity (shortrates)

Print NPVs
The questioner suggests that the statement df = self.short_rate.get_discount_factors (self.time_list) is not quite clear.

1, this is an object-oriented concept of class aggregation.
2, need to see the initialization method of cashflowseries class self.short_rate = Short_rate
Short_rate is an instance of the Shortrate class, but your code name is problematic, shortrate instance calls its own instance method, no problem.

3, sr.rate = rate # This instantiation of rate means?
To see from the following statement, the SR is the following SR, which is an instance of Shortrate. SR has instance attribute rate.
Sr.rate = 0.05
CFS = cashflowseries (' Cfs0 ', Time_list, Cash_flows, SR)
There are a variety of relationships between classes, please see Wikipedia, the category map below the main introduction of aggregation (Aggregation) and combination (composition).

Aggregation (aggregation): refers to the relationship between the whole and the part. Usually after defining a whole class, we analyze the composition structure of the whole class. To find out some constituent classes, which form the aggregation relationship between the whole class and the constituent class. Requirements described in the "inclusion", "composition", "divided into ..." The term "part" often means the aggregation of relationships.

Composition (composition): It also indicates the relationship between the whole and part of a class, but the part and the whole have a unified lifetime in a composite relationship. Once the whole object does not exist, some objects will not exist. There is a total life-death relationship between some objects and the whole object.

Take a look at the code below. class, the computer instance object does not exist, and the internal combination of the CPU instance does not exist. The aggregate computer instance object does not exist, and the CPU instance passed in from the initialization method does not belong to the computer instance object.

#! /usr/bin/env python
# Coding:utf-8

'''
Class object Composition Relationship
'''


Class Cpu (object):

def __init__ (self):
Self.type = ' 286 '


Class Computer (object):

def __init__ (self):
SELF.CPU = CPU () # contains instance objects for CPU classes

def __del__ (self):
Print "Cpu by by!"

Old_computer = computer ()
Del Old_computer

#! /usr/bin/env python
# Coding:utf-8

'''
Class object Aggregation Relationships
'''


Class Cpu (object):

def __init__ (self):
Self.type = ' 286 '


Class Computer (object):

def __init__ (self, CPU):
SELF.CPU = CPU # has an instance object for a CPU class

def __del__ (self):
Print "No power and CPU by by!"

OLD_CPU = CPU ()
Old_computer = computer (OLD_CPU)
Del Old_computer

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.