[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Automatic polynomial approximation

From: heberf
Subject: Re: Automatic polynomial approximation
Date: Fri, 16 Oct 1998 11:53:52 -0500 (CDT)

This is not exactly what you want but here is a function which fits a curve to 
data which is non-parametric, meaning that no functional form is imposed.  The 
procedure is called kernel regression and the function is closely related to 
kernel.m which I submitted to the sources list several years ago.  Kernel 
regression is simpler than splines and better than polynomial fitting.  In fact 
it can be shown that kernel regression is equivalent to locally fitting a 
quadratic function to your data.

A good book on non-parametric regression in general and kernels in particular is

  author =       {Hardle, Wolfgang},
  title =        {Applied nonparametric regression},
  publisher =    {Cambridge University Press},
  year =         {1990},
  OPTaddress =   {New York},

function [xx, yy] = kreg(x,y,n,c)

usage [xx, yy] = kreg(x,y,n,c)

# Curve fitting via kernel regression. This version uses a Gaussian kernel
# and a bandwidth equal to 
#      h(m) = c*std(x)*m^(.2)
# where std(x) is the standard deviation of the data and m is the number of
# observations in x. 
# Given a third argument, use that as the number of evenly spaced points
# where the density will be evaluated.  Default is 40.
# The fourth argument,c, is a smoothing parameter.  Larger values of c lead to 
#  smoother curves.  The default value of c is one.
# With no output arguments the curve is plotted.  Given two output
# arguments the values xx and yy are returned such that plot(xx,yy) will
# plot the curve.  

# author: Heber Farnsworth 
# Dept of Finance,
# Olin School of Business
# Washington University in St. Louis
# address@hidden  

  if (nargin < 1 || nargin > 4) 
    usage ("[xx, yy] = kreg (x, y, n, c)");

  if nargin < 4
    c = 1;
  elseif c <=0 
    error ("kreg: second argument must be positive");
  if nargin < 3 
    n = 40;
  elseif n <= 2
    error ("kreg: number of points must be greater than 2");
 if nargin < 2 
   error ("kreg: x and y data vectors required");
 elseif (is_vector (x) && is_vector (y))
    max_val = max (x);
    min_val = min (x);
    m = max(size(x));
    my = max(size(y));
    if m != my 
      error ("kreg: data vectors must have same length")
    if m > size(x,1)
      x = x';
    if my > size(y,1)
      y = y';
    error ("kreg: first two arguments must be vectors");

  s = std(x);
  h = c*s/(m^(.2));
  z = linspace(min_val,max_val,n);
  if size(z,1) > 1
    z = z';
  for i = 1:n
    u = (z(i) - x)./h;
    K = (1/sqrt(2*pi))*exp(-.5*u.^2);
    f(i) = sum(y.*K)/sum(K);

  if (nargout == 2)
    yy = f;
    xx = z;


reply via email to

[Prev in Thread] Current Thread [Next in Thread]