Code covered by the BSD License  

Highlights from
Easy Sensitivity (Tornado) Plot Function

image thumbnail
from Easy Sensitivity (Tornado) Plot Function by Richard McCulloch
This function makes a tornado sensitivity plot and returns the sensitivity values.

TorPlot(data,names,sensitivity,save)
function [low,high] = TorPlot(data,names,sensitivity,save)
% TorPlot  A function for generating tornado plots. This program calculates
% a baseline value by summing the vector 'data'. Each element of 'data' is
% then changed by a certain sensitivity value and the elements in 'data'
% are summed again, with only the current single element having been
% changed.
%
% Sensitivity plots are useful in assessing importance and weight in
% engineering systems. Preliminary assessments benefit from sensitivity
% analysis, mainly because they highlight the driving factors.
%
%   INPUTS:
%       data = baseline data entered as a vector
%       names = tick mark names for the y axis
%       sensitivity = amount each element is individually changed
%       save = flag determining whether or not to save the plot
%
%   OUTPUTS:
%       low = lower sensitivities
%       high = higher sensitivies
%
%   EXAMPLE:
%       
%       data=[0.24,2.55,0.60,0.02,0.09,0.36,0.18];
%       names = {'Distribution';'Crude';'Refinery';'Storage';
%           'Sales Tax';'State Excise Tax';'Federal Excise Tax'};
%       [low, high] = TorPlot(data, names, 0.15, true);
%
%   Copyright 2013 Richard C.J. McCulloch
%   $Revision.1 $  $Date: 27-Jun-2013 $

if isempty(data) 
    error(message('No data was passed to the function TorPlot')); 
end

if nargin < 4, save=0; % if save isn't specified, don't save
    if nargin < 3, sensitivity=0.2; % if no sensitivity is given assume 20%
        if nargin < 2
            names = repmat(char(0),length(data),1);
        end
    end
end

% Initialize low and high matricies for speed
Objective_low=zeros(1,length(names));
Objective_high=zeros(1,length(names));
Objective_low_sum=zeros(1,length(names));
Objective_high_sum=zeros(1,length(names));
low=zeros(1,length(names));
high=zeros(1,length(names));

% Calculate the base change due to a single factor at a time
for i=1:length(names)
    Objective_low=data;
    Objective_high=data;
    Objective_low(i)=Objective_low(i)*(1-sensitivity);
    Objective_high(i)=Objective_high(i)*(1+sensitivity);
    low(i)=Objective_low(i);
    high(i)=Objective_high(i);
    Objective_low_sum(i)=sum(Objective_low);
    Objective_high_sum(i)=sum(Objective_high);
end

% The base value is where the y axis is centered
Objective_base_value=sum(data);

% Sort the values based on the lower change
% Sort the higher values and the names arrays
%    using the same indices
[Objective_low_sum,ind]=sort(Objective_low_sum,'descend');
Objective_high_sum=Objective_high_sum(ind);
names_Objective=names(ind);

% Create a figure and plot the low and high horizontally
figure
h = barh(Objective_high_sum);
hold on
barh(Objective_low_sum,'r')
bh = get(h,'BaseLine');
set(bh,'BaseValue',Objective_base_value);
title('Sensitivities')
if nargin > 1
    set(gca,'yticklabel',names)
    set(gca,'Ytick',[1:length(names)],'YTickLabel',[1:length(names)])
    set(gca,'yticklabel',names_Objective)
end
xlabel('Objective')
if(save)
    saveas(gcf,'Objective.png')
end

Contact us