1. 过程能力指数概述

过程能力指数(Process Capability Index,简称PCI)是衡量制造过程能否稳定生产出符合规格要求的产品的重要指标。它通过比较过程的实际波动与规格限之间的关系,量化过程的加工精度和能力。

1.1 为什么需要过程能力指数?

在制造业和质量管理中,过程能力指数帮助我们回答以下关键问题:

  • 当前过程能否满足客户要求?
  • 过程是否需要改进?
  • 改进后的效果如何评估?

1.2 常见的过程能力指数类型

指数类型 公式 适用场景
Cp (USL - LSL) / (6σ) 过程中心与规格中心重合时
Cpk min[(USL - μ)/3σ, (μ - LSL)/3σ] 过程中心与规格中心不重合时
Pp (USL - LSL) / (6σ_s) 长期过程能力评估
Ppk min[(USL - μ)/3σ_s, (μ - LSL)/3σ_s] 长期过程能力评估

其中:

  • USL:规格上限
  • LSL:规格下限
  • μ:过程均值
  • σ:过程标准差(短期)
  • σ_s:过程标准差(长期)

2. 基础计算例题详解

例题1:简单Cp计算

题目:某零件的直径规格为10.0±0.1mm,即USL=10.1mm,LSL=9.9mm。通过抽样测量得到样本均值μ=10.02mm,样本标准差s=0.02mm。计算该过程的Cp和Cpk。

解题步骤

  1. 确定参数

    • USL = 10.1 mm
    • LSL = 9.9 mm
    • μ = 10.02 mm
    • σ ≈ s = 0.02 mm(假设样本标准差能代表过程标准差)
  2. 计算Cp

    Cp = (USL - LSL) / (6σ)
      = (10.1 - 9.9) / (6 × 0.02)
      = 0.2 / 0.12
      = 1.67
    
  3. 计算Cpk

    • 计算上侧能力指数:Cpu = (USL - μ) / (3σ) = (10.1 - 10.02) / (3 × 0.02) = 0.08 / 0.06 = 1.33
    • 计算下侧能力指数:Cpl = (μ - LSL) / (3σ) = (10.02 - 9.9) / (3 × 0.02) = 0.12 / 0.06 = 2.00
    • Cpk = min(Cpu, Cpl) = min(1.33, 2.00) = 1.33
  4. 结果解读

    • Cp = 1.67 > 1.33,说明过程潜力足够,但实际中心偏离了规格中心
    • Cpk = 1.33,处于可接受范围(通常Cpk ≥ 1.33为良好)
    • 过程均值μ=10.02mm,略高于规格中心10.0mm,需要调整过程中心

例题2:考虑过程偏移的计算

题目:某注塑件的重量规格为50±2g,即USL=52g,LSL=48g。生产过程中,由于模具磨损,过程均值逐渐偏移。当前测量数据:μ=50.8g,σ=0.5g。计算当前的Cpk。

解题步骤

  1. 参数确定

    • USL = 52 g
    • LSL = 48 g
    • μ = 50.8 g
    • σ = 0.5 g
  2. 计算Cpk

    Cpu = (USL - μ) / (3σ) = (52 - 50.8) / (3 × 0.5) = 1.2 / 1.5 = 0.8
    Cpl = (μ - LSL) / (3σ) = (50.8 - 48) / (3 × 0.5) = 2.8 / 1.5 = 1.87
    Cpk = min(0.8, 1.87) = 0.8
    
  3. 结果解读

    • Cpk = 0.8 < 1.0,说明过程能力不足
    • 主要问题在于上侧能力不足(Cpu=0.8),过程均值偏大
    • 需要立即调整过程中心或减少过程变异

3. 实战应用:Python代码实现

3.1 基础计算函数

import numpy as np
import pandas as pd
from scipy import stats

