SNAP Library 2.0, User Reference  2013-05-13 16:33:57
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
TSigmoid Class Reference

#include <linalg.h>

List of all members.

Public Member Functions

 TSigmoid ()
 TSigmoid (const double &A_, const double &B_)
 TSigmoid (const TFltIntKdV &data)
 TSigmoid (TSIn &SIn)
void Load (TSIn &SIn)
void Save (TSOut &SOut) const
double GetVal (const double &x) const
double operator() (const double &x) const
void GetSigmoidAB (double &A_, double &B_)

Static Private Member Functions

static double EvaluateFit (const TFltIntKdV &data, const double A, const double B)
static void EvaluateFit (const TFltIntKdV &data, const double A, const double B, double &J, double &JA, double &JB)
static void EvaluateFit (const TFltIntKdV &data, const double A, const double B, const double U, const double V, const double lambda, double &J, double &JJ, double &JJJ)

Private Attributes

TFlt A
TFlt B

Detailed Description

Definition at line 454 of file linalg.h.


Constructor & Destructor Documentation

TSigmoid::TSigmoid ( ) [inline]

Definition at line 474 of file linalg.h.

{ };
TSigmoid::TSigmoid ( const double &  A_,
const double &  B_ 
) [inline]

Definition at line 475 of file linalg.h.

: A(A_), B(B_) { };
TSigmoid::TSigmoid ( const TFltIntKdV data)

Definition at line 1538 of file linalg.cpp.

                                         {
  // Let z_i be the projection of the i'th training example, and y_i \in {-1, +1} be its class label.
  // Our sigmoid is: P(Y = y | Z = z) = 1 / [1 + e^{-Az + B}]
  // and we want to maximize \prod_i P(Y = y_i | Z = z_i)
  //                       = \prod_{i : y_i = 1} 1 / [1 + e^{-Az_i + B}]  \prod_{i : y_i = -1} e^{-Az_i + B} / [1 + e^{-Az_i + B}]
  // or minimize its negative logarithm,
  //               J(A, B) = \sum_{i : y_i = 1} ln [1 + e^{-Az_i + B}] + \sum_{i : y_i = -1} [ln [1 + e^{-Az_i + B}] - {-Az_i + B}]
  //                       = \sum_i ln [1 + e^{-Az_i + B}] - \sum_{i : y_i = -1} {-Az_i + B}.
  // partial J / partial A = \sum_i (-z_i) e^{-Az_i + B} / [1 + e^{-Az_i + B}] + \sum_{i : y_i = -1} Az_i.
  // partial J / partial B = \sum_i        e^{-Az_i + B} / [1 + e^{-Az_i + B}] + \sum_{i : y_i = -1} (-1).
  double minProj = data[0].Key, maxProj = data[0].Key;
  {for (int i = 1; i < data.Len(); i++) {
    double zi = data[i].Key; if (zi < minProj) minProj = zi; if (zi > maxProj) maxProj = zi; }}
  //const bool dump = false;
  A = 1.0; B = 0.5 * (minProj + maxProj);
  double bestJ = 0.0, bestA = 0.0, bestB = 0.0, lambda = 1.0;
  for (int nIter = 0; nIter < 50; nIter++)
  {
    double J, JA, JB; TSigmoid::EvaluateFit(data, A, B, J, JA, JB);
    if (nIter == 0 || J < bestJ) { bestJ = J; bestA = A; bestB = B; }
    // How far should we move?
    //if (dump) printf("Iter %2d: A = %.5f, B = %.5f, J = %.5f, partial = (%.5f, %.5f)\n", nIter, A, B, J, JA, JB);
        double norm = TMath::Sqr(JA) + TMath::Sqr(JB);
    if (norm < 1e-10) break;
    const int cl = -1; // should be -1

    double Jc = TSigmoid::EvaluateFit(data, A + cl * lambda * JA / norm, B + cl * lambda * JB / norm);
    //if (dump) printf("  At lambda = %.5f, Jc = %.5f\n", lambda, Jc);
    if (Jc > J) {
      while (lambda > 1e-5) {
        lambda = 0.5 * lambda;
        Jc = TSigmoid::EvaluateFit(data, A + cl * lambda * JA / norm, B + cl * lambda * JB / norm);
        //if (dump) printf("  At lambda = %.5f, Jc = %.5f\n", lambda, Jc);
      } }
    else if (Jc < J) {
      while (lambda < 1e5) {
        double lambda2 = 2 * lambda;
        double Jc2 = TSigmoid::EvaluateFit(data, A + cl * lambda2 * JA / norm, B + cl * lambda2 * JB / norm);
        //if (dump) printf("  At lambda = %.5f, Jc = %.5f\n", lambda2, Jc2);
        if (Jc2 > Jc) break;
        lambda = lambda2; Jc = Jc2; } }
    if (Jc >= J) break;
    A += cl * lambda * JA / norm; B += cl * lambda * JB / norm;
    //if (dump) printf("   Lambda = %.5f, new A = %.5f, new B = %.5f, new J = %.5f\n", lambda, A, B, Jc);
  }
  A = bestA; B = bestB;
}
TSigmoid::TSigmoid ( TSIn SIn) [inline]

