Navigation Menu

Skip to content

Commit

Permalink
Added sparse inverse covariance estimator
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn committed Jun 10, 2012
1 parent b979aae commit bc2cf3e
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/interfaces/modular/Mathematics.i
Expand Up @@ -12,3 +12,5 @@
%include <shogun/mathematics/Math.h>
%rename(Statistics) CStatistics;
%include <shogun/mathematics/Statistics.h>
%rename(SparseInverseCovariance) CSparseInverseCovariance;
%include <shogun/mathematics/SparseInverseCovariance.h>
1 change: 1 addition & 0 deletions src/interfaces/modular/Mathematics_includes.i
@@ -1,5 +1,6 @@
%{
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/Statistics.h>
#include <shogun/mathematics/SparseInverseCovariance.h>
%}

2 changes: 1 addition & 1 deletion src/shogun/lib/slep/SpInvCoVa/invCov.cpp
Expand Up @@ -158,7 +158,7 @@ int lassoCD(double *Theta, double *W, double *S, double lambda, int n, int ith,
}


void invConv(double *Theta, double *W, double *S, double lambda, double sum_S, int n,
void invCov(double *Theta, double *W, double *S, double lambda, double sum_S, int n,
int LassoMaxIter, double fGap, double xGap, /*for the Lasso (inner iteration)*/
int maxIter, double xtol) /*for the outer iteration*/
{
Expand Down
10 changes: 5 additions & 5 deletions src/shogun/lib/slep/SpInvCoVa/invCov.h
Expand Up @@ -36,10 +36,10 @@ void m_Ax(double *Ax, double *A, double *x, int n, int ith);
int lassoCD(double *Theta, double *W, double *S, double lambda, int n,
int ith, int flag, int maxIter, double fGap, double xGap);

void invConv(double *Theta, double *W, double *S, double lambda,
double sum_S, int n,
int LassoMaxIter, double fGap,
double xGap, /*for the Lasso (inner iteration)*/
int maxIter, double xtol); /*for the outer iteration*/
void invCov(double *Theta, double *W, double *S, double lambda,
double sum_S, int n,
int LassoMaxIter, double fGap,
double xGap, /*for the Lasso (inner iteration)*/
int maxIter, double xtol); /*for the outer iteration*/

#endif /* ----- #ifndef INVCOV_SLEP ----- */
57 changes: 57 additions & 0 deletions src/shogun/mathematics/SparseInverseCovariance.cpp
@@ -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);
}
124 changes: 124 additions & 0 deletions src/shogun/mathematics/SparseInverseCovariance.h
@@ -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

0 comments on commit bc2cf3e

Please sign in to comment.