Economic and Game Theory
|"Inside every small problem is a large problem struggling to get out."|
Thread and Full Text View
function [predx,ftop, score,lscore] = binpredict(x,numpred);
% [predx,ftop, score,lscore] = binpredict(x,numpred); % % purpose % % predict a binary string. % % inputs % % x: a vector of 1's and 0's, this is what we are trying to predict % numpred: number of realizations of the prediction; default = 1; % % outputs % % predx: a (N) x (numpred) matrix of predictions, where each column is a realization. % ftop: a vector of the probability of seeing a 1 at each time point % score: probability of a correct choice % lscore: computed probabilities of a correct choice for each realization % the mean of lscore asymptotially approaches score as we increase numpred. % % reference: % % D. Fudenberg and D.K. Levine % Conditional Universal Consistency % Games and Economic Behavior, 29::104-130, 1999. % % web site link (with Java source codes) is % % http://levine.sscnet.ucla.edu//Games/binlearn.htm % % send comments to firstname.lastname@example.org
if ~exist('numpred'); numpred = 1; end
winlen = 2; % this is how many previous samples we use to predict the next one.
x = x(:);
N = length(x); predx = NaN*ones(N,numpred);
num_entries = 2^winlen; dict = zeros(num_entries,2); % stores number of ones that follow
binmult = 2.^((winlen -1):-1:0);
% initialize the dictionary in the same way that Levine's program does dict(:,2) = 2*ones(num_entries,1); % total number of observations so far dict(1,1) = 0; dict(2,1) = 1; dict(3,1) = 1; dict(4,1) = 2;
ftop = NaN*ones(N,1); % column of probabilities of getting a 1.
% lag laglag init_state = 3; % initially assume a [1 0] pattern. present_state_index = init_state; lscore = 0; % initial score for dind = 1:N; % make the prediction if dind == 1 % first prediction is based on probability of 0.5 of getting a 1. ftop(dind) = 0.5; else past_state_index = present_state_index; n = dict(past_state_index,1) ;% number of ones following a state Nobs = dict(past_state_index,2); % number of observations ftop(dind) = exp(4*n/Nobs) / (exp(4*n/Nobs) + exp(4*(Nobs-n)/Nobs)); end
predx(dind,:) = rand(1,numpred) > (1-ftop(dind)); % this updates the dictionary % the previous state with the current data point. % e.g. if state 2 ([0 1]) is followed by a 1, it adds a one to % the number of ones that follow state 2. dict(present_state_index,1) = dict(present_state_index,1) + x(dind); dict(present_state_index,2) = dict(present_state_index,2) + 1; if dind > 1 present_state_index = binmult*x(dind + (0:-1:(-winlen+1))) + 1; else present_state_index = binmult*[x(dind) 1]' + 1; end
end xcompare = x(:)*ones(1,numpred); score = (sum(predx == xcompare))/N; lscore = mean(ftop.*x(:) + (1-ftop).*(1-x(:)));