Skip to content

Commit 41f363e

Browse files
author
minjk-bl
committed
Edit probDist app
1 parent c17d5f6 commit 41f363e

File tree

2 files changed

+199
-79
lines changed

2 files changed

+199
-79
lines changed

visualpython/html/m_stats/probDist.html

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,55 @@
1010
-->
1111
<body>
1212
<div class="vp-grid-box">
13+
<select id="distType" class="vp-big-select vp-state">
14+
</select>
1315
<div class="vp-grid-border-box">
14-
<div class="vp-grid-col-160">
15-
<label for="distType" class="vp-orange-text">Probability distribution</label>
16-
<select id="distType" class="vp-select vp-state">
17-
</select>
18-
</div>
1916
<div class="vp-pd-dist-option-box vp-grid-col-160">
2017

2118
</div>
19+
<hr style="margin: 5px 0;"/>
2220
<div class="vp-grid-col-160">
23-
<label for="allocateTo" class="vp-orange-text">Allocate to</label>
24-
<input type="text" class="vp-input vp-state" id="allocateTo" placeholder="_res" />
21+
<label for="action" class="vp-orange-text">Action</label>
22+
<select id="action" class="vp-select l">
23+
<option value="random-number">Generate random numbers</option>
24+
<option value="distribution-plot">Show distribution plot</option>
25+
<option value="stats-to-pvalue">Statistics to P-value</option>
26+
<option value="pvalue-to-stats">P-value to Statistics</option>
27+
</select>
2528
</div>
26-
</div>
27-
<div class="vp-grid-border-box">
28-
<label for="probMassFunc">Display</label>
29-
<div class="vp-grid-box">
30-
<label class="vp-pd-display-option dist cont"><input type="checkbox" id="probMassFunc" class="vp-state"><span>Probability mass function</span></label>
29+
<div class="vp-grid-border-box vp-pd-action-box random-number">
30+
<div class="vp-grid-col-160">
31+
<label for="size" class="vp-orange-text">Size</label>
32+
<input type="number" class="vp-input vp-state" id="size" value="10000" />
33+
<label for="randomState">Random state</label>
34+
<input type="number" class="vp-input vp-state" id="randomState"/>
35+
<label for="allocateTo" class="vp-orange-text">Allocate to</label>
36+
<input type="text" class="vp-input vp-state" id="allocateTo" value="samples"/>
37+
</div>
38+
<label><input type="checkbox" id="sampledDist" class="vp-state" checked><span>Show sampled distribution</span></label>
39+
</div>
40+
<div class="vp-grid-border-box vp-pd-action-box distribution-plot" style="display:none;">
41+
<label class="vp-pd-display-option dist"><input type="checkbox" id="probDensityFunc" class="vp-state"><span>Probability density function</span></label>
42+
<label class="vp-pd-display-option cont"><input type="checkbox" id="probMassFunc" class="vp-state"><span>Probability mass function</span></label>
3143
<label class="vp-pd-display-option cont"><input type="checkbox" id="cumDistFunc" class="vp-state"><span>Cumulative distribution function</span></label>
32-
<label class="vp-pd-display-option dist cont"><input type="checkbox" id="sampledDist" class="vp-state" checked><span>Sampled distribution</span></label>
44+
</div>
45+
<div class="vp-grid-border-box vp-grid-col-160 vp-pd-action-box stats-to-pvalue" style="display:none;">
46+
<label for="stats" class="vp-orange-text">Statistic</label>
47+
<input type="number" class="vp-input vp-state" id="stats" />
48+
<label for="pAlter">Alternative</label>
49+
<select id="pAlter" class="vp-select vp-state">
50+
<option value="one-sided">One-sided</option>
51+
<option value="two-sided" selected>Two-sided</option>
52+
</select>
53+
</div>
54+
<div class="vp-grid-border-box vp-grid-col-160 vp-pd-action-box pvalue-to-stats" style="display:none;">
55+
<label for="pvalue" class="vp-orange-text">Proportional value</label>
56+
<input type="number" class="vp-input vp-state" id="pvalue" />
57+
<label for="statsAlter">Alternative</label>
58+
<select id="statsAlter" class="vp-select vp-state">
59+
<option value="one-sided">One-sided</option>
60+
<option value="two-sided" selected>Two-sided</option>
61+
</select>
3362
</div>
3463
</div>
3564
</div>

