I need help. I have an assignment for an exam where i have to compute the price of a portfolio composed by floating coupon bonds taking into account the issuer credit risk and the recovery rate in case of default. I have to create a code in python. After computed the discount factors, i should price the bonds, but i don't know how to include the issuer risk and the recovery rate in the formula. The professor gave us the interest rates to compute the coupons, and i know that the price of a bonds is given by the sum of the discounted cash flows. How can i insert in the forumula these others two elements? He told me that is a sort of weighted average of the bond's price with the survival probabilitiy and the bond's price in case of default taking into account the recovery rate, with weight survival probability and default probability. But i didn't catch this. Sorry for my basic english and thanks to everyone in advance.
It sounds like your professor is asking you to code the formulas in the Duffie-Singleton paper https://web.stanford.edu/~duffie/ds.pdf . (Also found in excellent papers by Bielecki and other sources.) A few technical details to keep in mind:
In case of default, bond's accrued coupon is wiped out, and you have recovery only on the remaining principal payments, which you need to price.
Discount each cash flow (coupons and principal) by the riskfree discount factor (from your bond financing curve) $\times$ the probability of survival (i.e. 1 - probability of default) at the time of the cash flow.
The fair dirty price of the bond is the sum of the above: all the cash flows, plus the value of the recovery in case of default. Subtract the accrued to get the clean price.