matlab for marketing people

38
MATLAB for Marketing People Toshi Takeuchi

Upload: toshi-takeuchi

Post on 05-Dec-2014

443 views

Category:

Marketing


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Matlab for marketing people

MATLAB for Marketing PeopleToshi Takeuchi

Page 2: Matlab for marketing people

MATLAB = MATrix LABoratoryWhy should you care?

Excel is super intuitive and useful if:● The data fits the computer screen● You can manipulate data manually● Your analysis is relatively simple.Excel can be a nightmare if:● The data is too large to fit the computer

screen or manipulate directly● Your analysis is complex and needs

debugging

Page 3: Matlab for marketing people

You already use matrices and vectors in Excel

This is a matrix● has rows and columns

This is a vector● only one row or column

They contain numbers● numbers are in each cell

Page 4: Matlab for marketing people

MATLAB also has rows and columns like Excel

Functions

Functions

Data

Data

More focus on direct data manipulation

More focus on manipulating data via code

Page 5: Matlab for marketing people

MATLAB and Excel = BFF

Import data

Export data

Page 6: Matlab for marketing people

Before we start:MATLAB user interface

Command Window: where you type your command and run your code

Workspace: where your data

lives as variables

Editor, but we don’t use it in our example here, to keep it simple.

Command History, but we don’t use it in our example here

Files, but we don’t use it our examplehere.

Page 7: Matlab for marketing people

Motivating Example: Price modeling for a new mobile app

A small scale market research was done for a new mobile phone app that recognizes music playing in the background Learn more

After the service was demonstrated, we asked:“How much are you willing to pay for a monthly subscription?”

Note: this was back in 2003 or 2004, when people still paid for such apps.

Page 8: Matlab for marketing people

Creating the sample dataset in MATLAB

Copy and Paste this:>> Price = [0;100;200;300];

>> Responses = [304;336;215;145];

Two vectors are now in the Workspace

Double click

Page 9: Matlab for marketing people

Data is stored as matrices/vectors in MATLAB

A matrix with 3 rows, 2 columns3x2 matrix

A column vector with 3 rows, 1 column3x1 vector

A row vector with 1 row, 3 columns1x3 vector

Numbers inside are called “elements”

Page 10: Matlab for marketing people

Referencing elements/cells in MATLAB and Excel

How to reference an element in a matrix “M”

How to reference a cell in Excel

Column 1

Row 2

Page 11: Matlab for marketing people

Referencing elements in vectors in MATLAB

Referencing an element in column vector V1

Referencing an element in row vector V2

You only need one index for vectors

Indexing starts with 1 for both matrices and vectors

Page 12: Matlab for marketing people

Price modeling exampleFind the responses for Price=100

Hint: vectors Price and Responses share the same indexing

Print Price>> Price

Index into Responses>> Responses(2)

Answer: 336

It’s the second element

Variable contents are printed when variable names are entered without an ending semicolon.

Page 13: Matlab for marketing people

What functions do you use in Excel often?

Sum, Average, Count, Max, Min?How do you do these in MATLAB?

First, create a new matrix:>> M = [1, 2; 3, 4; 5, 6]

● Use [ ] to start and end the matrix or vector● Use comma or space to separate elements● Use semicolons to start a new row● Content will be displayed when you enter this

without an ending semicolon.

Page 14: Matlab for marketing people

Functions in MATLAB work on columns and rows

Sum ≈ Sum

Average ≈ MeanBy columns By rows Both

By columns By rows Both

Page 15: Matlab for marketing people

Functions in MATLAB work on columns and rows (continued)

Max ≈ Max

Min ≈ MinBy columns By rows Both

By columns By rows Both

Page 16: Matlab for marketing people

Functions in MATLAB work on columns and rows (continued)

Count ≈ Size

Count ≈ Numel

Both Rows Columns

All elements

● size gives you the dimensions – the number of rows and columns for matrices

● length does the same for vectors● numel gives you the number of elements,

