Example
In order to run attribution on a bond, we need a function that takes a set of parameters
that feed into a valuation and which can be altered, one at a time, to get the final attribution
The following function takes a bond contract as in input, and then returns a function that
will value
def value(bond):
def value(m1,m3,m6,y1,y5,y10,y20,y30,date):
data = {"date":date, "1M":m1, "3M":m3, "6M":m6, "1Y":y1, "5Y":y5, "10Y":y10, "20Y":y20, "30Y":y30}
def convert(data):
data2 = []
for key in data:
if key != 'date':
item = [key, data[key]/100]
data2.append(item)
pass
return data2
data = dt.treasury_term_data(convert(data),data['date'])
curve = ts.BootstrappedCurve(type=ts.BootstrapTypes.PiecewiseLogLinearDiscount,day_count=dt.treasury.DAY_COUNT, data=data)
value1 = vl.value(contract=bond, curve=curve)
return value1.net_present_value
return value
Next, we can run the attribution as follows
attribution_list,att_map = at.scallop(value(bond),
[3.79,3.89,4.0,4.04,4.41, 4.72, 5.25, 5.25, "2026-08-10"],
[3.8,3.9,4.0,4.1,4.5, 4.75, 5.3, 5.28, "2026-08-11"])
The script calls the scallop function. The first parameter is the function to be attributed, here
provided by calling value(bond). The second argument is the list of parameters to the value function,
(here a set of yields and a date) and the third argument is a new set of parameters. The scallop function
will attribute the change in net present based on each input.
Note, other values other than net presetn value could be used as the the thing being attributed.