UNPKG

@turnbuckle/aprs-calculator-services

Version:

Anchorpoint Risk Calculator Service.

37 lines (36 loc) 1.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.calculateCohortMatrix = void 0; var hazard_rate_helper_module_1 = require("../../common/hazard-rate-helper.module"); var mathjs_1 = require("mathjs"); var ts_stopwatch_1 = require("ts-stopwatch"); var assert_1 = require("../../common/assert"); function calculateCohortMatrix(input) { var sw = new ts_stopwatch_1.Stopwatch(); sw.start(); (0, assert_1.assert)(input.absorbingStates === 1 || input.absorbingStates === 2, "absorbingStates can only be 1 or 2"); var ageAnalysis = (0, hazard_rate_helper_module_1.applyAgeAnalysisCalculations)(input.ageAnalysis); var nlambdaMatrix = (0, hazard_rate_helper_module_1.nlambda)(ageAnalysis, input.maxRating, input.absorbingStates); var migrationMatrix = (0, hazard_rate_helper_module_1.createMatrix)(input.maxRating); for (var row = 0; row < nlambdaMatrix.length; row++) { var sumRow = (0, mathjs_1.sum)(nlambdaMatrix[row]); for (var col = 0; col < nlambdaMatrix[row].length; col++) { if (sumRow === 0) { migrationMatrix[row][col] = (row === col ? 1 : 0); } else { migrationMatrix[row][col] = nlambdaMatrix[row][col] / sumRow; } } } if (input.dataFrequencyScalar > 0) { migrationMatrix = (0, hazard_rate_helper_module_1.nthPowerMatrix)(migrationMatrix, input.dataFrequencyScalar); } sw.stop(); var output = { executeDuration: sw.getTime(), matrix: migrationMatrix, }; return output; } exports.calculateCohortMatrix = calculateCohortMatrix;