Difference between revisions of "LinearInterp"

 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
[[Category:Interpolation functions]]
 
[[Category:Interpolation functions]]
 
[[Category:Doc Status C]] <!-- For Lumina use, do not change -->
 
[[Category:Doc Status C]] <!-- For Lumina use, do not change -->
 +
[[Category: Array Library]]
  
== LinearInterp(d,r,x'',i'') ==
+
== LinearInterp(xi, yi, x'', i'') ==
 +
Given arrays of numerical coordinates «xi» and «yi», each indexed by «i», it returns the y value corresponding to «x», interpolated linearly between the two values of «yi» nearest to «x». The numbers in «xi» must be in increasing order.  «xi» may itself be a simple index of «yi», in which case you may omit parameter «i».  Otherwise, «i» must be a common index of «xi» and «yi». «x» may be a scalar or have any dimensions. 
  
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.
+
If any values of «xi» and «yi» are [[Null]], it ignores those coordinates, and interpolates between the nearest values of «xi» and «yi» with valid numbers.  
  
[[LinearInterp]] finds the two points in «d» that «x» lands between -- i.e.,
+
If «x» is less than the first value in «xi» (''x < xi[@i = 1]''), by default it returns the first value of «yi», '' yi[@i = 1]''. Similarly, if «x» is larger than the last (largest) value in «xi» (''x > xi[@i = Size(i)]''), it returns the largest value ''yi[@i = Size(i)]''. You can modify this default behavior with the optional parameter «extrapolationMethod» (details below).
:<code>d[@I=j] <= x <= d[@I=j+1]</code>
 
and then interpolates between the two points <code>(d[@I=j],r[@I=j])</code> and <code>(d[@I=j+1],r[@I=j+1])</code> 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]]. 
+
[[Image:LinearInterp-graph.png]]
  
[[Image:LinearInterp-graph.png]]
+
== Examples ==
 +
This example can be found in the [[User Guide Examples]]:
  
=== Extrapolation ===
+
:<code>Linearinterp(Index_b, Array_a, 1.5, Index_b) &rarr;</code>
 +
:{| class="wikitable"
 +
!! colspan="3" | Index_a &#9654;
 +
|-
 +
! style="width:75px;" | '''a'''
 +
! style="width:75px;" | '''b'''
 +
! style="width:75px;" | '''c'''
 +
|-
 +
| 2
 +
| -2.5
 +
| 2
 +
|}
  
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».  
+
== Optional Parameters ==
 +
=== ''i'' ===
 +
Specifies the common index of «xi» and «yi». You can omit this parameter, if «xi» is itself an index of «yi».
  
(''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.
+
=== ''extrapolationMethod'' ===
* 1 = Use the «r» value for the nearest point (default).
+
Specifies the value to return if «x» is outside the values of «xi»:
* 2 = Disallow extrapolation, return [[Null]].
+
# Use the «yi» for nearest «xi» (default method)
* 3 = Use the «r» value for the nearest point during evaluation, but disallow extrapolation while solving an LP or QP optimization.
+
# Return [[Null]].
* 4 = Extrapolate by extending the slope of the first or last segment.
+
# Use the «yi» value for the nearest point during evaluation, but disallow extrapolation while solving an LP or QP optimization.
* 5 = Extrapolate by extending the slope of the first or last segment, but disallow extrapolation while solving an LP or QP optimization.
+
# Extrapolate by extending the slope of the first or last segment.
 +
# Extrapolate by extending the slope of the first or last segment, but disallow extrapolation while solving an LP or QP optimization.
 +
Example:'
  
Example usage:
+
:<code>LinearInterp(xi, yi, x, i, extrapolationMethod: 4) &rarr;</code>  
:<code>[[LinearInterp]](d,r,x,i, extrapolationMethod:4 )</code> &rarr;
 
::[[image:LinearInterp_ExtrapolationMethod4.png]]
 
  
=== Piecewise-linear relationships in Optimizations ===
+
:[[image:LinearInterp_ExtrapolationMethod4.png]]
  
 +
== Piecewise-linear relationships in Optimization ==
 
''(Applies to Analytica Optimizer edition)''
 
''(Applies to Analytica Optimizer edition)''
  
[[LinearInterp]] can be used inside a linear and quadratic optimization problem, known 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).
+
[[LinearInterp]] can be used inside a linear and quadratic optimization problem, known as a Linear Program (LP), Quadratic Program (QP) or Quadratically Constrained Program (QCP). When parameters «xi» and «yi» do not depend on any decision variables, but «x» is a function of decision variables, [[DefineOptimization]] automatically incorporates this piecewise-linear relationship into your LP, keeping the problem linear (or quadratic if already quadratic). This often results in much faster and more reliable optimization than using a nonlinear program (NLP) solver.
  
Since a continuous non-linear scalar function <code>y=f(x)</code> 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.  
+
Since you can approximate any continuous non-linear scalar function <code>y = f(x)</code> by a piecewise-linear function, this makes it possible to approximate non-linear relationships inside an LP. The Optimizer accomplishes this automatically 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. Since some of the variables are Boolean, it creates a combinatoric search space for the LP engine (also known as a Mixed Integer Program or MIP). This may increase search times dramatically, so it may not be a panacea for solving your non-linear problem. But converting to an LP does have two important advantages: 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 <code>"Vacation plan with PWL tax.ana"</code>, 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.
 
The example model <code>"Vacation plan with PWL tax.ana"</code>, 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, 3 or 5. When you use any 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).
+
When solving an optimization problem with  a piecewise linear function, extrapolation adds complexity. So, f you know that the optimal value for «x» will always be within the range of «xi»'s values, it is a good idea to disable extrapolation. Do this  by specifying the «extrapolationMethod» parameter to be either 2, 3 or 5. These options implicitly constrain «x» to be within the range of «xi». But, if you are wrong about «x» being in that range, your problem might become infeasible (because of the extra constraint).
  