class ProcessCapability:
    """过程能力指数计算器"""
    
    def __init__(self, usl, lsl, data=None, mean=None, std=None):
        """
        初始化过程能力计算器
        
        参数:
        usl: 规格上限
        lsl: 规格下限
        data: 数据数组(可选)
        mean: 过程均值(如果提供了data,则自动计算)
        std: 过程标准差(如果提供了data,则自动计算)
        """
        self.usl = usl
        self.lsl = lsl
        self.spec_range = usl - lsl
        
        if data is not None:
            self.data = np.array(data)
            self.mean = np.mean(self.data)
            self.std = np.std(self.data, ddof=1)  # 样本标准差
        else:
            self.mean = mean
            self.std = std
            
    def calculate_cp(self):
        """计算Cp指数"""
        if self.std == 0:
            return float('inf')
        return self.spec_range / (6 * self.std)
    
    def calculate_cpk(self):
        """计算Cpk指数"""
        if self.std == 0:
            return float('inf')
        
        cpu = (self.usl - self.mean) / (3 * self.std)
        cpl = (self.mean - self.lsl) / (3 * self.std)
        return min(cpu, cpl)
    
    def calculate_pp(self):
        """计算Pp指数(长期)"""
        if len(self.data) < 2:
            return None
        # 长期标准差使用所有数据的标准差
        long_term_std = np.std(self.data, ddof=1)
        if long_term_std == 0:
            return float('inf')
        return self.spec_range / (6 * long_term_std)
    
    def calculate_ppk(self):
        """计算Ppk指数(长期)"""
        if len(self.data) < 2:
            return None
        long_term_std = np.std(self.data, ddof=1)
        if long_term_std == 0:
            return float('inf')
        
        cpu = (self.usl - self.mean) / (3 * long_term_std)
        cpl = (self.mean - self.lsl) / (3 * long_term_std)
        return min(cpu, cpl)
    
    def get_all_indices(self):
        """获取所有过程能力指数"""
        indices = {
            'Cp': self.calculate_cp(),
            'Cpk': self.calculate_cpk(),
            'Pp': self.calculate_pp(),
            'Ppk': self.calculate_ppk(),
            'Mean': self.mean,
            'Std': self.std,
            'Spec_Range': self.spec_range
        }
        return indices
    
    def interpret_indices(self):
        """解读过程能力指数"""
        cp = self.calculate_cp()
        cpk = self.calculate_cpk()
        
        interpretation = []
        
        # Cp解读
        if cp >= 1.67:
            interpretation.append("Cp ≥ 1.67: 过程能力优秀,过程变异远小于规格范围")
        elif cp >= 1.33:
            interpretation.append("Cp ≥ 1.33: 过程能力良好,过程变异在可接受范围内")
        elif cp >= 1.0:
            interpretation.append("Cp ≥ 1.0: 过程能力尚可,但需密切监控")
        else:
            interpretation.append("Cp < 1.0: 过程能力不足,需要改进")
        
        # Cpk解读
        if cpk >= 1.67:
            interpretation.append("Cpk ≥ 1.67: 过程能力优秀,且中心位置合适")
        elif cpk >= 1.33:
            interpretation.append("Cpk ≥ 1.33: 过程能力良好,中心位置基本合适")
        elif cpk >= 1.0:
            interpretation.append("Cpk ≥ 1.0: 过程能力尚可,但中心位置可能需要调整")
        else:
            interpretation.append("Cpk < 1.0: 过程能力不足,需要立即改进")
        
        # Cp与Cpk比较
        if cp > cpk:
            interpretation.append(f"注意:Cp ({cp:.2f}) > Cpk ({cpk:.2f}),说明过程中心偏离规格中心")
        else:
            interpretation.append(f"过程中心与规格中心基本一致")
        
        return interpretation

