Home > data-analysis > stats > cstream > cstream_bigram_matrix.m

cstream_bigram_matrix

PURPOSE ^

cstream_bigram_matrix Generate a bigram matrix of the input cstream.

SYNOPSIS ^

function [matrix condi_p_row, condi_p_col] = cstream_bigram_matrix(cstream, flag_draw)

DESCRIPTION ^

 cstream_bigram_matrix   Generate a bigram matrix of the input cstream. 
                         Bigram is two consecutive symbols in the given cstream.

 matrix = cstream_bigram_matrix(cstream);

 [matrix condi_p_row condi_p_col] = cstream_bigram_matrix(cstream);

 [matrix condi_p_row condi_p_col] = cstream_bigram_matrix(cstream, flag_draw);
 
 cstream:     n*2 matrix; thr first column is time stamp and the seond column
              is category data.
 flag_draw:   ture - draw the figures of three output matrix; 
              false - don't draw them.
 matrix:      Each element in the bigram matrix is the count of corresponding brigram.
              For example, if matrix(3,7) = 14, the bigram, '37', appears 14 tims in the given cstream.
 condi_p_row: the conditional probablity, p(X2|X1), where X1X2 is bigram in
              cstream and X1 appears right before X2.
 condi_p_col: the conditional probablity, p(X1|X2), where X1X2 is bigram in
              cstream and X1 appears right before X2.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [matrix condi_p_row, condi_p_col] = cstream_bigram_matrix(cstream, flag_draw)
0002 % cstream_bigram_matrix   Generate a bigram matrix of the input cstream.
0003 %                         Bigram is two consecutive symbols in the given cstream.
0004 %
0005 % matrix = cstream_bigram_matrix(cstream);
0006 %
0007 % [matrix condi_p_row condi_p_col] = cstream_bigram_matrix(cstream);
0008 %
0009 % [matrix condi_p_row condi_p_col] = cstream_bigram_matrix(cstream, flag_draw);
0010 %
0011 % cstream:     n*2 matrix; thr first column is time stamp and the seond column
0012 %              is category data.
0013 % flag_draw:   ture - draw the figures of three output matrix;
0014 %              false - don't draw them.
0015 % matrix:      Each element in the bigram matrix is the count of corresponding brigram.
0016 %              For example, if matrix(3,7) = 14, the bigram, '37', appears 14 tims in the given cstream.
0017 % condi_p_row: the conditional probablity, p(X2|X1), where X1X2 is bigram in
0018 %              cstream and X1 appears right before X2.
0019 % condi_p_col: the conditional probablity, p(X1|X2), where X1X2 is bigram in
0020 %              cstream and X1 appears right before X2.
0021 %
0022 
0023 % check for special cases
0024 if isempty(cstream)
0025     matrix = {};
0026     condi_p_row = {};
0027     condi_p_col = {};
0028     return;
0029 end
0030 
0031 if ~exist('flag_draw','var')
0032     flag_draw = false;
0033 end
0034 
0035 data = cstream(:,2);
0036 
0037 if min(data) < 0
0038     error('cstream should not contain value that is less than 0');
0039 end
0040 
0041 % calculate bigram frequency matrix from data ( cstream(:,2) )
0042 matrix = get_bigram_matrix(data);
0043 
0044 % calculate conditonal probability from bigram frequency matrix
0045 [condi_p_row condi_p_col] = get_condi_p(matrix);
0046 
0047 % draw figures;
0048 if flag_draw
0049     draw_matrix(matrix, 'bigram frequency');
0050     draw_matrix(condi_p_row, 'conditional probability for each row');
0051     draw_matrix(condi_p_col, 'conditional probability for each column');
0052 end
0053 
0054 end % function cstream_bigram_matrix
0055 
0056 % calculate bigram frequency matrix from data
0057 function matrix = get_bigram_matrix(data)
0058 cnum = max(data);
0059 matrix = zeros(cnum, cnum);
0060 for i=2:length(data)
0061     if data(i-1) > 0 && data(i) >0   % 0 means no data or we don't care about it.
0062         matrix(data(i-1),data(i)) = matrix(data(i-1),data(i)) + 1;
0063     end
0064 end
0065 end
0066 
0067 % calculate conditonal probability from bigram frequency matrix
0068 function [condi_p_row condi_p_col] = get_condi_p(matrix)
0069 row_sum = sum(matrix, 2);
0070 col_sum = sum(matrix, 1);
0071 [row_num col_num] = size(matrix);
0072 
0073 condi_p_row = matrix ./ repmat(row_sum,1,col_num);
0074 condi_p_col = matrix ./ repmat(col_sum,row_num,1);
0075 end
0076 
0077 % draw figure for a matrix
0078 function draw_matrix(matrix, map_title)
0079 figure;
0080 imshow(matrix);
0081 title(map_title);
0082 end

Generated on Wed 24-May-2017 00:00:56 by m2html © 2005