# Thread: Calculate risk of death in a computer simulated world

1. ## Calculate risk of death in a computer simulated world

Hi all

This is my first post here, so hello. I have a very difficult math problem, that I hope someone can help with. I am a programmer and I am interested in evolution, so I decided to create a computer simulated world. It is a very simple world with only a few functions at the moment. You can be born, live and then die (I have commented out the pair bonding and reproduction out until I can fix my problem).

The problem I am having is calculating when people should die. Basically in the program, there is a loop that runs and each iteration of the loop represents a year for the people. In the loop I have a function that calculates weather each person should die on that die. The calculation goes like this. It gets a random number between 0 and 100 and if the random number is less that a risk factor (I explain the risk next) the person dies. The risk factor is the problem.

I want the average age (in years) for my people to be lets use 4 for now. So over a few hundred people, the average age for the people that have died should be 4. So if one person dies at age 3 and anothe dies at age 5, then the average age is 4.

I have tried a few different ways of calculating the risk. Some have been my own thoughts and some are math functions that exist, like binomeal distribution, negative binomeal distribution, logistics functions etc..

So my question is, how do I calculate the risk on a daily basis, so that the average age for my people is a specific number (lets say 4 for now)? Obviously the equation/s needs to take into account the current year and the average that I am aiming for.

For anyone willing to help, please bear in mind that I have been out of school for a while, so my math knowledge is pretty basic at the moment. Also my world isnt going to represent the real world 100%, so I'm not concerned about real life death tables and spikes in mortality rates at birth etc.. Also risk of death should never be 100% or more, so the risk should get close to 100% but never actually get there. If I am not mistaken, this would mean that the risk curve (risk and days on a graph) cant be linear, it would be some kind of curve, probably something close to the top left corner of a circle.

2. While simulations are something I want to get into in the future, I don't think I'm knowledgeable enough on survival analysis to answer your question. With that said, what you're looking at sounds to me to be a survival analysis. Each person is born with, say, an expected useful life. If a population averages a life of 80 years 99% of the time, then we should expect to see strong clustering around 80 for their deaths. What you're trying to do, however, is to analyze this on a day-by-day or year-by-year basis. This is where I'm foggy. How much can we say a person's yearly life contributes to their meeting their expected death rate of the population to which they belong? I wouldn't think it is a forgetful process such that each year is independent of the previous. It would then be a continuous conditional probability, such that we want to know

$Pr("X\ dies\ in\ year\ Y"\ |\ "X\ has\ lived\ Z\ years")$

This probability should increase each year toward a high probability around that expected death rate. I'm sure if you do some research into survival analysis, you'll find the sort of functions that can help you process this. In particular, check out the hazard function.

3. Hey buddy thanks for the reply
Much appreciated!
To answer your questions, for my particular problem, dont get caught up in the survival analysis.
I will add survival analysis later on to the simulation, but for now its just the maths that I'm worried about.
I just need to be able to run the simulation, and over a period of time the average age of the people must be X.
The reason for this is, at a later stage I'm going to add, resources and strategy into the simulation and I will see how that effects the average age of the people
I do agree that it would be continuous conditional probability.
Thanks for the page on hazard function.
I am going through it now, and at a breif glance it looks like the right sort of thing that I'm looking for.
Unfortunately I have been out of school for a while, so my knowledge of maths symbols is a little limited at the moment so it takes a while to decipher what they are talking about, lol.

4. Hello.

You're describing a classic problem in actuarial science, which insurance companies and pension funds have been working on for several hundred years now. They have't quite got the answer yet as important factors (such as the rate of improvement in medical care) continue to be unpredictable.

The governments of most major countries produce "life tables" which have the probabilities you are looking for. These generally contain two sets of mortality rates:

1) $q_x$ which give the probability on dying between your x and x+1 birthday). You can use this to do an annual projection. You could also assume that the daily rate of mortality was $\frac{q_x}{365}$ and do a daily projection.

2) $\mu_x$ which gives the instantaneous rate of death at age x, expressed as an annual rate. This can be used to calculate the chance of dying in any interval (eg, 1 day) but this is fairly complicated procedure so i wont explain it unless you ask me to. It turns out that $\mu_x$ is the hazard function mentioned by the last poster.

When using standard mortality tables be careful that:
- You understand the population that the table covers, which may not be similar to the one you are simulating. it is common to adjust the table mrotality rates by a flat (or variable) percentage to allow for this.

- You understand what allowance (if any) is made for future medical improvement in the table, and adjust as required.

As a technical point you should be aware like any statistical process, the tables are not exact and indivudals will have random experience that differs from the table.

If you dont want to load a table into your program there are some agebraic functions which will roughly or exactly reproduce the table, but again its probably a lot easier to load the table than to do this proceedure.

The latest UK tables i can find are here:
http://www.statistics.gov.uk/StatBas...asp?vlnk=14459

5. You still should look up "Gompertz" and "Makeham" and ponder on "Force of Mortality" and maybe come to terms with a more rational design.

Your biggest problem is the Integer nature of death compared to the Real Number nature of the arithmetic. Unless you have a very, very large population, and it stays very, very large, you will have to decide if fractional people can die.

Note: You also should not post on more than one website. This is conisdered poor taste, at best. You are taxing the limited resources of volunteers. Don't do that. Volunteers are valuable. No wating of their time is allowed.

6. Hi everybody

And thanks for the help.

@ SpringFan25
Thanks for the suggestions. I have looked at life tables before, but thats not what I going for at the moment. Also what I'm trying to do at the moment does not take into account medical care ect. The aim at the moment is to make sure the average for for the people comes out at what I set it too. Once that's done, I'll stick in things like medical care to see what that does to the average age.

