How to Calculate Normalized MSE in NS2

To Calculate the Normalized Mean Squared Error (NMSE) in ns2, it is often utilized to measure the precision of estimation or expected values that related to the original or expected value. NMSE is often used parameters in wireless communications, machine learning, and sensor networks, in which we need to evaluate on how well the network is achieves in terms of estimation, prediction, or data reconstruction. NMSE is estimated by normalizing the Mean Squared Error (MSE) with the variance of the original values.

The formula for NMSE is:

NMSE=MSEVariance of Actual Data\text{NMSE} = \frac{\text{MSE}}{\text{Variance of Actual Data}}NMSE=Variance of Actual DataMSE​

Where:

  • MSE is the Mean Squared Error, calculated as: MSE=1n∑i=1n(yi−y^i)2\text{MSE} = \frac{1}{n} \sum_{i=1}^{n} (y_i – \hat{y}_i)^2MSE=n1​i=1∑n​(yi​−y^​i​)2 where yiy_iyi​ is the actual value, y^i\hat{y}_iy^​i​ is the predicted or estimated value, and nnn is the number of data points.

Steps to Calculate NMSE in NS2:

To compute NMSE, we will need two sets of data:

  1. Actual values: These are the true values, like the real packet delivery ratio (PDR), delay, or any other metric.
  2. Predicted or estimated values: These are the values calculated by a predictive model, techniques, or another node in the network.
  1. Collecting Data in NS2:

Example Scenario:

Let’s assume we are replicating a wireless network in which nodes expect the packet delivery ratio (PDR) over time, and we need to estimate the NMSE among the original and predicted PDR values.

we can adapt the NS2 TCL script to log the original and expected values in the course of the simulation.

TCL Script to Collect Actual and Predicted Data:

# Create the simulator

set ns [new Simulator]

# Create nodes and set up the scenario as usual

# …

# Variables to store actual and predicted values

set actualPDRList {}

set predictedPDRList {}

# Function to store actual and predicted PDR values at time t

proc logPDR {actual predicted} {

global actualPDRList predictedPDRList

lappend actualPDRList $actual

lappend predictedPDRList $predicted

}

# Schedule logging of actual and predicted PDR at intervals

$ns at 1.0 “logPDR 0.95 0.90”  ;# Example: actual PDR = 0.95, predicted PDR = 0.90

$ns at 2.0 “logPDR 0.93 0.92”

$ns at 3.0 “logPDR 0.90 0.88”

# …

# End the simulation

proc finish {} {

global actualPDRList predictedPDRList

puts “Actual PDRs: $actualPDRList”

puts “Predicted PDRs: $predictedPDRList”

exit 0

}

# End simulation at time 10

$ns at 10.0 “finish”

  1. Calculating MSE and NMSE:

Once the original and expected values are logged in the course of the simulation, we can either estimate MSE and NMSE physically or by using a script. Below is an sample of how to compute MSE and NMSE using AWK or external tools such as Python.

Example AWK Script for MSE and NMSE Calculation:

BEGIN {

n = 0

mse = 0

actual_sum = 0

predicted_sum = 0

}

# Read the actual and predicted PDR values

{

actual = $1

predicted = $2

error = actual – predicted

mse += error * error

actual_sum += actual

predicted_sum += predicted

n++

}

END {

mse = mse / n  # Calculate the Mean Squared Error

# Calculate variance of the actual values

actual_mean = actual_sum / n

variance = 0

for (i = 1; i <= n; i++) {

variance += ($1 – actual_mean)^2

}

variance = variance / n

# Calculate NMSE

if (variance > 0) {

nmse = mse / variance

} else {

nmse = “undefined (variance is 0)”

}

print “MSE: ” mse

print “NMSE: ” nmse

}

To utilize this AWK script, save the actual and predicted values in a file, say data.txt, and execute the AWK script as follows:

awk -f calculate_nmse.awk data.txt

This script will estimate the MSE and NMSE for the deliverd actual and predicted values.

  1. Using Python for NMSE Calculation:

We can also utilize Python for a more upfront and resilience estimation of NMSE. Here’s an ample Python script:

import numpy as np

# Example actual and predicted data

actual_values = [0.95, 0.93, 0.90]

predicted_values = [0.90, 0.92, 0.88]

# Calculate MSE

mse = np.mean((np.array(actual_values) – np.array(predicted_values))**2)

# Calculate variance of actual values

variance_actual = np.var(actual_values)

# Calculate NMSE

nmse = mse / variance_actual if variance_actual != 0 else “undefined”

print(f”MSE: {mse}”)

print(f”NMSE: {nmse}”)

This script utilizes the NumPy library to estimate MSE and NMSE and outputs the outcomes.

  1. Interpreting NMSE Results:
  • NMSE ≈ 0: This means the predicted values are very close to the actual values, that signifies high accuracy.
  • NMSE > 1: This recommends that the prediction is poor compared to the altering of the actual data.
  • NMSE < 1: A value less than 1 signify which the forecasts are relatively good however it is not perfect.

The detailed guide contains the expounded manual that will support you with the computation of normalized mean squared error in the ns2 environment by accumulating data from the network and it also provides some samples. We plan deliver more information regarding the NMSE.

Get  top-notch project ideas from our specialists. To calculate the Normalized MSE in NS2, please share your parameter details, and we’ll help you enhance your project’s comparison performance.