Advertisement
Guest User

Untitled

a guest
Sep 14th, 2014
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Imports System.Collections.Concurrent
  2. Imports System.Threading
  3.  
  4. Public Class Form1
  5.     Const TCOUNT As Integer = 100000
  6.  
  7.     Dim rBag As ConcurrentBag(Of Double)
  8.  
  9.  
  10.     Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
  11.         rBag = New ConcurrentBag(Of Double)
  12.  
  13.         Dim swTimer As Stopwatch = Stopwatch.StartNew()
  14.  
  15.         Tasks.Parallel.For(1, 100001, Sub(i As Integer)
  16.                                           rBag.Add(NormalFunctions.StandardNormalInverse(0.00001 * (CInt(i) - 0.5)))
  17.                                       End Sub)
  18.  
  19.         Do Until rBag.Count = TCOUNT
  20.             Thread.Sleep(0)
  21.         Loop
  22.  
  23.         swTimer.Stop()
  24.  
  25.         MessageBox.Show("Run time = " & swTimer.ElapsedMilliseconds.ToString & " miliseconds.")
  26.     End Sub
  27.  
  28. End Class
  29.  
  30.  
  31. Public Class NormalFunctions
  32.     Private Shared ReadOnly k As Double = 0.2316419
  33.     Private Shared ReadOnly a() As Double = {0.31938153, -0.356563782, 1.781477937, -1.821255978, 1.330274429}
  34.  
  35.     Private Shared Function Density(x As Double) As Double
  36.         Return Math.Pow(2 * Math.PI, -0.5) * Math.Exp(-0.5 * x * x)
  37.     End Function
  38.  
  39.     Private Shared Function StandardNormal(x As Double) As Double
  40.         Dim L As Double, q As Double, nx As Double, snd As Double, j As Integer, s As Double
  41.  
  42.         L = Math.Abs(x)
  43.         q = 1 / (1 + k * L)
  44.         nx = Density(L)
  45.         'nx == f(abs(x))
  46.  
  47.         For j = 0 To 4
  48.             s = s + a(j) * q ^ (j + 1)
  49.         Next j
  50.  
  51.         snd = 1 - nx * s
  52.         'SND = PHI(abs(x))
  53.  
  54.         If (x < 0) Then
  55.             snd = 1 - snd
  56.         End If
  57.         'NOW SND = PHI(x)
  58.  
  59.         Return snd
  60.     End Function
  61.  
  62.     Public Shared Function StandardNormalInverse(r As Double) As Double
  63.         Dim Delta As Double, X As Double, Y As Double
  64.  
  65.         X = 0
  66.         Delta = 1
  67.         Do Until Delta <= 0.000000001
  68.             Y = X - (StandardNormal(X) - r) / Density(X)
  69.             Delta = Math.Abs(Y - X)
  70.             X = Y
  71.         Loop
  72.  
  73.         Return Y
  74.     End Function
  75.  
  76.  
  77. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement