2
$\begingroup$

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!

$\endgroup$
4
$\begingroup$

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} *)
$\endgroup$
6
$\begingroup$

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:

comparison of cubic approximation and interpolation

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:

extrapolation behavior for actual data

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.

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Bob Hanlon Jan 27 '15 at 17:18
  • $\begingroup$ @BobHanlon: Good hint. I will change my code accordingly! $\endgroup$ – Jinxed Jan 27 '15 at 17:21
0
$\begingroup$

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}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.