liboctaveで主成分分析 -サンプルデータ生成-

PCA (主成分分析) にかけるデータの生成プログラム. sample.dat に出力データを保存してあるので,これを使えばPCAのプログラムが実行できます(よって以下は興味がある人だけ).
3次元空間上の平面データを作って, x,y,z それぞれにガウスノイズを加えてデータを生成するプログラムです. gnuplot とかで表示するとボリュームがあるように見えます.
コンパイルには liboctave 以外に, boost が必要です.

#include <octave/config.h>
#include <octave/Matrix.h>
#include <iostream>
#include <boost/random.hpp>

namespace loco_rabbits
{

template< class T >
inline T _square( const T &val )
{
  return val*val;
}

class TAddGaussianNoise
{
private:
  boost::variate_generator< boost::mt19937, boost::normal_distribution<> >  ndrand;
public:
  TAddGaussianNoise(const double &std_dev=1.0)
    : ndrand (boost::mt19937(static_cast<unsigned long>(std::time(0))),
              boost::normal_distribution<>(0.0, _square(std_dev)))  {};
  void operator() (ColumnVector &x)
    {for(int r(x.dim1()-1); r>=0; --r)  x(r)+=ndrand();};
};

inline double u_rand (const double &max)
{
  return (max)*static_cast<double>(rand()) / static_cast<double>(RAND_MAX);
}

inline double u_rand (const double &min, const double &max)
{
  return u_rand(max - min) + min;
}

};  // end of namespace loco_rabbits

using namespace std;
using namespace loco_rabbits;

int main(int argc, char**argv)
{
  srand((unsigned)time(NULL));
  ColumnVector x(3);
  TAddGaussianNoise  addGaussianNoise(1.0);
  for(int i(0); i<1000; ++i)
  {
    x(0)=u_rand(-5.0,5.0);
    x(1)=x(0)+u_rand(-10.0,10.0);
    x(2)= 0.1*x(0) + 0.2*x(1);
    addGaussianNoise(x);
    cout<<x.transpose()<<endl;
  }

  return 0;
}