JavaScript vers VB

marinmarais Messages postés 104 Date d'inscription lundi 11 avril 2005 Statut Membre Dernière intervention 16 juillet 2010 - 18 avril 2005 à 09:17
marinmarais Messages postés 104 Date d'inscription lundi 11 avril 2005 Statut Membre Dernière intervention 16 juillet 2010 - 18 avril 2005 à 10:13
Bonjour à tous,

Permettez-moi de vous présenter mon problème :

J'ai récupéré un code source (dans le domaine public ;-) ) en Javascript permettant de calculer numériquement des fonctions de statistique assez lourdes.
Il s'agit notamment de la fonction équivalente nommée "Inverse Khi deux" dans Excel qui renvoie la valeur aléatoire à une probabilité p et un degré de liberté df.

J'ai traduis ce code pour l'insérer dans un programme.
Seulement, il y a une ligne de code que je ne comprends pas :

if (a > BIGX) {
.....

Ce "BIGX" n'est pas déclaré en tant que variable. Après moultes recherches sur la signification de ce terme, je doit avouer ma défaite et m'en remettre à votre aide.

Pour info,
a = 0.5 * p avec p la probabilité (comprise donc entre 0 et 1).

Le site sur lequel j'ai trouvé ce code source :

http://www.fourmilab.ch/rpkp/experiments/analysis/chiCalc.html


Si vous voulez plus de détails, voila le code source en JavaScript :


<html>
<head>
<title>Chi-Square Calculator</title>
<script language="JavaScript">
<!--


/* The following JavaScript functions for calculating normal and
chi-square probabilities and critical values were adapted by
John Walker from C implementations
written by Gary Perlman of Wang Institute, Tyngsboro, MA
01879. Both the original C code and this JavaScript edition
are in the public domain. */


/* POZ -- probability of normal z value


Adapted from a polynomial approximation in:
Ibbetson D, Algorithm 209
Collected Algorithms of the CACM 1963 p. 616
Note:
This routine has six digit accuracy, so it is only useful for absolute
z values < 6. For z values >= to 6.0, poz() returns 0.0.
*/


function poz(z) {
var y, x, w;
var Z_MAX = 6.0; /* Maximum meaningful z value */

if (z == 0.0) {
x = 0.0;
} else {
y = 0.5 * Math.abs(z);
if (y >= (Z_MAX * 0.5)) {
x = 1.0;
} else if (y < 1.0) {
w = y * y;
x = ((((((((0.000124818987 * w
- 0.001075204047) * w + 0.005198775019) * w
- 0.019198292004) * w + 0.059054035642) * w
- 0.151968751364) * w + 0.319152932694) * w
- 0.531923007300) * w + 0.797884560593) * y * 2.0;
} else {
y -= 2.0;
x = (((((((((((((-0.000045255659 * y
+ 0.000152529290) * y - 0.000019538132) * y
- 0.000676904986) * y + 0.001390604284) * y
- 0.000794620820) * y - 0.002034254874) * y
+ 0.006549791214) * y - 0.010557625006) * y
+ 0.011630447319) * y - 0.009279453341) * y
+ 0.005353579108) * y - 0.002141268741) * y
+ 0.000535310849) * y + 0.999936657524;
}
}
return z > 0.0 ? ((x + 1.0) * 0.5) : ((1.0 - x) * 0.5);
}



var BIGX = 20.0; /* max value to represent exp(x) */


function ex(x) {
return (x < -BIGX) ? 0.0 : Math.exp(x);
}


/* POCHISQ -- probability of chi-square value


Adapted from:
Hill, I. D. and Pike, M. C. Algorithm 299
Collected Algorithms for the CACM 1967 p. 243
Updated for rounding errors based on remark in
ACM TOMS June 1985, page 185
*/


function pochisq(x, df) {
var a, y, s;
var e, c, z;
var even; /* True if df is an even number */


var LOG_SQRT_PI = 0.5723649429247000870717135; /* log(sqrt(pi)) */
var I_SQRT_PI = 0.5641895835477562869480795; /* 1 / sqrt(pi) */

if (x <= 0.0 || df < 1) {
return 1.0;
}

a = 0.5 * x;
even = !(df & 1);
if (df > 1) {
y = ex(-a);
}
s = (even ? y : (2.0 * poz(-Math.sqrt(x))));
if (df > 2) {
x = 0.5 * (df - 1.0);
z = (even ? 1.0 : 0.5);
if (a > BIGX) {
e = (even ? 0.0 : LOG_SQRT_PI);
c = Math.log(a);
while (z <= x) {
e = Math.log(z) + e;
s += ex(c * z - a - e);
z += 1.0;
}
return s;
} else {
e = (even ? 1.0 : (I_SQRT_PI / Math.sqrt(a)));
c = 0.0;
while (z <= x) {
e = e * (a / z);
c = c + e;
z += 1.0;
}
return c * y + s;
}
} else {
return s;
}
}


/* CRITCHI -- Compute critical chi-square value to
produce given p. We just do a bisection
search for a value within CHI_EPSILON,
relying on the monotonicity of pochisq(). */


function critchi(p, df) {
var CHI_EPSILON = 0.000001; /* Accuracy of critchi approximation */
var CHI_MAX = 99999.0; /* Maximum chi-square value */
var minchisq = 0.0;
var maxchisq = CHI_MAX;
var chisqval;

if (p <= 0.0) {
return maxchisq;
} else {
if (p >= 1.0) {
return 0.0;
}
}

chisqval = df / Math.sqrt(p); /* fair first value */
while ((maxchisq - minchisq) > CHI_EPSILON) {
if (pochisq(chisqval, df) < p) {
maxchisq = chisqval;
} else {
minchisq = chisqval;
}
chisqval = (maxchisq + minchisq) * 0.5;
}
return chisqval;
}

// TRIMFLOAT -- Trim a floating point number to maximum number of digits

function trimfloat(ov, d) { var o "", v ov.toString(); var c, i, n 0, indec false, aftdec = false;

for (i = 0; i < v.length; i++) {
c = v.charAt(i);
if (!indec) {
if (c == '.') {
indec = true;
}
o += c;
} else {
if (aftdec) {
o += c;
} else {
if ((c >= '0') && (c <= '9')) {
if (n < d) {
o += c;
}
n++;
} else {
aftdec = true;
o += c;
}
}
}
}
return o;
}


// CALC_X_DF -- Button action to calculate Q from X and DF


function calc_x_df()
{
document.calc1.q.value = trimfloat(pochisq(document.calc1.x.value, document.calc1.df.value), 4);
}


// CALC_Q_DF -- Button action to calculate X from Q and DF


function calc_q_df() {
document.calc2.x.value = trimfloat(critchi(document.calc2.q.value, document.calc2.df.value), 4);
}
// -->
</script>
</head>


<center>
Chi-Square Calculator

</center>


<hr>




The chi-square statistic for an experiment with k
possible outcomes, performed n times, in which
Y1, Y2,... Yk are the number of
experiments which resulted in each possible outcome, with
probabilities of each outcome p1,
p2,... pk is:




X2 will be larger to the extent that the
observed results diverge from those expected by chance. The
probability Q that a X2 value calculated
for an experiment with d degrees of freedom (where
d=k-1, one less the number of possible outcomes) is
due to chance is:








Where Gamma is the generalisation of the factorial
function to real and complex arguments:








Unfortunately, there is no closed form solution for Q, so it
must be evaluated numerically. This page allows you to calculate the
probability of chance occurrence of a given X2 for
an experiment with d degrees of freedom, or to calculate
X2 given d and the probability
Q. All calculations are performed with six decimal places of
accuracy; the maximum X2 accepted is thus 99999.




Note that the probability calculated from the X2
is an approximation which is valid only for large
values of n, and is therefore only meaningful when
calculated from a large number of independent experiments.





In order to use this page, your browser must support JavaScript.
The text field below indicates whether JavaScript is available; if
not, consider switching to a browser which implements it.


<form name="not">
<center>

</center>
</form>


<script language="JavaScript">
<!--


// Set text field to indicate JavaScript is working


document.not.doesit.value = "Your browser supports JavaScript; you can use this calculator.";
// -->
</script>




<hr>


Calculate probability from X2 and d


One of the most common chi-square calculations is determining,
given the measured X2 value for a set of
experiments with a degree of freedom d, the probability
of the result being due to chance. Enter the X2
and d values in the boxes below, press the Calculate
button, and the probability will appear in the Q box.




<form name ="calc1">
<center>
Given X2= and
d=

The chance probability, Q, is:

</center>
</form>




<hr>


Calculate X2 from probability Q and d


To determine the chi-square value indicating a probability Q
of non-chance occurrence for an experiment with d degrees
of freedom, enter Q and d in the boxes below and
press Calculate .


<form name ="calc2">
<center>
Given probability
Q= and
d=

The X2 value is:

</center>
</form>


[../statistics.html Mathematical Details]

[../contents.html Table of Contents]

[../ Run Experiments]

[../../ ] [../../ RetroPsychoKinesis Project Home]



<hr>
by John Walker

</html>


Marin Marais

2 réponses

cs_corsica Messages postés 147 Date d'inscription lundi 13 mai 2002 Statut Membre Dernière intervention 15 février 2010
18 avril 2005 à 09:43
J'avoue ne pas saisir le sens de ta question , en lisant le code que tu as recopié il y a bien une ligne :

var BIGX = 20.0; /* max value to represent exp(x) */

Sachant que "var" en JavaScript est la définition d'une variable non typée ( enfin normalement !! )

Que cherches-tu ???
0
marinmarais Messages postés 104 Date d'inscription lundi 11 avril 2005 Statut Membre Dernière intervention 16 juillet 2010 1
18 avril 2005 à 10:13
Alors là, cher Corsica,

Je me sens vraiment con...

Merci beaucoup.

En fait, j'avais traduit ça à l'arrache il y a quelques mois et je ne l'avais pas vu.
J'ai cherché ce WE mais je me suis focalisé sur ma traduction en VB sans m'attarder sur le code source en JavaScript

Si j'ose m'exprimer ainsi, je crois que je dois avoir de la merde dans les yeux...

Shame on me !!!

Comme quoi, rien ne vaut un regard extérieur.
Merci encore et A+


Marin Marais
0
Rejoignez-nous