Skip to content

Commit

Permalink
GC-82: Serializable annotation is needed on class Accord.Statistics.A…
Browse files Browse the repository at this point in the history
…nalysis.ContrastFunctions.Logcosh
  • Loading branch information
cesarsouza committed Dec 29, 2013
1 parent 7b48ebe commit d9c7d0f
Show file tree
Hide file tree
Showing 7 changed files with 390 additions and 225 deletions.
4 changes: 4 additions & 0 deletions Sources/Accord.Statistics/Accord.Statistics.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@
<Compile Include="Analysis\Base\IMultivariateRegressionAnalysis.cs" />
<Compile Include="Analysis\Base\IRegressionAnalysis.cs" />
<Compile Include="Analysis\Base\IDiscriminantAnalysis.cs" />
<Compile Include="Analysis\Contrast Functions\Exponential.cs" />
<Compile Include="Analysis\Contrast Functions\IContrastFunction.cs" />
<Compile Include="Analysis\Contrast Functions\Kurtosis.cs" />
<Compile Include="Analysis\Contrast Functions\Logcosh.cs" />
<Compile Include="Analysis\Performance\WeightedConfusionMatrix.cs" />
<Compile Include="Analysis\ProportionalHazardsAnalysis.cs" />
<Compile Include="Analysis\MultipleLinearRegressionAnalysis.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Accord Statistics Library
// The Accord.NET Framework
// http://accord-framework.net
//
// Copyright © César Souza, 2009-2013
// cesarsouza at gmail.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//

