STK++ 1.0

STK_Gaussian2BlocksStatModel.cpp

Go to the documentation of this file.
00001 /*--------------------------------------------------------------------*/
00002 /*     Copyright (C) 2004-2011  Serge Iovleff
00003 
00004  This program is free software; you can redistribute it and/or modify
00005  it under the terms of the GNU Lesser General Public License as
00006  published by the Free Software Foundation; either version 2 of the
00007  License, or (at your option) any later version.
00008 
00009  This program is distributed in the hope that it will be useful,
00010  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  GNU Lesser General Public License for more details.
00013 
00014  You should have received a copy of the GNU Lesser General Public
00015  License along with this program; if not, write to the
00016  Free Software Foundation, Inc.,
00017  59 Temple Place,
00018  Suite 330,
00019  Boston, MA 02111-1307
00020  USA
00021 
00022  Contact : Serge.Iovleff@stkpp.org
00023  */
00024 
00025 /*
00026  * Project:  stkpp::
00027  * created on: 13 août 2011
00028  * Purpose:  .
00029  * Author:   iovleff, serge.iovleff@stkpp.org
00030  *
00031  **/
00032 
00037 #include "../include/STK_Gaussian2BlocksStatModel.h"
00038 
00039 #include "../../STatistiK/include/STK_Stat_UnivariateReal.h"
00040 #include "../../STatistiK/include/STK_Stat_BivariateRealReal.h"
00041 #include "../../STatistiK/include/STK_Law_MultivariateNormal.h"
00042 
00043 namespace STK
00044 {
00045 
00046 Gaussian2BlocksStatModel::Gaussian2BlocksStatModel( const Matrix *p_data)
00047                                                   : GaussianStatModel(p_data)
00048                                                   , dim_(p_data_->sizeHo())
00049 { }
00050 
00051 
00052 /* destructor */
00053 Gaussian2BlocksStatModel::~Gaussian2BlocksStatModel()
00054 { }
00055 
00056 
00057 /* compute the empirical covariance matrix. */
00058 void Gaussian2BlocksStatModel::compCovariance()
00059 {
00060   // resize mean
00061   cov_.resize(p_data_->rangeHo());
00062   cov_ = 0.;
00063   // get dimensions for the first block
00064   const Integer first1 = p_data_->firstCol(), last1 = min(p_data_->lastCol(), first1+dim_-1);
00065   for (Integer i= first1; i <= last1; ++i)
00066   {
00067     cov_(i, i) = Stat::varianceWithFixedMean<Vector>(p_data_->col(i), mean_[i]);
00068     for (Integer j= first1; j < i; ++j)
00069     {
00070       cov_(i, j) = Stat::covarianceWithFixedMean<Vector>(p_data_->col(i), p_data_->col(j), mean_[i], mean_[j]);
00071       cov_(j, i) = cov_(i,j);
00072     }
00073   }
00074   // get dimensions for the second block
00075   const Integer first2 = last1+1, last2 = p_data_->lastCol(), size2 = last2 - last1 ;
00076   if (size2)
00077   {
00078     // compute variance of each column
00079     for (Integer i= first2; i <= last2; ++i)
00080     { cov_(i, i) = Stat::varianceWithFixedMean<Vector>(p_data_->col(i), mean_[i]);}
00081     variance2_ = trace(MatrixSquare(cov_, Range(first2, last2)))/(Real)size2;
00082     // compute variance of each column
00083     for (Integer i= first2; i <= last2; ++i)
00084     { cov_(i, i) = variance2_;}
00085   }
00086 }
00087 
00088 /* compute the empirical covariance matrix. */
00089 void Gaussian2BlocksStatModel::compWeightedCovariance(Vector const& weights)
00090 {
00091   // resize mean
00092   cov_.resize(p_data_->rangeHo());
00093   // get dimensions for the first block
00094   const Integer first1 = p_data_->firstCol(), last1 = min(p_data_->lastCol(), dim_);
00095   for (Integer i= first1; i <= last1; ++i)
00096   {
00097     cov_(i, i) = Stat::varianceWithFixedMean<Vector>(p_data_->col(i), weights, mean_[i]);
00098     for (Integer j= first1; j < i; ++j)
00099     {
00100       cov_(i, j) = 0.;
00101       cov_(j, i) = 0.;
00102     }
00103   }
00104   // get dimensions for the second block
00105   const Integer first2 = last1+1, last2 = p_data_->lastCol(), size2 = last2 - last1 ;
00106   if (size2)
00107   {
00108     // compute variance of each column
00109     for (Integer i= first2; i <= last2; ++i)
00110     { cov_(i, i) = Stat::varianceWithFixedMean<Vector>(p_data_->col(i), weights, mean_[i]);}
00111     variance2_ = trace(MatrixSquare(cov_, Range(first2, last2)))/(Real)size2;
00112     for (Integer i= first2; i <= last2; ++i)
00113     { cov_(i, i) = variance2_;}
00114   }
00115 }
00116 
00117 } // namespace STK