Created
June 26, 2017 13:04
-
-
Save hsinjungwu/de5848d80c73f1c1c368626e752d668a to your computer and use it in GitHub Desktop.
An example for migration matrix rate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Linq; | |
namespace MigrationMatrixRate | |
{ | |
internal class Program | |
{ | |
private enum CalcWays { PdMarginalNew, PdMarginalNewAcc, PDAcc } | |
private static void Main(string[] args) | |
{ | |
int maxDuration = 45; | |
int rateFrom = 21; | |
int rateTo = 21; | |
decimal[,] rateMatrix = new decimal[rateFrom, rateTo]; | |
var source = File.ReadAllLines(@"D:\source.txt"); | |
for (int i = 0; i < rateFrom; i++) | |
{ | |
var line = source[i].Split(','); | |
for (int j = 0; j < rateTo; j++) | |
rateMatrix[i, j] = decimal.Parse(line[j]); | |
} | |
decimal[,] tempMatrix = new decimal[rateFrom, maxDuration]; | |
decimal[,] result = new decimal[rateFrom, maxDuration]; | |
CalcWays calcWay = CalcWays.PDAcc; | |
for (int duration = 0; duration < maxDuration; duration++) | |
{ | |
for (int i = 0; i < rateFrom; i++) | |
{ | |
if (duration == 0) | |
{ | |
tempMatrix[i, duration] = rateMatrix[i, rateTo - 1] / 100; | |
result[i, duration] = tempMatrix[i, duration]; | |
} | |
else | |
{ | |
for (int j = 0; j < rateTo; j++) | |
tempMatrix[i, duration] += rateMatrix[i, j] / 100 * tempMatrix[j, duration - 1]; | |
switch (calcWay) | |
{ | |
case CalcWays.PdMarginalNew: | |
result[i, duration] = tempMatrix[i, duration] - tempMatrix[i, duration - 1]; | |
break; | |
case CalcWays.PdMarginalNewAcc: | |
result[i, duration] = result[i, duration - 1] + tempMatrix[i, duration - 1] - tempMatrix[i, duration - 2]; | |
break; | |
case CalcWays.PDAcc: | |
result[i, duration] = tempMatrix[i, duration]; | |
break; | |
} | |
} | |
} | |
} | |
string resultPath = @"D:\result.txt"; | |
if (File.Exists(resultPath)) File.WriteAllText(resultPath, string.Empty); | |
using (StreamWriter sw = File.AppendText(resultPath)) | |
{ | |
var resultList = result.Cast<decimal>().ToList(); | |
for (int i = 0; i < rateFrom; i++) | |
sw.WriteLine(string.Join(",", resultList.GetRange(i * maxDuration, maxDuration))); | |
} | |
Console.WriteLine("done"); | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment