# calculate missing values, Interpolation

I've following data:

gasprices={{{"Year", "EUR05/GJ"}, {2000., 14.4041}, {2005., 22.8756}, {2010.,
29.1499}, {2015., 29.4374}, {2020., 30.3778}, {2025.,
33.2288}, {2030., 35.099}, {2040., 36.8245}, {2050., 38.2697}}}


with ListLinePlot, I get a nice graphic.

How can I calculate the missing values like for the year 2001, 2002, 2003, 2004?

I'd like to calculate the interim values.

Any input would help me a lot!!

Many thanks!

You can just use the Interpolation Function:

gasprices={{{"Year", "EUR05/GJ"}, {2000., 14.4041}, {2005., 22.8756}, {2010., 29.1499}, {2015., 29.4374}, {2020., 30.3778}, {2025., 33.2288}, {2030., 35.099}, {2040., 36.8245}, {2050., 38.2697}}};
iFunct = Interpolation[gasprices[[1, 2 ;; All]]];


This creates a function iFunct[]. You can get the interpolated values like this:

Map[iFunct, {2001, 2002, 2003, 2004}]
(* {16.0923, 17.8138, 19.5384, 21.2358} *)


## Interpolation and Approximation

If you would like to have a functional model approximating instead of interpolating your data, you can use (besides others) Fit:

data=gasprices[[1,2;;]]/.{y_,v_}->{Round@y,v};
approx=Fit[data,{1,x,x^2,x^3},x]; (* simple cubic fit *)
interp=Interpolation[data];       (* cubic interpolation *)

With[{min=Min@data[[All,1]],max=Max@data[[All,1]]},
Plot[{approx,interp[x]},{x,min,max},
Epilog:>{{Dashed,Gray,Line[{{#[[1]],0},#}]&/@data},
{PointSize[Large],Red,Point@data}},
AxesLabel->gasprices[[1,1]],Ticks->{data[[All,1]],Automatic},
AxesOrigin->{min,0},
PlotLegends->Placed[{"Approximation","Interpolation"},Below]]]


Comparison of both methods:

## Discussion

It might be worthwile looking at the extrapolation behavior, too, so the following graph comes from the same data, but min set to the intersection of the approximating function with the x-axis, and max extended 10 years farther into the future than your data tries to predict:

In essence, both approaches are more or less questionable in their extrapolation behavior, so: Without a good functional model to fit for, extrapolation might be nonsensical in many cases. In any way, Mathematica's extrapolation warning message (issued for Interpolation objects when leaving the original domain) should raise your suspicions.

Depending on the data at hand, interpolation can lead to oscillating behavior even between the control points supplied by your data, so I would strongly advise to never use Interpolation without checking the actual behavior!

Hopefully, my utterings were of some assistance to your quest.

• Note that rather than mapping Point, i.e., Point/@data you can use Point@data or equivalently Point[data]. These are faster. Although for a small number of points such as in this example the difference is minimal. – Bob Hanlon Jan 27 '15 at 17:18
• @BobHanlon: Good hint. I will change my code accordingly! – Jinxed Jan 27 '15 at 17:21

## TimeSeriesResample

priceseries=gasprices[[1,2;;]];
priceseries[[All,1]] = Rationalize /@ priceseries[[All, 1]];
psinterpolated = TimeSeriesResample[priceseries, 1,
ResamplingMethod -> {"Interpolation", InterpolationOrder -> 2}];

gp = priceseries /. {a_, b_}:> {{a}, b};
ts = psinterpolated /. {a_, b_}:> {{a}, b};
DateListPlot[{gp, ts, ts}, Joined -> {False, False, True},
PlotStyle->{Directive[PointSize[.03], Opacity[.5], Blue], Red, Green},
Frame -> True, ImageSize -> 500,
PlotLegends->{"original", "interpolated", "interpolated - joined"},
FrameTicks -> {{Automatic, Automatic}, {gp[[All,1]], Automatic}},
GridLines -> {gp[[All, 1]], None}]


# Machine Learning

some vanilla classics:

• Neural Network
• Decision Tree
• Gaussian Process
• Nearest Neighbors

Borrowing the formatting from @kglr 's answer above:

gasprices = {{{"Year", "EUR05/GJ"}, {2000., 14.4041}, {2005.,
22.8756}, {2010., 29.1499}, {2015., 29.4374}, {2020.,
30.3778}, {2025., 33.2288}, {2030., 35.099}, {2040.,
36.8245}, {2050., 38.2697}}};
priceseries = gasprices[[1, 2 ;;]];
priceseries[[All, 1]] = Rationalize /@ priceseries[[All, 1]];
gp = priceseries /. {a_, b_} :> {{a}, b};

trainingData = Rule @@@ priceseries;
neuralNet = Predict[trainingData, Method -> "NeuralNetwork"];
decisionTree = Predict[trainingData, Method -> "DecisionTree"];
gaussianProcess = Predict[trainingData, Method -> "GaussianProcess"];
nearestNeighbors = Predict[trainingData, Method -> "NearestNeighbors"];

nNet = Table[{{year}, neuralNet[year]}, {year, 2000, 2050, 1}];
dTree = Table[{{year}, decisionTree[year]}, {year, 2000, 2050, 1}];
gProcess = Table[{{year}, gaussianProcess[year]}, {year, 2000, 2050, 1}];
nNeighbors = Table[{{year}, nearestNeighbors[year]}, {year, 2000, 2050, 1}];

DateListPlot[{gp, nNet, dTree, gProcess, nNeighbors},
Joined -> {False, True, True, True, True},
PlotStyle -> {Directive[PointSize[.03], Opacity[.5], Magenta],
Green, Blue, Red, Black}, Frame -> True, ImageSize -> 500,
PlotLegends -> {"original", "NeuralNetwork", "DecisionTree",
"GaussianProcess", "NearestNeighbors"},
FrameTicks -> {{Automatic, Automatic}, {gp[[All, 1]], Automatic}},
GridLines -> {gp[[All, 1]], None}]