regardless of dimensions

Page 17: Matlab for marketing people

Practical Advice: Organize your data well

As in Pivot Tables in Excel, data organization is a key in MATLAB and many other languages. MATLAB functions operate on columns by default, so:● Each column should represent one label ● Each row should represent one sample

Data Label 1 Data Label 2 Data Label ..n

Data Sample 1

Data Sample 2

Data Sample...m

Page 18: Matlab for marketing people

Price modeling exampleFind total number of responses

How large was this survey, in terms of number of responses?

You can just sum Responses>> sum(Responses)

Answer: 1000

So it is a fairly decent size.

Page 19: Matlab for marketing people

Using Matrices and VectorsMatrix Addition/Subtraction

First, create new matrices:>> A = [3,8;1,7;1,3];

>> B = [9,4;1,7;4,8];

How do you do A+B? By adding/subtracting corresponding elements.

Just A+B or A-B in MATLAB

The dimensions must match between two matrices.

Page 20: Matlab for marketing people

Matrix Scalar Multiplication/Division

How do you do A×3 or A÷3?By multiplying/dividing each element.

3×A? same. Also A÷3 = A×1/3 = 1/3×A

Scalar = just a number, ≠ a matrix or vector

Just A*3 or A/3 in MATLAB

Page 21: Matlab for marketing people

Price modeling exampleConvert responses to ratios

We want to express the responses in terms of percentages to the total: “at price = x, y% of people were willing to pay”.

You can just divide each element by the total. >> shares = Responses/sum(Responses)

Store the result in a new variable “shares”

Page 22: Matlab for marketing people

Price modeling exampleEstimate the subscriber demand

Assumption: we can use cumulative sum. ● If you are willing to pay 100, you will still use

the service at 90, 80, or any lower price.● So at 90, we get both people who are willing

to pay 100 as well as 90, and so forth. Price Share of responses Cumulative sum

0 0.304 0.145+0.215+0.336+0.304

100 0.336 0.145+0.215+0.336

200 0.215 0.145+0.215

300 0.145 0.145

Page 23: Matlab for marketing people

Price modeling example (continued)Estimate the subscriber demand

How to do it in MATLAB1. cumsum sums it in a wrong order, so flip the

vector first with flipud

>> subscribers = cumsum(flipud(shares))

2. The result is sorted in wrong order, so flipud again.

>> subscribers =flipud(subscribers)

3. The result is now stored in subscribers

Page 24: Matlab for marketing people

Price modeling example (continued)Plot the subscriber demand

plot takes vectors for x-axis and y-axis, so use Price for x-axis and subscribers for y-axis.>> plot(Price, subscribers)

We got a nice straight line.

Our pricing model will be a linear equation!

Page 25: Matlab for marketing people

Linear Equation Review

y = αx + β where α = slope, β = interceptExample: α = -0.25, β = 1

Calculate y for x =

>> y = -0.25*x+1

Hard-coding parameter values like α and β in your code makes it less flexible, however.

Page 26: Matlab for marketing people

Using Matrices and VectorsMatrix Vector Multiplication

First, create a new vector:>> B = [2;3];

How do you do A×B? Go row by row with A: Row 1: 3×2+8×3=6+24=30Row 2: 1×2+7×3=2+21=23Row 3: 1×2+3×3=2+ 9=11

It’s simple in MATLAB

Page 27: Matlab for marketing people

Matrix Vector Multiplication (continued)

Pay attention to the dimensions!3x2 matrix 2x1 vector 3x1 vector

4x3 matrix 3x1 vector 4x1 vector

m x n matrix × n x 1 vector = m x 1 vector

Questions:1. What do you get if

you multiply 5x2 matrix with 2x1 vector?

2. What happens when you multiply 7x3 matrix with 4x1 vector?

Answers:1.5x1 vector2.Dimension mismatch error

“n” must match.

Page 28: Matlab for marketing people

Linear Equation RevisitedApply Matrix Vector Multiplication

