forked from jimlehner/process-improvement
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloss_function_calculations.py
More file actions
160 lines (124 loc) · 4.35 KB
/
loss_function_calculations.py
File metadata and controls
160 lines (124 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from typing import Optional
import pandas as pd
import numpy as np
from process_improvement.charts.results import(
TaguchiLossCalcResults,
ExpectedLossCalcResults
)
def taguchi_loss_calcs(
USL: float,
LSL: float,
Target: Optional[float] = None
) -> TaguchiLossCalcResults:
"""
Compute quadratic loss function (Taguchi loss function) values over a
specification range.
This function generates the X and Y values for a Taguchi-style quadratic
loss function based on the provided specification limits and target.
The loss function represents the economic loss associated with deviation
from the target value, increasing quadratically as values move away from
target.
The generated domain extends beyond the specification limits to provide
visual context for plotting. Outside the specification limits, the loss
is held constant at the boundary loss value to create a piecewise
representation suitable for visualization.
Parameters
----------
USL : float
Upper Specification Limit.
LSL : float
Lower Specification Limit.
Target : float, optional
Target (nominal) value. If None, the target is set to the midpoint
between USL and LSL.
Returns
-------
TaguchiLossCalcResults
Dataclass containing:
- df : pandas.DataFrame
DataFrame with columns:
- 'X values' : numpy.ndarray
X-axis values spanning slightly beyond the specification limits.
- 'Y values' : list[float]
Corresponding Taguchi quadratic loss values.
Raises
------
ValueError
If USL is less than or equal to LSL.
Notes
-----
- The loss function is defined as:
L(x) = k (x - Target)^2
where k is a numerica constant typically expressed in dollars ($).
- Values outside the specification limits are clamped to the loss at
the nearest specification boundary to produce a flat extension region.
- The extended range (± tolerance/4) is intended for visualization and
does not affect capability calculations.
See Also
--------
taguchi_loss_function
"""
# --- VALIDATION ---
if USL <= LSL:
raise ValueError("USL must be greater than LSL")
# Set default target if not provided
if Target is None:
Target = (USL + LSL) / 2
# --- CONFIGURATION ---
tolerance = USL - LSL
extension = tolerance / 4
# --- TAGUCHI (QUADRATIC) LOSS FUNCTION ---
# Create list of values used to generate the parabola
x_values = np.linspace(LSL - extension, USL + extension, 500)
# List for y values based on x values
y_values = []
# Piecewise loss function
for value in x_values:
if value <= LSL:
y_values.append(tolerance * (Target - LSL) ** 2)
elif value >= USL:
y_values.append(tolerance * (USL - Target) ** 2)
else:
y_values.append(tolerance * (value - Target) ** 2)
# Results dataframe
results_df = pd.DataFrame({'X values':x_values,
'Y values':y_values})
return TaguchiLossCalcResults(
df=results_df
)
def expected_loss_calc(
data: pd.Series,
USL: float,
LSL: float,
Target: Optional[float] = None,
cost_of_scrap: float = 1,
# round_value: Optional[float] = 1
) -> ExpectedLossCalcResults:
"""
Docstring for expected_loss
A : Assigned loss at specification limits, default is 1
"""
# --- VALIDATION ---
if USL <= LSL:
raise ValueError("USL must be greater than LSL")
# Set default target if not provided
if Target is None:
Target = (USL + LSL) / 2
# --- CONFIGURATION ---
mean = data.mean()
stdev = data.std()
Tolerance = USL - LSL
x_scrap = Tolerance/2
# --- CALCULATIONS ---
K = cost_of_scrap/(x_scrap)**2
expected_loss = K*((mean - Target)**2 + stdev**2)
# --- RETURN ---
stats_df = pd.DataFrame({
"Metric": ["E{L(x)}", "Mean", "Stdev", "k", "Cost of Scrap ($)",
"USL", "LSL", "Target", "Tolerance"],
"Value": [expected_loss, mean, stdev, K, cost_of_scrap,
USL, LSL, Target, Tolerance]
})
return ExpectedLossCalcResults(
df=stats_df
)