#!/usr/bin/python
#
# Compute the coefficients of a Fisher discriminant given some
# data randomly generated according to a normal distrubtion for
# a 2D problem.
#
# The Fisher coefficients are given by:
# alpha = W^-1 * DeltaX
# where DeltaX is the difference in means between classes A and B,
# and W is the sum of covariance matrices for the two classess.
#
######################################################################
# TensorFlow test example prepared by Adrian Bevan (a.j.bevan@qmul)
# for use with the material found at:
#
# https://pprc.qmul.ac.uk/~bevan/statistics/TensorFlow.html
#
# Feb 2017
######################################################################
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
# set a 2D sample of N events to randomly generate
# multinormal distributions in order to compute a Fisher
# discriminant taking the weights as
#
# alpha = W^-1 (mu[A] - mu[B])
#
# specify the normal means, widths below; assume
# no correlation
ndim = 2
N = 1000
muA = [1.0, 0.7]
muB = [-1.0, -1.0]
sigmaA = [0.6, 0.5]
sigmaB = [0.5, 0.4]
# use random_normal to generate the data
classA = tf.Variable(tf.random_normal([N, ndim], muA, sigmaA))
classB = tf.Variable(tf.random_normal([N, ndim], muB, sigmaB))
#initialize the variable
init_op = tf.global_variables_initializer()
print "==============================================================="
print "Computing the coefficients of a Fisher discriminant for a toy"
print "example with "
print "muA = ", muA
print "muB = ", muB
print "sigmaA = ", muA
print "sigmaB = ", muB
print "(no correlation between x and y)"
print ""
print "A sample of", N, "examples has been generaated"
print "==============================================================="
#run the session
with tf.Session() as sess:
sess.run(init_op) #execute init_op
# class A
print "Mean and covariance for class A"
meanA, varA = tf.nn.moments(classA, axes=[0])
meanAx, meanAy = sess.run(meanA)
varAx, varAy = sess.run(varA)
# define the covariance matrix for the data in class A
SigmaA = tf.constant([[varAx, 0], [0, varAy]])
print (sess.run(meanA))
print (sess.run(SigmaA))
print ""
# classB
print "Mean and covariance for class B"
meanB, varB = tf.nn.moments(classB, axes=[0])
meanBx, meanBy = sess.run(meanB)
varBx, varBy = sess.run(varB)
# define the covariance matrix for the data in class A
SigmaB = tf.constant([[varBx, 0], [0, varBy]])
print (sess.run(meanB))
print (sess.run(SigmaB))
print ""
print "The sum of covariances for classes A and B (W)"
W = tf.add(SigmaA, SigmaB)
print (sess.run(W))
print ""
print "The inverse of W"
Winv = tf.matrix_inverse(W)
print (sess.run(Winv))
print ""
print "The difference between the class means"
DeltaMu = tf.subtract(meanA, meanB)
print (sess.run(DeltaMu))
print ""
print "==============================================================="
print "The fisher coefficients are (up to some arbitrary scaling)"
print "the diagonals of the matrix printed below"
alpha = tf.multiply(Winv, DeltaMu)
print (sess.run(alpha))
print "==============================================================="