Repository navigation
Description
Getting started with Control Charts, mainly multivariate where statistics is similar to several other issues.
I put a script in the next comment with some experiments done in a few hours.
(Issue so I find this again, I will come back to it another time.)
current reference:
Bersimis, Psarakis, Panaretos (2007): Multivariate Statistical Process Control Charts: An Overview
many open design issues
example Hotelling T2 shewart chart (one observation)
3 distributions for mahalanobis distance (details somewhere in a robust issue or PR)
- chisquare: in large samples or known covariance
- beta: insample estimate of covariance, evaluation of in-sample observation
- F: covariance estimated in a different sample, evaluation of out-of-sample observation
in control charts: beta is used in phase one, F is used in phase two
that is we need to distinguish of an observation is in-sample or out-of-sample
what information do we need and keep track of:
- "static" charts like shewart chart
- need only mean and standard deviation or control limits
- we need estimation sample for updating the estimates
- we need some history, recent observations if we want to plot
- "dynamic" charts like EWMA or CUSUM
- need also last "state" to evaluate next observation, others are the same
Note: sample for updating estimation and sample for plotting with recent history do not use the same observations if the history includes out-of-control/outlier observations. Those are plotted but not included in the estimation.
Sheward, EWMA or CUSUM are the basic charts, but then we also have model based charts, e.g. using regression or time series models with charting of residuals or forecast error, and decomposition, PCA, PLS, based charts.
a different pattern are self-starting and automatic updating charts: those don't distinguish between phase 1 estimation and phase 2 charting.
current version 3 classes with largely copy-paste because I don't know what the common attributes will be.
MEWMA doesn't have explicit distribution for control limits. The usual method seems to be simulation, but bootstrap is also possible.
Activity
josef-pkt commentedon Jan 6, 2018
shangyian commentedon Feb 15, 2019
Out of curiosity, is this still being worked on? I'm interested in working on something similar that might fit this usecase.
josef-pkt commentedon Feb 15, 2019
It's one of my long term side projects.
My main problem is that I don't know what would make a good API and what would be useful user facing functions or methods for this. That's the main reason the above is in a script and not a PR.
I was also working on related hypothesis test as background for control charts but with more general applicability, like #4107 for multivariate hypothesis test which we don't have yet.
If you can provide ideas and feedback for the API design and possibly work on it, then we could get started with adding this to statsmodels.
@shangyian What's your usecase, datasize, type of data, ...?
shangyian commentedon Feb 16, 2019
Yeah, I think the API design part is complicated since it depends on exactly what people want to use it for.
In my case, I was building a control chart for CUSUM and EWMA (specifically for event count timeseries data), but I was only planning on using the control chart to replay over (and visualize) historical data as a way of validating the control chart's performance. I can also see it being useful to extend control charts to time series modelling (i.e., seasonal decomposition and charting control limits over the residuals).
I think for the purposes of generalization, the control chart should be able to handle arbitrary functions for the upper/lower control limits, along with some default, commonly used ones.
The other thing is that something like CUSUM itself can be broadly generalized, since the decision weights can be an arbitrary function, even though in practice the likelihood ratio is often used.$$S_i = max(0, S_{i−1} + D_i)$$ we can assign whatever we want to $D_i$ and it would just be a framework for collecting the statistic.
I.e., in
In any case, these are all use cases that don't involve streaming updates, but I'm assuming something like that would be out of scope for statsmodels.
josef-pkt commentedon Feb 16, 2019
That sounds like a good usecase that we can target with the implementation. There are several python packages for monitoring and charting that do all the data handling.
If we target prototyping and evaluation of control chart performance, then we don't have to worry directly about how the control charts are tied into the larger system and e.g. about how the real-time monitoring works.
We still need updating of control charts but only to simulate the process and charts for historical or artificial data.
We do get questions about pickling and updating in time series models, so there are use cases where models, prediction and forecasting are directly used "in production". But we can look at that when it shows up, if it ever does.
josef-pkt commentedon Feb 16, 2019
I converted my script to a notebook so it's easier to see the plots
https://gist.github.com/josef-pkt/8a2893dd2a22e9724eb6417d493955b2
I think we can put
control_chartsas module in statsmodels.stats. I guess it will not get large enough in the near future to use a separate directory for it. It can always be moved later if needed.shangyian commentedon Feb 16, 2019
I agree that it's better to stick to prototyping/evaluation of control charts rather than an actual production system. It's also much harder to design for the latter without knowledge of the size and source of the data streams. For my work, I will eventually be building real-time monitoring control charts, but that's for later..
Nice! Do you have a branch you're working off somewhere? Maybe I can try to add a CUSUM component that builds off of what you have so far.
shangyian commentedon Feb 16, 2019
Scratch that, I see the PR! 👍
josef-pkt commentedon Feb 17, 2019
@shangyian Are you able to work off my PR branch?
Do you know R to write some unit tests?
I think we could merge some initial, experimental version pretty soon, and then enhance and refactor directly for master.
My guess is that the base functionality and API should be pretty stable, but that we will need some time to get the code structure in an appropriate form. There are a large number of options and cases of control charts, so we will need a structure that makes it easy to add new options and methods reusing a general modular structure of classes and helper functions.
e.g. I think
statistic,center,uppandlow, partially independent of the internal computation, e.g. plotting x(bar)-charts and t-charts in the same class, or plotting transformed variables in Poisson or proportion chartsI think we can stick at the beginning with requiring "clean" samples for the estimation and update, i.e. outlier removal has to be done by the user or caller.
I'm not sure how to best include subgroup charts, i.e. whether an observation is a single item or an average of several items. If the group or batch size is constant, then it will not matter much, but if batch size varies, then we need
stdto depend on the batch size. (Related if we have systematic variation in variance/std.)excess dispersion in count variables, proportion (Binomial p-chart) and rates (Poisson c-chart or u-chart)
minitab documentation uses "Laney P' and U'" charts to estimate and account for excess dispersion https://support.minitab.com/en-us/minitab/18/Assistant_Attribute_Control_Charts.pdf
another article uses Agresti Coull interval for proportions (which we have in stats.proportions)
Wang, Hsiuying. "Comparison of p control charts for low defective rate." Computational Statistics & Data Analysis 53, no. 12 (2009): 4210-4220.
It looks like for Binomial and Poisson counts the situation with small sample approximation is similar to what we have in hypothesis tests and confidence intervals for rates and proportions.
(Aside: It's easy to get lost in this literature. There are a large number of variations for each type of chart, and the number of citations for articles are pretty low in most cases.)
josef-pkt commentedon Feb 17, 2019
recent developments in 2014
William H. Woodall & Douglas C. Montgomery (2014) Some Current Directions in the Theory and Application of Statistical Process Monitoring, Journal of Quality Technology, 46:1, 78-94, DOI: 10.1080/00224065.2014.11917955
looks useful to provide some extension ideas, variable selection, large data, parameter uncertainty, nonparametric, and to show what the researchers in the field consider important.
shangyian commentedon Feb 18, 2019
Yup I can work off your PR branch!
It's been a while since I've worked with R, but I will try. It looks like the most up-to-date R package for control charts is this one: https://anhoej.github.io/qicharts2/
I have indeed been getting lost in the literature... It seems like there are many aspects that can be customized for all the different charts, but most of that can be left for later customization. I'm going to try to do some light refactoring so that some of the distribution computations can be reused across control charts.
josef-pkt commentedon Feb 18, 2019
@shangyian As a reminder: Most R packages are GPL licensed which is not compatible with our BSD-3 license. So we can use them for unit tests and the documentation, but we are not allowed to copy/translate the code.
Do you also want to create a base class and refactor the plot method, or should I do this before going back to other issues and PRs?
for unit tests: I have Stata which I most often use for unit tests, but they have only the very basic charts with just 3 sigma limits. And they use the range statistic for std, which from what I remember reading is not very efficient.
shangyian commentedon Feb 18, 2019
I can work on creating a base class and refactoring plot.
I'll make sure to reference papers for the control chart implementations. And just use R packages for verification in the unit tests.
josef-pkt commentedon Feb 19, 2019
Good, if you want to take over this part, then you could push a checkout of my branch to your github repo and open a new PR against statsmodels master.
In that case, you would get the CI test runs immediately after you push changes to your branch. I will then be reviewing the code, and eventually merge it without being involved in all changes.
shangyian commentedon Feb 19, 2019
Sounds good, just did that here: #5506