LinearInterp
LinearInterp(d,r,x,i)
Uses linear interpolation to determine the value of a piecewise linear function at «x». The piecewise curve is determined from a set of points, («d»,«r») indexed by «i», which represent the vertices of the curve. (Think of «d» as the domain, or horizontal axis, and «r» as the range, or vertical axis, when thinking about these points). The array «d» must be strictly increasing.
LinearInterp finds the two points in «d» that «x» lands between -- i.e.,
d[@I=j] <= x <= d[@I=j+1]
and then interpolates between the two points (d[@I=j],r[@I=j])
and (d[@I=j+1],r[@I=j+1])
for that value of «x». You can pass an array of values in for «x» to find many points on the curve at once.
«d» and «r» may contain Null values, in which case these points are ignored, with the interpolating occurring only between non-null points. Also, when «x» is Null or when there are no non-null data points, the result is Null.
Extrapolation
When «x» lands to the left or right of all points in «d», LinearInterp must extrapolate. By default it does this by assuming the function is a constant value, equal to the leftmost or rightmost value in «r».
(new in Analytica 4.6) You can select an alternate extrapolation method by specifying a value for the optional «extrapolationMethod» parameter. The following values are possible.
- 1 = Use the «r» value for the nearest point (default).
- 2 = Disallow extrapolation, return Null.
- 3 = Use the «r» value for the nearest point during evaluation, but disallow extrapolation during optimization.
Example usage:
LinearInterp(d,r,x,i, extrapolationMethod:2 )
Piecewise-linear relationships in Optimizations
(Applies to Analytica Optimizer edition)
LinearInterp can be used inside a linear and quadratic optimization problem, know as a Linear Program (LP), Quadratic Program (QP) or Quadratically Constrained Program (QCP). When the «d» and «r» parameters do not depend on any decision variables, but «x» is a function of decision variables, the structured optimization facility (i.e., DefineOptimization) will automatically incorporate the piecewise-linear relationship into your LP, keeping the problem linear (or quadratic if already quadratic).
Since a continuous non-linear scalar function y=f(x)
can be approximated by a piecewise-linear function, this makes it possible to approximate non-linear relationships inside an LP. Structured optimization accomplishes this by introducing auxiliary (decision) variables and constraints into the optimization formulation. This happens transparently, so you don't have to figure out how to do it yourself, but since some of the variables are boolean-valued, it creates a combinatoric search space for the LP engine. This often increases search times dramatically. Hence, it may not be a panacea for solving your non-linear problem. Nonetheless, there can still be advantages to keeping your problem an LP (rather than having to resort to an NLP). LPs are always array-abstractable, and when an optimal solution is returned, you can be assured it really is the global optimum.
The example model "Vacation plan with PWL tax.ana"
, found in the Optimizer Example folder in Analytica 4.6 and later, illustrates an example of LinearInterp in an optimization problem. In the example, a graduated income tax rate is modeled in a piecewise-linear fashion using LinearInterp in the context of a linear program.
(new to Analytica 4.6) When solving an optimization problem, extrapolation adds complexity to the formulation, so if you know that «x» (at the optimum) will always be within the range of «d»'s values, it is probably advantageous to disable extrapolation. This is done by specifying the «extrapolationMethod» parameter to be either 2 or 3. When you use either of these options, the resulting formulation implicitly constraints «x» to be within the range of «d». If you are wrong about «x» being in that range, your problem might become infeasible (because of the extra constraint).
Library
Array functions
Enable comment auto-refresher