## Author

## Keywords

Matlab, Objective Function Rendering.

## Review Status

*Unreviewed*

# Tip

## Introduction

In this short piece is demonstrated the means to render a 3D objective function in Matlab. It aims not only to give a clear, instant visual idea of the objective function but also create a quality image that is suitable for inclusion in research papers.

Part 1 extracts specific data required for rendering, which is then processed so that objective function can be calculated for the sample.

Part 2 shows how to cover the entire objective function space with parameter values drawn at regular intervals and create the main data structures required to render the objective function graph.

Part 3 demonstrates the Matlab graph functions to create the objective function image, and uses the 'camera' view utility to obtain a clear view of the landscape. This is output to an .eps file.

## The Example

The example shown is for creating the objective function for Least Medium of Squares as an estimation tool and used in connexion with the CAPM. (For a detailed account of this work see, Least Median of Squares Estimation by Optimization Heuristics with an Application to CAPM.)

## Code Description

First we read in the data from the .mat file. For this example we extract the returns for a particular stock - IBM in this example - from the Dow Jones Index and select the first 200 days for processing.

```
%Read in the data and take a subset of observations from a stock.
load DJIA30;
noOfObs = length(DJIA30.Date);
ibm = 14;
SECTOR = ibm;
% Daily return differnces
dj_return = log(DJIA30.AdjPrice(2:noOfObs,31)) - log(DJIA30.AdjPrice(1:noOfObs-1,31));
ibm_return = log(DJIA30.AdjPrice(2:noOfObs,SECTOR)) - log(DJIA30.AdjPrice(1:noOfObs-1,SECTOR));
%Compound Interest rate calculated to give a daily rate
rs = (1+DJIA30.AdjPrice(2:noOfObs,32)).^(1/250) - 1;
xx = dj_return(1:noOfObs-1) - rs(1:noOfObs-1);
yy = ibm_return(1:noOfObs-1) - rs(1:noOfObs-1);
%Subset of observations
start = 1;
n = 200;
x = xx(start:n);
y = yy(start:n);
```

Next we store the results of objective function calculations from a set of paired values for alpha and beta. We store this in a 2D matrix, where the dimensions n and m are equal to the length of the alpha and beta set.

```
%These are the number of points in our alpha and beta set
cells = 50;
%This is the size of the interval between the boundary values for alpha and beta
a_span = 0.02;
b_span = 1;
%Calculate the increments between alpha and beta boundary values
a_inc = a_span/cells;
b_inc = b_span/cells;
%Generate arrays for alpha and beta
Alpha = -0.01:a_inc:0.01;
Beta = 1:b_inc:2;
%Initialise the 2D matrix for the objective function calculation
LMS = [cells,cells];
%Calculate the objective function for each alpha and beta combination, write the result
%to the objective function matrix.
for i=1:cells+1
for j=1:cells+1
LMS(j,i) = LMedSq(x,y,Alpha(i),Beta(j));
end
end
```

We now produce a 3D graph of objective function landscape and output the graph to an .eps file in the home directory.

```
%Render the graph
meshc(Alpha,Beta,LMS);
%Set the x (alpha),y (beta) , z (objective function) axis boundary limits
z_low = 4 * 10^-5;
z_hi = 8.6 * 10^-5;
set(gca,'ZLIM',[z_low,z_hi]);
x_low = -0.01;
x_hi = 0.01;
set(gca,'XLIM',[x_low,x_hi]);
y_low = 1;
y_hi = 2;
set(gca,'YLIM',[y_low,y_hi]);
%Assign the view point position - this is a trial and error process to get the 'best' view
azimuth = 2;
elevation = 26;
%Set the position
view([azimuth elevation])
%Write the graph labels in Latex - add a title as required
ylabel('\textbf{$\beta$}','fontsize',16, 'interpreter','latex');
xlabel('\textbf{$\alpha$}','fontsize',16, 'interpreter','latex');
zlabel('\textbf{med$(\varepsilon_{t}^2)$}','fontsize',16, 'interpreter','latex');
%Make the graph monochrome so that it is nice and clear
colormap(gray)
%Output the result to file
print -deps ssr.eps;
```

Here is the output. One important point to note for such graphs: they are only an illustration of the objective function landscape. They appear to show that the function values for the grid points are connected by a smooth surface, but this is not the case as even between the grid points further local minima/maxima might exist.

# Internal Links

Concepts |

Tutorials |

Tips |

Related Articles |

# External links

References |

Weblinks |