Пример кода для создания матрицы перехода на Python. Для этого используются фиктивные данные для создания вероятностей перехода, а затем оценивается чистая приведенная стоимость будущих денежных потоков.
Пример кода для создания матрицы перехода на Python. Для этого используются фиктивные данные для создания вероятностей перехода, а затем оценивается чистая приведенная стоимость будущих денежных потоков.
Пример кода для создания матрицы перехода на Python. Для этого используются фиктивные данные для создания вероятностей перехода, а затем оценивается чистая приведенная стоимость будущих денежных потоков.
Пример кода для создания матрицы перехода на Python. Для этого используются фиктивные данные для создания вероятностей перехода, а затем оценивается чистая приведенная стоимость будущих денежных потоков.
Пример кода для создания матрицы перехода на Python. Для этого используются фиктивные данные для создания вероятностей перехода, а затем оценивается чистая приведенная стоимость будущих денежных потоков.
Пример кода для создания матрицы перехода на Python. Для этого используются фиктивные данные для создания вероятностей перехода, а затем оценивается чистая приведенная стоимость будущих денежных потоков.
Пример кода для создания матрицы перехода на Python. Для этого используются фиктивные данные для определения вероятностей перехода, а затем оценивается чистая приведенная стоимость будущих денежных потоков.
##############
# Dummy data #
##############
df = pd.concat([
pd.DataFrame(range(1, 180001), columns = ['LOAN_ID']),
pd.DataFrame(np.random.randint(10000, 100000, size = (180000, 4)), columns = ['cur_' + y for y in ['cur', '30DPD', '60DPD', '90DPD']]),
pd.DataFrame(np.random.randint(0, 5000, size = (180000, 2)), columns = ['cur_' + y for y in ['def', 'pre']]),
pd.DataFrame(np.random.randint(10000, 100000, size = (180000, 4)), columns = ['30DPD_' + y for y in ['cur', '30DPD', '60DPD', '90DPD']]),
pd.DataFrame(np.random.randint(0, 5000, size = (180000, 2)), columns = ['30DPD_' + y for y in ['def', 'pre']]),
pd.DataFrame(np.random.randint(10000, 100000, size = (180000, 4)), columns = ['60DPD_' + y for y in ['cur', '30DPD', '60DPD', '90DPD']]),
pd.DataFrame(np.random.randint(0, 5000, size = (180000, 2)), columns = ['60DPD_' + y for y in ['def', 'pre']]),
pd.DataFrame(np.random.randint(10000, 100000, size = (180000, 4)), columns = ['90DPD_' + y for y in ['cur', '30DPD', '60DPD', '90DPD']]),
pd.DataFrame(np.random.randint(0, 5000, size = (180000, 2)), columns = ['90DPD_' + y for y in ['def', 'pre']]),
], axis = 1)
df['PBAL_BEG'] = 100000
df['installment'] = 1980.12
df['int_rate'] = 0.07
df['rem'] = 60
df.loc[0:150000, 'status'] = 'Current'
df.loc[150000:175000, 'status'] = '30DPD'
df.loc[175000:177500, 'status'] = '60DPD'
df.loc[177500:, 'status'] = '90DPD'
df.loc[:, 'cur_cur':'cur_pre'] = df.loc[:, 'cur_cur':'cur_pre'].div(df.loc[:, 'cur_cur':'cur_pre'].sum(axis = 1), axis = 0)
df.loc[:, '30DPD_cur':'30DPD_pre'] = df.loc[:, '30DPD_cur':'30DPD_pre'].div(df.loc[:, '30DPD_cur':'30DPD_pre'].sum(axis = 1), axis = 0)
df.loc[:, '60DPD_cur':'60DPD_pre'] = df.loc[:, '60DPD_cur':'60DPD_pre'].div(df.loc[:, '60DPD_cur':'60DPD_pre'].sum(axis = 1), axis = 0)
df.loc[:, '90DPD_cur':'90DPD_pre'] = df.loc[:, '90DPD_cur':'90DPD_pre'].div(df.loc[:, '90DPD_cur':'90DPD_pre'].sum(axis = 1), axis = 0)
# Repeate each loan by the number of months remaining on the loan and get the observation number for each
shell = df.loc[df.index.repeat(df.rem)].reset_index(drop = True)
shell['period'] = shell.groupby(['LOAN_ID']).cumcount() + 1
# Set the probability distributions in period 1 based on the current loan state
filtCur = (shell['status']=='Current') & (shell['period']==1)
shell.loc[filtCur, 'cur_end'] = shell['cur_cur']
shell.loc[filtCur, '30DPD_end'] = shell['cur_30DPD']
shell.loc[filtCur, '60DPD_end'] = shell['cur_60DPD']
shell.loc[filtCur, '90DPD_end'] = shell['cur_90DPD']
shell.loc[filtCur, 'def_end'] = shell['cur_def']
shell.loc[filtCur, 'pre_end'] = shell['cur_pre']
filt30DPD = (shell['status']=='30DPD') & (shell['period']==1)
shell.loc[filt30DPD, 'cur_end'] = shell['30DPD_cur']
shell.loc[filt30DPD, '30DPD_end'] = shell['30DPD_30DPD']
shell.loc[filt30DPD, '60DPD_end'] = shell['30DPD_60DPD']
shell.loc[filt30DPD, '90DPD_end'] = shell['30DPD_90DPD']
shell.loc[filt30DPD, 'def_end'] = shell['30DPD_def']
shell.loc[filt30DPD, 'pre_end'] = shell['30DPD_pre']
filt60DPD = (shell['status']=='60DPD') & (shell['period']==1)
shell.loc[filt60DPD, 'cur_end'] = shell['60DPD_cur']
shell.loc[filt60DPD, '30DPD_end'] = shell['60DPD_30DPD']
shell.loc[filt60DPD, '60DPD_end'] = shell['60DPD_60DPD']
shell.loc[filt60DPD, '90DPD_end'] = shell['60DPD_90DPD']
shell.loc[filt60DPD, 'def_end'] = shell['60DPD_def']
shell.loc[filt60DPD, 'pre_end'] = shell['60DPD_pre']
filt90DPD = (shell['status']=='90DPD') & (shell['period']==1)
shell.loc[filt90DPD, 'cur_end'] = shell['90DPD_cur']
shell.loc[filt90DPD, '30DPD_end'] = shell['90DPD_30DPD']
shell.loc[filt90DPD, '60DPD_end'] = shell['90DPD_60DPD']
shell.loc[filt90DPD, '90DPD_end'] = shell['90DPD_90DPD']
shell.loc[filt90DPD, 'def_end'] = shell['90DPD_def']
shell.loc[filt90DPD, 'pre_end'] = shell['90DPD_pre']
endCols = ['cur_end', '30DPD_end', '60DPD_end', '90DPD_end', 'def_end', 'pre_end']
# Run the transition matrix over the number of periods in the data
for per in range(2, shell['period'].max() + 1):
shell[[x + '_L1' for x in endCols]] = shell.groupby(['LOAN_ID'])[endCols].shift(1)
filtPer = (shell['period']==per)
shell.loc[filtPer, 'cur_end'] = shell['cur_cur'] * shell['cur_end_L1'] + shell['30DPD_cur'] * shell['30DPD_end_L1'] + shell['60DPD_cur'] * shell['60DPD_end_L1'] + shell['90DPD_cur'] * shell['90DPD_end_L1']
shell.loc[filtPer, '30DPD_end'] = shell['cur_30DPD'] * shell['cur_end_L1'] + shell['30DPD_30DPD'] * shell['30DPD_end_L1'] + shell['60DPD_30DPD'] * shell['60DPD_end_L1'] + shell['90DPD_30DPD'] * shell['90DPD_end_L1']
shell.loc[filtPer, '60DPD_end'] = shell['cur_60DPD'] * shell['cur_end_L1'] + shell['30DPD_60DPD'] * shell['30DPD_end_L1'] + shell['60DPD_60DPD'] * shell['60DPD_end_L1'] + shell['90DPD_60DPD'] * shell['90DPD_end_L1']
shell.loc[filtPer, '90DPD_end'] = shell['cur_90DPD'] * shell['cur_end_L1'] + shell['30DPD_90DPD'] * shell['30DPD_end_L1'] + shell['60DPD_90DPD'] * shell['60DPD_end_L1'] + shell['90DPD_90DPD'] * shell['90DPD_end_L1']
shell.loc[filtPer, 'def_end'] = shell['cur_def'] * shell['cur_end_L1'] + shell['30DPD_def'] * shell['30DPD_end_L1'] + shell['60DPD_def'] * shell['60DPD_end_L1'] + shell['90DPD_def'] * shell['90DPD_end_L1'] + 1 * shell['def_end_L1']
shell.loc[filtPer, 'pre_end'] = shell['cur_pre'] * shell['cur_end_L1'] + shell['30DPD_pre'] * shell['30DPD_end_L1'] + shell['60DPD_pre'] * shell['60DPD_end_L1'] + shell['90DPD_pre'] * shell['90DPD_end_L1'] + 1 * shell['pre_end_L1']
shell.drop(['cur_end_L1', '30DPD_end_L1', '60DPD_end_L1', '90DPD_end_L1', 'def_end_L1', 'pre_end_L1'], axis = 1, inplace = True)
### Calculate the marginal default and prepayment rates in each period
shell['cur_end_L1'] = shell.groupby('LOAN_ID')['cur_end'].shift(1)
shell['30DPD_end_L1'] = shell.groupby('LOAN_ID')['30DPD_end'].shift(1)
shell['60DPD_end_L1'] = shell.groupby('LOAN_ID')['60DPD_end'].shift(1)
shell['90DPD_end_L1'] = shell.groupby('LOAN_ID')['90DPD_end'].shift(1)
shell['def_end_L1'] = shell.groupby('LOAN_ID')['def_end'].shift(1).fillna(0)
shell['pre_end_L1'] = shell.groupby('LOAN_ID')['pre_end'].shift(1).fillna(0)
shell.loc[(shell['status']=='Current') & shell['cur_end_L1'].isnull(), ['cur_end_L1', '30DPD_end_L1', '60DPD_end_L1', '90DPD_end_L1']] = [1, 0, 0, 0]
shell.loc[(shell['status']=='30DPD') & shell['cur_end_L1'].isnull(), ['cur_end_L1', '30DPD_end_L1', '60DPD_end_L1', '90DPD_end_L1']] = [0, 1, 0, 0]
shell.loc[(shell['status']=='60DPD') & shell['cur_end_L1'].isnull(), ['cur_end_L1', '30DPD_end_L1', '60DPD_end_L1', '90DPD_end_L1']] = [0, 0, 1, 0]
shell.loc[(shell['status']=='90DPD') & shell['cur_end_L1'].isnull(), ['cur_end_L1', '30DPD_end_L1', '60DPD_end_L1', '90DPD_end_L1']] = [0, 0, 0, 1]
shell['margDef'] = (shell['def_end'] - shell['def_end_L1']) / (1 - shell['def_end_L1'])
shell['margPre'] = (shell['pre_end'] - shell['pre_end_L1']) / (1 - shell['pre_end_L1'])
shell.loc[shell['period']!=1, 'PBAL_BEG'] = -999999
shell.loc[shell['period']==1, 'PBAL_BEG_ADJ'] = shell['PBAL_BEG']
shell.loc[shell['period']!=1, 'PBAL_BEG_ADJ'] = -999999
shell['pmtExp'] = (1 * (shell['cur_cur'] * shell['cur_end_L1'] + shell['30DPD_30DPD'] * shell['30DPD_end_L1'] + shell['60DPD_60DPD'] * shell['60DPD_end_L1'] + shell['90DPD_90DPD'] * shell['90DPD_end_L1']) + 2 * (shell['30DPD_cur'] * shell['30DPD_end_L1'] + shell['60DPD_30DPD'] * shell['60DPD_end_L1'] + shell['90DPD_60DPD'] * shell['90DPD_end_L1']) + 3 * (shell['60DPD_cur'] * shell['60DPD_end_L1'] + shell['90DPD_30DPD'] * shell['90DPD_end_L1']) + 4 * (shell['90DPD_30DPD'] * shell['90DPD_end_L1']))
for per in range(1, shell['period'].max() + 1):
# Contractual amortization
shell.loc[shell['period']==per, 'INT_DUE'] = shell['PBAL_BEG'] * shell['int_rate'] / 12
shell.loc[shell['period']==per, 'PRNCP_DUE'] = shell['installment'] - shell['INT_DUE']
shell.loc[shell['period']==per, 'PBAL_END'] = shell['PBAL_BEG'] - shell['PRNCP_DUE']
shell.loc[shell['period'] > per, 'PBAL_BEG'] = shell.groupby(['LOAN_ID'])['PBAL_END'].shift(1)
# Expected cash flows
shell.loc[shell['period']==per, 'PBAL_BEG_ADJ'] = shell['PBAL_BEG_ADJ'] * (1 - (shell['margDef']))
shell.loc[shell['period']==per, 'INT_PAID'] = shell['INT_DUE'] * shell['pmtExp']
shell.loc[shell['period']==per, 'PRNCP_PAID'] = shell['PRNCP_DUE'] * shell['pmtExp'] + shell['margPre'] * shell['PBAL_BEG_ADJ']
shell.loc[shell['period']==per, 'PBAL_END_ADJ'] = shell['PBAL_BEG_ADJ'] - shell['PRNCP_PAID'].clip(0, None)
shell.loc[shell['period'] > per, 'PBAL_BEG_ADJ'] = shell.groupby(['LOAN_ID'])['PBAL_END_ADJ'].shift(1)
Подробнее здесь: https://stackoverflow.com/questions/792 ... -in-python
Как создать матрицу перехода в Python ⇐ Python
Программы на Python
1734386912
Anonymous
Пример кода для создания матрицы перехода на Python. Для этого используются фиктивные данные для создания вероятностей перехода, а затем оценивается чистая приведенная стоимость будущих денежных потоков.
Пример кода для создания матрицы перехода на Python. Для этого используются фиктивные данные для создания вероятностей перехода, а затем оценивается чистая приведенная стоимость будущих денежных потоков.
Пример кода для создания матрицы перехода на Python. Для этого используются фиктивные данные для создания вероятностей перехода, а затем оценивается чистая приведенная стоимость будущих денежных потоков.
Пример кода для создания матрицы перехода на Python. Для этого используются фиктивные данные для создания вероятностей перехода, а затем оценивается чистая приведенная стоимость будущих денежных потоков.
Пример кода для создания матрицы перехода на Python. Для этого используются фиктивные данные для создания вероятностей перехода, а затем оценивается чистая приведенная стоимость будущих денежных потоков.
Пример кода для создания матрицы перехода на Python. Для этого используются фиктивные данные для создания вероятностей перехода, а затем оценивается чистая приведенная стоимость будущих денежных потоков.
Пример кода для создания матрицы перехода на Python. Для этого используются фиктивные данные для определения вероятностей перехода, а затем оценивается чистая приведенная стоимость будущих денежных потоков.
##############
# Dummy data #
##############
df = pd.concat([
pd.DataFrame(range(1, 180001), columns = ['LOAN_ID']),
pd.DataFrame(np.random.randint(10000, 100000, size = (180000, 4)), columns = ['cur_' + y for y in ['cur', '30DPD', '60DPD', '90DPD']]),
pd.DataFrame(np.random.randint(0, 5000, size = (180000, 2)), columns = ['cur_' + y for y in ['def', 'pre']]),
pd.DataFrame(np.random.randint(10000, 100000, size = (180000, 4)), columns = ['30DPD_' + y for y in ['cur', '30DPD', '60DPD', '90DPD']]),
pd.DataFrame(np.random.randint(0, 5000, size = (180000, 2)), columns = ['30DPD_' + y for y in ['def', 'pre']]),
pd.DataFrame(np.random.randint(10000, 100000, size = (180000, 4)), columns = ['60DPD_' + y for y in ['cur', '30DPD', '60DPD', '90DPD']]),
pd.DataFrame(np.random.randint(0, 5000, size = (180000, 2)), columns = ['60DPD_' + y for y in ['def', 'pre']]),
pd.DataFrame(np.random.randint(10000, 100000, size = (180000, 4)), columns = ['90DPD_' + y for y in ['cur', '30DPD', '60DPD', '90DPD']]),
pd.DataFrame(np.random.randint(0, 5000, size = (180000, 2)), columns = ['90DPD_' + y for y in ['def', 'pre']]),
], axis = 1)
df['PBAL_BEG'] = 100000
df['installment'] = 1980.12
df['int_rate'] = 0.07
df['rem'] = 60
df.loc[0:150000, 'status'] = 'Current'
df.loc[150000:175000, 'status'] = '30DPD'
df.loc[175000:177500, 'status'] = '60DPD'
df.loc[177500:, 'status'] = '90DPD'
df.loc[:, 'cur_cur':'cur_pre'] = df.loc[:, 'cur_cur':'cur_pre'].div(df.loc[:, 'cur_cur':'cur_pre'].sum(axis = 1), axis = 0)
df.loc[:, '30DPD_cur':'30DPD_pre'] = df.loc[:, '30DPD_cur':'30DPD_pre'].div(df.loc[:, '30DPD_cur':'30DPD_pre'].sum(axis = 1), axis = 0)
df.loc[:, '60DPD_cur':'60DPD_pre'] = df.loc[:, '60DPD_cur':'60DPD_pre'].div(df.loc[:, '60DPD_cur':'60DPD_pre'].sum(axis = 1), axis = 0)
df.loc[:, '90DPD_cur':'90DPD_pre'] = df.loc[:, '90DPD_cur':'90DPD_pre'].div(df.loc[:, '90DPD_cur':'90DPD_pre'].sum(axis = 1), axis = 0)
# Repeate each loan by the number of months remaining on the loan and get the observation number for each
shell = df.loc[df.index.repeat(df.rem)].reset_index(drop = True)
shell['period'] = shell.groupby(['LOAN_ID']).cumcount() + 1
# Set the probability distributions in period 1 based on the current loan state
filtCur = (shell['status']=='Current') & (shell['period']==1)
shell.loc[filtCur, 'cur_end'] = shell['cur_cur']
shell.loc[filtCur, '30DPD_end'] = shell['cur_30DPD']
shell.loc[filtCur, '60DPD_end'] = shell['cur_60DPD']
shell.loc[filtCur, '90DPD_end'] = shell['cur_90DPD']
shell.loc[filtCur, 'def_end'] = shell['cur_def']
shell.loc[filtCur, 'pre_end'] = shell['cur_pre']
filt30DPD = (shell['status']=='30DPD') & (shell['period']==1)
shell.loc[filt30DPD, 'cur_end'] = shell['30DPD_cur']
shell.loc[filt30DPD, '30DPD_end'] = shell['30DPD_30DPD']
shell.loc[filt30DPD, '60DPD_end'] = shell['30DPD_60DPD']
shell.loc[filt30DPD, '90DPD_end'] = shell['30DPD_90DPD']
shell.loc[filt30DPD, 'def_end'] = shell['30DPD_def']
shell.loc[filt30DPD, 'pre_end'] = shell['30DPD_pre']
filt60DPD = (shell['status']=='60DPD') & (shell['period']==1)
shell.loc[filt60DPD, 'cur_end'] = shell['60DPD_cur']
shell.loc[filt60DPD, '30DPD_end'] = shell['60DPD_30DPD']
shell.loc[filt60DPD, '60DPD_end'] = shell['60DPD_60DPD']
shell.loc[filt60DPD, '90DPD_end'] = shell['60DPD_90DPD']
shell.loc[filt60DPD, 'def_end'] = shell['60DPD_def']
shell.loc[filt60DPD, 'pre_end'] = shell['60DPD_pre']
filt90DPD = (shell['status']=='90DPD') & (shell['period']==1)
shell.loc[filt90DPD, 'cur_end'] = shell['90DPD_cur']
shell.loc[filt90DPD, '30DPD_end'] = shell['90DPD_30DPD']
shell.loc[filt90DPD, '60DPD_end'] = shell['90DPD_60DPD']
shell.loc[filt90DPD, '90DPD_end'] = shell['90DPD_90DPD']
shell.loc[filt90DPD, 'def_end'] = shell['90DPD_def']
shell.loc[filt90DPD, 'pre_end'] = shell['90DPD_pre']
endCols = ['cur_end', '30DPD_end', '60DPD_end', '90DPD_end', 'def_end', 'pre_end']
# Run the transition matrix over the number of periods in the data
for per in range(2, shell['period'].max() + 1):
shell[[x + '_L1' for x in endCols]] = shell.groupby(['LOAN_ID'])[endCols].shift(1)
filtPer = (shell['period']==per)
shell.loc[filtPer, 'cur_end'] = shell['cur_cur'] * shell['cur_end_L1'] + shell['30DPD_cur'] * shell['30DPD_end_L1'] + shell['60DPD_cur'] * shell['60DPD_end_L1'] + shell['90DPD_cur'] * shell['90DPD_end_L1']
shell.loc[filtPer, '30DPD_end'] = shell['cur_30DPD'] * shell['cur_end_L1'] + shell['30DPD_30DPD'] * shell['30DPD_end_L1'] + shell['60DPD_30DPD'] * shell['60DPD_end_L1'] + shell['90DPD_30DPD'] * shell['90DPD_end_L1']
shell.loc[filtPer, '60DPD_end'] = shell['cur_60DPD'] * shell['cur_end_L1'] + shell['30DPD_60DPD'] * shell['30DPD_end_L1'] + shell['60DPD_60DPD'] * shell['60DPD_end_L1'] + shell['90DPD_60DPD'] * shell['90DPD_end_L1']
shell.loc[filtPer, '90DPD_end'] = shell['cur_90DPD'] * shell['cur_end_L1'] + shell['30DPD_90DPD'] * shell['30DPD_end_L1'] + shell['60DPD_90DPD'] * shell['60DPD_end_L1'] + shell['90DPD_90DPD'] * shell['90DPD_end_L1']
shell.loc[filtPer, 'def_end'] = shell['cur_def'] * shell['cur_end_L1'] + shell['30DPD_def'] * shell['30DPD_end_L1'] + shell['60DPD_def'] * shell['60DPD_end_L1'] + shell['90DPD_def'] * shell['90DPD_end_L1'] + 1 * shell['def_end_L1']
shell.loc[filtPer, 'pre_end'] = shell['cur_pre'] * shell['cur_end_L1'] + shell['30DPD_pre'] * shell['30DPD_end_L1'] + shell['60DPD_pre'] * shell['60DPD_end_L1'] + shell['90DPD_pre'] * shell['90DPD_end_L1'] + 1 * shell['pre_end_L1']
shell.drop(['cur_end_L1', '30DPD_end_L1', '60DPD_end_L1', '90DPD_end_L1', 'def_end_L1', 'pre_end_L1'], axis = 1, inplace = True)
### Calculate the marginal default and prepayment rates in each period
shell['cur_end_L1'] = shell.groupby('LOAN_ID')['cur_end'].shift(1)
shell['30DPD_end_L1'] = shell.groupby('LOAN_ID')['30DPD_end'].shift(1)
shell['60DPD_end_L1'] = shell.groupby('LOAN_ID')['60DPD_end'].shift(1)
shell['90DPD_end_L1'] = shell.groupby('LOAN_ID')['90DPD_end'].shift(1)
shell['def_end_L1'] = shell.groupby('LOAN_ID')['def_end'].shift(1).fillna(0)
shell['pre_end_L1'] = shell.groupby('LOAN_ID')['pre_end'].shift(1).fillna(0)
shell.loc[(shell['status']=='Current') & shell['cur_end_L1'].isnull(), ['cur_end_L1', '30DPD_end_L1', '60DPD_end_L1', '90DPD_end_L1']] = [1, 0, 0, 0]
shell.loc[(shell['status']=='30DPD') & shell['cur_end_L1'].isnull(), ['cur_end_L1', '30DPD_end_L1', '60DPD_end_L1', '90DPD_end_L1']] = [0, 1, 0, 0]
shell.loc[(shell['status']=='60DPD') & shell['cur_end_L1'].isnull(), ['cur_end_L1', '30DPD_end_L1', '60DPD_end_L1', '90DPD_end_L1']] = [0, 0, 1, 0]
shell.loc[(shell['status']=='90DPD') & shell['cur_end_L1'].isnull(), ['cur_end_L1', '30DPD_end_L1', '60DPD_end_L1', '90DPD_end_L1']] = [0, 0, 0, 1]
shell['margDef'] = (shell['def_end'] - shell['def_end_L1']) / (1 - shell['def_end_L1'])
shell['margPre'] = (shell['pre_end'] - shell['pre_end_L1']) / (1 - shell['pre_end_L1'])
shell.loc[shell['period']!=1, 'PBAL_BEG'] = -999999
shell.loc[shell['period']==1, 'PBAL_BEG_ADJ'] = shell['PBAL_BEG']
shell.loc[shell['period']!=1, 'PBAL_BEG_ADJ'] = -999999
shell['pmtExp'] = (1 * (shell['cur_cur'] * shell['cur_end_L1'] + shell['30DPD_30DPD'] * shell['30DPD_end_L1'] + shell['60DPD_60DPD'] * shell['60DPD_end_L1'] + shell['90DPD_90DPD'] * shell['90DPD_end_L1']) + 2 * (shell['30DPD_cur'] * shell['30DPD_end_L1'] + shell['60DPD_30DPD'] * shell['60DPD_end_L1'] + shell['90DPD_60DPD'] * shell['90DPD_end_L1']) + 3 * (shell['60DPD_cur'] * shell['60DPD_end_L1'] + shell['90DPD_30DPD'] * shell['90DPD_end_L1']) + 4 * (shell['90DPD_30DPD'] * shell['90DPD_end_L1']))
for per in range(1, shell['period'].max() + 1):
# Contractual amortization
shell.loc[shell['period']==per, 'INT_DUE'] = shell['PBAL_BEG'] * shell['int_rate'] / 12
shell.loc[shell['period']==per, 'PRNCP_DUE'] = shell['installment'] - shell['INT_DUE']
shell.loc[shell['period']==per, 'PBAL_END'] = shell['PBAL_BEG'] - shell['PRNCP_DUE']
shell.loc[shell['period'] > per, 'PBAL_BEG'] = shell.groupby(['LOAN_ID'])['PBAL_END'].shift(1)
# Expected cash flows
shell.loc[shell['period']==per, 'PBAL_BEG_ADJ'] = shell['PBAL_BEG_ADJ'] * (1 - (shell['margDef']))
shell.loc[shell['period']==per, 'INT_PAID'] = shell['INT_DUE'] * shell['pmtExp']
shell.loc[shell['period']==per, 'PRNCP_PAID'] = shell['PRNCP_DUE'] * shell['pmtExp'] + shell['margPre'] * shell['PBAL_BEG_ADJ']
shell.loc[shell['period']==per, 'PBAL_END_ADJ'] = shell['PBAL_BEG_ADJ'] - shell['PRNCP_PAID'].clip(0, None)
shell.loc[shell['period'] > per, 'PBAL_BEG_ADJ'] = shell.groupby(['LOAN_ID'])['PBAL_END_ADJ'].shift(1)
Подробнее здесь: [url]https://stackoverflow.com/questions/79286183/how-do-i-generate-a-transition-matrix-in-python[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия