forked from gilnoh/gigaword-lm-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweighted_sum.m
36 lines (28 loc) · 945 Bytes
/
weighted_sum.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
function probsum = weighted_sum (X)
% calculates weighted sum,
% gets an input matrix with two columns: X
% column 1: document_prob (weight vector, per doc, weight as logprob)
% column 2: sequence_prob (prob per doc, as logprob)
%% POSSIBLE IMPROVEMENT? this is a "for loop" function. Can't we do this
%% somehow in matrix operation? hmm. Not trivial, but might be possible.
result = 0; % careful not to pass zero to logprob_sum.
col1_sum = 0;
for i=X' % for each row
doc_log_prob = i(1);
seq_log_prob = i(2);
this_log_prob = doc_log_prob + seq_log_prob;
if (result == 0)
result = this_log_prob;
else
result = logprob_sum(result, this_log_prob);
endif
if (col1_sum == 0)
col1_sum = doc_log_prob;
else
col1_sum = logprob_sum(col1_sum, doc_log_prob);
endif
endfor
% Ok, now divide it with column1 sum
probsum = result - col1_sum;
%probsum = result;
end