Sample 25001: Compute approximate first and second derivatives for (x,y) data
Compute approximate first and second derivatives for (x,y) data
- PURPOSE:
-
This sample program illustrates how to use PROC EXPAND to compute
approximate first and second derivatives for paired (x,y) data.
More specifically, this sample program does the following:
- Generates data (X,Y) according to a high degree polynomial.
- Plots Y vs X.
- Uses PROC EXPAND to fit a cubic spline to this data. See
Conversion Methods: The Spline Method in the EXPAND chapter of the SAS/ETS User's Guide.
- Outputs the derivatives of the spline at each of the x values.
These values are stored in the variable DERIV1.
- Graphs Y and DERIV1 vs X.
- Uses PROC EXPAND to fit a cubic spline to the values of DERIV1.
- Outputs the derivatives of the spline at each X value. These
values are stored in the variable called DERIV2.
- Graphs Y, DERIV1 and DERIV2 vs. X.
- REQUIREMENTS:
-
SAS/ETS Software, Version 6 or later.
- USAGE:
-
Note that these splines work well when there are many (x,y) values
so that the curve is fairly well-defined. You can vary the
BY 0.01
value in the first DATA step to see how well (or how poorly) this
method works when there are more (or fewer) x values in the input
data set for this curve.
- DETAILS:
-
The approximate first derivatives are obtained by computing the
exact first derivative of a cubic smoothing spline which is fit to
the (x,y) data.
Under certain conditions, as in this example, a cubic spline as
fit by PROC EXPAND offers a good approximation of the underlying
function. In this case, the exact derivative values computed from
the spline are close approximations to the actual derivative values
of the underlying function.
Likewise, it's true that if the spline poorly approximates the
underlying function, then the exact derivatives of the spline will
poorly approximate the actual function derivatives. So the quality
of the derivative approximation depends on how closely the cubic
smoothing spline approximates the underlying function.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
See the
Results tab for the results of running the following SAS code.
**** Generate some data ;
data one;
do x=-2 to 2 by 0.01;
y=(x-2)*(x-1.5)*(x-1)*(x-.5)*x*(x+.5)*(x+1)*(x+1.5)*(x+2);
output;
end;
run;
**** Plot Y vs X ;
proc gplot data=one;
title 'Original Series';
plot y*x;
run;
proc sort data=one;
by x;
run;
**** Compute the first derivative of the fitted spline ;
proc expand data=one outest=two out=three ;
convert y=slope/observed=(beginning,derivative);
id x;
run;
**** Clip the slope when abs(slope)>20 so that the graph appears clearly ;
data four;
set three;
deriv1=slope;
if slope >20 then deriv1=20;
if slope <-20 then deriv1=-20;
run;
**** Create the graph of Y vs X and DERIV1 vs X ;
proc gplot data=four;
title 'Series and First Derivative';
title2 'DERIV1 is clipped at -20/20';
plot (y deriv1)*x/overlay;
run;
**** Compute the second derivative ;
proc expand data=four out=five;
convert slope=deriv/observed=(beginning,derivative);
id x;
run;
**** Clip the values of the second derivative ;
data six;
set five;
deriv2=deriv;
if deriv >25 then deriv2=25;
if deriv <-25 then deriv2=-25;
run;
proc gplot data=six(firstobs=2);
title 'Series, First Derivative and Second Derivative';
title2 'DERIV1 is clipped at -20/20';
title3 'DERIV2 is clipped at -25/25';
plot (y deriv1 deriv2)*x / overlay;
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
- RESULTS:
-
The example code produces the following graphs:
This sample program illustrates how to use PROC EXPAND to compute approximate first and second derivatives for paired (x,y) data.
Type: | Sample |
Topic: | SAS Reference ==> Procedures ==> EXPAND Analytics ==> Missing Value Imputation Analytics ==> Time Series Analysis Analytics ==> Transformations Analytics ==> Financial Data Access and Manipulation
|
Date Modified: | 2022-01-07 14:05:33 |
Date Created: | 2005-01-13 15:03:15 |
Operating System and Release Information
SAS System | SAS/ETS | All | n/a | n/a |