# 使用示例
if __name__ == "__main__":
    # 例题1数据
    data1 = [10.01, 10.02, 10.03, 10.02, 10.01, 10.02, 10.03, 10.02, 10.01, 10.02]
    pc1 = ProcessCapability(usl=10.1, lsl=9.9, data=data1)
    
    print("例题1计算结果:")
    indices1 = pc1.get_all_indices()
    for key, value in indices1.items():
        print(f"{key}: {value:.4f}")
    
    print("\n解读:")
    for interp in pc1.interpret_indices():
        print(f"- {interp}")
    
    # 例题2数据
    data2 = [50.8, 50.9, 50.7, 50.8, 50.9, 50.7, 50.8, 50.9, 50.7, 50.8]
    pc2 = ProcessCapability(usl=52, lsl=48, data=data2)
    
    print("\n" + "="*50)
    print("例题2计算结果:")
    indices2 = pc2.get_all_indices()
    for key, value in indices2.items():
        print(f"{key}: {value:.4f}")
    
    print("\n解读:")
    for interp in pc2.interpret_indices():
        print(f"- {interp}")

3.2 高级分析:过程能力分析报告

import matplotlib.pyplot as plt
import seaborn as sns

class ProcessCapabilityAnalysis:
    """过程能力综合分析"""
    
    def __init__(self, usl, lsl, data):
        self.usl = usl
        lsl = lsl
        self.data = np.array(data)
        self.pc = ProcessCapability(usl, lsl, data=data)
        
    def plot_capability_analysis(self, title="过程能力分析图"):
        """绘制过程能力分析图"""
        fig, axes = plt.subplots(2, 2, figsize=(12, 10))
        
        # 1. 直方图与正态分布
        ax1 = axes[0, 0]
        ax1.hist(self.data, bins=15, density=True, alpha=0.7, color='skyblue', edgecolor='black')
        
        # 拟合正态分布
        mu, sigma = np.mean(self.data), np.std(self.data, ddof=1)
        x = np.linspace(self.data.min(), self.data.max(), 100)
        y = stats.norm.pdf(x, mu, sigma)
        ax1.plot(x, y, 'r-', linewidth=2, label=f'N({mu:.2f}, {sigma:.2f}²)')
        
        # 添加规格限
        ax1.axvline(self.usl, color='red', linestyle='--', linewidth=2, label=f'USL={self.usl}')
        ax1.axvline(lsl, color='green', linestyle='--', linewidth=2, label=f'LSL={lsl}')
        ax1.axvline(mu, color='orange', linestyle='-', linewidth=2, label=f'μ={mu:.2f}')
        
        ax1.set_xlabel('测量值')
        ax1.set_ylabel('概率密度')
        ax1.set_title('数据分布与规格限')
        ax1.legend()
        ax1.grid(True, alpha=0.3)
        
        # 2. 过程能力指数条形图
        ax2 = axes[0, 1]
        indices = ['Cp', 'Cpk', 'Pp', 'Ppk']
        values = [self.pc.calculate_cp(), self.pc.calculate_cpk(), 
                  self.pc.calculate_pp(), self.pc.calculate_ppk()]
        
        colors = ['skyblue' if v >= 1.33 else 'orange' if v >= 1.0 else 'red' for v in values]
        bars = ax2.bar(indices, values, color=colors, edgecolor='black')
        
        # 添加参考线
        ax2.axhline(y=1.0, color='red', linestyle='--', linewidth=1, label='Cpk=1.0')
        ax2.axhline(y=1.33, color='green', linestyle='--', linewidth=1, label='Cpk=1.33')
        
        ax2.set_ylabel('指数值')
        ax2.set_title('过程能力指数')
        ax2.legend()
        ax2.grid(True, alpha=0.3, axis='y')
        
        # 在柱子上添加数值
        for bar, value in zip(bars, values):
            height = bar.get_height()
            ax2.text(bar.get_x() + bar.get_width()/2., height + 0.05,
                    f'{value:.2f}', ha='center', va='bottom')
        
        # 3. 控制图(X-bar图)
        ax3 = axes[1, 0]
        n = len(self.data)
        ax3.plot(range(1, n+1), self.data, 'b-o', linewidth=1, markersize=4)
        ax3.axhline(mu, color='orange', linestyle='-', linewidth=2, label=f'均值={mu:.2f}')
        ax3.axhline(self.usl, color='red', linestyle='--', linewidth=1.5, label=f'USL={self.usl}')
        ax3.axhline(lsl, color='green', linestyle='--', linewidth=1.5, label=f'LSL={lsl}')
        
        ax3.set_xlabel('样本序号')
        ax3.set_ylabel('测量值')
        ax3.set_title('单值控制图')
        ax3.legend()
        ax3.grid(True, alpha=0.3)
        
        # 4. 能力指数趋势(如果有多个批次)
        ax4 = axes[1, 1]
        # 模拟多个批次的数据
        batch_means = []
        batch_cpks = []
        
        # 生成模拟数据
        for i in range(5):
            batch_data = np.random.normal(mu, sigma, 10)
            batch_mean = np.mean(batch_data)
            batch_std = np.std(batch_data, ddof=1)
            
            batch_means.append(batch_mean)
            batch_cpks.append(min((self.usl - batch_mean) / (3 * batch_std),
                                 (batch_mean - lsl) / (3 * batch_std)))
        
        ax4.plot(range(1, 6), batch_cpks, 'g-o', linewidth=2, markersize=6, label='Cpk')
        ax4.axhline(y=1.0, color='red', linestyle='--', linewidth=1, label='Cpk=1.0')
        ax4.axhline(y=1.33, color='green', linestyle='--', linewidth=1, label='Cpk=1.33')
        
        ax4.set_xlabel('批次')
        ax4.set_ylabel('Cpk值')
        ax4.set_title('批次间Cpk趋势')
        ax4.legend()
        ax4.grid(True, alpha=0.3)
        
        plt.suptitle(title, fontsize=16, fontweight='bold')
        plt.tight_layout()
        plt.show()
    
    def generate_report(self):
        """生成过程能力分析报告"""
        indices = self.pc.get_all_indices()
        interpretation = self.pc.interpret_indices()
        
        report = f"""
        过程能力分析报告
        {'='*50}
        
        基本信息:
        - 规格上限 (USL): {self.usl}
        - 规格下限 (LSL): {self.lsl}
        - 规格范围: {self.usl - self.lsl}
        - 样本数量: {len(self.data)}
        - 过程均值 (μ): {indices['Mean']:.4f}
        - 过程标准差 (σ): {indices['Std']:.4f}
        
        过程能力指数:
        - Cp:  {indices['Cp']:.4f}
        - Cpk: {indices['Cpk']:.4f}
        - Pp:  {indices['Pp']:.4f}
        - Ppk: {indices['Ppk']:.4f}
        
        解读与建议:
        """
        
        for interp in interpretation:
            report += f"- {interp}\n"
        
        # 添加改进建议
        report += "\n改进建议:\n"
        if indices['Cpk'] < 1.0:
            report += "- 立即采取措施改进过程,可能需要调整过程中心或减少变异\n"
        elif indices['Cpk'] < 1.33:
            report += "- 过程能力尚可,但建议优化以提高稳定性\n"
        else:
            report += "- 过程能力良好,继续保持并定期监控\n"
        
        if indices['Cp'] > indices['Cpk']:
            report += "- 注意:过程中心偏离规格中心,建议调整过程设置\n"
        
        return report

