Skip to content

Commit

Permalink
Adding checks to ensure MATLAB inputs are doubles.
Browse files Browse the repository at this point in the history
  • Loading branch information
Craigacp committed Jan 10, 2016
1 parent fbfc35e commit efccae1
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 18 deletions.
6 changes: 6 additions & 0 deletions MIToolbox.m
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,33 +35,39 @@ 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
2
1
3
4

```
======

To compile the library for use in MATLAB/OCTAVE, execute CompileMIToolbox.m
Expand All @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions RenyiMIToolbox.m
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
6 changes: 6 additions & 0 deletions WeightedMIToolbox.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 12 additions & 6 deletions cmi.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 10 additions & 4 deletions condh.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions h.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
3 changes: 3 additions & 0 deletions joint.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions mi.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit efccae1

Please sign in to comment.