# Variable calculations should be done in this file.
# It is read and interpreted by wims for all valid calls to the module.


# how to detect the need to compute?
!if $cmd=reply
 	error=
 	compute=yes
	 pari_precision=8
	octave_precision=7
	wims_multiexec=pari,octave
	wims_backslash_insmath=yes
	ins_align=middle
	!distribute item antiquewhite, navajowhite into color0, color1

	data=!declosing $data
	data=!replace internal ; by , in $data
	data=!lines2items $data

	!if $data=$empty and $type iswordof pdf cdf inv
		error=!append item bad_data to $error
	!endif
	!if $type=$empty
		error=!append item error_qu to $error
	!endif
	!if $law=$empty
		error= !append item error_click to $error
	!endif		
	tableau=$empty
	cnt_data= !itemcnt $data
	datasort=!sort item $data
	datamin=!item 1 of $datasort
	datamax=!item -1 of $datasort
	
	!if inv iswordof $type and ($datamin<0 or $datamax >1) 
		error=!append item range_inv to $error
	!endif

	!for i in $law
		u=!positionof item $i in $liste
		v=!item $u of $liste2
		w=!item $u of $liste3
		parm_liste=!replace internal ; by $\
$ in $(parm_$i)
		parm_lcnt=!linecnt $parm_liste
		!for j= 1 to $parm_lcnt
			parmj=!line $j of $parm_liste
			parm_cnt= !itemcnt $parmj
			!if $parm_cnt <>$w
				error=!append item parm$i to $error
			!endif
			!distribute item $parmj into parm1, parm2
			!if $parm1=$empty
				error=!append item bad_data to $error
			!endif
			!if $i isitemof binomial,pascal, chi2,student,fisher and $[floor($parm1)]<>$parm1
				error=!append item integer$i to $error
			!endif
			!if ($i isitemof binomial,pascal and (($parm2>=1) or ($parm2<=0))) or ($i isin geometric and  (($parm1>=1) or ($parm1<=0)))
				error= !append item neg$i to $error
			!endif
			!if ($i isitemof poisson,exponentiel,weibull,gamma,beta and $parm1<=0) or ($i isitemof normal,weibull,gamma,beta and $parm2<=0)
				error=!append item pos$i to $error
			!endif
		!next j
	!next i
	!if $error<>$empty
		!exit
	!endif
	data=[$data]
	 t=1
	!for i in $law
		!if  $i iswordof laplace logistic
			parm_$i=$empty
		!endif	
		u=!positionof item $i in $liste
		v=!item $u of $liste2
		w=!item $u of $liste3
		loi=$i$type
		parm_liste=!replace internal ; by $\
$ in $(parm_$i)
		parm_lcnt=!linecnt $parm_liste
		!if laplace isin $law or logistic isin $law
			parm_lcnt=1
		!endif	
		!if $type=histo
			!set rangex=
			!set range_y=
			!for j= 1 to $parm_lcnt
				!set  parmj=!line $j of $parm_liste
				!distribute item $parmj into parm1,parm2,parm3
				!set couleur=$(couleur$j)
				!read histoloi
				!set histogram1= !line 3 to -1  of  $(hist_$i)
				!if $j=1
					!set histogram=$histogram1
				!else
					!set histogram=$histogram\
						$histogram1
				!endif
				!set taillex= !line 1  of $(hist_$i)
				!set taillex=!replace internal xrange by $empty in $taillex
				!set rangex= !sort  numeric item $taillex, $rangex
				!set rangex= !item 1,-1 of $rangex
				!set tailley= !line 2  of $(hist_$i)
				!set tailley=!replace internal yrange by $empty in $tailley
				!set range_y= !sort numeric item $tailley, $range_y
				!set range_y= !item 1,-1 of $range_y
			!next j	
			!set  hist_$i=xrange $rangex\
			yrange $range_y\
		$histogram
			!if $i isitemof binomial,geometric, poisson, hypergeometric,pascal	
				!distribute item $rangex into ech1,ech2
				taille=$[10^(round(log($ech2-$ech1)/log(10))-1)]
				hist_$i=$(hist_$i)\
				parallel  0,-0.01, 0, 0.01,$taille,0,100,blue\
				text black, $ech2,0,medium, $ech2\
				text black, 0,0,medium , 0\
				text black, $taille,0,medium, $taille\
				text black, 0,0,medium , 0\
				$i
			!endif
		!else  
			!for j= 1 to $parm_lcnt
				parmj=!line $j of $parm_liste
				ans=$empty
				!read slib/stat/$loi $data, $parmj
				ans= $slib_out
				tableau2= !makelist  <td><font color="black"> x</b></font></td> for x in $ans
				tableau2=!items2words $tableau2
				tableau=$tableau\
				<tr align="center" bgcolor="$(color$t)"><th>$v</th><th>$parmj</th>$tableau2 </tr>
			!next j
			t=$[1-$t]
			!endif
	!next i
	data=!declosing $data
	tableau1= !makelist  <th> x </th> for x in $data
	tableau1=!items2words $tableau1
	tableau=<table align="center" border="1"> <tr align="center" >\
<th>Ley</th><th>Parámetro</th>$tableau1 </tr>\
$tableau\
</table>

wims_module_log=$cmd: law $law, type $type

