# Safety Maintains Lean Sustainability and Increases Performance through Fault Control
## Appendix A
t and had k!= downtime_s.length; k++)  
curr_avg += downtime_s[k]['price']/Cost_track;  
r_vags.push({ set: downtime_s.slice(i, i + Cost_track), avg: curr_avg });  
avg_prev = curr_avg;  
_return_ raysg;_  
_var input_dataset = [], result = [];_  
_var data_rav = []; var sma_vec = [];_  
_function_Init_() _{  
_initTabs_('Dataset'); _initDataset_();  
_document.getElementByild_(\"n-items\").value = \"50\";_  
_document.getElementByild_(\"window-size\").value = \"12\";_  
_document.getElementByild_('input-data').addEventListener_('change', readInputFile, false);_  
_function_initTabs_(tab) {varnbar = document.getElementsByClassName(\"nav navbar-nav\"); navbar[0].getElementsByTagName(\"li\")[0].className += \"active\"; document.getElementByld(\"dataset\").style.display = \"none\"; document.getElementByld(\"graph-plot\").style.display = \"none\"; setContentView(tab); } function setTabActive(event, tab) { var navbar = document.getElementsByClassName(\"nav navbar-nav\"); var tabs = navbar[0].getElementsByTagName(\"li\"); for (var index = 0; index < tabs.length; index++) if (tabs[index].className == \"active\") tabs[index].className = \"\"; if (event.currentTarget!= null) { event.currentTarget.className += \"active\"; } var callback = null; if (tab == \"Neural Network\") { callback = function () { document.getElementByld(\"train_set\").innerHTML = getSMATable(1); } } setContentView(tab, callback); } function setContentView(tab, callback) { var tabs.content = document.getElementsByClassName(\"container\"); for (var index = 0; index < tabs.content.length; index++) tabs.content[index].style.display = \"none\"; if (document.getElementByld(tab).style.display == \"none\") document.getElementByld(tab).style.display = \"block\"; if (callback!= null) { callback(); } } function readInputFile() { var file = e.target.files[0]; var reader = new FileReader(); reader.onload = function() { var contents = e.target.result; document.getElementByld(\"input-data\").value = \"\"; parseCSVData(contents); }; reader.readAsText(file); } function parseCSVData(contents) { data_rau = []; sma_vec = []; var rows = contents.split(\"\(\backslash\)\"); var params = rows[0].split(\",\"); var size = parseInt(params[0].split(\"=\")[1]); var window_size = parseInt(params[1].split(\"=\")[1]); document.getElementByld(\"n-items\").value = size.toString(); document.getElementByld(\"window-size\").value = window_size.toString(); for (var index = 1; index < size + 1; index++) { var cols = rows[index].split(\",\"); data_rau.push[ {id: cols[0], timestamp: cols[1], price: cols[2]}); } sma_vec = ComputeSMA(data_rau, window_size);onInputDataClick(); } functioninitDataset(){ varn_items=parselnt(document,getElementByld(\"n-items\").value); varwindow_size=parselnt(document,getElementByld(\"window-size\").value); data_raw=GenerateDataset(n_items); sma_vec=ComputeSMA(data_raw,window_size); onInputDataClick(); } async functiononTrainClick(){ varinputs=sma_vec.mapfunction(inp_){ returninp_f| returninp_f|(val){ returninp_f|set'.mapfunction(val){ returnval'price'};}})); varoutputs=sma_vec.mapfunction(output_f|returnout_f|avg?|); varn_epochs=parselnt(document,getElementByld(\"n-epochs\").value); varwindow_size=parselnt(document,getElementByld(\"window-size\").value); varlr_rate=parseFloat(document.getElementByld(\"learning-rate\").value); varn_hl=parselnt(document,getElementByld(\"hidden-layers\").value); varn_items=parselnt(document,getElementByld(\"n-items-percent\").value); var callback=function(epoch,log){ varlog_nn=document.getElementByld(\"nn_log\").innerHTML; log_nn+=\"<div>Epoch:\"+(epoch+1)+\"Loss:\"+log_loss+\"<div>\"; document.getElementByld(\"nn_log\").innerHTML=log_nn; document.getElementByld(\"training_pg\").style.width=((epoch+1)+\"(100/n_epochs)).toString()+\"%\"; document.getElementByld(\"training_pg\").innerHTML=((epoch+1)+\"(100/n_epochs)).toString()+\"%\"; } result=awaitrainModel(inputs,outputs, n_items,window_size,n_epochs,lr_rate,n_hl, callback); alert('Your model has been successfully trained...'); } functiononPredictClick(view){ varinputs=sma_vec.mapfunction(inp_f){ returninp_f|(val){ returnval'price'};}); }; varoutputs=sma_vec.mapfunction(output_f|returnout_f|avg?|); varn_items=parselnt(document.getElementByld(\"n-items-percent\").value); varoutps=outputs.slice(Math,floor(n_items/100*outputs.length),outputs.length); varpred_vals=Predict(inputs,n_items,result['model']); vardata_output=\"\"; for(varindex=0;index<pred_vals.length;index++){ data_output=\"\"<tr><td>\"+(index+1)+\"</td><td>\"+(index+1)+\"</td><td>\"+(n_steps[index]+\"</td><td>\"; } document.getElementByld(\"pred-res\").innerHTML=\"<tableclass=\"<table<)\"><thead><tr><th scope=\"<col\">+(hl><th scope=\"<col\">+(hl><thand><thand><thand><thand><thand><thand><thand>\"; <thand><thand><thand><thand><thand><thand>\"; varwindow_size=parselnt(document.getElementByld(\"window-size\").value); vartimestamps_a=data_raw.mapfunction(val){ returnval'timestamp'};}); vartimestamps_b=data_raw.mapfunction(val){ returnval['timestamp'];}); returnval['timestamp'];}).slice(window_size,data_raw.length); vartimestamps_c=data_raw.mapfunction(val){ returnval['timestamp'];}).slice(window_size+Math,floor(n_items/100*outputs.length),data_raw.length); varsma=sma_vec.mapfunction(val){ returnval'ag'};}); varprices=data_raw.mapfunction(val){ returnval'price'};}); vargraph_plot=document.getElementByld('graph-pred');Polty.newPlot(graph_plot, [l x: timestamps_a, y: prices, name: \"Series\" ]], { margin: { t: 0 1}};  Plotly.plot(graph_plot, [l x: timestamps_b, y: sma, name: \"SMA\" ]], { margin: { t: 0 1}};  Plotly.plot(graph_plot, [l x: timestamps_c, y: pred_vals, name: \"Predicted\" ]], { margin: { t: 0 1}}); } function getInputDataTable() { var data_output = \"\";  for (var index = 0; index < data_raw.length; index++) {  data_output += \"\"<tr><td>\" + data_raw[index['id'] + \"<td><td>\" + data_raw[index]['timestep'] + \"<td><td>\" + data_raw[index]['price'] + \"<td><tr>\"; }  return \"<table class=\"table\"><td><tr>\"<th scope=\"col\">\">Timestamp<th> <th scope=\"col\">Feature<th></head><tbody>\" + data_output + \"</thody></table>\"; } function getSMATable(view) { var data_output = \"\";  if (view == 0) { for (var index = 0; index < sma_vec.length; index++) {  var set_output = \"\";  var set = sma_vec[index]['set'];  for (var = 0; t < set.length; t++) {  set_output += \"\"<tr><td width=\" \"30 px\">\" + set[tll'price'] + \"<td><td>\" + set[tll'timestamp'] + \"<td><td>\" + set[tll'timestamp'] + \"<td><tr>\"; } data_output += \"\"<tr><td width=\" \"20 px\">\" + (index + 1) + \"<td><td>\" + \"<table width=\"100 px\" class=\"table\">\" + set_output + \"<tr><td><<>\" + \"SMA(t) = \"+ sma_vec[index]['avg'] + \"</b></tr></td></table></td></tr>\";  return \"<table class=\"table\"><thehead><tr></th scope=\"col\">Time Series</th> </head><tbody>\" + data_output + \"</thody></table>\"; } else if (view == 1) { var set = sma_vec.map(function (val) { return wall'set']; }); for (var index = 0; index < sma_vec.length; index++) {  data_output += \"\"<tr><td width=\"20 px\">\" + (index + 1) + \"<td><td>\" + set[index].map(function (val) { return (Math.round(['price'] * 10000)/10000).toString(); ).toString() + \" ]<td><td>\" + sma_vec[index]['avg'] + \"</td></td></td>\"; } return \"<table class=\"table\"><thehead><tr><th scope=\"col\">\">#</th><top=\"Input</th><th scope=\"col\">Output</th></head><tbody>\" + data_output + \"</body></table>\"; } function onInputDataClick() {  document.getElementByld(\"dataset\").style.display = \"block\"; document.getElementByld(\"graph-plot\").style.display = \"block\"; document.getElementByld(\"data\").innerHTML = getInputDataTable(); var timestamps = data_raw.map(function (val) { return wall'timestamp']; }); var prices = data_raw.map(function (val) { return wall'price']; }); var graph_plot = document.getElementByld('graph');Polty.newPlot(graph_plot,[l x: timestamps, y: prices, name: \"Stocks Prices\"], [ margin: { t: 0 } ] ); } function onSMAClick() { document.getElementByld(\"data\").innerHTML=getSMATable(0); var sma=sma_vec.map(function(val){ returnval'avg'l:}); var prices=data_raw.map(function(val){ returnval'price'}; l); var window_size=parseult(document.getElementByld(\"window-size\").value); var timestamps_a=data_raw.map(function(val){ returnval'timestamp'l:}); var timestamps_b=data_raw.map(function(val){ returnval'timestamp'l:}).slice(window_size,data_raw.length); var graph_plot=document.getElementByld('graph'); Plotly.newPlot(graph_plot,[l x: timestamps_a, y: prices, name: \"Series\"], { margin: { t: 0 } }); Plotly.plot(graph_plot,[l x: timestamps_b, y: sma, name: \"SMA\"], { margin: { t: 0 } }) function ComputeSMA(time_s,window_size) { var r_args=[],avg_prev=0; for (let i=0; i<=time_s.length-window_size; i++) { var curr_args=0.00, t=i+window_size; for (let k=i; k<tandandk<=time_s.length; k++) curr_avg_+=time_s[k]!price']window_size; r_args.push([ set:time_s.slice(i, i+window_size),avg: curr_avg_]); avg_prev=curr_avg; } returnr_args; } functionGenerateDataset(size) { var dataset=[]; var dt1=newDate(), dt2=newDate(); dt1.setDate(dt1.getDate()-1); dt2.setDate(dt2.getDate()-size); var time_start=dt2.getTime(); var time_diff=newDate().getTime()-dt1.getTime(); let curr_time=time_start; for (let i=0; i<size; i++, curr_time+=time_diff){ var curr_dt=newDate(curr_time); var hours=Math.floor(Math.random()*100%24); var minutes=Math.floor(Math.random()*100%60); var seconds=Math.floor(Math.random()*100%60); dataset.push([ id: i+1, price: (Math.floor(Math.random()*10)+5)+Math.random(), timestamp: curr_dt.getFullYear()+\"-\"+((curr_dt.getMonth()>9)?curr_dt.getMonth()): (\"0\"+ curr_dt.getMonth())+\"-\"+ ((curr_dt.getDate())>9)?curr_dt.getDate(): (\"0\"+curr_dt.getDate()))+\"+((hours>9)?hours: (\"0\"+hours)) + \"+ (((minutes>9)?minutes: (\"0\"+minutes))+\":\"+((seconds>9)?seconds: (\"0\"+seconds)+\"]\")); } return dataset; } async functiontrainModel(inputs, outputs, size, window_size, n_epochs, learning_rate, n_layers, callback)\(\{\)  
\(\text{{\it const input\_layer\_shape}=window\_size;}\)  
\(\text{{\it const input\_layer\_neurons}=100;}\)  
\(\text{{\it const run\_input\_layer\_features}=10;}\)  
\(\text{{\it const run\_input\_layer\_timesteps}=input\_layer\_neurons}\)/\(\text{{\it run\_input\_layer\_features};}\)  
\(\text{{\it const run\_input\_shape}=\text{{\it 1 run\_input\_layer\_features},}\) \(\text{{\it rnn\_input\_layer\_timesteps}}\) \(\text{{\it]}}\)  
\(\text{{\it const run\_output\_neurons}=20;}\)  
\(\text{{\it const run\_batch\_size}=window\_size;}\)  
\(\text{{\it const output\_layer\_shape}=\text{{\it rnn\_output\_neurons};}}\)  
\(\text{{\it const output\_layer\_neurons}=1;}\)  
\(\text{{\it const model}={\it tf\_sequential}();}\)  
\(\text{{\it inputs}=inputs\_slice(0,{\it Math\_floor(size/100\ *inputs.length)});}\)  
\(\text{{\it outputs}={\it outputs}.slice(0,{\it Math\_floor(size/100\ *outputs.length)});}\)  
\(\text{{\it const xs}={\it tf\_tensor2d(inputs,{\it [inputs\_length,{\it inputs[0].length]}.div(tf.scalar(10))}})}\)  
\(\text{{\it const ys}={\it tf\_tensor2d(outputs,{\it [outputs\_length,{\it 1}].reshape[out\_paths.length,{\it 1}].div(tf.scalar(10))}})}\)  
\(\text{{\it model.add(tf\_layers.dense({\it [units: input\_layer\_neurons, inputShape: [input\_layer\_shape]]})})}\)  
\(\text{{\it model.add(tf\_layers.reshape({\it targetShape: rnn\_input\_ shape]})})}\)  
\(\text{{\it var lstm\_cells}=[];}\)  
\(\text{{\it for (let index=0; index<n\_layers; index++)}}\)  
\(\text{{\it lstm\_cells}.push({\it tf\_layers.lstmCell({\it [units: rnn\_output\_neurons}))})}\)  
\(\text{{\it}}\)  
\(\text{{\it model.add(tf\_layers.rnn({\it cell: lstm\_cells},}\)  
\(\text{{\it inputShape: rnn\_input\_shape},{\it returnSequences: false})})}\)  
\(\text{{\it model.add(tf\_layers.dense({\it [units: output\_layer\_neurons, inputShape: [output\_layer\_shape]]})})}\)  
\(\text{{\it const opt\_adam}={\it tf\_train.adam({\it learning\_rate})}}\)  
\(\text{{\it model.compile({\it optimizer: opt\_adam},{\it loss:'meanSquaredError'})}}\)  
\(\text{{\it const\ list}={\it await\_model.fit(xs,ys},\)  
\(\{\) batchSize: rnn\_batch_size, epochs: n_epochs, callbacks: {  
\(\) onEpochEnd: async (epoch, log) => { callback(epoch, log); }}\)  
\(\text{{\it return\ {model: model, stats: list}}}\)  
\(\text{{\it function\ Predict(inputs,{\it size, model)}}}\)  
\(\text{{\it var\ in\ {\it ips}=inputs.slice({\it Math\_floor(size/100\ *inputs.length), inputs.length)}}}\)  
\(\text{{\it const\ out\ {\it ps}=model.predict(tf.Tensor2d({\it in\ {\it ips}.length,{\it ips}[0].length}).div(tf.scalar(10))).mul(10)}}\)  
\(\text{{\it return\ Array\_from(out\_s.dataSync(0))}}\)