Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
39 changes: 39 additions & 0 deletions ProblemSets/pset1/Denis Sokolov/PS1/Matlab/CVfun.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function MSE = CVfun(data,bw,k,num)
% bw - bandwith
% k - # of data bids for CV
% num - number of the bid for test data

l = length(data);
ind1Train = ceil((l/k)*(num-1))+1;
ind2Train = floor((l/k)*num);
train = data(ind1Train:ind2Train); %train data
test = data(setdiff(1:l,ind1Train:ind2Train)); %test data

train = sort(train);
pdEp = fitdist(train,'kernel','Kernel','epanechnikov','BandWidth',bw);
pEp = pdf(pdEp,train); %estimated pdf on train data
%--------------
% we need indexes i s.t. train(i) = train(i-1) (we cant have same x points for griddedInterpolant(x,y))
t1 = train(2:end) - train(1:end-1);
indx = find(t1==0)+1; % needed indexes
indx1 = setdiff(1:length(train),indx);
train1 = train(indx1);
pEp1 = pEp(indx1);
%--------------
F = griddedInterpolant(train1,pEp1);
fun = @(t) F(t); % estimated pdf on train data as function

b = 5; % # of bids for MSE

[N,edges] = histcounts(test,b);
edges = [min(train) edges(2:end-1) max(train)]; % edges for integration for MSE
testN = sum(N); % # of test obs
N = N/testN; % N as fractions

MSE = 0;
for i = 1:b
q = integral(fun, edges(i), edges(i+1));
MSE = MSE + (q - N(i))^2;
end

end
38 changes: 38 additions & 0 deletions ProblemSets/pset1/Denis Sokolov/PS1/Matlab/CVfun.m~
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
function MSE = CVfun(data,bw,k,num)
% bw - bandwith
% k - # of data clusters for CV
% num - number of the cluster for test data
testShare = 1/k;
l = length(data);

train = data(1:end*(1-testShare)); %train data
test = data(end*(1-testShare)+1:end); %test data

train = sort(train);
pdEp = fitdist(train,'kernel','Kernel','epanechnikov','BandWidth',bw);
pEp = pdf(pdEp,train); %estimated pdf on train data
%--------------
% we need indexes i s.t. train(i) = train(i-1) (we cant have same x points for griddedInterpolant(x,y))
t1 = train(2:end) - train(1:end-1);
indx = find(t1==0)+1; % needed indexes
indx1 = setdiff(1:length(train),indx);
train1 = train(indx1);
pEp1 = pEp(indx1);
%--------------
F = griddedInterpolant(train1,pEp1);
fun = @(t) F(t); % estimated pdf on train data as function

b = 9; % # of bids for MSE

[N,edges] = histcounts(test,b);
edges = [min(train) edges(2:end-1) max(train)]; % edges for integration for MSE
testN = sum(N); % # of test obs
N = N/testN; % N as fractions

MSE = 0;
for i = 1:b
q = integral(fun, edges(i), edges(i+1));
MSE = MSE + (q - N(i))^2;
end

end
83 changes: 83 additions & 0 deletions ProblemSets/pset1/Denis Sokolov/PS1/Matlab/PS1.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
clear all
close all

bids = csvread('bids1.csv',0,0);
histogram(bids,9,'Normalization','probability');
x = sort(bids);
p = normpdf(x,mean(bids),var(bids)^(1/2));
hold on
del = 2.6;
plot(x,p/del,'Color','g','LineStyle','-');

bw = 0.2;

pdEp = fitdist(bids,'kernel','Kernel','epanechnikov','BandWidth',bw);
pEp = pdf(pdEp,x);
plot(x,pEp/del,'Color','r','LineStyle','-.');

pdGa = fitdist(bids,'kernel','BandWidth',bw);
pGa = pdf(pdGa,x);
plot(x,pGa/del,'Color','b','LineStyle','--');

%% Cross Validation

%s = rng;
%save('randomiser.mat','s')
load('randomiser.mat');
rng(s);
bidsRand = bids(randperm(length(bids)));
k = 10; % # of bids in CV

func = @(bw) kCVfun(bidsRand,bw,k);

options = optimset('MaxFunEval', 1e6, 'MaxIter', 1e5, 'TolFun', 1e-10);
bwCV = fminsearch(func,0.05,options);

pdEpCV = fitdist(bids,'kernel','Kernel','epanechnikov','BandWidth',bwCV);
pEpCV = pdf(pdEpCV,x);
plot(x,pEpCV/del,'Color','r','LineStyle','--');

hname = {'histogram','normal' 'epanechnikov' 'gaussian' 'CV-epanechnikov'};
legend(hname);

title('Bids PDF')
xlabel('Bid')
ylabel('pdf')
%% Valuations

n = 3;
gB = pEpCV; %PDF for bids distr
GB = cumtrapz(x,gB); %CDF for bids distr
v = x + GB./((n-1)*gB);
figure
histogram(v,15,'Normalization','probability');
hold on
pdEpV = fitdist(v,'kernel','Kernel','epanechnikov','BandWidth',1);
pEpV = pdf(pdEpV,sort(v));
plot(sort(v),pEpV,'Color','r','LineStyle','-');

y = wblpdf(sort(v),3.5,2);
plot(sort(v),y,'Color','g','LineStyle','-')

hname = {'histogram' 'epanechnikov' 'weibull'};
legend(hname);

title('Valuations PDF')
xlabel('Valuation')
ylabel('pdf')
















54 changes: 54 additions & 0 deletions ProblemSets/pset1/Denis Sokolov/PS1/Matlab/PS1.m~
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
clear all
close all

bids = csvread('bids1.csv',0,0);
histogram(bids,8,'Normalization','probability');
x = sort(bids);
p = normpdf(x,mean(bids),var(bids)^(1/2));
hold on
plot(x,p/2.5,'Color','r','LineStyle','-');

bw = 0.2;

pdEp = fitdist(bids,'kernel','Kernel','epanechnikov','BandWidth',bw);
pEp = pdf(pdEp,x);
plot(x,pEp/2.5,'Color','g','LineStyle','-.');

pdGa = fitdist(bids,'kernel','BandWidth',bw);
pGa = pdf(pdGa,x);
plot(x,pGa/2.5,'Color','b','LineStyle','--');

hname = {'histogram','normal' 'epanechnikov' 'gaussian'};
legend(hname);

%% Cross Validation

s = rng;
save('randomiser.mat','s')
load('randomiser.mat');
rng(s);
bidsRand = bids(randperm(length(bids)));
k = 10; % # of bids in CV

func = @(bw) kCVfun(bidsRand,bw,k);

options = optimset('MaxFunEval', 1e6, 'MaxIter', 1e5, 'TolFun', 1e-10);
bwEst = fminsearch(func,0.1,options);


















Loading