# 使用示例
if __name__ == "__main__":
    # 生成模拟数据
    np.random.seed(42)
    # 模拟一个中心偏移的过程
    simulated_data = np.random.normal(10.02, 0.02, 50)
    
    # 创建分析对象
    analysis = ProcessCapabilityAnalysis(usl=10.1, lsl=9.9, data=simulated_data)
    
    # 生成报告
    print(analysis.generate_report())
    
    # 绘制分析图
    analysis.plot_capability_analysis("过程能力综合分析图")

4. 实战案例:制造业质量控制

4.1 案例背景

某汽车零部件制造商生产发动机缸体,关键尺寸为缸径,规格要求为100±0.05mm。生产过程中,由于刀具磨损和温度变化,过程能力逐渐下降。

4.2 数据收集与分析

import pandas as pd
from datetime import datetime, timedelta

class ManufacturingCaseStudy:
    """制造业过程能力分析案例"""
    
    def __init__(self):
        # 模拟生产数据(实际应用中应从MES系统获取)
        self.production_data = self.generate_production_data()
        
    def generate_production_data(self):
        """生成模拟生产数据"""
        np.random.seed(42)
        
        # 生成30天的生产数据,每天10个样本
        data = []
        dates = []
        
        for day in range(30):
            date = datetime(2024, 1, 1) + timedelta(days=day)
            
            # 模拟过程中心逐渐偏移(刀具磨损)
            mean_shift = 0.001 * day  # 每天偏移0.001mm
            
            # 模拟过程变异逐渐增大(温度变化)
            std_increase = 0.0005 * day  # 每天标准差增加0.0005mm
            
            # 生成当天数据
            for i in range(10):
                # 基准值100mm,规格±0.05mm
                value = np.random.normal(100 + mean_shift, 0.02 + std_increase)
                data.append(value)
                dates.append(date)
        
        df = pd.DataFrame({
            'date': dates,
            'value': data,
            'day': np.repeat(range(30), 10)
        })
        
        return df
    
    def analyze_process_trend(self):
        """分析过程能力趋势"""
        # 按天分组计算过程能力
        daily_stats = []
        
        for day in range(30):
            day_data = self.production_data[self.production_data['day'] == day]['value']
            
            if len(day_data) > 0:
                mean = np.mean(day_data)
                std = np.std(day_data, ddof=1)
                
                # 计算Cpk(假设规格100±0.05)
                usl = 100.05
                lsl = 99.95
                
                cpu = (usl - mean) / (3 * std)
                cpl = (mean - lsl) / (3 * std)
                cpk = min(cpu, cpl)
                
                daily_stats.append({
                    'day': day,
                    'mean': mean,
                    'std': std,
                    'cpk': cpk,
                    'in_spec': (mean >= lsl and mean <= usl)
                })
        
        return pd.DataFrame(daily_stats)
    
    def plot_trend_analysis(self):
        """绘制趋势分析图"""
        stats_df = self.analyze_process_trend()
        
        fig, axes = plt.subplots(2, 2, figsize=(14, 10))
        
        # 1. 过程均值趋势
        ax1 = axes[0, 0]
        ax1.plot(stats_df['day'], stats_df['mean'], 'b-o', linewidth=2, markersize=4)
        ax1.axhline(y=100.0, color='green', linestyle='--', linewidth=1, label='目标值')
        ax1.axhline(y=100.05, color='red', linestyle='--', linewidth=1, label='USL')
        ax1.axhline(y=99.95, color='red', linestyle='--', linewidth=1, label='LSL')
        ax1.set_xlabel('生产天数')
        ax1.set_ylabel('缸径均值 (mm)')
        ax1.set_title('过程均值趋势')
        ax1.legend()
        ax1.grid(True, alpha=0.3)
        
        # 2. 过程标准差趋势
        ax2 = axes[0, 1]
        ax2.plot(stats_df['day'], stats_df['std'], 'r-s', linewidth=2, markersize=4)
        ax2.axhline(y=0.02, color='green', linestyle='--', linewidth=1, label='初始标准差')
        ax2.set_xlabel('生产天数')
        ax2.set_ylabel('标准差 (mm)')
        ax2.set_title('过程变异趋势')
        ax2.legend()
        ax2.grid(True, alpha=0.3)
        
        # 3. Cpk趋势
        ax3 = axes[1, 0]
        ax3.plot(stats_df['day'], stats_df['cpk'], 'g-^', linewidth=2, markersize=4)
        ax3.axhline(y=1.33, color='green', linestyle='--', linewidth=1, label='目标Cpk=1.33')
        ax3.axhline(y=1.0, color='orange', linestyle='--', linewidth=1, label='最低要求Cpk=1.0')
        ax3.axhline(y=0.67, color='red', linestyle='--', linewidth=1, label='危险区域')
        ax3.set_xlabel('生产天数')
        ax3.set_ylabel('Cpk值')
        ax3.set_title('过程能力趋势')
        ax3.legend()
        ax3.grid(True, alpha=0.3)
        
        # 4. 过程能力分布
        ax4 = axes[1, 1]
        cpk_values = stats_df['cpk']
        ax4.hist(cpk_values, bins=15, alpha=0.7, color='skyblue', edgecolor='black')
        ax4.axvline(x=1.33, color='green', linestyle='--', linewidth=2, label='目标值')
        ax4.axvline(x=1.0, color='orange', linestyle='--', linewidth=2, label='最低要求')
        ax4.set_xlabel('Cpk值')
        ax4.set_ylabel('频数')
        ax4.set_title('Cpk分布')
        ax4.legend()
        ax4.grid(True, alpha=0.3)
        
        plt.suptitle('发动机缸体生产过程能力趋势分析', fontsize=16, fontweight='bold')
        plt.tight_layout()
        plt.show()
        
        return stats_df
    
    def generate_improvement_plan(self):
        """生成改进建议"""
        stats_df = self.analyze_process_trend()
        
        # 找出Cpk低于1.0的天数
        critical_days = stats_df[stats_df['cpk'] < 1.0]
        
        report = f"""
        发动机缸体生产过程能力分析报告
        {'='*60}
        
        数据概况:
        - 总生产天数: {len(stats_df)}
        - 平均Cpk: {stats_df['cpk'].mean():.3f}
        - 最低Cpk: {stats_df['cpk'].min():.3f}
        - Cpk低于1.0的天数: {len(critical_days)}
        
        问题识别:
        """
        
        if len(critical_days) > 0:
            report += f"- 发现{len(critical_days)}天过程能力不足(Cpk < 1.0)\n"
            report += f"- 最低Cpk出现在第{critical_days.iloc[0]['day']}天,值为{critical_days.iloc[0]['cpk']:.3f}\n"
        
        # 分析趋势
        if stats_df['cpk'].iloc[-1] < stats_df['cpk'].iloc[0]:
            report += "- 过程能力呈下降趋势,需要立即干预\n"
        
        # 改进建议
        report += "\n改进建议:\n"
        report += "1. 刀具管理优化:\n"
        report += "   - 建立刀具寿命监控系统\n"
        report += "   - 实施预防性刀具更换策略\n"
        report += "   - 优化刀具材质和涂层\n\n"
        
        report += "2. 过程控制优化:\n"
        report += "   - 增加温度补偿机制\n"
        report += "   - 优化冷却液系统\n"
        report += "   - 实施SPC实时监控\n\n"
        
        report += "3. 设备维护:\n"
        report += "   - 定期校准测量设备\n"
        report += "   - 检查主轴精度\n"
        report += "   - 优化夹具定位\n\n"
        
        report += "4. 操作培训:\n"
        report += "   - 加强过程能力培训\n"
        report += "   - 建立标准作业程序\n"
        report += "   - 实施质量责任制\n"
        
        return report

