COMPUTER PROBLEM-SOLVING IN - Oakland University



Computer Problem-Solving in EGR 141

Engineering and Computer Science Fall 2006

PROBLEM-SOLVING EXERCISE #4

SIMULATION - SOLUTION

AS WE’VE PREVIOUSLY SEEN, EQUATIONS DESCRIBING SITUATIONS OFTEN CONTAIN UNCERTAIN PARAMETERS, THAT IS, PARAMETERS THAT AREN’T NECESSARILY A SINGLE VALUE BUT INSTEAD ARE ASSOCIATED WITH A PROBABILITY DISTRIBUTION FUNCTION. WHEN MORE THAN ONE OF THE VARIABLES IS UNKNOWN, THE OUTCOME IS DIFFICULT TO VISUALIZE. A COMMON WAY TO OVERCOME THIS DIFFICULTY IS TO SIMULATE THE SCENARIO MANY TIMES AND COUNT THE NUMBER OF TIMES DIFFERENT RANGES OF OUTCOMES OCCUR. ONE SUCH POPULAR SIMULATION IS CALLED A MONTE CARLO SIMULATION. IN THIS PROBLEM-SOLVING EXERCISE YOU WILL DEVELOP A PROGRAM THAT WILL PERFORM A MONTE CARLO SIMULATION ON A SIMPLE PROFIT FUNCTION.

CONSIDER THE FOLLOWING TOTAL PROFIT FUNCTION:

PT = NPV

WHERE PT IS THE TOTAL PROFIT, N IS THE NUMBER OF VEHICLES SOLD AND PV IS THE PROFIT PER VEHICLE.

PART A

COMPUTE 5 ITERATIONS OF A MONTE CARLO SIMULATION GIVEN THE FOLLOWING INFORMATION:

n follows a uniform distribution with minimum of 1 and maximum 10

Pv follows a normal distribution with a mean of $4000 and a standard deviation of $1000

Number of bins: 10

