vendredi 6 décembre 2019

How to get correlated random numbers for each monte carlo simulation?

How do I change the random number generated to be a correlated random number for each monte carlo simulation?

Montecarlo simulation uncorrelated case

import pandas as pd
import math
import matplotlib.pyplot as plt
import numpy as np
import random

Df=pd.read_excel(r'C:\Users\User\Documents\AMA3022\portfolio.xlsx')
pd_vector=df.loc[: , "pd"] #vector of all probability of defaults
value_vector=df.loc[: , "Value"] #vector of all value of loans
N = len(df)#sets size of portfoliio
M = 50000 #number of terations to be performed
finalloss=np.zeros((M,1))#defines an array all of value zero
for I in range (0, M): #monte-carlo simulation
    loss = 0 #initailisng the total loss          
    for j in range (0,N): #loop for the portfolio of loans 
        if random.uniform(0,1) < pd_vector[j]: 
             loss=loss+value_vector[j] 
        else: 
            loss=loss+0 #otherwise zero loss    
    finalloss[i,0]=loss 
print(finalloss)



Aucun commentaire:

Enregistrer un commentaire