%% Summarizing Data
% This example shows how to summarize data.
%% Overview
% Many MATLAB® functions enable you to summarize the overall location, scale, and shape of a data sample.
% One of the advantages of working in MATLAB® is that functions operate on entire arrays of data, not just on single scalar values. The functions are said to be vectorized. Vectorization allows for both efficient problem formulation, using array-based data, and efficient computation, using vectorized statistical functions.
%% Measures of Location
% Summarize the location of a data sample by finding a "typical" value. Common measures of location or "central tendency" are computed by the functions mean, median, and mode:

load count.dat
x1 = mean(count)
x2 = median(count)
x3 = mode(count)

% Like all of its statistical functions, the MATLAB® functions above summarize data across observations (rows) while preserving variables (columns). The functions compute the location of the data at each of the three intersections in a single call.
%
%% Measures of Scale
% There are many ways to measure the scale or "dispersion" of a data sample. The MATLAB® functions max, min, std, and var compute some common measures:

dx1 = max(count)-min(count)
dx2 = std(count)
dx3 = var(count)

% Like all of its statistical functions, the MATLAB® functions above summarize data across observations (rows) while preserving variables (columns). The functions compute the scale of the data at each of the three intersections in a single call.
%
%% Shape of a Distribution
% The shape of a distribution is harder to summarize than its location or scale. The MATLAB® hist function plots a histogram that provides a visual summary:

figure
hist(count)
legend('Intersection 1',...
       'Intersection 2',...
       'Intersection 3')

%
% Parametric models give analytic summaries of distribution shapes. Exponential distributions, with parameter mu given by the data mean, are a good choice for the traffic data:

c1 = count(:,1); % Data at intersection 1
[bin_counts,bin_locations] = hist(c1);
bin_width = bin_locations(2) - bin_locations(1);
hist_area = (bin_width)*(sum(bin_counts));

figure
hist(c1)
hold on

mu1 = mean(c1);
exp_pdf = @(t)(1/mu1)*exp(-t/mu1); % Integrates
                                   % to 1
t = 0:150;
y = exp_pdf(t);
plot(t,(hist_area)*y,'r','LineWidth',2)
legend('Distribution','Exponential Fit')

% Methods for fitting general parametric models to data distributions are beyond the scope of this section. Statistics and Machine Learning Toolbox™ software provides functions for computing maximum likelihood estimates of distribution parameters.
% Copyright 2015 The MathWorks, Inc.