Advertisement
Guest User

linear loess in C++

a guest
May 5th, 2010
659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <iterator>
  3. #include <vector>
  4. #include <cmath>
  5. #include <gsl_fit.h>
  6. #include "NumericUtils.h"
  7.  
  8. double tricube(double u, double t)
  9. {
  10.     // In our case, u represents a distance and hence should be strictly positive.
  11.     if (u < 0) {
  12.         std::cout << "In tricube(u, t), u < 0" << std::endl;
  13.         std::abort();
  14.     }
  15.  
  16.     // 0 <= u < t
  17.     if ( (closeAtTolerance(u, 0.0) || 0.0 < u) && (u < t) ) {
  18.         // (1 - (u/t)^3)^3
  19.         return pow( ( 1.0 - pow(u/t, 3)), 3 );
  20.     }
  21.     // u >= t
  22.     else {
  23.         return 0.0;
  24.     }
  25. }
  26.  
  27. double getDistance(double x, double xi)
  28. {
  29.     return fabs(x - xi);
  30. }
  31.  
  32. int main (int argc, char* argv[])
  33. {
  34.     // Loess algorithm parameters.
  35.     double alpha = 0.33;
  36.  
  37.     // Create test datasets.
  38.     const unsigned n = 21;
  39.  
  40.     double xValues[n] = { 0.5578196, 2.0217271, 2.5773252, 3.4140288, 4.3014084, 4.7448394, 5.1073781,
  41.                          6.5411662, 6.7216176, 7.2600583, 8.1335874, 9.1224379, 11.9296663, 12.3797674,
  42.                          13.2728619, 14.2767453, 15.3731026, 15.6476637, 18.5605355, 18.5866354, 18.7572812 };
  43.  
  44.     double yValues[n] = { 18.63654, 103.49646, 150.35391, 190.51031, 208.70115, 213.71135, 228.49353,
  45.                          233.55387, 234.55054, 223.89225, 227.68339, 223.91982, 168.01999, 164.95750,
  46.                          152.61107, 160.78742, 168.55567, 152.42658, 221.70702, 222.69040, 243.18828 };
  47.  
  48.     // Loess neighborhood parameter.
  49.     const unsigned q = floor(n * alpha);
  50.  
  51.     // Working arrays. Yes we need both distances and sortedDistances.
  52.     double distances[n];
  53.     double sortedDistances[n];
  54.     double weights[n];
  55.     double ySmooth[n];
  56.     double c0, c1, cov00, cov01, cov11, chisq, y, yErr;
  57.  
  58.     for (unsigned i = 0; i < n; ++i) {
  59.         // Compute distances.
  60.         for (unsigned j = 0; j < n; ++j) {
  61.             distances[j] = getDistance(xValues[i], xValues[j]);
  62.             sortedDistances[j] = distances[j];
  63.         }
  64.  
  65.         // Sort distances in order from smallest to largest.
  66.         std::sort(sortedDistances, sortedDistances + n);
  67.  
  68.         // Compute weights.
  69.         for (unsigned j = 0; j < n; ++j) {
  70.             weights[j] = tricube(distances[j], sortedDistances[q]);
  71.         }
  72.  
  73.         // Compute the best-fit linear regression coefficients (c0, c1) of the model Y = c_0 + c_1 X for the weighted
  74.         // dataset (x, y).
  75.         gsl_fit_wlinear(xValues, 1, weights, 1, yValues, 1, n, &c0, &c1, &cov00, &cov01, &cov11, &chisq);
  76.  
  77.         // Compute the fitted function y and its standard deviation yErr for the model Y = c_0 + c_1 X at the point x.
  78.         gsl_fit_linear_est(xValues[i], c0, c1, cov00, cov01, cov11, &y, &yErr);
  79.  
  80.         ySmooth[i] = y;
  81.     }
  82.  
  83.     // Compare to: http://www.itl.nist.gov/div898/handbook/pmd/section1/dep/dep144.htm
  84.     std::cout.setf(std::ios::fixed, std::ios::floatfield);    
  85.     for (unsigned i = 0; i < n; ++i) {
  86.         std::cout << xValues[i] << " "
  87.                   << yValues[i] << " "
  88.                   << ySmooth[i]
  89.                   << std::endl;
  90.     }
  91.  
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement