Write the following three functions in a module called Simulate: Public Function
ID: 3805156 • Letter: W
Question
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 Include the module created in part B to develop a Visual Basic .NET program that will simulate the basic profit calculation, P_T = nP_v, where n follows a uniform distribution, P_v 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 P_v. Finally, the user min can click a button and the results will be graphed on a bar chart using the Microsoft Chart Control and the average total profit (P_T) will be displayed in a textbox. Turn in a screen shot of the resulting chart using: 1. Iterations: 10000 Bins: 5 n-min: 1 n-max: 10 P_v-mean: 9000 P_v-stddev: 1200 2. Iterations: 10000 Bins: 10 n-min: 1 n-max: 10 P_v-mean: 9000 P_v-stddev: 1200 3. Iterations: 10000 Bins: 10 n-min: 1 n-max: 10 P_v-mean: 7000 P_v-stddev: 1500 That's three screen shots. Extend the Visual Basic .NET program developed in part C to simulate the basic profit calculation, P_T = nP_v, 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 P_v 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 and the average total profit (P_T) will be displayed in a textbox. Also include in the program any necessary input validation for all input values. 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 P_v-mean: 10000 P_v-stddev: 2500 2. Iterations: 10000 Bins: 10 n-mean: 6 n-stddev: 1 P_v-min: 1500 P_v-max: 9000 3. Iterations: 10000 Bins: 10 n-mean: 10 n- stddev: 3 P_v-min: 1500 P_v-max: 10000 That's three screen shots and a listing.Explanation / Answer
Part B : following three functions module
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 C and D: code for part c and d
'Description: This application runs a Monte Carlo Simulation on a ' simple profit function. ' This code is used for Parts C and D .
' 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 , these buttons are reset to visible = ' =True.
Private Sub basPRT_Cal(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles basPRT.Cal
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 SubPrivate 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 basExit_Cal(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles basExit.Cal
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 SubPrivate 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 frmPRT_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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.