# How to calculate a time estimate and plot it?

I recently made a website for a game's community. The game basically has an online dragon which people can fight. Each fight chips off a little bit health. On the next encounter with the dragon, people will then report his health status. This is a number from $11$ down to $0$. If the dragon reaches zero, a so-called grace-period will occur where everyone can jump into the game and kill him very easily for some nice rewards.

This site has been going for a small week now and I have gathered about a thousand reports by now, so I felt its time to calculate some stats! The most requested stat is an estimation on when the grace-period will occur.

The idea I have with this is that I want plot a graph on the site, on the $y$-axis a range of $0$ to $11$, on the $x$-axis a range of time. And basically plot all the reports I have with $3$ different lines:

• The health estimation I generate based on all reports
• The health based on the highest reported value
• The health based on the lowest reported value

Doing this is not really a problem, just plot the numbers I have on the graph, where it becomes interesting is doing an estimation beyond these numbers. I could do something like calculate the drop in health between two points, and use that for the downward slope, but it'll be linear and not very accurate (as it wouldn't consider all the previous reports).

I'm therefor looking for some pointers how I can approach this? I tried googling this, but I have a very hard time finding what I need. Probably because I simply lack the knowledge of terms I have to look for. Is there some general use formula for this? How would you approach this yourself? Anything would help me at this point really!

@Moderators: Feel free to add extra tags as I really have no idea.

• Please decide which site you prefer & delete the other one. Feb 12, 2016 at 16:04
• @gung Done! I feel this place is more appropriate since it's about stats, not math in particular. Feb 12, 2016 at 16:07
• (+1) for the context of the question, it's cute Feb 16, 2016 at 18:57
• What happens when the dragon is killed? A new one pops up? Or are there multiple dragons around?
– xan
Feb 16, 2016 at 21:57
• @xan If the dragon is killed and the grace period is over, a new one spawns and everything starts over. Each "generation" is stronger than before. SO right now killing it takes a day or 2, a few hundred generations later, it could take over a week. It is believed there are actually 4 dragons at the same time, and they sync up averaged health every 30 minutes until one dies (taking the others with him). Feb 17, 2016 at 11:57

The task you are attempting is called extrapolation and is notoriously dangerous compared to interpolation between the points. I would suggest three approaches:

1. Linear extrapolation using the last two points. As you point out, this is of limited use as you go much further than the spacing between points.

$$y = y_{n-1} + (y_n - y_{n-1}) \frac{x - x_{n-1}}{ x_n - x_{n-1}}$$

2. Go to higher order with quadratic and cubic using the last three or four points. This will capture some of the higher order variation at the end of the curve. Caution though, higher order interpolation can become quite unstable.

3. To do better, you need a model for your data - some idea of the shape of the overall trend. This combined with the least squares method of interpolation that may be used to predict beyond your data.

I'd suggest trying #3. You don't tell us about the nature of the data but assuming the dragon's health is primarily decreasing, you could fit it do an exponential function with a polynomial argument, such as

$$\exp( - |p(t)| )$$ where |p(t)| is the absolute value of a low order (<4) polynomial. This function will tends to decrease but can capture some variation. Alternatively you can fit the $\log$ of your data to a polynomial and plot the exponential of the fit. Either approach assures health stays positive in the fitted function but can still decrease. Then you can estimate the dragon's demise at time $t_{dead}$ when the fitted function drops below the minimum health unit (eg 1).

The example data from Lennard's comment are fairly linear (correlation of 0.99) so I'm just using a low order polynomial. The actual data may be more fine grain and it sounds like the concern is the detailed behavior at the end of the distribution. If this is the case a a global fit for plotting with a local fit over the last few hours to determine the zero crossing might be the most accurate. For the data provided:

time=[0,8,16,24,36,38]
health=[11,9,6,4,1,0]


I found a simple quadratic fit worked best (adding a cubic term actually prevented a zero crossing). Making the fit excluding the last data point gives a predicted death at 40 hrs vs 38 in the actual data. As noted above, more samples with a fit over the last hour using a low order polynomial would likely improve this.

The code in R is:

require(stats)
require(graphics)
a=1
b=1
c=10
d=1
thefit=nls(health ~ (a*time*time+b*time+c),start=list(a=a,b=b,c=c))
plot(time,health)
new = data.frame(xdata = time)
p=coef(thefit)
p

a            b            c
0.001173024 -0.325246497 11.149887563 Obviously fairly linear.

For PHP, google turns up a polynomial regression class that may be useful for the question.

• Thank you for this! I'm unfortunately unfamiliar with most of these notations, care to have a small example if I present some data? eg. can a trend change, or is it an averaged number, or can it be an exponential changing value? Basically, the dragon starts at 11, goes to 9 in 8 hours, another 8 hours it goes to 6, another 8 hours later its at 4, and as it goes closer to 0, the more people start helping the faster it goes. Feb 12, 2016 at 19:31
• I stumbled across xuru.org/rt/TOC.asp which applies the least squares method in multiple ways. Which of these would be most fitting and why? I initially chose "exponential", but it looks like polynomial also looks okay, although I have no idea what that actually is :P Feb 12, 2016 at 19:57
• In response to Fonteijn's comments : you can point me to some example data if you like. what language are you using ? The trend will change as more points are added. We can try a few fits to see what works best. Least squares approximately fits the points rather than going through then like interpolation. imaging rubber bands between the points and the fit that stretch it to approximate the curve. Feb 13, 2016 at 23:11
• I'm doing this in PHP, but you can present it to me in whatever is comfortable to you. When I look at old stats:The dragon starts at 11, goes to 9 in 8 hours, another 8 hours it goes to 6, another 8 hours later its at 4. Then in like 12 hours it goes to 1 and then things become crazy as people start reporting pretty consistently and it's usually dead within a hour or 2. So pretty irregular curve speed-wise. Feb 14, 2016 at 23:08
• Awesome, I'm gonna played with this, thank you very much. I'll report back soon! Feb 17, 2016 at 11:55

Based on your comment about the respawning dragons and lengthening lifespans, one thing to model would be the lifespan itself rather than the interim health values. If you look at lifespan as a function of generation number or start time, you may find a model that fits. You've observed the lifespans getting longer, and modeling can help you assess whether increase is linear, quadratic or something else.

For within-lifespan predictions, the best case would be if all lifespans followed a similar curve, at least loosely. Then you can normalize things by looking at percent of lifespan passed as a function of current health. For past dragon lives, you can retroactively determine the percent of lifespan passed at each point. For the current lifespan, that's what you want to predict. Based on your other comment, the curve would not have a nice formula, but if they do follow similar patterns even something like a spline fit could be useful in making the prediction.