# 使用示例
if __name__ == "__main__":
    case_study = ManufacturingCaseStudy()
    
    # 生成趋势分析图
    stats_df = case_study.plot_trend_analysis()
    
    # 生成分析报告
    print(case_study.generate_improvement_plan())
    
    # 显示详细统计
    print("\n详细统计:")
    print(stats_df.describe())

5. 过程能力指数的局限性及注意事项

5.1 局限性

  1. 假设正态分布:传统Cp/Cpk计算假设数据服从正态分布,对于非正态数据需要转换或使用非参数方法。

  2. 样本量要求:通常需要至少25-30个样本才能得到可靠估计,样本量过小会导致估计不准确。

  3. 过程稳定性:过程能力指数基于稳定过程,如果过程不稳定(如存在特殊原因变异),指数可能误导。

  4. 规格限的合理性:指数计算依赖于规格限,如果规格限设置不合理,指数可能失去意义。

5.2 注意事项

  1. 数据收集:确保数据来自稳定过程,排除异常值和特殊原因。

  2. 过程稳定性验证:在计算过程能力前,先使用控制图验证过程稳定性。

  3. 非正态数据处理

    • 使用Box-Cox变换
    • 使用非参数过程能力指数
    • 使用Johnson变换
  4. 长期与短期能力

    • Cp/Cpk反映短期过程能力(仅考虑普通原因变异)
    • Pp/Ppk反映长期过程能力(包含所有变异)

