I have read the code of room Light Monitoring Project Using Z-Score Analysis but I am still finding it hard to understand the code of room Light Monitoring Project Using Z-Score Analysis. Could you please help me to better understand the code.

Did you understand the VPS section code where we have set the thresholds for the sensor values, if it falls outside that range it will send a message

It’s the same just we are dynamically changing the thresholds to detect anomalies using z-score

See Z-score detects anomaly using mean and standard deviation

It measures how many standard deviations a data point is from the mean. It’s used to identify outliers or anomalies in a dataset.

so we are setting a frame size, suppose of 10 so 10 readings from sensors will be taken, their mean and std deviation will be calculated and with the help of this we will get low bound and high bound

now for every data point, it will check they are under the high and low bounds,

Also, the frame size will remain the same 10 but previously we considered the first 10 readings for calculating the mean and std dev now as we get more readings the starting reading will be discarded

I have implemented z-score based anomaly detection in jupyter, It will help you to understand it better

*Blue Line: Represents the sensor values over time.

*Green Dashed Line: The mean of the sensor values in the current window.

*Red Dashed Lines: The high and low Z-score bounds are calculated using the mean and variance.

*Red Points: Data points that fall outside the bounds, flagged as anomalies.

PS: I am not good at explaining if you have any doubts feel free to ask

Here is a brief explanation of the code for room light monitoring project

The code monitors the light in your room using an LDR (Light Dependent Resistor) connected to a Bolt IoT device. It uses Z-score analysis to detect sudden changes in light levels (e.g., turning lights on/off) and sends an SMS alert when these changes occur.

#### Key Steps in the Code

**Import Necessary Libraries:**

`conf`

,`json`

,`time`

,`math`

,`statistics`

: Various libraries for configuration, data handling, time delays, math functions, and statistical calculations.`Sms`

,`Bolt`

: Libraries from the Bolt IoT module to handle SMS sending and device interaction.

```
import conf, json, time, math, statistics
from boltiot import Sms, Bolt
```

**2. Function to Compute Z-Score Bounds:**

- This function calculates the upper and lower bounds for normal light levels based on past data.

```
def compute_bounds(history_data, frame_size, factor):
if len(history_data) < frame_size:
return None
if len(history_data) > frame_size:
del history_data[0:len(history_data) - frame_size]
Mn = statistics.mean(history_data)
Variance = sum(math.pow((data - Mn), 2) for data in history_data)
Zn = factor * math.sqrt(Variance / frame_size)
High_bound = history_data[frame_size - 1] + Zn
Low_bound = history_data[frame_size - 1] - Zn
return [High_bound, Low_bound]
```

**3. Initialize Bolt and SMS Objects:**

- Set up the Bolt device and SMS service using your configuration details from
`conf.py`

.

```
mybolt = Bolt(conf.API_KEY, conf.DEVICE_ID)
sms = Sms(conf.SSID, conf.AUTH_TOKEN, conf.TO_NUMBER, conf.FROM_NUMBER)
history_data = []
```

**4. Main Loop to Monitor Light Levels:**

- The code continuously checks the light sensor value, computes Z-score bounds, and sends an SMS if the light level is abnormal.

```
while True:
response = mybolt.analogRead('A0')
data = json.loads(response)
if data['success'] != 1:
print("Error: " + data['value'])
time.sleep(10)
continue
sensor_value = int(data['value'])
bound = compute_bounds(history_data, conf.FRAME_SIZE, conf.MUL_FACTOR)
if not bound:
history_data.append(sensor_value)
time.sleep(10)
continue
if sensor_value > bound[0]:
sms.send_sms("Someone turned on the lights")
elif sensor_value < bound[1]:
sms.send_sms("Someone turned off the lights")
history_data.append(sensor_value)
time.sleep(10)
```

If you still face any issues please feel free to get back to us. Happy learning!

Thank you. It was very helpful and informative.

Thank you for your help.