Recall that for all practical purposes we will use 3 std. deviations from the mean as the maximum value for parameters following a normal distribution. Obviously, 5 iterations are not very many. In fact, typically you would simulate 10,000 iterations or so to view meaningful results but I figured that I’d give you a break (. If you’d like to compute 10,000 iterations by hand for extra credit, go ahead…

i. What are the ranges for the 10 bins?

n : nmin = 1 , nmax = 10

Pv: Pvmin = $1000, Pvmax = $7,000

Ptmin = nmin * Pvmin = $1000

Ptmax = nmax * Pvmax = $70,000

Range/Bin = (70000 – 1000)/10 = 6,900

Bin 1: 1000 – 7900

Bin 2: 7901 – 14800

Bin 3: 14801 – 21700

Bin 4: 21701 – 28600

Bin 5: 28601 – 35500

Bin 6: 35501 – 42400

Bin 7: 42401 – 49300

Bin 8: 49301 – 56200

Bin 9: 56201 – 63100

Bin 10: 63101 – 70000

ii. Fill in the table below:

|Parameter |Iteration 1 |Iteration 2 |Iteration 3 |Iteration 4 |Iteration 5 |

|n |2 |7 |9 |4 |5 |

|Pv |$3100 |$4500 |$3400 |$6500 |$2200 |

|PT |$6200 |$31500 |$30600 |$26000 |

|6: 0 |7: 0 |8: 0 |9: 0 |10: 0 |

Part B

WRITE THE FOLLOWING THREE FUNCTIONS IN A MODULE CALLED SIMULATE:

PUBLIC FUNCTION GETRANDOMUNIFORM(BYVAL MIN AS INTEGER, BYVAL MAX AS INTEGER) AS INTEGER

‘ THIS FUNCTION RETURNS A RANDOM NUMBER FROM A UNIFORM DISTRIBUTION BETWEEN MIN AND MAX.

PUBLIC FUNCTION GETRANDOMNORMAL(BYVAL MEAN AS SINGLE, BYVAL STDDEV AS SINGLE) AS SINGLE

‘ THIS FUNCTION RETURNS A RANDOM NUMBER FROM A NORMAL DISTRIBUTION WITH A MEAN OF MEAN AND STANDARD

‘ DEVIATION OF STDDEV

PUBLIC FUNCTION GETBININDEX(BYVAL MINI AS SINGLE, BYVAL MAXI AS SINGLE, BYVAL NUMBINS AS _

INTEGER, BYVAL VALUETOBIN AS SINGLE) AS INTEGER

‘ THIS FUNCTION RETURNS THE BIN INDEX GIVEN AN OUTPUT MINIMUM OF MINI, OUTPUT MAXIMUM OF MAXI,

‘ NUMBINS NUMBER OF BINS, AND A VALUE TO BIN OF VALUETOBIN

SEE PART D FOR CODE.

PART C

INCLUDE THE MODULE CREATED IN PART B TO DEVELOP A VISUAL BASIC .NET PROGRAM THAT WILL SIMULATE THE BASIC PROFIT CALCULATION, PT = NPV, WHERE N FOLLOWS A UNIFORM DISTRIBUTION, PV FOLLOWS A NORMAL DISTRIBUTION, AND THE USER CAN INPUT THE NUMBER OF BINS AND NUMBER OF ITERATIONS. THE USER MUST ALSO INPUT THE MIN AND MAX FOR N AND THE MEAN AND STANDARD DEVIATION FOR PV. FINALLY, THE USER CAN CLICK A BUTTON AND THE RESULTS WILL BE GRAPHED ON A BAR CHART USING THE MICROSOFT CHART CONTROL. TURN IN A LISTING OF THE CODE AND A SCREEN SHOT OF THE RESULTING CHART USING:

1. ITERATIONS: 10000 BINS: 5 N-MIN: 1 N-MAX: 10 PV-MEAN: 9000 PV-STDDEV: 2000

2. ITERATIONS: 10000 BINS: 10 N-MIN: 1 N-MAX: 10 PV-MEAN: 9000 PV-STDDEV: 2000

3. ITERATIONS: 10000 BINS: 10 N-MIN: 1 N-MAX: 10 PV-MEAN: 6000 PV-STDDEV: 1000

THAT’S THREE SCREEN SHOTS AND A LISTING.

SEE PART D FOR CODE.

PART D

EXTEND THE VISUAL BASIC .NET PROGRAM DEVELOPED IN PART C TO SIMULATE THE BASIC PROFIT CALCULATION, PT = NPV, WHERE THE USER CAN SELECT EITHER A UNIFORM OR NORMAL DISTRIBUTION FOR N USING RADIO BUTTONS AND THEN MUST INPUT THE APPROPRIATE PARAMETERS (MIN AND MAX IF THEY SELECT UNIFORM, MEAN AND STANDARD DEVIATION IF THEY SELECT NORMAL) AND THEY CAN SIMILARLY SELECT EITHER A UNIFORM OR NORMAL DISTRIBUTION FOR PV WITH APPROPRIATE PARAMETERS DEPENDING ON THE SELECTION. OF COURSE, THE USER WILL INPUT THE NUMBER OF BINS AND NUMBER OF ITERATIONS. FINALLY, THE USER CAN CLICK A BUTTON AND THE RESULTS WILL BE GRAPHED ON A BAR CHART USING THE MICROSOFT CHART CONTROL. TURN IN A LISTING OF THE CODE AND A SCREEN SHOT OF THE RESULTING CHART USING:

1. ITERATIONS: 10000 BINS: 5 N-MIN: 1 N-MAX: 10 PV-MEAN: 8000 PV-STDDEV: 2000

2. ITERATIONS: 10000 BINS: 10 N-MEAN: 9 N-STDDEV: 2 PV-MIN: 2000 PV-MAX: 10000

3. ITERATIONS: 10000 BINS: 10 N- MEAN: 14 N- STDDEV: 3 PV-MIN: 2000 PV-MAX: 10000

THAT’S THREE SCREEN SHOTS AND A LISTING.

CODE LISTING FOR PARTS C, D AND E

'FORM FILE: FRMSIM.VB

'Date: March 31, 2005

'Description: This application runs a Monte Carlo Simulation on a

' simple profit function.

' This code is used for Parts C, D and E.

' For Part C, the radio buttons for Pv uniform and N normal

' have their visible property set to False so as not to be

' accessible to the user.

' For Part D and E, these buttons are reset to visible =

' =True.

Private Sub cmdSIM_Click(ByVal sender As System.Object,

ByVal e As System.EventArgs)

Handles cmdSIM.Click

Dim NumBins, n, x, i As Integer

Dim Pt, Pv As Single

Dim Ptmin, Ptmax, Pvmin, Pvmax As Single

Dim nmax, nmin, nbins, cycles As Integer

Dim Bins() As Integer 'Array holds the frequency of profit total

nbins = Val(txtBins.Text)

cycles = Val(TextBox1.Text)

txtTotalProfit.Text = 0 'reset the total profit

If rdoNuni.Checked = True Then 'uniform distribution

'txtN1 is maximum

'txtN2 is minimum

nmin = Val(txtN2.Text)

nmax = Val(txtN1.Text)

Else 'rdoNnor is checked and normal distribution

'txtN1 is mean

'txtN2 is stddev

nmin = Val(txtN1.Text) - 3 * Val(txtN2.Text)

nmax = Val(txtN1.Text) + 3 * Val(txtN2.Text)

End If

If rdoPvuni.Checked = True Then ' uniform distribution

'Pv1 is maximum

'Pv2 is minimum

Pvmin = Val(txtPv2.Text)

Pvmax = Val(txtPv1.Text)

Else 'rdoPvnor is checked and normal distribution

'Pv1 is mean

'Pv2 is stddev

Pvmin = Val(txtPv1.Text) - 3 * Val(txtPv2.Text)

Pvmax = Val(txtPv1.Text) + 3 * Val(txtPv2.Text)

End If

Ptmin = nmin * Pvmin

Ptmax = nmax * Pvmax

ReDim Bins(nbins + 1)

For x = 1 To cycles

If rdoNuni.Checked = True Then 'uniform

n = GetRandomUniform(nmin, nmax)

Else 'rdoNnor is checked then normal

n = GetRandomNormal(Val(txtN1.Text), Val(txtN2.Text))

End If

If n > nmax Then 'Value must be placed in last bin

n = nmax

End If

If rdoPvuni.Checked = True Then 'uniform

'Pvmax is maximum

'Pvmin is minimum

Pv = GetRandomUniform(Pvmin, Pvmax)

Else 'rdoPvnor is checked then normal

'txtPv1 is mean

'txtPv2 is stddev

Pv = GetRandomNormal(Val(txtPv1.Text), Val(txtPv2.Text))

End If

If Pv > Pvmax Then

Pv = Pvmax

End If

'Total profit calculation

Pt = n * Pv

'Keep track of Pt for aveage calculation

txtTotalProfit.Text += Pt

i = GetBinIndex(Ptmin, Ptmax, nbins, Pt)

If i > nbins Then

MessageBox.Show("2Index is out of Bounds")

Exit For

End If

Bins(i) += 1

Next

'Now chart the results of the simulation

MSChart1.RowCount = nbins

MSChart1.ColumnCount = 1

For i = 1 To nbins

MSChart1.Row = i

MSChart1.Data = Bins(i)

'Row label will show the maximum of each bin

MSChart1.RowLabel = Ptmin + (((Ptmax - Ptmin) / nbins) * (i))

Next

'Average Total Profit calculation

txtTotalProfit.Text /= cycles

txtTotalProfit.Text = FormatNumber(txtTotalProfit.Text, 2)

End Sub

Private Sub rdoPvuni_CheckedChanged(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles rdoPvuni.CheckedChanged

If rdoPvuni.Checked = True Then

lblMax1.Visible = True

lblMin1.Visible = True

lblMean1.Visible = False

lblStddev1.Visible = False

End If

End Sub

Private Sub rdoPvnor_CheckedChanged(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles rdoPvnor.CheckedChanged

If rdoPvnor.Checked = True Then

lblMax1.Visible = False

lblMin1.Visible = False

lblMean1.Visible = True

lblStddev1.Visible = True

End If

End Sub

Private Sub cmdExit_Click(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles cmdExit.Click

End

End Sub

Private Sub rdoNuni_CheckedChanged(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles rdoNuni.CheckedChanged

If rdoNuni.Checked = True Then

lblMax2.Visible = True

lblMin2.Visible = True

lblMean2.Visible = False

lblStdDev2.Visible = False

End If

End Sub

Private Sub rdoNnor_CheckedChanged(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles rdoNnor.CheckedChanged

If rdoNnor.Checked = True Then

lblMax2.Visible = False

lblMin2.Visible = False

lblMean2.Visible = True

lblStdDev2.Visible = True

End If

End Sub

Private Sub frmSIM_Load(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles MyBase.Load

'This clears the form of the chart for startup

MSChart1.RowCount = 0

End Sub

MODULE SIMULATE

Public Function GetRandomUniform(ByVal min As Integer,

ByVal max As Integer) As Integer

'Function generates a uniformly distributed random number between min

‘and max.

Dim u As Integer

Randomize()

u = Int(min + Rnd() * (max - min + 1))

'u is a uniformly distributed random number

Return u

End Function

Public Function GetRandomNormal(ByVal Mean As Single,

ByVal StdDev As Single) As Single

'Function generates a random number on a normal distribution with

‘mean as Mean and standard deviation as StdDev.

Dim r, phi, x, z As Single

Randomize()

r = Rnd()

phi = Rnd()

z = Math.Cos(2 * 3.14159 * r) * Math.Sqrt(-2 * Math.Log(phi))

'z is a random number on the standard normal distribution

x = (z * StdDev) + Mean

'x is a random number on the normal disrtibution of interest

Return x

End Function

Public Function GetBinIndex(ByVal mini As Single, ByVal maxi As Single,

ByVal numbins As Integer, ByVal ValueToBin As Single) As Integer

'Function generates a bin index for the bin array to track frequency

'of occurance of Pt in the simulation.

Dim q As Double

Dim qc As Double

'ValueToBin is Pt

'mini is Ptmin

'maxi is Ptmax

'numbins is nbins

q = CDbl(ValueToBin - mini) * (numbins / (maxi - mini))

qc = Math.Ceiling(q)

If qc > numbins Then

MessageBox.Show("1Index is out of Bounds " & qc)

End If

'qc is the index for the Bins array

Return qc

End Function

End Module

PART E

YOU’RE GOING TO GO TO A JOB INTERVIEW FOR OU CAR CO. KNOWING THAT THE FIELD IS HIGHLY COMPETITIVE, YOU HAVE RUN SALES SCENARIOS AHEAD OF TIME EXPERIMENTING WITH DIFFERENT NUMBERS OF CUSTOMERS AND VEHICLE PROFITS GIVEN THAT IN ONE MONTH OU CAR CO. SELLS BETWEEN 4 AND 10 CARS UNIFORMLY DISTRIBUTED WITH PROFITS OF $4,000 ON THE AVERAGE WITH STANDARD DEVIATION OF $900. WHICH HAS A HIGHER PAYOFF, FOCUSING ON SELLING TO A COUPLE MORE CUSTOMERS OR BY INCREASING THE AVERAGE SALE (WITH THE SAME STD. DEV. OF $900) BY RETRAINING YOUR SALES FORCE OR DO THEY HAVE BASICALLY THE SAME EFFECT ON TOTAL SALES? SUPPORT YOUR ANSWER.

SOLUTION:

NOTE: RESULTS FOR PTAVE WILL VARY DUE TO BEING GENERATED BY RANDOM NUMBERS.

THEREFORE, RESULTS CAN BE IN A REASONABLE RANGE FROM WHAT IS SHOWN HERE.

BASELINE

FIRST, A BASELINE IS ESTABLISHED, BASED UPON THE ABOVE GIVEN INFORMATION.

PV = PROFIT PER VEHICLE

FITS NORMAL DISTRIBUTION WITH MEAN OF $4000 AND A STANDARD DEVIATION OF $900

N = NUMBER OF VEHICLES SOLD

FITS A UNIFORM DISTRIBUTION WITH A MAXIMUM OF 10 AND A MINIMUM OF 4

YIELDS PTAVE = AVERAGE PROFIT TOTAL = $28,901.00

CASE 1: 10.0 % INCREASE IN EITHER PV OR N

PVMEAN = $4400 ALL OTHER INPUTS REMAIN AT BASELINE INPUT LEVEL

YIELDS PTAVE = AVERAGE PROFIT TOTAL = $30.881.86

NMAX = 11 ALL OTHER INPUTS REMAIN AT BASELINE INPUT LEVEL

YIELDS PTAVE = AVERAGE PROFIT TOTAL = $30,112.84

THE INCREASE IN PV YIELDS A RESULTING PTAVE $769.02 HIGHER

THAN THE INCREASE IN N.

CASE 2: 20.0 % INCREASE IN EITHER PVMEAN OR NMAX

PVMEAN = $4800 ALL OTHER INPUTS REMAIN AT BASELINE INPUT LEVEL

YIELDS PTAVE = AVERAGE PROFIT TOTAL = $33,686.49

NMAX = 12 ALL OTHER INPUTS REMAIN AT BASELINE INPUT LEVEL

YIELDS PTAVE = AVERAGE PROFIT TOTAL = $32,095.11

THE INCREASE IN PV YIELDS A RESULTING PTAVE $1591.38 HIGHER

THAN THE INCREASE IN N..

CONTINUED ON NEXT PAGE

CASE 3: 30.0 % INCREASE IN EITHER PV OR N

PVMEAN = $5200 ALL OTHER INPUTS REMAIN AT BASELINE INPUT LEVEL

YIELDS PTAVE = AVERAGE PROFIT TOTAL = $36,473.37

NMAX = 15 ALL OTHER INPUTS REMAIN AT BASELINE INPUT LEVEL

YIELDS PTAVE = AVERAGE PROFIT TOTAL = $34,115.03

THE INCREASE IN PV YIELDS A RESULTING PTAVE $2358.34 HIGHER

THAN THE INCREASE IN N.

IN CONCLUSION:

FROM THESE SCENARIOS, AN INCREASE IN EITHER PV OR N WILL INCREASE PTAVE.

HOWEVER, INCREASING PV HAS A GREATER IMPACT ON THE AVERAGE TOTAL PROFIT (PTAVE) FROM THE BASELINE THAN A SIMILAR INCREASE IN THE NUMBER OF VEHICLES SOLD (N). SO FROM THESE THREE CASES EFFORTS TO INCREASE THE AVERAGE SALE (PV) WOULD BE RECOMMENDED.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download