Definition at line 480 of file linalg.h.

{ A.Load(SIn); B.Load(SIn); }

Member Function Documentation

double TSigmoid::EvaluateFit ( const TFltIntKdV data,
const double  A,
const double  B 
) [static, private]

Definition at line 1480 of file linalg.cpp.

{
  double J = 0.0;
  for (int i = 0; i < data.Len(); i++)
  {
    double zi = data[i].Key; int yi = data[i].Dat;
    double e = exp(-A * zi + B);
    double denum = 1.0 + e;
    double prob = (yi > 0) ? (1.0 / denum) : (e / denum);
    J -= log(prob < 1e-20 ? 1e-20 : prob);
  }
  return J;
}
void TSigmoid::EvaluateFit ( const TFltIntKdV data,
const double  A,
const double  B,
double &  J,
double &  JA,
double &  JB 
) [static, private]

Definition at line 1494 of file linalg.cpp.

{
  //               J(A, B) = \sum_{i : y_i = 1} ln [1 + e^{-Az_i + B}] + \sum_{i : y_i = -1} [ln [1 + e^{-Az_i + B}] - {-Az_i + B}]
  //                       = \sum_i ln [1 + e^{-Az_i + B}] - \sum_{i : y_i = -1} {-Az_i + B}.
  // partial J / partial A = \sum_i (-z_i) e^{-Az_i + B} / [1 + e^{-Az_i + B}] + \sum_{i : y_i = -1} Az_i.
  // partial J / partial B = \sum_i        e^{-Az_i + B} / [1 + e^{-Az_i + B}] + \sum_{i : y_i = -1} (-1).
  J = 0.0; double sum_all_PyNeg = 0.0, sum_all_ziPyNeg = 0.0, sum_yNeg_zi = 0.0, sum_yNeg_1 = 0.0;
  for (int i = 0; i < data.Len(); i++)
  {
    double zi = data[i].Key; int yi = data[i].Dat;
    double e = exp(-A * zi + B);
    double denum = 1.0 + e;
    double prob = (yi > 0) ? (1.0 / denum) : (e / denum);
    J -= log(prob < 1e-20 ? 1e-20 : prob);
    sum_all_PyNeg += e / denum;
    sum_all_ziPyNeg += zi * e / denum;
    if (yi < 0) { sum_yNeg_zi += zi; sum_yNeg_1 += 1; }
  }
  JA = -sum_all_ziPyNeg +     sum_yNeg_zi;
  JB =  sum_all_PyNeg   -     sum_yNeg_1;
}
void TSigmoid::EvaluateFit ( const TFltIntKdV data,
const double  A,
const double  B,
const double  U,
const double  V,
const double  lambda,
double &  J,
double &  JJ,
double &  JJJ 
) [static, private]

Definition at line 1516 of file linalg.cpp.

{
  // Let E_i = e^{-(A + lambda U) z_i + (B + lambda V)}.  Then we have
  // J(lambda) = \sum_i ln [1 + E_i] - \sum_{i : y_i = -1} {-(A + lambda U)z_i + (B + lambda V)}.
  // J'(lambda) = \sum_i (V - U z_i) E_i / [1 + E_i] - \sum_{i : y_i = -1} {V - U z_i).
  //            = \sum_i (V - U z_i) [1 - 1 / [1 + E_i]] - \sum_{i : y_i = -1} {V - U z_i).
  // J"(lambda) = \sum_i (V - U z_i)^2 E_i / [1 + E_i]^2.
  J = 0.0; JJ = 0.0; JJJ = 0.0;
  for (int i = 0; i < data.Len(); i++)
  {
    double zi = data[i].Key; int yi = data[i].Dat;
    double e = exp(-A * zi + B);
    double denum = 1.0 + e;
    double prob = (yi > 0) ? (1.0 / denum) : (e / denum);
    J -= log(prob < 1e-20 ? 1e-20 : prob);
    double VU = V - U * zi;
    JJ += VU * (e / denum); if (yi < 0) JJ -= VU;
    JJJ += VU * VU * e / denum / denum;
  }
}
void TSigmoid::GetSigmoidAB ( double &  A_,
double &  B_ 
) [inline]

Definition at line 489 of file linalg.h.

{ A_=A; B_=B; }
double TSigmoid::GetVal ( const double &  x) const [inline]

Definition at line 484 of file linalg.h.

                                         {
        return 1.0 / (1.0 + exp(-A * x + B)); }
void TSigmoid::Load ( TSIn SIn) [inline]

Definition at line 481 of file linalg.h.

{ A.Load(SIn); B.Load(SIn); }
double TSigmoid::operator() ( const double &  x) const [inline]

Definition at line 486 of file linalg.h.

                                             {
        return GetVal(x); }
void TSigmoid::Save ( TSOut SOut) const [inline]

Definition at line 482 of file linalg.h.

{A.Save(SOut); B.Save(SOut);}

Member Data Documentation

TFlt TSigmoid::A [private]

Definition at line 456 of file linalg.h.

TFlt TSigmoid::B [private]

Definition at line 457 of file linalg.h.


The documentation for this class was generated from the following files: