clear; format compact;
oD=dlmread('out2.txt');

%remove those lengths that are years
oD(find(and(oD(:,2)>1900,oD(:,2)<2005)),:)=[];
%Get rid of extreme outliers
normD = oD(find(oD(:,2)<200),:);
longD = normD(find(normD(:,2)>60),:);
shortD = normD(find(normD(:,2)<60),:);

nrMovies = length(oD)
nrMoviesLessThan200Min = length(normD)
nrMoviesBetween60And200Min = length(longD)
nrMoviesLessThan30Min = length(shortD)

minYear = min(oD(:,1))
maxYear = max(oD(:,1))
maxMovie = max(oD(:,2))


%------------------------------------------------------------
%  Make pretty 3D plot
%------------------------------------------------------------
figure(1); clf
[bins, xcoords, ycoords] = hist3(normD, 100, 50);
[XX YY] = meshgrid(xcoords, ycoords);
surfl(XX, YY, bins)
colormap(jet)
f = findobj('Type','surface');
set(f,'FaceLighting','phong');

material shiny
shading interp
light
set(gca, 'FontSize', 14)
set(gca, 'FontWeight', 'demi')
xlabel('Runtime [minutes]') 
ylabel('Year of Production')
zlabel('Number of Movies')
axis([0 200 1900 2000 0 1000])
print -djpeg 3dhistogram.jpg

%------------------------------------------------------------
% Average Runtime per year
%------------------------------------------------------------
i=1;j=1;
binSize=5;
for year=minYear:binSize:maxYear
  vals1=longD(find(and(longD(:,1)>=year,longD(:,1)<year+binSize)),2);
  vals2=oD(find(and(oD(:,1)>=year,oD(:,1)<year+binSize)),2);
  if length(vals1)>0
    avgs1(i)=mean(vals1);
    stdev1(i)=std(vals1);
    years1(i)=year+binSize/2;
    i=i+1;
  end
  avgs2(j)=median(vals2);
  stdev2(j)=std(vals2);
  years2(j)=year+binSize/2;
  j=j+1;


end

figure(2); clf
H1 = errorbar(years1, avgs1,stdev1, '-o'); hold on
H2 = errorbar(years2, avgs2,stdev2, 'r-o');
set(gca, 'FontSize', 14)
set(gca, 'FontWeight', 'demi')
set(gca, 'LineWidth', 2)
set([H1(1), H2(1)], 'LineWidth', 1)
set([H1(2), H2(2)], 'LineWidth', 2)
xlabel('Year of Production') 
ylabel('Runtime [minutes]')
legend([H1(2), H2(2)], 'Mean (movies between 60 and 200 min)', 'Median (all movies)',4)
print -djpeg avgruntime.jpg



%------------------------------------------------------------
% Number of produced films per year
%------------------------------------------------------------
i=1;

years=minYear:maxYear-3;
for year=years
  nFilms(i) = length(find(oD(:,1)==year));
  nShorts(i) = length(find(shortD(:,1)==year));
  i=i+1;
end
  
figure(3); clf
H = plot(years, nFilms, years, nShorts);
set(gca, 'FontSize', 14)
set(gca, 'FontWeight', 'demi')
set(gca, 'LineWidth', 2)
set(H, 'LineWidth', 2)
xlabel('Year of Production') 
ylabel('Number of Movies Produced')
legend('All Movies', 'Short Movies (<60min)')
print -djpeg nrmoviesmade.jpg