== Library ==
+
==History==
 
+
*[[Analytica 4.6]]
Array functions
+
**optional «extrapolationMethod» parameter
  
 
== See Also ==
 
== See Also ==
 
 
* [[CubicInterp]]
 
* [[CubicInterp]]
 
* [[StepInterp]]
 
* [[StepInterp]]
 
+
* [[MonoCubicInterp]]
=User Guide=
+
* [[Piecewise-Linear (PWL) Relationships]]
Returns linearly interpolated values of x, given d and r representing an arbitrary piecewise linear function. d and r must both be indexed by i, and d must be increasing along i. r is an array of the corresponding output values for the function (not necessarily increasing and might be more than one dimension). x might be probabilistic and/or an array.
+
*[[User Guide Examples]] / [[Media: Array Function Examples.ana | Array Function Examples.ana]] / Interpolation Functions Module
 
 
For each value of x, Linearinterp() finds the nearest two values from d and interpolates linearly between the corresponding values from r. By default, if x is less than the minimum value in d, it returns the first value in r. If x is greater than the maximum value in d, it returns the last value in r.
 
 
 
==Library==
 
Array
 
 
 
==Example==
 
Linearinterp(Index_b, Array_a, 1.5, Index_b) →
 
Index_a >
 
{| class="wikitable"
 
|-
 
! scope="col"| a
 
! scope="col"| b
 
! scope="col"| c
 
|-
 
| 2
 
| -2.5
 
| 2.5
 
|}
 
 
 
==Extrapolation==
 
You can set the optional '''extrapolationMethod''' parameter to alter how extrapolation is handled when '''x''' is outside the range of '''d''' values. The possible values for '''extrapolationMethod''' are
 
*1 = (default) Return '''r''' value of nearest point.
 
*2 = Disable extrapolation. Return null if outside '''d'''’s range.
 
*3 = Return r value of nearest point, but disable extrapolation when solving an LP or QP optimization problem. See the [[Analytica Optimization]] manual.
 
*4 = Extrapolate using the slope of the first or last segment.
 
*5 = Extrapolate using the slope of the first or last segment, but disable extrapolation when solving an LP or QP optimization problem. See the [[Analytica Optimization]] manual
 

Latest revision as of 22:19, 29 March 2016


LinearInterp(xi, yi, x, i)

Given arrays of numerical coordinates «xi» and «yi», each indexed by «i», it returns the y value corresponding to «x», interpolated linearly between the two values of «yi» nearest to «x». The numbers in «xi» must be in increasing order. «xi» may itself be a simple index of «yi», in which case you may omit parameter «i». Otherwise, «i» must be a common index of «xi» and «yi». «x» may be a scalar or have any dimensions.

If any values of «xi» and «yi» are Null, it ignores those coordinates, and interpolates between the nearest values of «xi» and «yi» with valid numbers.

If «x» is less than the first value in «xi» (x < xi[@i = 1]), by default it returns the first value of «yi», yi[@i = 1]. Similarly, if «x» is larger than the last (largest) value in «xi» (x > xi[@i = Size(i)]), it returns the largest value yi[@i = Size(i)]. You can modify this default behavior with the optional parameter «extrapolationMethod» (details below).

LinearInterp-graph.png

Examples

This example can be found in the User Guide Examples:

Linearinterp(Index_b, Array_a, 1.5, Index_b) →
Index_a ▶
a b c
2 -2.5 2

Optional Parameters

i

Specifies the common index of «xi» and «yi». You can omit this parameter, if «xi» is itself an index of «yi».

extrapolationMethod

Specifies the value to return if «x» is outside the values of «xi»:

  1. Use the «yi» for nearest «xi» (default method)
  2. Return Null.
  3. Use the «yi» value for the nearest point during evaluation, but disallow extrapolation while solving an LP or QP optimization.
  4. Extrapolate by extending the slope of the first or last segment.
  5. Extrapolate by extending the slope of the first or last segment, but disallow extrapolation while solving an LP or QP optimization.

Example:'

LinearInterp(xi, yi, x, i, extrapolationMethod: 4) →
LinearInterp ExtrapolationMethod4.png

Piecewise-linear relationships in Optimization

(Applies to Analytica Optimizer edition)

LinearInterp can be used inside a linear and quadratic optimization problem, known as a Linear Program (LP), Quadratic Program (QP) or Quadratically Constrained Program (QCP). When parameters «xi» and «yi» do not depend on any decision variables, but «x» is a function of decision variables, DefineOptimization automatically incorporates this piecewise-linear relationship into your LP, keeping the problem linear (or quadratic if already quadratic). This often results in much faster and more reliable optimization than using a nonlinear program (NLP) solver.

Since you can approximate any continuous non-linear scalar function y = f(x) by a piecewise-linear function, this makes it possible to approximate non-linear relationships inside an LP. The Optimizer accomplishes this automatically 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. Since some of the variables are Boolean, it creates a combinatoric search space for the LP engine (also known as a Mixed Integer Program or MIP). This may increase search times dramatically, so it may not be a panacea for solving your non-linear problem. But converting to an LP does have two important advantages: 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.

When solving an optimization problem with a piecewise linear function, extrapolation adds complexity. So, f you know that the optimal value for «x» will always be within the range of «xi»'s values, it is a good idea to disable extrapolation. Do this by specifying the «extrapolationMethod» parameter to be either 2, 3 or 5. These options implicitly constrain «x» to be within the range of «xi». But, if you are wrong about «x» being in that range, your problem might become infeasible (because of the extra constraint).

History

See Also

Comments


You are not allowed to post comments.