@ TKHunny
Thanks for suggesting "Gompertz". That is definately on the right track! I used Excel to do some tests and got some good results (by adjusting the values manually, basically rigging it to work). Unfortunately because there are 2 input values (b and c) I dont know how to get it to work in my problem. I can use the average I want to get as one of the variables, but then what about the other variable... To answer your question fractional people cant die. The way I have coded it, no fractional people will die. Basically once I have the correct risk (ie the problem at hand), Ill get a random number between 0 and 100, and if the random number is less than the risk, the person dies. So a fraction of a person cant die, you are either dead or alive (after doing that I read somewhere, and I think thats called Bernoulli trails). Sorry about posting on multiple websites. I didnt realise that you volunteered on both. I just wanted to get a little more exposure because this problem has turned out to be a little more difficult than I though Actually

Thanks for all the links that everyone sent. They lead me onto to a few other interesting things, like the bathtub curve. I dont know if its the way I'm applying it, but I dont seem to get the right answers

Thanks for the help so far.
Has anyone got any other suggestions?

7. If the chance of death each day is p, then the average age at death will be 1/p days. This is an example of the geometric distribution.

Maybe that's all you need. In the Real World it's much more complicated.

8. Originally Posted by awkward
If the chance of death each day is p, then the average age at death will be 1/p days. This is an example of the geometric distribution.

Maybe that's all you need. In the Real World it's much more complicated.
But doesn't that assume each day is independent of every yesterday? Shouldn't there be some conditioning on how long someone's been alive? This is why I suggested survival analysis. Isn't that what the hazard model aims to model?

9. You are not understanding. Telling me your design is not going to help, since I already see what it is you are doing. This is why I have suggested that your design is not particularly rational. If you want the arithmetic to work consistently, you WILL HAVE to come to terms with partial deaths. It's not an option that you can just code out.

The other option is to maintain a very large population. With a life expectancy of four years, I'm guessing that will be a challenge. 100 lives is several orders of magnitude smaller than the population size I'm talking about. With only 100 lives, how many will die when your probability of death is 10^(-6)? 10^(-5)?

10. Originally Posted by bryangoodrich
But doesn't that assume each day is independent of every yesterday? Shouldn't there be some conditioning on how long someone's been alive? This is why I suggested survival analysis. Isn't that what the hazard model aims to model?
Yes, that assumes each day is independent of every yesterday. That seems to be the case in this particular model, based on my reading of the original post, although it is possible that I misunderstood it. Maybe polz99 will tell us if that is his intent or not. Computer models are not always realistic. Whether this is a problem or not depends on their use.

As I said, in the Real World it's more complicated.

11. @awkward
Thanks for the help bro. You seem to be on the same track as me I tried your suggestion of geometric distribution. I used the forumla (1 - p)^(k - 1) * p and (1 - p)^k * p, where k = the year and p was the average I wanted to acheive divided by 10. The curve increases too quickly (in other words the risk increases too quickly) and so the average for the people was too low. You are right though, the real world is lot more complicated, as this currently does not take into account medical research, diseases or other issues in the real world.

@bryangoodrich
I agree with you, there should be some conditioning on how long someone's been alive. Years are not totally independant of each other. In other words the risk should increase from year to year, but never be equal to 100%.

@ TKHunny
I don't understand why you say my design is not particular rational? I have a feeling I know what you are saying, but please explain that to me. As far as the large population goes. The population does not all have to be alive at the same time. Lets say there are 100 people alive to start with. They live for an average of 4 years, so on average there are 25 generations in 100 years. Lets also assume that out of the 100 people, there are 50 pair bonds and each pair bond has 2 children, so the population in each generation never changes. So over our 100 generations there would have been 10000 (100 per generation * 100 generations) people, which takes a matter of seconds to simulate on a computer. I have run simulations with other equations (wrong equations) with a population of much smaller than that, and got consistant answers (consistant wrong answers). The point I'm trying to make here is you dont need a rediculously huge population so that fractional prople dont have to die. Also as far as your last point goes, in this model where we are using 4 as an average age, why would the risk ever be as low as 10^(-6)? If you only have 4 years to live there is no way the risk in any year would ever be that low. Even if the average age for people to live was 100 years, the risk of death in any given year will never be that low.

12. Just consider my design hints as you try to refine your model. When things jump around too much, come back and read it again. If you can develop a model that fits your needs, maybe it won't be much of a problem. On the other hand...

13. Hi all

Ok, so good times here.
I have come up with a semi solution...
Semi, well it gets the right answers, but it kinda a bit of a cop out (you'll see why).
Basically I again used my average of 4.
Lets say the population starts off with 7 (4 * 2 - 1 = 7) people.
I then assumed that 1 person dies in each year.
The result is then that the average age is 4 (what I wanted).
The risk (or probability) per year can then be calculated thusly.
In year 1 there were 7 people and 1 died, so risk in year 1 = 1 / 7 = 0.142857143
So then the risk for any year can be calculated as
r = 1 / (a * 2 - y)
Where
r = risk
a = average
y = year
So if you wanted to get an average age of 5 and it was the 4th year
r = 1 / (5 * 2 - 4) = 0.166666666666666666666666666666666666667 or 16.67%
The flaw with this, is that by year "average age * 2 - 1", the risk will be 100%, so no "freaks" will live past that age.
Its not a perfect solution, but a solution none the less, a semi solution.
I guess other than the fact that at a point you have 100% certainty of dying and that fact that the same number of people will die on each day, its pretty decent.

Thanks to everyone who helped me
I'm still open to other ideas or ways of modifying what I have just done to make it better

Have a good evening