function [p, uY] = joint_probabilities(Y, prec)
%% Soucast mutual_information toolbox
% [p, uY] = joint_probabilities(Y, prec)
% 
% This function calculates the joint probability function calculated from
% the columns of input matrix.
% 
% Y     - input matrix (each column corresponds to particular observation)
% prec  - precision of calculation; if prec = -1 then the precision will be
%         maximal (default: depending on the length of vector)
% p     - vector with probabilities
% uY    - unique rows in matrix Y after quantization

%% Paths and variables
if((nargin < 2) || isempty(prec))
    if(size(Y,1) < 64)
        prec = 32;
    elseif(size(Y,1) < 128)
        prec = 64;
    else
        prec = 256;
    end
end

%% Quantize the input sequences
if(prec > 0)
    for i = 1:size(Y,2)
        Y(:,i) = quantiz(Y(:,i),min(Y(:,i)):range(Y(:,i))/prec:max(Y(:,i)));
        Y(Y(:,i) == 0, i) = 1;
    end
end

%% Calculate the joint probability
[uY, tmp2, n] = unique(Y,'rows');

p = hist(n,max(n));
p = p./size(Y,1);