# Prediction Algorithm [closed]

Trying to identify what algorythm I need to use to predict a battery full time (100%), based on historic data (charge rate is variable).

I suspect it's some form of linear regression of a sort, however I don't really know. It's best if I visualize what i'm trying to do: Eventually I want to do this in code, but if I can get it working in excel, or understand the maths behind, that would be a good start.

• Plot your data.. Mar 27 at 9:29
• Welcome to the Mathematica Stack Exchange. This stack site is about the technical computing software called Mathematica and the associated Wolfram Language. This question is technically off-topic unless you want to solve it using Mathematica. As a start, you will have to edit the post to indicate that and also load your copy-paste-able data and any Mathematica code that you have tried so far. Thanks.
– Syed
Mar 27 at 9:56

Your question ended up being rather amusing to play with so I ended up with some over-the-top method.

## The data

(* the data *)
battery = {10, 20, 30, 45, 48, 50, 55, 60, 77};
time = {
TimeObject[{9, 10, 0}],
TimeObject[{9, 35, 0}],
TimeObject[{10, 30, 0}],
TimeObject[{11, 0, 0}],
TimeObject[{11, 35, 0}],
TimeObject[{12, 0, 0}],
TimeObject[{12, 30, 0}],
TimeObject[{13, 0, 0}],
TimeObject[{13, 45, 0}]
};

(* moving from times to absolute values *)
diff = Accumulate[{0}~Join~
QuantityMagnitude[
Table[time[[i + 1]] - time[[i]], {i, Length@time - 1}]]];
data = {battery, diff}\[Transpose];

(* plotting *)
ListPlot[data, Frame -> {True, True, False, False},
FrameLabel -> {"Batery charge [%]", "Time [minutes]"},
FrameStyle -> Directive[Black, 14], PlotStyle -> {Black}]


As you can see, the charging behaviour changes at around 50%, and the charging rate becomes slower. (Note that I've inverted the axes on purpose.) ## Fitting the data

The simplest is to find some asymptotic functions. I'm sure there are better ways using R-square and bla, but I'm too lazy to sit down and write that code. So i'm just using the most basic functions I can think of.

funcs = {a + b/x, a Log[ x] + b, (a x)/Sqrt[1 + x^2] + c,
a ArcTan[ x] + c};
stdFits = NonlinearModelFit[data[[-6 ;;]], #, {a, b, c}, x] & /@ funcs;
Show[{Plot[Evaluate[#[x] & /@ stdFits], {x, 0, 100},
PlotRange -> {{0, 105}, {-5, 400}}, PlotLegends -> funcs,
Epilog -> {Red,
AbsolutePointSize, (Point[{100, #}] & /@ stdFits)}],
ListPlot[data],
ListPlot[
Evaluate@(Callout[{100, #}, #, Left] & /@ stdFits)]}] ## Using (the more fun) Predict

methods = {"DecisionTree", "GradientBoostedTrees", "NearestNeighbors",
"NeuralNetwork", "RandomForest", "GaussianProcess"};
assoc = MapThread[#1 -> #2 &, {battery, diff}];
p = Predict[assoc, Method -> #, PerformanceGoal -> "Quality"] & /@
methods;

Show[{Quiet@
Plot[Evaluate[#[x] & /@ p], {x, 0, 100},
Frame -> {True, True, False, False},
FrameLabel -> {"Batery charge [%]", "Time [minutes]"},
FrameStyle -> Directive[Black, 14], PlotLegends -> methods],
ListPlot[data, PlotStyle -> {Black}, PlotMarkers -> "OpenMarkers"],
ListPlot[{Callout[{100, p[]}, p[], Left]}]}] Out of all the models, the "NeuralNetwork" method seems to give the more reasonable answer of about 308 minutes, or about 5 hours or so.

• I mean, you threw a whole bunch of tree based algorithms in there, of course they are going to do a miserable job. This seems to beg for some sort of logistic regression, maybe some other distributional regression for duration like cox proportional hazards...
– Ryan
Mar 27 at 20:22
• Of course! But I wanted to make a 'fair' comparison between all the other methods that were available to Predict. I didn't care in particular about accuracy, I was just playing a bit with the data. :) As I said in the answer, I couldn't be bothered to do it the 'clean' way. But I might revisit it when I'll have more time.
– alex
Mar 27 at 20:49