From efccae11bff5394a77723f6d89d819a8cbf2f554 Mon Sep 17 00:00:00 2001 From: Adam Pocock Date: Sun, 10 Jan 2016 16:09:19 -0500 Subject: [PATCH] Adding checks to ensure MATLAB inputs are doubles. --- MIToolbox.m | 6 ++++++ README.md | 21 +++++++++++++++------ RenyiMIToolbox.m | 6 ++++++ WeightedMIToolbox.m | 6 ++++++ cmi.m | 18 ++++++++++++------ condh.m | 14 ++++++++++---- h.m | 7 +++++-- joint.m | 3 +++ mi.m | 3 +++ 9 files changed, 66 insertions(+), 18 deletions(-) diff --git a/MIToolbox.m b/MIToolbox.m index 880924a..a64ad66 100644 --- a/MIToolbox.m +++ b/MIToolbox.m @@ -26,6 +26,12 @@ %Mutual Information = 7 %Conditional MI = 8 +for i = 1:length(varargin) + if (~isa(varargin{i},'double')) + error('Error, MIToolbox requires inputs to be double vector or matrices') + end +end + if (strcmpi(functionName,'Joint') || strcmpi(functionName,'Merge')) [varargout{1}] = MIToolboxMex(3,varargin{1}); elseif (strcmpi(functionName,'Entropy') || strcmpi(functionName,'h')) diff --git a/README.md b/README.md index f7e078a..3e9bfd2 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,8 @@ These functions are targeted for use with feature selection algorithms rather than communication channels and so expect all the data to be available before execution and sample their own probability distributions from the data. +All functions expect the inputs to be vectors or matrices of doubles. + Functions contained: - Entropy - Conditional Entropy @@ -33,25 +35,31 @@ Note: all functions are calculated in log base 2, so return units of "bits". Examples: +``` >> y = [1 1 1 0 0]'; >> x = [1 0 1 1 0]'; - +``` +``` >> mi(x,y) %% mutual information I(X;Y) ans = 0.0200 - +``` +``` >> h(x) %% entropy H(X) ans = 0.9710 - +``` +``` >> condh(x,y) %% conditional entropy H(X|Y) ans = 0.9510 - +``` +``` >> h( [x,y] ) %% joint entropy H(X,Y) ans = 1.9219 - +``` +``` >> joint([x,y]) %% joint random variable XY ans = 1 @@ -59,7 +67,7 @@ ans = 1 3 4 - +``` ====== To compile the library for use in MATLAB/OCTAVE, execute CompileMIToolbox.m @@ -72,6 +80,7 @@ install MIToolbox into /usr/local/lib & /usr/local/include. All code is licensed under the 3-clause BSD license. Update History + - 10/01/2016 - v2.1.2 - Relicense from LGPL to BSD. Added checks to ensure input MATLAB types are doubles. - 02/02/2015 - v2.1.1 - Fixed up the Makefile so it installs the headers too. - 22/02/2014 - v2.1 - Fixed a couple of bugs related to memory handling. Added a make install for compatibility with PyFeast. diff --git a/RenyiMIToolbox.m b/RenyiMIToolbox.m index cd235e9..204d2d8 100644 --- a/RenyiMIToolbox.m +++ b/RenyiMIToolbox.m @@ -17,6 +17,12 @@ %Renyi Entropy = 1; %Renyi MI = 3; +for i = 1:length(varargin) + if (~isa(varargin{i},'double')) + error('Error, MIToolbox requires inputs to be double vector or matrices') + end +end + if (alpha ~= 1) if (strcmpi(functionName,'Entropy') || strcmpi(functionName,'h')) %disp('Calculating Entropy'); diff --git a/WeightedMIToolbox.m b/WeightedMIToolbox.m index a47c8cd..fb8d54a 100644 --- a/WeightedMIToolbox.m +++ b/WeightedMIToolbox.m @@ -23,6 +23,12 @@ %Mutual Information = 4 %Conditional MI = 5 +for i = 1:length(varargin) + if (~isa(varargin{i},'double')) + error('Error, MIToolbox requires inputs to be double vector or matrices') + end +end + if (strcmpi(functionName,'Entropy') || strcmpi(functionName,'h')) %disp('Calculating Entropy'); if (size(varargin{1},2)>1) diff --git a/cmi.m b/cmi.m index 30e4bb0..20c1254 100644 --- a/cmi.m +++ b/cmi.m @@ -8,23 +8,29 @@ %returns the mutual information between X and Y conditioned on Z, I(X;Y|Z) if nargin == 3 + if (~isa(X,'double') || ~isa(Y,'double') || ~isa(Z,'double')) + error('Error, inputs must be double vectors or matrices') + end if (size(X,2)>1) - mergedFirst = MIToolboxMex(3,X); + mergedFirst = MIToolboxMex(3,X); else - mergedFirst = X; + mergedFirst = X; end if (size(Y,2)>1) - mergedSecond = MIToolboxMex(3,Y); + mergedSecond = MIToolboxMex(3,Y); else - mergedSecond = Y; + mergedSecond = Y; end if (size(Z,2)>1) - mergedThird = MIToolboxMex(3,Z); + mergedThird = MIToolboxMex(3,Z); else - mergedThird = Z; + mergedThird = Z; end [output] = MIToolboxMex(8,mergedFirst,mergedSecond,mergedThird); elseif nargin == 2 + if (~isa(X,'double') || ~isa(Y,'double')) + error('Error, inputs must be double vectors or matrices') + end output = mi(X,Y); else output = 0; diff --git a/condh.m b/condh.m index 9f966db..6b0b798 100644 --- a/condh.m +++ b/condh.m @@ -8,18 +8,24 @@ %returns the conditional entropy of X given Y, H(X|Y) if nargin == 2 + if (~isa(X,'double') || ~isa(Y,'double')) + error('Error, inputs must be double vectors or matrices') + end if (size(X,2)>1) - mergedFirst = MIToolboxMex(3,X); + mergedFirst = MIToolboxMex(3,X); else - mergedFirst = X; + mergedFirst = X; end if (size(Y,2)>1) - mergedSecond = MIToolboxMex(3,Y); + mergedSecond = MIToolboxMex(3,Y); else - mergedSecond = Y; + mergedSecond = Y; end [output] = MIToolboxMex(6,mergedFirst,mergedSecond); elseif nargin == 1 + if (~isa(X,'double')) + error('Error, inputs must be double vectors or matrices') + end output = h(X); else output = 0; diff --git a/h.m b/h.m index 8fd8999..df437a2 100644 --- a/h.m +++ b/h.m @@ -5,9 +5,12 @@ % %returns the entropy of X, H(X) +if (~isa(X,'double')) + error('Error, inputs must be double vectors or matrices') +end if (size(X,2)>1) - mergedVector = MIToolboxMex(3,X); + mergedVector = MIToolboxMex(3,X); else - mergedVector = X; + mergedVector = X; end [output] = MIToolboxMex(4,mergedVector); diff --git a/joint.m b/joint.m index f40ff25..6a5460a 100644 --- a/joint.m +++ b/joint.m @@ -9,6 +9,9 @@ %if the joint variable is only compared with variables using the same samples, %then arity information is not required +if (~isa(X,'double') || ~isa(arities,'double')) + error('Error, inputs must be double vectors or matrices') +end if (nargin == 2) [output] = MIToolboxMex(3,X,arities); else diff --git a/mi.m b/mi.m index 2fd8766..360f6f6 100644 --- a/mi.m +++ b/mi.m @@ -7,6 +7,9 @@ % %returns the mutual information between X and Y, I(X;Y) +if (~isa(X,'double') || ~isa(Y,'double')) + error('Error, inputs must be double vectors or matrices') +end if (size(X,2)>1) mergedFirst = MIToolboxMex(3,X); else