@@ -36,11 +36,23 @@ define([
36
36
37
37
this . state = {
38
38
distType : 'normal' ,
39
- allocateTo : '' ,
40
39
userOption : '' ,
40
+ action : 'random-number' ,
41
+ // random-number
42
+ size : 10000 ,
43
+ randomState : '' ,
44
+ allocateTo : '' ,
45
+ sampledDist : true ,
46
+ // distribution-plot
47
+ probDensityFunc : false ,
41
48
probMassFunc : false ,
42
49
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' ,
44
56
...this . state
45
57
} ;
46
58
@@ -65,7 +77,7 @@ define([
65
77
let distType = $ ( this ) . val ( ) ;
66
78
that . state . distType = distType ;
67
79
$ ( that . wrapSelector ( '.vp-pd-dist-option-box' ) ) . html ( that . templateForOption ( distType ) ) ;
68
-
80
+
69
81
$ ( that . wrapSelector ( '.vp-pd-display-option' ) ) . hide ( ) ;
70
82
// show/hide display option
71
83
if ( that . distList [ 0 ] . child . includes ( distType ) ) {
@@ -83,6 +95,24 @@ define([
83
95
$ ( that . wrapSelector ( '#vp_installLibrary' ) ) . hide ( ) ;
84
96
}
85
97
} ) ;
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
+ } ) ;
86
116
}
87
117
88
118
templateForBody ( ) {
@@ -195,10 +225,14 @@ define([
195
225
196
226
generateCode ( ) {
197
227
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
+
202
236
let codeList = [ ] ;
203
237
let code = new com_String ( ) ;
204
238
/**
@@ -215,66 +249,123 @@ define([
215
249
code . append ( modelCode ) ;
216
250
codeList . push ( code . toString ( ) ) ;
217
251
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 ( ) ) ;
268
263
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 ;
278
369
}
279
370
280
371
return codeList ;
0 commit comments