namespace Accord.Statistics.Analysis.ContrastFunctions
{
using System;

/// <summary>
/// Exponential contrast function.
/// </summary>
///
/// <remarks>
/// According to Hyvärinen, the Exponential contrast function may be
/// used when the independent components are highly super-Gaussian or
/// when robustness is very important.
/// </remarks>
///
/// <seealso cref="IndependentComponentAnalysis"/>
///
[Serializable]
public class Exponential : IContrastFunction
{
double alpha = 1;

/// <summary>
/// Initializes a new instance of the <see cref="Exponential"/> class.
/// </summary>
/// <param name="alpha">The exponential alpha constant. Default is 1.</param>
///
public Exponential(double alpha)
{
this.alpha = alpha;
}

/// <summary>
/// Gets the exponential alpha constant.
/// </summary>
///
public double Alpha { get { return alpha; } }

/// <summary>
/// Initializes a new instance of the <see cref="Exponential"/> class.
/// </summary>
///
public Exponential() { }

/// <summary>
/// Contrast function.
/// </summary>
///
/// <param name="x">The vector of observations.</param>
/// <param name="output">At method's return, this parameter
/// should contain the evaluation of function over the vector
/// of observations <paramref name="x"/>.</param>
/// <param name="derivative">At method's return, this parameter
/// should contain the evaluation of function derivative over
/// the vector of observations <paramref name="x"/>.</param>
///
public void Evaluate(double[] x, double[] output, double[] derivative)
{
// Exponential contrast function and its derivative, as given
// in original Hyvärinen's paper. See main references for the
// Independent Component Analysis class for details.

for (int j = 0; j < x.Length; j++)
{
double w = x[j];
double e = System.Math.Exp(-alpha * (w * w) / 2.0);

// g(w*x) = wx * exp(-(wx^2)/2)
output[j] = w * e;

// g'(w*x) = (1 - wx^2) * exp(-(wx^2)/2)
derivative[j] = (1.0 - alpha * w * w) * e;
}
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Accord Statistics Library
// The Accord.NET Framework
// http://accord-framework.net
//
// Copyright © César Souza, 2009-2013
// cesarsouza at gmail.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//

namespace Accord.Statistics.Analysis.ContrastFunctions
{
using System;

/// <summary>
/// Common interface for contrast functions.
/// </summary>
///
/// <remarks>
/// Contrast functions are used as objective functions in
/// neg-entropy calculations.
/// </remarks>
///
/// <seealso cref="IndependentComponentAnalysis"/>
///
public interface IContrastFunction
{
/// <summary>
/// Contrast function.
/// </summary>
///
/// <param name="x">The vector of observations.</param>
/// <param name="output">At method's return, this parameter
/// should contain the evaluation of function over the vector
/// of observations <paramref name="x"/>.</param>
/// <param name="derivative">At method's return, this parameter
/// should contain the evaluation of function derivative over
/// the vector of observations <paramref name="x"/>.</param>
///
void Evaluate(double[] x, double[] output, double[] derivative);
}
}
78 changes: 78 additions & 0 deletions Sources/Accord.Statistics/Analysis/Contrast Functions/Kurtosis.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Accord Statistics Library
// The Accord.NET Framework
// http://accord-framework.net
//
// Copyright © César Souza, 2009-2013
// cesarsouza at gmail.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//

namespace Accord.Statistics.Analysis.ContrastFunctions
{
using System;

/// <summary>
/// Kurtosis contrast function.
/// </summary>
/// <remarks>
/// According to using to Hyvärinen, the kurtosis contrast function is
/// justified on statistical grounds only for estimating sub-Gaussian
/// independent components when there are no outliers.
/// </remarks>
///
/// <seealso cref="IndependentComponentAnalysis"/>
///
[Serializable]
public class Kurtosis : IContrastFunction
{

/// <summary>
/// Initializes a new instance of the <see cref="Kurtosis"/> class.
/// </summary>
///
public Kurtosis() { }

/// <summary>
/// Contrast function.
/// </summary>
///
/// <param name="x">The vector of observations.</param>
/// <param name="output">At method's return, this parameter
/// should contain the evaluation of function over the vector
/// of observations <paramref name="x"/>.</param>
/// <param name="derivative">At method's return, this parameter
/// should contain the evaluation of function derivative over
/// the vector of observations <paramref name="x"/>.</param>
///
public void Evaluate(double[] x, double[] output, double[] derivative)
{
for (int j = 0; j < x.Length; j++)
{
// Kurtosis contrast function and its derivative, as given
// in original Hyvärinen's paper. See main references for the
// Independent Component Analysis class for details.

double v = x[j];

// g(w*x)
output[j] = v * v * v;

// g'(w*x)
derivative[j] = (1.0 / 3.0) * v * v;
}
}
}
}
97 changes: 97 additions & 0 deletions Sources/Accord.Statistics/Analysis/Contrast Functions/Logcosh.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Accord Statistics Library
// The Accord.NET Framework
// http://accord-framework.net
//
// Copyright © César Souza, 2009-2013
// cesarsouza at gmail.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//

namespace Accord.Statistics.Analysis.ContrastFunctions
{
using System;

/// <summary>
/// Log-cosh (Hyperbolic Tangent) contrast function.
/// </summary>
///
/// <remarks>
/// According to Hyvärinen, the Logcosh contrast function
/// is a good general-purpose contrast function.
/// </remarks>
///
/// <seealso cref="IndependentComponentAnalysis"/>
///
[Serializable]
public class Logcosh : IContrastFunction
{
double alpha = 1;

/// <summary>
/// Initializes a new instance of the <see cref="Logcosh"/> class.
/// </summary>
///
public Logcosh() { }

/// <summary>
/// Initializes a new instance of the <see cref="Logcosh"/> class.
/// </summary>
///
/// <param name="alpha">The log-cosh alpha constant. Default is 1.</param>
///
public Logcosh(double alpha)
{
this.alpha = alpha;
}

/// <summary>
/// Gets the exponential log-cosh constant.
/// </summary>
///
public double Alpha { get { return alpha; } }

/// <summary>
/// Contrast function.
/// </summary>
///
/// <param name="x">The vector of observations.</param>
/// <param name="output">At method's return, this parameter
/// should contain the evaluation of function over the vector
/// of observations <paramref name="x"/>.</param>
/// <param name="derivative">At method's return, this parameter
/// should contain the evaluation of function derivative over
/// the vector of observations <paramref name="x"/>.</param>
///
public void Evaluate(double[] x, double[] output, double[] derivative)
{
// Log-cosh contrast function and its derivative, as given
// in original Hyvärinen's paper. See main references for the
// Independent Component Analysis class for details.

for (int j = 0; j < x.Length; j++)
{
double f;

// g(w*x)
f = output[j] = System.Math.Tanh(alpha * x[j]);

// g'(w*x)
derivative[j] = alpha * (1.0 - f * f);
}
}
}

}
Loading

0 comments on commit d9c7d0f

Please sign in to comment.