KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Vector Utility Class for Financial and Statistical Calculations – Part 3
PRODUCT: 4D | VERSION: 21 | PLATFORM: Mac
Published On: March 31, 2026
Building on Part 1 and Part 2, we continue extending the native 4D.Vector class with the lightweight utility class VectorUtility.
In this part we add two very practical functions:

calculateVariance
calculateStdDev

These functions let you quickly measure the dispersion or spread of any collection of numbers.

property defaultDimension : Integer

Class constructor

 This.defaultDimension:=3

...

Function calculateVariance($values : Collection) : Real
  If ($values.length<2)
    return 0
  End if
  var $average : Real
  $average:=$values.average()
  var $sumOfSquares : Real
  $sumOfSquares:=0
  var $value : Real
  For each ($value; $values)
    $sumOfSquares:=$sumOfSquares+(($value-$average)^2)
  End for each
  $values.length

Function calculateStdDev($values : Collection) : Real
  return Square root(This.calculateVariance($values))


Example: Measuring consistency of processing times


var $util : cs.VectorUtility
$util:=cs.VectorUtility.new()

//collection of numeric values: processing times in seconds
var $values : Collection
$values:=New collection(45; 52; 38; 61; 49; 55; 42; 58; 47; 50)

var $variance : Real
$variance:=$util.calculateVariance($values)
var $standardDeviation : Real
$standardDeviation:=$util.calculateStdDev($values)

  • Average = 49.7 seconds

  • Variance = 45.61 => It is the average of the squared differences between each processing time and the mean 49.7 seconds. In other words, it quantifies how spread out the values are, the higher the number, the greater the variation.

  • Standard deviation = 6,753517601961 seconds =>most processing times differ by about 6.75 seconds above or below the average. A smaller standard deviation means more consistent results or data.