5.3 非正态数据处理示例

from scipy.stats import boxcox, johnsonsu, johnsonsb

class NonNormalProcessCapability:
    """非正态数据的过程能力分析"""
    
    def __init__(self, usl, lsl, data):
        self.usl = usl
        self.lsl = lsl
        self.data = np.array(data)
        
    def check_normality(self):
        """检查数据正态性"""
        from scipy.stats import shapiro, kstest
        
        # Shapiro-Wilk检验
        shapiro_stat, shapiro_p = shapiro(self.data)
        
        # Kolmogorov-Smirnov检验
        ks_stat, ks_p = kstest(self.data, 'norm', args=(np.mean(self.data), np.std(self.data, ddof=1)))
        
        return {
            'shapiro_stat': shapiro_stat,
            'shapiro_p': shapiro_p,
            'ks_stat': ks_stat,
            'ks_p': ks_p,
            'is_normal': shapiro_p > 0.05 and ks_p > 0.05
        }
    
    def boxcox_transform(self):
        """Box-Cox变换"""
        # Box-Cox变换要求数据为正数
        if np.any(self.data <= 0):
            # 如果有非正数,先平移
            shifted_data = self.data - np.min(self.data) + 1
        else:
            shifted_data = self.data
        
        transformed, lambda_val = boxcox(shifted_data)
        
        # 计算变换后的过程能力
        transformed_usl = boxcox(self.usl - np.min(self.data) + 1, lambda_val)
        transformed_lsl = boxcox(self.lsl - np.min(self.data) + 1, lambda_val)
        
        transformed_mean = np.mean(transformed)
        transformed_std = np.std(transformed, ddof=1)
        
        cp = (transformed_usl - transformed_lsl) / (6 * transformed_std)
        cpu = (transformed_usl - transformed_mean) / (3 * transformed_std)
        cpl = (transformed_mean - transformed_lsl) / (3 * transformed_std)
        cpk = min(cpu, cpl)
        
        return {
            'lambda': lambda_val,
            'cp': cp,
            'cpk': cpk,
            'transformed_data': transformed
        }
    
    def johnson_transform(self):
        """Johnson变换"""
        # Johnson分布有三种类型:Su, Sb, Sn
        # 这里使用Su分布(适用于无界数据)
        try:
            # 拟合Johnson Su分布
            params = johnsonsu.fit(self.data)
            # 计算变换后的过程能力
            # 这里简化处理,实际应用中需要更复杂的计算
            return {'method': 'Johnson Su', 'params': params}
        except:
            return {'method': 'Johnson Su', 'params': None, 'error': '拟合失败'}
    
    def nonparametric_capability(self, confidence=0.95):
        """非参数过程能力指数"""
        # 使用分位数方法
        n = len(self.data)
        
        # 计算分位数
        lsl_quantile = np.percentile(self.data, 100 * (self.lsl - np.min(self.data)) / (np.max(self.data) - np.min(self.data)))
        usl_quantile = np.percentile(self.data, 100 * (self.usl - np.min(self.data)) / (np.max(self.data) - np.min(self.data)))
        
        # 计算过程能力
        # 这里使用简化的非参数方法
        within_spec = np.sum((self.data >= self.lsl) & (self.data <= self.usl))
        proportion_within = within_spec / n
        
        # 计算置信区间
        from statsmodels.stats.proportion import proportion_confint
        ci_low, ci_high = proportion_confint(within_spec, n, alpha=1-confidence, method='wilson')
        
        return {
            'proportion_within': proportion_within,
            'confidence_interval': (ci_low, ci_high),
            'confidence_level': confidence
        }

