# Example 2: Minimal -> Three state transition matrix

M = tm.TransitionMatrix(values=Generic)
# Lets take a look at the values
M.print()
M.validate()

# The size of the rating scale
Ratings = M.dimension

# The Default (absorbing state)
Default = Ratings - 1

# Lets extend the matrix into multi periods
Periods = 5
T = tm.TransitionMatrixSet(values=M, periods=Periods, method='Power', temporal_type='Cumulative')

# Initialize a threshold set
As = ThresholdSet(TMSet=T)

print("> Fit Multiperiod Thresholds")
for ri in range(0, Ratings):
    print("RI: ", ri)
    As.fit(AR_Model, ri)

print("> Validate Multiperiod Thresholds against Input Transition Matrix Set")
Q = As.validate(AR_Model)

print("> Save Multiperiod Thresholds in JSON Format")
As.to_json('generic_thresholds.json')
Esempio n. 2
0
# The size of the rating scale
Ratings = M.dimension

# The Default (absorbing state)
Default = Ratings - 1

# Lets extend the matrix into multi periods
Periods = 10
T = tm.TransitionMatrixSet(values=M,
                           periods=Periods,
                           method='Power',
                           temporal_type='Cumulative')

# Initialize a threshold set
As = ThresholdSet(TMSet=T)

for ri in range(0, Ratings):
    print("RI: ", ri)
    As.fit(AR_Model, ri)

lines = []
ri = 3
for rf in range(0, Ratings):
    for k in range(0, Periods):
        if rf != ri:
            value = As.A[ri, rf, k]
            line = [(k, value), (k + 1.0, value)]
            lines.append(line)

lc = mc.LineCollection(lines, linewidths=2)
    # The size of the rating scale
    Ratings = M.dimension

    # The Default (absorbing state)
    Default = Ratings - 1

    # Lets extend the matrix into multi periods
    Periods = 5
    T = tm.TransitionMatrixSet(values=M,
                               periods=Periods,
                               method='Power',
                               temporal_type='Cumulative')

    # Initialize a threshold set
    As = ThresholdSet(TMSet=T)

    print("> Fit Multiperiod Thresholds")
    for ri in range(0, Ratings):
        print("RI: ", ri)
        As.fit(AR_Model, ri)

    print(
        "> Validate Multiperiod Thresholds against Input Transition Matrix Set"
    )
    Q = As.validate(AR_Model)

    print("> Save Multiperiod Thresholds in JSON Format")
    As.to_json('generic_thresholds.json')

elif example == 2:
from portfolioAnalytics import source_path
dataset_path = source_path + "datasets/"

# A Generic matrix with 7 non-absorbing and one absorbing state

Generic = [[0.92039, 0.0709, 0.0063, 0.0015, 0.0006, 0.0002, 0.0001, 1e-05],
           [0.0062, 0.9084, 0.0776, 0.0059, 0.0006, 0.001, 0.0002, 0.0001],
           [0.0005, 0.0209, 0.9138, 0.0579, 0.0044, 0.0016, 0.0004, 0.0005],
           [0.0004, 0.0021, 0.041, 0.8936, 0.0482, 0.0086, 0.0024, 0.0037],
           [0.0003, 0.0008, 0.014, 0.0553, 0.8225, 0.0815, 0.0111, 0.0145],
           [0.0001, 0.0004, 0.0057, 0.0134, 0.0539, 0.8114, 0.0492, 0.0659],
           [1e-05, 0.0002, 0.0029, 0.0058, 0.0155, 0.1054, 0.52879, 0.3414],
           [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0]]

# Initialize a threshold set from file
As = ThresholdSet(json_file=dataset_path + 'generic_thresholds.json')

# Inspect values (we assume these inputs have already been validated after generation!)
# As.print(accuracy=4)

# Specify the initial rating of interest
ri = 3

# As.plot(ri)

# Initialize a conditional migration matrix with the given thresholds

Q = ConditionalTransitionMatrix(thresholds=As)

# # Q.print()
#
    Ratings = M.dimension

    # The Default (absorbing state)
    Default = Ratings - 1

    # Lets extend the matrix into multi periods
    Periods = 10
    T = tm.TransitionMatrixSet(values=M,
                               periods=Periods,
                               method='Power',
                               temporal_type='Cumulative')
    print("> Extend the matrix into 10 periods")
elif example == 2:
    T = tm.TransitionMatrixSet(periods=120,
                               temporal_type='Cumulative',
                               json_file=dataset_path + "generic_monthly.json")
    Ratings = T.entries[0].dimension

# Initialize a threshold set
As = ThresholdSet(TMSet=T)

# Calculate thresholds per initial rating state
print("> Calculate thresholds per initial rating state")
for ri in range(0, Ratings):
    print("Initial Rating: ", ri)
    As.fit(AR_Model, ri, dt=1.0 / 12.0)
As.to_json(json_file="monthly_thresholds.json")

# Display the calculated thresholds
# print("> Display the calculated thresholds")
# As.print()
# Example 2: Minimal -> Three state transition matrix

M = tm.TransitionMatrix(values=Generic)

# The size of the rating scale
Ratings = M.dimension

# The Default (absorbing state)
Default = Ratings - 1

# Lets extend the matrix into multi periods
Periods = 10
T = tm.TransitionMatrixSet(values=M, periods=Periods, method='Power', temporal_type='Cumulative')

# Initialize a threshold set
As = ThresholdSet(TMSet=T)

for ri in range(0, Ratings):
    print("RI: ", ri)
    As.fit(AR_Model, ri)

lines = []
ri = 3
for rf in range(0, Ratings):
    for k in range(0, Periods):
        if rf != ri:
            value = As.A[ri, rf, k]
            line = [(k, value), (k + 1.0, value)]
            lines.append(line)

lc = mc.LineCollection(lines, linewidths=2)