Search code examples
.net.net-coremathmath.netmathnet-numerics

Calculation of Cramer's V matrix using MathNet.Numerics


How to use MathNet.Numerics to calculate Cramer's V matrix?

Example data and problem:

Finding intercorrelation between each category occurrence.

Time Categories
14/06 AM banana, tomatoe
14/06 PM milk, beans, banana
15/06 AM apple, meat, tomatoe
15/06 PM chicken, banana, coffee
16/06 AM milk, beans, chicken
16/06 PM tomatoe, orange, coffee

Thanks.


Solution

  • First you want to count all the categories involved, and assign each an index, indicating which row/col in the matrix will relate to that category. eg

    0->banana, ... N->orange

    Next build a matrix of appropriate size (NxN) filled with 0s.

    (in MathNet I think this goes like Matrix<int> m = Matrix<int>.Build.Dense(N, N); )

    Perform the Cramér's V calculation for each pair of categories and replace the value in the matrix with the calculated value.eg

    m(i,j)= cramers_v(categories[i],categories[j])

    Very Lastly, after this is working and tested, and if it benchmarks too slow; consider the option to optimise it, maybe by calculating the values before instantiating the matrix, or perform the calculations using matrix/vector operations.