-1
DOTNET FIDDLE
Editing
The following calculations correspond to actuarial calculation variables. The ages from "present age" to the age of 120 years run through the probabilities of a person’s death using a tablet called RP-2000.
The second loop ranging from 0 to 22 corresponds to retirement entry ages, for calculating eligibility to plan benefits, which also takes into account the ages of the previous paragraph for each beneficiary or plan participant.
I do not think it necessary to publish the complete calculations for the problem, because it would be very difficult to understand everything.
Original
In the code below, the variables sampleVar
and dummyVar
are calculated by the loop internal. In the class Program
there are three methods: the method Program.Qx()
returns the value of qx
, which is a pre-defined value. The method Program.SampleVar()
calculates the value of sampleVar
; and the method Program.DummyVar()
calculates the value of dummyVar
.
The idea here is to "age" a person, say. That person has a current age and we have to go through each value of Program.Qx()
, which ranges from 0 to 120 years, from the current age of the person up to 120 years, which is the last age of Program.Qx()
.
My problem here is to calculate the correct value of dummyVar
.
dummyVar
always starts with 1 in the first iteration, then uses its previous value and the current value of sampleVar
for the calculation of its other values, i.e., dummyVar
refers to its own previous value.
Below is the code:
using System;
public class Program
{
public static void Main()
{
Program p = new Program();
// A idade é calculada para várias pessoas e vem do banco de dados
// A idade abaixo serve para essa demonstração
int idade = 25;
// "Envelhece" a pessoa da idade atual até a idade de 120 anos
for (int i = idade; i <= 120; i++) {
double qx = p.Qx(i - 1);
double sampleVar;
double dummyVar = 1;
// Para cada idade, da idade atual até a idade de 120 anos, calcula os valores de
// sampleVar e dummyVar e depois escreve a saída no Console
for (int j = 0; j <= 22; j++) {
sampleVar = p.SampleVar(qx);
Console.WriteLine("Idade: " + i + " - Sample var: " + sampleVar + " - Dummy Var: " + dummyVar);
// Atualiza o valor de dummyVar
dummyVar = p.DummyVar(sampleVar, dummyVar);
}
}
}
// Representa a conjunção de Program.SampleVar() e Program.DummyVar()
public double DummyVar(double sampleVar, double dummyVar)
{
return dummyVar *= sampleVar;
}
// Representa a probabilidade complementar de Program.Qx()
// Program.SampleVar() é calculada a partir da idade atual da pessoa
public double SampleVar(double qx)
{
return 1 - qx;
}
// Representa uma probabilidade para as idade de 0 até 120 anos
public double Qx(int idade)
{
double[] rpm = new double[] {
0.000000,
0.000510,
0.000344,
0.000286,
0.000222,
0.000204,
0.000195,
0.000187,
0.000173,
0.000167,
0.000170,
0.000175,
0.000182,
0.000192,
0.000203,
0.000215,
0.000227,
0.000241,
0.000253,
0.000265,
0.000276,
0.000286,
0.000293,
0.000298,
0.000300,
0.000301,
0.000302,
0.000306,
0.000314,
0.000330,
0.000355,
0.000399,
0.000450,
0.000505,
0.000562,
0.000618,
0.000673,
0.000723,
0.000771,
0.000817,
0.000863,
0.000914,
0.000972,
0.001039,
0.001118,
0.001206,
0.001293,
0.001387,
0.001488,
0.001596,
0.001710,
0.001959,
0.002134,
0.002333,
0.002557,
0.002899,
0.003360,
0.003754,
0.004218,
0.004756,
0.005398,
0.006141,
0.007006,
0.008010,
0.009024,
0.010190,
0.011527,
0.012860,
0.014297,
0.015842,
0.017765,
0.019656,
0.021825,
0.024310,
0.027120,
0.030267,
0.033735,
0.037525,
0.041698,
0.046342,
0.051494,
0.057633,
0.064389,
0.071774,
0.079823,
0.088606,
0.098238,
0.108834,
0.120472,
0.133136,
0.146726,
0.159815,
0.173284,
0.186930,
0.200554,
0.213993,
0.227124,
0.239882,
0.252237,
0.264166,
0.275645,
0.286902,
0.297348,
0.306432,
0.313602,
0.318309,
0.320000,
0.320000,
0.320000,
0.320000,
0.320000,
0.320000,
0.320000,
0.320000,
0.320000,
0.320000,
0.320000,
0.320000,
0.320000,
0.320000,
1.000000
};
if (idade < rpm.Length)
{
return rpm[idade];
}
else
{
return 1;
}
}
}
The output of the above code is the following for the first two ages:
Idade: 25 - Sample var: 0.9997 - Dummy Var: 1
Idade: 25 - Sample var: 0.9997 - Dummy Var: 0.9997
Idade: 25 - Sample var: 0.9997 - Dummy Var: 0.99940009
Idade: 25 - Sample var: 0.9997 - Dummy Var: 0.999100269973
Idade: 25 - Sample var: 0.9997 - Dummy Var: 0.998800539892008
Idade: 25 - Sample var: 0.9997 - Dummy Var: 0.998500899730041
Idade: 25 - Sample var: 0.9997 - Dummy Var: 0.998201349460122
Idade: 25 - Sample var: 0.9997 - Dummy Var: 0.997901889055284
Idade: 25 - Sample var: 0.9997 - Dummy Var: 0.997602518488567
Idade: 25 - Sample var: 0.9997 - Dummy Var: 0.997303237733021
Idade: 25 - Sample var: 0.9997 - Dummy Var: 0.997004046761701
Idade: 25 - Sample var: 0.9997 - Dummy Var: 0.996704945547672
Idade: 25 - Sample var: 0.9997 - Dummy Var: 0.996405934064008
Idade: 25 - Sample var: 0.9997 - Dummy Var: 0.996107012283789
Idade: 25 - Sample var: 0.9997 - Dummy Var: 0.995808180180104
Idade: 25 - Sample var: 0.9997 - Dummy Var: 0.99550943772605
Idade: 25 - Sample var: 0.9997 - Dummy Var: 0.995210784894732
Idade: 25 - Sample var: 0.9997 - Dummy Var: 0.994912221659263
Idade: 25 - Sample var: 0.9997 - Dummy Var: 0.994613747992766
Idade: 25 - Sample var: 0.9997 - Dummy Var: 0.994315363868368
Idade: 25 - Sample var: 0.9997 - Dummy Var: 0.994017069259207
Idade: 25 - Sample var: 0.9997 - Dummy Var: 0.99371886413843
Idade: 25 - Sample var: 0.9997 - Dummy Var: 0.993420748479188
Idade: 26 - Sample var: 0.999699 - Dummy Var: 1
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.999699
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.999398090601
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.999097271775729
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.998796543496925
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.998495905737332
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.998195358469705
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.997894901666806
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.997594535301404
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.997294259346278
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.996994073774215
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.996693978558009
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.996393973670463
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.996094059084388
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.995794234772604
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.995494500707937
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.995194856863224
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.994895303211309
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.994595839725042
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.994296466377285
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.993997183140905
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.99369798998878
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.993398886893793
Visually the calculation is done as follows:
dummyVar sampleVar Idade
+-------------------------+-----------------------+-------------+
| 1 | 0.9997 | 25 |
+-------------------------+-----------------------+-------------+
| 1 * 0.999699 | 0.999699 | 26 |
+-------------------------+-----------------------+-------------+
| 0.999699 * 0.999698 | 0.999698 | 27 |
+-------------------------+-----------------------+-------------+
| 0.9993970909 * 0.999694 | 0.999694 | 28 |
+-------------------------|-----------------------+-------------+
Realize the value of sampleVar
for the age of 25 is never used, ie the first value of sampleVar
has no dependents.
As this happens in the second loop, the expected result would be the following:
Idade: 25 - Sample var: 0.9997 - Dummy Var: 1
Idade: 25 - Sample var: 0.9997 - Dummy Var: 1
Idade: 25 - Sample var: 0.9997 - Dummy Var: 1
Idade: 25 - Sample var: 0.9997 - Dummy Var: 1
Idade: 25 - Sample var: 0.9997 - Dummy Var: 1
Idade: 25 - Sample var: 0.9997 - Dummy Var: 1
Idade: 25 - Sample var: 0.9997 - Dummy Var: 1
Idade: 25 - Sample var: 0.9997 - Dummy Var: 1
Idade: 25 - Sample var: 0.9997 - Dummy Var: 1
Idade: 25 - Sample var: 0.9997 - Dummy Var: 1
Idade: 25 - Sample var: 0.9997 - Dummy Var: 1
Idade: 25 - Sample var: 0.9997 - Dummy Var: 1
Idade: 25 - Sample var: 0.9997 - Dummy Var: 1
Idade: 25 - Sample var: 0.9997 - Dummy Var: 1
Idade: 25 - Sample var: 0.9997 - Dummy Var: 1
Idade: 25 - Sample var: 0.9997 - Dummy Var: 1
Idade: 25 - Sample var: 0.9997 - Dummy Var: 1
Idade: 25 - Sample var: 0.9997 - Dummy Var: 1
Idade: 25 - Sample var: 0.9997 - Dummy Var: 1
Idade: 25 - Sample var: 0.9997 - Dummy Var: 1
Idade: 25 - Sample var: 0.9997 - Dummy Var: 1
Idade: 25 - Sample var: 0.9997 - Dummy Var: 1
Idade: 25 - Sample var: 0.9997 - Dummy Var: 1
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.999699
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.999699
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.999699
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.999699
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.999699
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.999699
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.999699
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.999699
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.999699
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.999699
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.999699
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.999699
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.999699
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.999699
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.999699
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.999699
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.999699
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.999699
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.999699
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.999699
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.999699
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.999699
Idade: 26 - Sample var: 0.999699 - Dummy Var: 0.999699
Idade: 27 - Sample var: 0.999698 - Dummy Var: 0.9993970909
Idade: 27 - Sample var: 0.999698 - Dummy Var: 0.9993970909
Idade: 27 - Sample var: 0.999698 - Dummy Var: 0.9993970909
Idade: 27 - Sample var: 0.999698 - Dummy Var: 0.9993970909
Idade: 27 - Sample var: 0.999698 - Dummy Var: 0.9993970909
Idade: 27 - Sample var: 0.999698 - Dummy Var: 0.9993970909
Idade: 27 - Sample var: 0.999698 - Dummy Var: 0.9993970909
Idade: 27 - Sample var: 0.999698 - Dummy Var: 0.9993970909
Idade: 27 - Sample var: 0.999698 - Dummy Var: 0.9993970909
Idade: 27 - Sample var: 0.999698 - Dummy Var: 0.9993970909
Idade: 27 - Sample var: 0.999698 - Dummy Var: 0.9993970909
Idade: 27 - Sample var: 0.999698 - Dummy Var: 0.9993970909
Idade: 27 - Sample var: 0.999698 - Dummy Var: 0.9993970909
Idade: 27 - Sample var: 0.999698 - Dummy Var: 0.9993970909
Idade: 27 - Sample var: 0.999698 - Dummy Var: 0.9993970909
Idade: 27 - Sample var: 0.999698 - Dummy Var: 0.9993970909
Idade: 27 - Sample var: 0.999698 - Dummy Var: 0.9993970909
Idade: 27 - Sample var: 0.999698 - Dummy Var: 0.9993970909
Idade: 27 - Sample var: 0.999698 - Dummy Var: 0.9993970909
Idade: 27 - Sample var: 0.999698 - Dummy Var: 0.9993970909
Idade: 27 - Sample var: 0.999698 - Dummy Var: 0.9993970909
Idade: 27 - Sample var: 0.999698 - Dummy Var: 0.9993970909
Idade: 27 - Sample var: 0.999698 - Dummy Var: 0.9993970909
What would be the correct way to implement the logic to achieve the above output?
I am available to resolve doubts about the problem.
Surely you overshadowed the name of the variables, it’s super bad to try to understand this, you could publish what each variable means?
– user178974
I added more information at the top, Edney. Thank you!
– user41210
The code I posted solved? And why do you have 2 loops? if the values are the same?
– user178974
The first loop is to "age" the person, the second loop is to calculate eligibility according to 23 different retirement entry ages for each person’s "aging" age. Still not solved the problem, but I gave +1 on account of the use of dictionaries.
– user41210
What went wrong? Why was the output identical, except for rounding errors...
– user178974
I’m still testing, that’s all. I’ll let you know in a minute what happened.
– user41210
All right, I’m sorry if I pushed you, if you don’t happen to make it anyway, put an excel with the right calculations, to get an idea of what the exact calculation is.
– user178974