KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: 4D.Vector Class: Anomaly Detector Utility Method
PRODUCT: 4D | VERSION: 20 R | PLATFORM: Mac & Win
Published On: January 7, 2026
The 4D.Vector class excels in high dimensional numeric computations, making it ideal for advanced anomaly detection in time-series datasets (e.g., sensor readings, logs, or financial metrics). By treating sequences as vectors, developers can use Euclidean distance or dot similarity to identify outliers

Sensors table




Util_detectAnomalies

#DECLARE($series : Collection; $baseline : 4D.Vector; $threshold : Real)\
  ->$anomalies : Collection

$anomalies:=New collection

var $paramCount : Integer
$paramCount:=Count parameters
If ($paramCount<3)
   $threshold:=2.5
End if

var $i : Integer
var $point : Object
For each ($point; $series; $i)
  var $pointVector : 4D.Vector
  If (OB Instance of($point; 4D.Object))
   if (OB Instance of($point.readingVector; 4D.Vector))
    $pointVector:=$point.readingVector
   Else
    continue
   End if
  Else
   continue
  End if

  If ($pointVector.length=$baseline.length)
   var $distance : Real
   $distance:=$pointVector.euclideanDistance($baseline)
   If ($distance>$threshold)
    $anomalies.push(New object("index"; $i; "vector"; $pointVector; "distance"; \
     $distance; "original"; $point))
   End if
  Else

   continue
  End if
End for each

$anomalies:=$anomalies.orderBy("distance desc")


Test Exemple

var $baseline : 4D.Vector
$baseline:=4D.Vector.new([22.5; 1013.2; 45.8])
var $readings : Collection
$readings:=ds.Sensors.all().toCollection("readingVector")

var $outliers : Collection
$outliers:=Util_detectAnomalies($readings; $baseline; 3)