Simulation & Probabilistic Analysis SDK
Add Distribution Fitting & Simulation Features to Your Applications

SPA SDK Help HomeLearn More About the SPA SDK

Distribution Fitting Results

After the distributions are fitted, you can extract the results, such as the best fitting distribution model, as well as the distribution parameters and goodness of fit results for all the fitted models.

How to Determine the Best Fitting Distribution

The distribution fitting function SPADataSet.Fit returns an array of objects:

  Dim fitResults() As Variant
  Dim count As Long

  ' run the fit  
  count = dataSet.Fit(SPADistAll, fitResults)
  

The fitting results array elements have the type SPAFitResult, and the return value of the fitting method indicates the size of this array. The array is sorted accordingly to the ranking criteria specified using the SPAApplication.FitOptions.RankBy property, so the zeroth array element contains the information about the best fitting distribution:

  Dim bestFit As SPAFitResult
  
  If count > 0 And fitResults(0).Status = SPAStatusOk Then
    Set bestFit = fitResults(0)

    ' show the best fitting distribution name
    MsgBox bestFit.Distribution.Name
  End If
  

How to Extract the Fitting Results

The SPAFitResult object provides the following properties:

#
Property name
Description
1
SPAFitResult.Status Indicates whether the distribution was successfully fitted.
2
SPAFitResult.Rank The goodness of fit rank of the distribution among all the fitted models.
3
SPAFitResult.Distribution The actual distribution object that can be used to obtain the distribution type and parameter values.
4
SPAFitResult.GofValid Indicates whether a specific goodness of fit test was successfully applied to the distribution.
5
SPAFitResult.GofStatistic
SPAFitResult.GofPValue
SPAFitResult.GofCriticalValue
SPAFitResult.GofRejectNull
Goodness of fit testing results.

Distribution Fitting Example

The following Visual Basic example shows how you can use these properties in your applications:

Sub Test()
  Dim dataSet As New SPADataSet
  Dim fitResults() As Variant
  Dim result As SPAFitResult
  Dim dist As SPADistribution
  Dim i, j, count As Long
  Dim s As String

  SPAApplication.Initialize

  ' specify the data
  data(0) = 1.5
  data(1) = 3.2
  data(2) = 9.6
  data(3) = 4.1
  data(4) = 7.8

  ' assign the data
  Call dataSet.Assign(SPADomainContinuous, SPADataSample, data, empty)

  ' sort the results by distribution name
  SPAApplication.FitOptions.RankBy = SPARankByName

  ' run the fit  
  count = dataSet.Fit(SPADistAll, fitResults)

  ' iterate over the fitted distributions
  For i = 0 To count - 1
    Set result = fitResults(i)
    Set dist = result.Distribution

    ' check whether this is a good fit
    If result.Status = SPAStatusOk Then
       s = "Good Fit: " & dist.Name & vbCr & vbCr
       s = s & "Parameters: " & vbCr

       ' distribution parameters
       For j = 0 To dist.ParamCount - 1
         s = s & dist.ParamName(j) & "=" & dist.Param(j) & " "
       Next

       ' Kolmogorov-Smirnov goodness of fit test
       If result.GofValid(SPAGofTestKS) Then
         Dim rank, alpha, statistic, pValue, cValue As Double
         Dim reject As Boolean
         
         ' confidence level (the supported values are: 0.2; 0.1; 0.05; 0.02; 0.01)
         alpha = 0.05
         
         ' extract the results
         rank = result.rank(SPARankByKS)
         statistic = result.GofStatistic(SPAGofTestKS)
         pValue = result.GofPValue(SPAGofTestKS)
         cValue = result.GofCriticalValue(SPAGofTestCS, alpha)
         reject = result.GofRejectNull(SPAGofTestKS, alpha)
       
         s = s & vbCr & vbCr & "Kolmogorov-Smirnov GOF Test:" & vbCr
         s = s & "Rank: " & rank & vbCr
         s = s & "Statistic: " & statistic & vbCr
         s = s & "P-value: " & pValue & vbCr
         s = s & "Critical value at alpha=0.05: " & cValue & vbCr
         s = s & "Accept or reject the Null hypothesis at alpha=0.05: "
         If reject Then
           s = s & "REJECT"
         Else
           s = s & "ACCEPT"
         End If
       Else
         MsgBox "The Kolmogorov-Smirnov test could not be performed with this distribution"
       End If
       
       ' show the results
       MsgBox s
    Else
       MsgBox "Bad Fit: " & dist.Name
    End If
  Next
End Sub
  
Copyright © MathWave Technologies
www.mathwave.com