Control Charts for Six Sigma and AI with Python

1. Introduction to Control Charts

Control charts are statistical tools used to monitor process stability and control over time. They help distinguish between common cause variation (inherent to the process) and special cause variation (indicating potential issues).

2. Importing Required Libraries


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats
    

3. Generating Sample Data


np.random.seed(42)

# Generate sample data
n_points = 100
data = np.random.normal(loc=10, scale=1, size=n_points)

# Add some out-of-control points
data[80] += 4  # Point above upper control limit
data[90] -= 4  # Point below lower control limit

df = pd.DataFrame({'Measurement': data})
print(df.head())
    

4. Implementing X-bar and R Charts


def create_control_charts(data, subgroup_size=5):
    subgroups = [data[i:i+subgroup_size] for i in range(0, len(data), subgroup_size)]
    
    x_bar = [np.mean(subgroup) for subgroup in subgroups]
    ranges = [np.ptp(subgroup) for subgroup in subgroups]
    
    grand_mean = np.mean(x_bar)
    mean_range = np.mean(ranges)
    
    # Constants for control limits (assuming subgroup size of 5)
    A2, D3, D4 = 0.577, 0, 2.114
    
    # X-bar chart limits
    x_ucl = grand_mean + A2 * mean_range
    x_lcl = grand_mean - A2 * mean_range
    
    # R chart limits
    r_ucl = D4 * mean_range
    r_lcl = D3 * mean_range
    
    return x_bar, ranges, grand_mean, mean_range, x_ucl, x_lcl, r_ucl, r_lcl

x_bar, ranges, grand_mean, mean_range, x_ucl, x_lcl, r_ucl, r_lcl = create_control_charts(df['Measurement'])

# Plotting X-bar chart
plt.figure(figsize=(12, 6))
plt.plot(x_bar, marker='o')
plt.axhline(grand_mean, color='g', linestyle='--')
plt.axhline(x_ucl, color='r', linestyle='--')
plt.axhline(x_lcl, color='r', linestyle='--')
plt.title('X-bar Chart')
plt.xlabel('Subgroup')
plt.ylabel('Subgroup Mean')
plt.show()

# Plotting R chart
plt.figure(figsize=(12, 6))
plt.plot(ranges, marker='o')
plt.axhline(mean_range, color='g', linestyle='--')
plt.axhline(r_ucl, color='r', linestyle='--')
plt.axhline(r_lcl, color='r', linestyle='--')
plt.title('R Chart')
plt.xlabel('Subgroup')
plt.ylabel('Subgroup Range')
plt.show()
    

5. Implementing Individual and Moving Range (I-MR) Charts


def create_imr_charts(data):
    individual = data
    moving_range = np.abs(np.diff(individual))
    
    mean_i = np.mean(individual)
    mean_mr = np.mean(moving_range)
    
    # Constants for control limits
    E2 = 2.66  # For moving range of 2
    
    # Individual chart limits
    i_ucl = mean_i + 3 * mean_mr / 1.128
    i_lcl = mean_i - 3 * mean_mr / 1.128
    
    # Moving Range chart limits
    mr_ucl = E2 * mean_mr
    mr_lcl = 0  # Lower control limit for MR chart is always 0
    
    return individual, moving_range, mean_i, mean_mr, i_ucl, i_lcl, mr_ucl, mr_lcl

individual, moving_range, mean_i, mean_mr, i_ucl, i_lcl, mr_ucl, mr_lcl = create_imr_charts(df['Measurement'])

# Plotting Individual chart
plt.figure(figsize=(12, 6))
plt.plot(individual, marker='o')
plt.axhline(mean_i, color='g', linestyle='--')
plt.axhline(i_ucl, color='r', linestyle='--')
plt.axhline(i_lcl, color='r', linestyle='--')
plt.title('Individual Chart')
plt.xlabel('Observation')
plt.ylabel('Individual Value')
plt.show()

# Plotting Moving Range chart
plt.figure(figsize=(12, 6))
plt.plot(moving_range, marker='o')
plt.axhline(mean_mr, color='g', linestyle='--')
plt.axhline(mr_ucl, color='r', linestyle='--')
plt.axhline(mr_lcl, color='r', linestyle='--')
plt.title('Moving Range Chart')
plt.xlabel('Observation')
plt.ylabel('Moving Range')
plt.show()
    

6. Interpreting Control Charts

When interpreting control charts, look for these patterns that may indicate special cause variation:

7. AI Applications in Control Charts

AI can enhance the use of control charts in several ways:

8. Monitoring AI Model Performance with Control Charts

Control charts can also be used to monitor the performance of AI models over time:

Conclusion

Control charts are powerful tools for monitoring process stability and detecting special cause variation. By integrating AI techniques with traditional control chart methods, we can enhance our ability to monitor, analyze, and control processes in real-time. This integration is particularly valuable in the context of Industry 4.0 and IoT, where large amounts of data are generated continuously. Furthermore, applying control chart principles to AI model performance monitoring ensures that our AI systems remain reliable and accurate over time.