Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Collections.Concurrent
- Imports System.Threading
- Public Class Form1
- Const TCOUNT As Integer = 100000
- Dim rBag As ConcurrentBag(Of Double)
- Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
- rBag = New ConcurrentBag(Of Double)
- Dim swTimer As Stopwatch = Stopwatch.StartNew()
- Tasks.Parallel.For(1, 100001, Sub(i As Integer)
- rBag.Add(NormalFunctions.StandardNormalInverse(0.00001 * (CInt(i) - 0.5)))
- End Sub)
- Do Until rBag.Count = TCOUNT
- Thread.Sleep(0)
- Loop
- swTimer.Stop()
- MessageBox.Show("Run time = " & swTimer.ElapsedMilliseconds.ToString & " miliseconds.")
- End Sub
- End Class
- Public Class NormalFunctions
- Private Shared ReadOnly k As Double = 0.2316419
- Private Shared ReadOnly a() As Double = {0.31938153, -0.356563782, 1.781477937, -1.821255978, 1.330274429}
- Private Shared Function Density(x As Double) As Double
- Return Math.Pow(2 * Math.PI, -0.5) * Math.Exp(-0.5 * x * x)
- End Function
- Private Shared Function StandardNormal(x As Double) As Double
- Dim L As Double, q As Double, nx As Double, snd As Double, j As Integer, s As Double
- L = Math.Abs(x)
- q = 1 / (1 + k * L)
- nx = Density(L)
- 'nx == f(abs(x))
- For j = 0 To 4
- s = s + a(j) * q ^ (j + 1)
- Next j
- snd = 1 - nx * s
- 'SND = PHI(abs(x))
- If (x < 0) Then
- snd = 1 - snd
- End If
- 'NOW SND = PHI(x)
- Return snd
- End Function
- Public Shared Function StandardNormalInverse(r As Double) As Double
- Dim Delta As Double, X As Double, Y As Double
- X = 0
- Delta = 1
- Do Until Delta <= 0.000000001
- Y = X - (StandardNormal(X) - r) / Density(X)
- Delta = Math.Abs(Y - X)
- X = Y
- Loop
- Return Y
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement