Как создать следующий цикл для получения ставок купона на CDO - PullRequest
0 голосов
/ 23 июня 2019

Я пытаюсь создать цикл, который будет выбирать купоны для разных траншей CDO на основе единообразных случайных чисел, созданных numpy.

если rand <0.25 <br>A получает ранд * $ 5

если rand

если rand <0.75 A получает 0.25 * $ 5 B получает 0.25 * $ 5 C получает (rand-0.5)* $ 5 </p>

если ранд <1 A получает 0,25 * $ 5 B получает 0,25 * $ 5 C получает 0,25 * $ 5 D получает (rand-0,75) * $ 5 </p>

Я создал CSV для рандов с 20столбцы для портфеля из 20 облигаций. (ranfile1)

и 100 000 строк для сценариев.

I created a code up to the third trench without the "D". 

    randfile1=np.random.rand(20,100000)
spots = [0.0211,0.0188,0.0184,0.0187,0.0189,0.0195,0.0201,0.0205,0.0208,0.02120,0.0215,0.0218,0.0221,0.0224,0.0227,0.0229,0.0232,0.0235,0.0238,0.0241]

A = []
B = []
C = []
D = []


a = randfile1[randfile1 < 0.25].sum() * 5
A.append(a)

b = (randfile1[np.logical_and(0.25 <= randfile1, randfile1 < 0.5)] - 0.25).sum()  * 5
B.append(b)
a = np.logical_and(0.25 < randfile1, randfile1 < 0.5).sum() * 1.25
A.append(a)



c = (randfile1[np.logical_and(0.5 <= randfile1, randfile1 < 0.75)] - 0.5).sum()  * 5
C.append(c)
b = np.logical_and(0.5 < randfile1, randfile1 < 0.75).sum() * 1.25
B.append(b)
a = np.logical_and(0.5 < randfile1, randfile1 < 0.75).sum() * 1.25
A.append(a)

d = (randfile1[np.logical_and(0.75 <= randfile1, randfile1 < 1)] - 0.5).sum()  * 5
D.append(d)
c = np.logical_and(0.75 < randfile1, randfile1 < 1).sum() * 1.25
C.append(c)
b = np.logical_and(0.75 < randfile1, randfile1 < 1).sum() * 1.25
B.append(b)
a += np.logical_and(0.75 < randfile1, randfile1 < 1).sum() * 1.25
A.append(a)

1 Ответ

0 голосов
/ 23 июня 2019

Colab Link

То, чего вы пытаетесь достичь, может быть достигнуто с помощью расширенного индексирования, условий для массивов numpy и логических операторов Numpy.

import numpy as np

rand_data = np.random.rand(1, 40) # Generate 40 random number

A = rand_data[rand_data < 0.25].sum() * 5 # Advanced indexing is in use here
# Get the ones between 0 and 0.25 and multiply the sum with 5

# Get the ones larger than 0.25 and smaller than 0.5 apply logical-and to indices
# Get the sum of the elements in those indices and substract 0.25 from each of them
# Finally multiply the sum with 5
B = (rand_data[np.logical_and(0.25 <= rand_data, rand_data < 0.5)] - 0.25).sum()  * 5
# We are interested in the count of values between 0.25 and 0.5
# So no advanced indexing here just get the count on binary values using the sum
# and multiply with 0.25 * 5
A += np.logical_and(0.25 < rand_data, rand_data < 0.5).sum() * 1.25

C = (rand_data[np.logical_and(0.5 <= rand_data, rand_data < 0.75)] - 0.5).sum()  * 5
B += np.logical_and(0.5 < rand_data, rand_data < 0.75).sum() * 1.25
A += np.logical_and(0.5 < rand_data, rand_data < 0.75).sum() * 1.25
print(A)
print(B)
print(C)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...