# 使用示例
if __name__ == "__main__":
    # 生成非正态数据(指数分布)
    np.random.seed(42)
    non_normal_data = np.random.exponential(scale=2, size=100) + 10
    
    # 创建分析对象
    nnpc = NonNormalProcessCapability(usl=15, lsl=12, data=non_normal_data)
    
    # 检查正态性
    normality_check = nnpc.check_normality()
    print("正态性检验结果:")
    print(f"Shapiro-Wilk p值: {normality_check['shapiro_p']:.4f}")
    print(f"数据是否正态: {normality_check['is_normal']}")
    
    # Box-Cox变换
    boxcox_result = nnpc.boxcox_transform()
    print(f"\nBox-Cox变换结果:")
    print(f"λ值: {boxcox_result['lambda']:.4f}")
    print(f"变换后Cp: {boxcox_result['cp']:.4f}")
    print(f"变换后Cpk: {boxcox_result['cpk']:.4f}")
    
    # 非参数方法
    nonparam_result = nnpc.nonparametric_capability()
    print(f"\n非参数方法结果:")
    print(f"规格内比例: {nonparam_result['proportion_within']:.4f}")
    print(f"置信区间: ({nonparam_result['confidence_interval'][0]:.4f}, {nonparam_result['confidence_interval'][1]:.4f})")

