function score = score_pst(pst, data, model_score)

X = data(:,2:end-1);
y = data(:,1);
counts = data(:,end);
m = size(X,1); 
K = length(pst); % number of nodes

% for each data point find the node under which it falls
data2node.node     = zeros(1,m);
data2node.node_len = -ones(1,m);
for i=1:K
  nx = min(length(pst{i}.x),size(X,2)); % length of history
  if nx == 0
      I = 1:m;
  else
      I  = find((sum(abs(X(:,1:nx)-repmat(pst{i}.x(1:nx),[m 1])),2)==0));
  end
  
  % exclude matches that matched a longer node
  J = I(find(nx > data2node.node_len(I)));

  data2node.node(J) = i;
  data2node.node_len(J) = nx;
end

% compute n_plus and n_minus
n_plus  = zeros(1,K);
n_minus = zeros(1,K);

for i = 1:m
    if y(i) == 1
        n_plus(data2node.node(i)) = n_plus(data2node.node(i)) + counts(i); 
    else
        n_minus(data2node.node(i)) = n_minus(data2node.node(i)) + counts(i); 
    end
end

% discard cases in which n_plus = n_minus = 0
I = (n_plus ~= 0) | (n_minus ~= 0);
n_plus = n_plus(I);
n_minus = n_minus(I);

% sorting is necessary only to prevent some numerical anomalies
[n_, I] = sort(n_plus + n_minus);
n_plus = n_plus(I);
n_minus = n_minus(I);

score = feval(model_score, n_plus, n_minus);