visualpython/js/m_stats/ProbDist.js

Lines changed: 157 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,23 @@ define([
3636

3737
this.state = {
3838
distType: 'normal',
39-
allocateTo: '',
4039
userOption: '',
40+
action: 'random-number',
41+
// random-number
42+
size: 10000,
43+
randomState: '',
44+
allocateTo: '',
45+
sampledDist: true,
46+
// distribution-plot
47+
probDensityFunc: false,
4148
probMassFunc: false,
4249
cumDistFunc: false,
43-
sampledDist: true,
50+
// stats-to-pvalue
51+
stats: '',
52+
pAlter: 'two-sided',
53+
// pvalue-to-stats
54+
pvalue: '',
55+
statsAlter: 'two-sided',
4456
...this.state
4557
};
4658

@@ -65,7 +77,7 @@ define([
6577
let distType = $(this).val();
6678
that.state.distType = distType;
6779
$(that.wrapSelector('.vp-pd-dist-option-box')).html(that.templateForOption(distType));
68-
80+
6981
$(that.wrapSelector('.vp-pd-display-option')).hide();
7082
// show/hide display option
7183
if (that.distList[0].child.includes(distType)) {
@@ -83,6 +95,24 @@ define([
8395
$(that.wrapSelector('#vp_installLibrary')).hide();
8496
}
8597
});
98+
99+
$(this.wrapSelector('#action')).on('change', function() {
100+
let action = $(this).val();
101+
that.state.action = action;
102+
103+
$(that.wrapSelector('.vp-pd-action-box')).hide();
104+
$(that.wrapSelector('.vp-pd-action-box.' + action)).show();
105+
106+
$(that.wrapSelector('.vp-pd-display-option')).hide();
107+
// show/hide display option
108+
if (that.distList[0].child.includes(that.state.distType)) {
109+
// discrete option
110+
$(that.wrapSelector('.vp-pd-display-option.dist')).show();
111+
} else {
112+
// continuous option
113+
$(that.wrapSelector('.vp-pd-display-option.cont')).show();
114+
}
115+
});
86116
}
87117

88118
templateForBody() {
@@ -195,10 +225,14 @@ define([
195225

196226
generateCode() {
197227
this.config.checkModules = ['pd'];
198-
let { distType, userOption, probMassFunc, cumDistFunc, sampledDist, allocateTo } = this.state;
199-
if (allocateTo === '') {
200-
allocateTo = '_res';
201-
}
228+
let {
229+
distType, userOption, action,
230+
size, randomState, allocateTo, sampledDist,
231+
probDensityFunc, probMassFunc, cumDistFunc,
232+
stats, pAlter,
233+
pvalue, statsAlter
234+
} = this.state;
235+
202236
let codeList = [];
203237
let code = new com_String();
204238
/**
@@ -215,66 +249,123 @@ define([
215249
code.append(modelCode);
216250
codeList.push(code.toString());
217251

218-
/**
219-
* Display option
220-
*/
221-
if (probMassFunc === true) {
222-
this.addCheckModules('np');
223-
this.addCheckModules('plt');
224-
code = new com_String();
225-
if (this.distList[0].child.includes(distType)) {
226-
code.appendFormatLine("# Probability mass function ({0})", label);
227-
code.appendLine("_x = [0, 1]");
228-
code.appendLine("plt.bar(_x, _rv.pmf(_x))");
229-
code.appendLine();
230-
code.appendLine("plt.title('Probability mass function: Bernoulli distribution')");
231-
code.appendLine("plt.xlim(-1, 2)");
232-
code.appendLine("plt.ylim(0, 1)");
233-
code.appendLine("plt.xticks([0, 1])");
234-
code.appendLine("plt.xlabel('$x$')");
235-
code.appendLine("plt.ylabel('$p(x)$')");
236-
code.appendLine("plt.show()");
237-
} else {
238-
code.appendFormatLine("# Probability density function ({0})", label);
239-
code.appendLine("_x = np.linspace(-5, 5, 100)");
240-
code.appendLine("plt.plot(_x, _rv.pdf(_x))");
241-
code.appendLine();
242-
code.appendLine("plt.title('Probability density function: Normal distribution')");
243-
code.appendLine("plt.xlabel('$x$')");
244-
code.appendLine("plt.ylabel('$p(x)$')");
245-
code.appendLine("plt.show()");
246-
}
247-
codeList.push(code.toString());
248-
}
249-
if (this.distList[1].child.includes(distType) && cumDistFunc === true) {
250-
this.addCheckModules('np');
251-
this.addCheckModules('plt');
252-
code.appendFormatLine("# Cumulative distribution function ({0})", label);
253-
code.appendLine("_x = np.linspace(-5, 5, 100)");
254-
code.appendLine("plt.plot(_x, _rv.cdf(_x))");
255-
code.appendLine();
256-
code.appendLine("plt.title('Cumulative distribution function: Normal distribution')");
257-
code.appendLine("plt.xlabel('$x$')");
258-
code.appendLine("plt.ylabel('$F(x)$')");
259-
code.appendLine("plt.show()");
260-
}
261-
if (sampledDist === true) {
262-
this.addCheckModules('plt');
263-
code = new com_String();
264-
code.appendFormatLine("# Generate random numbers ({0})", label);
265-
code.appendFormatLine('{0} = _rv.rvs(size=10000, random_state=0)', allocateTo);
266-
code.append(allocateTo);
267-
codeList.push(code.toString());
252+
switch (action) {
253+
case 'random-number':
254+
code = new com_String();
255+
code.appendFormatLine("# Generate random numbers ({0})", label);
256+
code.appendFormatLine('{0} = _rv.rvs(size={1}', allocateTo, size);
257+
if (randomState !== '') {
258+
code.appendFormat(", random_state={0}", randomState);
259+
}
260+
code.appendLine(')');
261+
code.append(allocateTo);
262+
codeList.push(code.toString());
268263

269-
code = new com_String();
270-
code.appendFormatLine("# Sample distribution ({0})", label);
271-
code.appendLine("import seaborn as sns");
272-
code.appendLine();
273-
code.appendFormatLine("sns.histplot({0}, stat='density', kde=True)", allocateTo);
274-
code.appendLine("plt.title('Generate random numbers: Normal distribution')");
275-
code.appendLine("plt.xlabel('$x$')");
276-
code.append("plt.show()");
277-
codeList.push(code.toString());
264+
if (sampledDist === true) {
265+
this.addCheckModules('plt');
266+
this.addCheckModules('sns');
267+
code = new com_String();
268+
code.appendFormatLine("# Sample distribution ({0})", label);
269+
code.appendLine("import warnings");
270+
code.appendLine("with warnings.catch_warnings():");
271+
code.appendLine(" warnings.simplefilter(action='ignore', category=Warning)");
272+
code.appendFormatLine(" sns.histplot({0}, stat='density', kde=True)", allocateTo);
273+
code.appendLine(" plt.title('Generate random numbers: Normal distribution')");
274+
code.appendLine(" plt.xlabel('$x$')");
275+
code.append(" plt.show()");
276+
codeList.push(code.toString());
277+
}
278+
break;
279+
case 'distribution-plot':
280+
if (this.distList[0].child.includes(distType)) {
281+
if (probDensityFunc === true) {
282+
this.addCheckModules('np');
283+
this.addCheckModules('plt');
284+
code = new com_String();
285+
code.appendFormatLine("# Probability density function ({0})", label);
286+
code.appendLine("import warnings");
287+
code.appendLine("with warnings.catch_warnings():");
288+
code.appendLine(" _x = np.linspace(-5, 5, 100)");
289+
code.appendLine(" plt.plot(_x, _rv.pdf(_x))");
290+
code.appendLine();
291+
code.appendLine(" plt.title('Probability density function: Normal distribution')");
292+
code.appendLine(" plt.xlabel('$x$')");
293+
code.appendLine(" plt.ylabel('$p(x)$')");
294+
code.append(" plt.show()");
295+
codeList.push(code.toString());
296+
}
297+
} else {
298+
if (probMassFunc === true) {
299+
this.addCheckModules('np');
300+
this.addCheckModules('plt');
301+
code = new com_String();
302+
code.appendFormatLine("# Probability mass function ({0})", label);
303+
code.appendLine("import warnings");
304+
code.appendLine("with warnings.catch_warnings():");
305+
code.appendLine(" _x = [0, 1]");
306+
code.appendLine(" plt.bar(_x, _rv.pmf(_x))");
307+
code.appendLine();
308+
code.appendLine(" plt.title('Probability mass function: Bernoulli distribution')");
309+
code.appendLine(" plt.xlim(-1, 2)");
310+
code.appendLine(" plt.ylim(0, 1)");
311+
code.appendLine(" plt.xticks([0, 1])");
312+
code.appendLine(" plt.xlabel('$x$')");
313+
code.appendLine(" plt.ylabel('$p(x)$')");
314+
code.append(" plt.show()");
315+
codeList.push(code.toString());
316+
}
317+
if (cumDistFunc === true) {
318+
this.addCheckModules('np');
319+
this.addCheckModules('plt');
320+
code = new com_String();
321+
code.appendFormatLine("# Cumulative distribution function ({0})", label);
322+
code.appendLine("import warnings");
323+
code.appendLine("with warnings.catch_warnings():");
324+
code.appendLine(" _x = np.linspace(-5, 5, 100)");
325+
code.appendLine(" plt.plot(_x, _rv.cdf(_x))");
326+
code.appendLine();
327+
code.appendLine(" plt.title('Cumulative distribution function: Normal distribution')");
328+
code.appendLine(" plt.xlabel('$x$')");
329+
code.appendLine(" plt.ylabel('$F(x)$')");
330+
code.append(" plt.show()");
331+
codeList.push(code.toString());
332+
}
333+
}
334+
break;
335+
case 'stats-to-pvalue':
336+
if (pAlter === 'one-sided') {
337+
// one-sided
338+
code = new com_String();
339+
code.appendLine("# Proportional values");
340+
code.appendFormatLine("p_value = _rv.sf(abs({0}))", stats);
341+
code.append("p_value");
342+
codeList.push(code.toString());
343+
} else {
344+
// two-sided
345+
code = new com_String();
346+
code.appendLine("# Proportional values");
347+
code.appendFormatLine("p_value = _rv.sf(abs({0}))*2", stats);
348+
code.append("p_value");
349+
codeList.push(code.toString());
350+
}
351+
break;
352+
case 'pvalue-to-stats':
353+
if (statsAlter === 'one-sided') {
354+
// one-sided
355+
code = new com_String();
356+
code.appendLine("# Statistic");
357+
code.appendFormatLine("statistic = _rv.isf({0})", pvalue);
358+
code.append("statistic");
359+
codeList.push(code.toString());
360+
} else {
361+
// two-sided
362+
code = new com_String();
363+
code.appendLine("# Statistic");
364+
code.appendFormatLine("statistic = _rv.isf({0}/2)", pvalue);
365+
code.append("statistic");
366+
codeList.push(code.toString());
367+
}
368+
break;
278369
}
279370

280371
return codeList;

0 commit comments

Comments
 (0)