y = αx + β where α = -0.25, β = 1

Calculate y for x = , p =

Add Intercept , then do >> y = x*p

This column was added to represent the intercept

1×-0.25+1×1=0.752×-0.25+1×1=0.503×-0.25+1×1=0.25

Parameters are now a variable, too!

x: became 3x2 matrixp: 2x1 matrix

● Now the dimensions match!● Multiplying intercept by 1

doesn’t change the value

Page 29: Matlab for marketing people

Linear EquationHow to do this in MATLAB

To add the intercept term to x:1. ones(3,1) creates a 3x1 vector of 1’s – same

as [1;1;1]

2. Concatenate it with x using [x ones(3,1)]

3. Now you are ready to apply multiplication

>> y = [x ones(3,1)]*p

This makes the code more general and run faster.

Page 30: Matlab for marketing people

Using Matrices and VectorsMatrix Matrix Multiplication

First, create a new matrix:>> B = [2,1;3,4];

How do you do A×B? Break it down to matrix vector combinations: First

Then

In MATLAB,

same as before

>> A*B

Page 31: Matlab for marketing people

Matrix Matrix Multiplication (continued)

Pay attention to the dimensions!3x2 matrix 2x2 vector 3x2 vector

2x3 matrix 3x2 vector 2x2 vector

m x n matrix × n x o matrix = m x o matrix

Questions:1. What do you get if

you multiply 5x2 matrix with 2x3 vector?

2. What happens when you multiply 5x3 matrix with 2x3 matrix?

Answers:1.5x3 matrix2.Dimension mismatch error

“n” must match.

Page 32: Matlab for marketing people

Linear Equation RevisitedApply Matrix Matrix Multiplication

y = αx + β, but now we have multiple possible parameters.

No problem! Use matrix matrix multiplication.

P =

The code is still x*p, but it is now really flexible :)

Intercept term added

Page 33: Matlab for marketing people

Transpose

You now know the dimensions matter in matrix calculationsMultiply a 3x2 matrix with another 3x2 matrix? This won’t work, unless you can flip the second one into 2x3 matrix. This flipping is called “transpose”, and you will use this trick a lot in MATLAB.Transpose of A is AT,or A' in MATLAB

Page 34: Matlab for marketing people

Price modeling exampleEstimate parameters from the data

You can apply linear regression to estimate the parameters α and β, using polyfit (n=1 for linear).>> [p_fit, Stats] = polyfit(Price,subscribers,1)

α = -0.0029 β = 0.9854

Pricing Model: y = -0.0029x+0.9584

Page 35: Matlab for marketing people

Price modeling examplePlot the estimate against the data

Compare the estimate to the data

>> y_fit =[Price ones(4,1)]*p_fit'

>> plot(Price,subscribers)

>> hold on

>> plot(Price,y_fit,'r')

>> hold off

The estimate fits well with the data! So our pricing model is fairly decent.

Page 36: Matlab for marketing people

The next step

Now you understand why you want to use matrices and vectors for large datasets.

To actually learn how to use MATLAB, it is easier if you watch tutorial videos.

For tutorial videos for beginners, go to www.youtube.com/user/MATLAB

Enjoy!

Page 37: Matlab for marketing people

Appendix I Summary of the code used

Price modeling example >> Price = [0;100;200;300];>> Responses = [304;336;215;145];>> shares = Responses/sum(Responses);>> subscribers = cumsum(flipud(shares));>> subscribers = flipud(subscribers);>> plot(Price,subscribers)>> [p_fit, Stats] = polyfit(Price,subscribers,1);>> y_fit =[Price ones(4,1)]*p_fit';>> hold on>> plot(Price,y_fit,'r')>> hold off

Page 38: Matlab for marketing people

Appendix IIAn optional assignment

Figure out the revenue-maximizing price from this pricing model with MATLAB

HintsRevenue curve should looklike this. You need more data points to plot a smooth curve Type >> 1:10 in MATLAB and see what happens.