Ignore:
Timestamp:
Dec 13, 2008, 1:23:39 AM (13 years ago)
Author:
Jari Häkkinen
Message:

Addresses #466 and #425. Added cubic spline interpolation (not complete interface to GSL interpolation stuff).

File:
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/yat/regression/CSpline.cc

    r1637 r1643  
    22
    33/*
    4   Copyright (C) 2004, 2005, 2006, 2007 Jari Häkkinen, Peter Johansson
    5   Copyright (C) 2008 Peter Johansson
     4  Copyright (C) 2008 Jari Häkkinen
    65
    76  This file is part of the yat library, http://dev.thep.lu.se/yat
     
    2120*/
    2221
    23 #include "Linear.h"
    24 #include "yat/statistics/AveragerPair.h"
    25 #include "yat/utility/VectorBase.h"
     22#include "CSpline.h"
     23
     24#include <gsl/gsl_interp.h>
    2625
    2726namespace theplu {
     
    2928namespace regression {
    3029
    31   Linear::Linear(void)
    32     : OneDimensional(), alpha_(0), alpha_var_(0), beta_(0), beta_var_(0)
     30
     31  CSpline::CSpline(const utility::VectorConstView& x,
     32                   const utility::VectorConstView& y)
     33    : GSLInterpolation(gsl_interp_cspline,x,y)
    3334  {
    3435  }
    3536
    36   Linear::~Linear(void)
     37
     38  CSpline::~CSpline(void)
    3739  {
    3840  }
    3941
    40   double Linear::alpha(void) const
    41   {
    42     return alpha_;
    43   }
    44 
    45   double Linear::alpha_var(void) const
    46   {
    47     return alpha_var_;
    48   }
    49 
    50   double Linear::beta(void) const
    51   {
    52     return beta_;
    53   }
    54 
    55   double Linear::beta_var(void) const
    56   {
    57     return beta_var_;
    58   }
    59 
    60   void Linear::fit(const utility::VectorBase& x, const utility::VectorBase& y)
    61   {
    62     ap_.reset();
    63     for (size_t i=0; i<x.size(); i++)
    64       ap_.add(x(i),y(i));
    65 
    66     alpha_ = ap_.y_averager().mean();
    67     beta_ = ap_.sum_xy_centered() / ap_.x_averager().sum_xx_centered();
    68 
    69     // calculating deviation between data and model
    70     chisq_ = (ap_.y_averager().sum_xx_centered() - ap_.sum_xy_centered()*
    71               ap_.sum_xy_centered()/ap_.x_averager().sum_xx_centered() );
    72     alpha_var_ = s2() / x.size();
    73     beta_var_ = s2() / ap_.x_averager().sum_xx_centered();
    74   }
    75 
    76   double Linear::predict(const double x) const
    77   {
    78     return alpha_ + beta_ * (x - ap_.x_averager().mean());
    79   }
    80 
    81   double Linear::s2(void) const
    82   {
    83     return chisq()/(ap_.n()-2);
    84   }
    85 
    86   double Linear::standard_error2(const double x) const
    87   {
    88     return alpha_var_+beta_var_*(x-ap_.x_averager().mean())*
    89       (x-ap_.x_averager().mean());
    90   }
    9142
    9243}}} // of namespaces regression, yat, and theplu
Note: See TracChangeset for help on using the changeset viewer.