Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added sparse inverse covariance estimator
- Loading branch information
Showing
6 changed files
with
190 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
%{ | ||
#include <shogun/mathematics/Math.h> | ||
#include <shogun/mathematics/Statistics.h> | ||
#include <shogun/mathematics/SparseInverseCovariance.h> | ||
%} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Copyright (C) 2009-2011 Jun Liu, Jieping Ye | ||
* Copyright (C) 2012 Sergey Lisitsyn | ||
*/ | ||
|
||
#include <shogun/mathematics/SparseInverseCovariance.h> | ||
#include <shogun/base/Parameter.h> | ||
#include <shogun/lib/slep/SpInvCoVa/invCov.h> | ||
|
||
using namespace shogun; | ||
|
||
CSparseInverseCovariance::CSparseInverseCovariance() : | ||
CSGObject(), m_lasso_max_iter(1000), | ||
m_max_iter(1000), m_f_gap(1e-6), m_x_gap(1e-4), | ||
m_xtol(1e-4) | ||
{ | ||
register_parameters(); | ||
} | ||
|
||
CSparseInverseCovariance::~CSparseInverseCovariance() | ||
{ | ||
} | ||
|
||
void CSparseInverseCovariance::register_parameters() | ||
{ | ||
SG_ADD(&m_lasso_max_iter,"lasso_max_iter", | ||
"maximum iteration of LASSO step",MS_NOT_AVAILABLE); | ||
SG_ADD(&m_max_iter,"max_iter","maximum total iteration", | ||
MS_NOT_AVAILABLE); | ||
SG_ADD(&m_f_gap,"f_gap","f gap",MS_NOT_AVAILABLE); | ||
SG_ADD(&m_x_gap,"x_gap","x gap",MS_NOT_AVAILABLE); | ||
SG_ADD(&m_xtol,"xtol","xtol",MS_NOT_AVAILABLE); | ||
} | ||
|
||
SGMatrix<float64_t> CSparseInverseCovariance::estimate(SGMatrix<float64_t> S, float64_t lambda_c) | ||
{ | ||
ASSERT(S.num_cols==S.num_rows); | ||
|
||
int32_t n = S.num_cols; | ||
float64_t sum_S = 0.0; | ||
for (int32_t i=0; i<n; i++) | ||
sum_S += S(i,i); | ||
|
||
float64_t* Theta = SG_CALLOC(float64_t, n*n); | ||
float64_t* W = SG_CALLOC(float64_t, n*n); | ||
|
||
invCov(Theta, W, S.matrix, lambda_c, sum_S, n, m_lasso_max_iter, | ||
m_f_gap, m_x_gap, m_max_iter, m_xtol); | ||
|
||
SG_FREE(W); | ||
return SGMatrix<float64_t>(Theta,n,n); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Copyright (C) 2012 Sergey Lisitsyn | ||
*/ | ||
|
||
#ifndef SPINVCOV_H_ | ||
#define SPINVCOV_H_ | ||
#include <shogun/base/SGObject.h> | ||
#include <shogun/lib/SGMatrix.h> | ||
|
||
namespace shogun | ||
{ | ||
|
||
/** @brief used to estimate inverse covariance matrix using graphical lasso | ||
* | ||
* implementation is based on SLEP library's code | ||
*/ | ||
class CSparseInverseCovariance : public CSGObject | ||
{ | ||
public: | ||
|
||
/** constructor */ | ||
CSparseInverseCovariance(); | ||
|
||
/** destructor */ | ||
virtual ~CSparseInverseCovariance(); | ||
|
||
/** estimate inverse covariance matrix | ||
* | ||
* @param S empirical covariance matrix | ||
* @param lambda_C regularization constant | ||
*/ | ||
SGMatrix<float64_t> estimate(SGMatrix<float64_t> S, float64_t lambda_c); | ||
|
||
/** get name */ | ||
const char* get_name() const { return "SparseInverseCovariance"; }; | ||
|
||
|
||
/** get lasso max iter | ||
* @return lasso max iter | ||
*/ | ||
int32_t get_lasso_max_iter() const { return m_lasso_max_iter; } | ||
/** get max iter | ||
* @return max iter | ||
*/ | ||
int32_t get_max_iter() const { return m_max_iter; } | ||
/** get lasso max iter | ||
* @return lasso max iter | ||
*/ | ||
float64_t get_f_gap() const { return m_f_gap; } | ||
/** get lasso max iter | ||
* @return lasso max iter | ||
*/ | ||
float64_t get_x_gap() const { return m_x_gap; } | ||
/** get lasso max iter | ||
* @return lasso max iter | ||
*/ | ||
float64_t get_xtol() const { return m_xtol; } | ||
|
||
/** set lasso max iter | ||
* @param lasso_max_iter lasso max iter | ||
*/ | ||
void set_lasso_max_iter(int32_t lasso_max_iter) | ||
{ | ||
m_lasso_max_iter = lasso_max_iter; | ||
} | ||
/** set max iter | ||
* @param max_iter max iter | ||
*/ | ||
void set_max_iter(int32_t max_iter) | ||
{ | ||
m_max_iter = max_iter; | ||
} | ||
/** set f gap | ||
* @param f_gap f gap | ||
*/ | ||
void set_f_gap(int32_t f_gap) | ||
{ | ||
m_f_gap = f_gap; | ||
} | ||
/** set x gap | ||
* @param x_gap x gap | ||
*/ | ||
void set_x_gap(int32_t x_gap) | ||
{ | ||
m_x_gap = x_gap; | ||
} | ||
/** set xtol | ||
* @param xtol xtol | ||
*/ | ||
void set_xtol(int32_t xtol) | ||
{ | ||
m_xtol = xtol; | ||
} | ||
|
||
private: | ||
|
||
/** register parameters */ | ||
void register_parameters(); | ||
|
||
protected: | ||
|
||
/** LASSO max iter */ | ||
int32_t m_lasso_max_iter; | ||
|
||
/** max iter */ | ||
int32_t m_max_iter; | ||
|
||
/** fGap */ | ||
float64_t m_f_gap; | ||
|
||
/** xGap */ | ||
float64_t m_x_gap; | ||
|
||
/** xtol */ | ||
float64_t m_xtol; | ||
}; | ||
|
||
} | ||
#endif |