6. 总结与最佳实践

6.1 关键要点总结

  1. 过程能力指数是质量管理的核心工具,用于量化过程满足规格要求的能力。

  2. Cp和Cpk的区别

    • Cp衡量过程潜力(不考虑中心偏移)
    • Cpk衡量实际过程能力(考虑中心偏移)
  3. 指数解读标准

    • Cpk ≥ 1.67:优秀
    • Cpk ≥ 1.33:良好
    • Cpk ≥ 1.0:可接受
    • Cpk < 1.0:不足
  4. 计算前提

    • 过程稳定(使用控制图验证)
    • 数据正态(或适当变换)
    • 样本量充足(至少25-30个)

6.2 实施最佳实践

  1. 建立过程能力监控体系

    • 定期计算过程能力指数
    • 设置预警阈值(如Cpk < 1.33)
    • 建立趋势分析机制
  2. 结合其他质量工具

    • 与SPC(统计过程控制)结合
    • 与FMEA(失效模式分析)结合
    • 与DOE(实验设计)结合
  3. 持续改进循环

    测量 → 分析 → 改进 → 控制
    
  4. 人员培训

    • 培训质量工程师计算和解读能力指数
    • 培训操作人员理解过程能力的重要性
    • 建立跨部门的质量改进团队

6.3 未来发展趋势

  1. 数字化与智能化

    • 实时过程能力监控
    • 基于AI的预测性维护
    • 数字孪生技术应用
  2. 大数据分析

    • 多变量过程能力分析
    • 供应链过程能力整合
    • 全生命周期质量追溯
  3. 可持续发展

    • 绿色制造过程能力评估
    • 能源效率与过程能力结合
    • 环境影响与质量平衡

通过本指南的学习和实践,您将能够熟练掌握过程能力指数的计算方法,并在实际工作中有效应用,持续提升产品质量和过程稳定性。