Skip to content

Commit

Permalink
Cookbook for chi_square distance (#4324)
Browse files Browse the repository at this point in the history
* updated csv_file in chi_square.sg to new api and wrote a cookbook for the chi_square distance
  • Loading branch information
FaroukY authored and karlnapf committed Jun 12, 2018
1 parent 11755cc commit 4d46c05
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
35 changes: 35 additions & 0 deletions doc/cookbook/source/examples/distance/chi_square.rst
@@ -0,0 +1,35 @@
===================
Chi Square Distance
===================

The Chi Square Distance for real valued features :math:`\bf{x},\bf{x'} \in \mathbb{R}^{n}` extends the concept of :math:`\chi^{2}` distance to negative values.
This distance is calculated by the equation:

.. math::
d(\bf{x},\bf{x'}) = \sum_{i=1}^{n}\frac{(x_{i}-x'_{i})^2}{|x_{i}|+|x'_{i}|}
-------
Example
-------

We first create some sample data. So we instantiate CDenseFeatures containing the sample data.

.. sgexample:: chi_square.sg:create_features

We create an instance of :sgclass:`CChiSquareDistance` by passing it the sample data :sgclass:`CDenseFeatures`.

.. sgexample:: cosine.sg:create_instance

The distance matrix can be extracted as follows:

.. sgexample:: chi_square.sg:extract_distance

We can use the same instance with new :sgclass:`CDenseFeatures` to compute asymmetrical distance as follows:

.. sgexample:: chi_square.sg:refresh_distance

----------
References
----------
:wiki:`Chi-squared_test`
4 changes: 2 additions & 2 deletions examples/meta/src/distance/chi_square.sg
@@ -1,5 +1,5 @@
CSVFile f_feats_a("../../data/fm_train_real.dat")
CSVFile f_feats_b("../../data/fm_test_real.dat")
File f_feats_a = csv_file("../../data/fm_train_real.dat")
File f_feats_b = csv_file("../../data/fm_test_real.dat")

#![create_features]
Features features_a = features(f_feats_a)
Expand Down

0 comments on commit 4d46c05

Please sign in to comment.