2016-04-12 2 views
-1

Javascript>Wie konvertiere ich die Wahrscheinlichkeit in z-Score

Wenn Sie in der Data Science-Industrie sind, würden Sie gestört werden, wenn Sie keine normale Verteilung Tabelle haben. Ich bin auf den Artikel in Stackoverflow gestoßen, der den z-Score in JavaScript in eine Wahrscheinlichkeit umwandelt. Was ich wirklich wissen möchte, ist die umgekehrte Berechnung dieser Funktion.

/** 
 
* @param {number} z - Number of standard deviations from the mean. 
 
*/ 
 
function GetZPercent(z) { 
 
    // If z is greater than 6.5 standard deviations from the mean 
 
    // the number of significant digits will be outside of a reasonable 
 
    // range. 
 
    if (z < -6.5) 
 
    return 0.0; 
 

 
    if (z > 6.5) 
 
    return 1.0; 
 

 
    var factK = 1; 
 
    var sum  = 0; 
 
    var term  = 1; 
 
    var k  = 0; 
 
    var loopStop = Math.exp(-23); 
 
    
 
    while (Math.abs(term) > loopStop) { 
 
    term = 0.3989422804 * Math.pow(-1, k) * Math.pow(z, k)/(2 * k + 1)/
 
      Math.pow(2, k) * Math.pow(z, k + 1)/factK; 
 
    sum += term; 
 
    k++; 
 
    factK *= k; 
 
    } 
 

 
    sum += 0.5; 
 

 
    return sum; 
 
}

Ich habe ein Gefühl dafür, wie Z-Score in der Wahrscheinlichkeit zu konvertieren. Aber ich habe keine Ahnung, wie man den Z-Score (Standardabweichung) aus der entsprechenden Wahrscheinlichkeit in Javascript berechnet. Zum Beispiel, wenn ich 0,95 (oder 95%) einstelle, kann ich erwarten, 2,25 Standardabweichung zu bekommen. Der obige Code gibt mir 95%, wenn ich 2,25 eingeben.

+4

Sie haben also den JavaScript-Code und fragen nach ... dem JavaScript-Code? Was ist das Problem? – trincot

+0

Fragen Sie, wie Sie diese Funktion in einer Webseite aufrufen (z. B. wie Sie den Z-Wert der Funktion von einer Eingabe ausliefern und das Ergebnis anzeigen und anzeigen?) – TheHansinator

+0

Eine dieser [+1500 ähnlichen Fragen] (http: //stackoverflow.com/search?q=z-score+) könnte eine Antwort liefern, z. B. wie diese Frage mit einem Code, der mit dem Ihren identisch ist: [Suche nach einer statistischen JavaScript-Funktion zur Rückgabe des p-Wertes aus einem Z-Score] (http: //stackoverflow.com/questions/16194730/seeking-a-statistical-javascript-function-to-return-p-value-from-az-score) – Roberto

Antwort

1

Hier ist eine Funktion, die eine entgegengesetzte Berechnung (Wahrscheinlichkeit zu Z-Score) tut:

function percentile_z(p) { 
    var a0= 2.5066282, a1=-18.6150006, a2= 41.3911977, a3=-25.4410605, 
     b1=-8.4735109, b2= 23.0833674, b3=-21.0622410, b4= 3.1308291, 
     c0=-2.7871893, c1= -2.2979648, c2= 4.8501413, c3= 2.3212128, 
     d1= 3.5438892, d2= 1.6370678, r, z; 

    if (p>0.42) { 
     r=Math.sqrt(-Math.log(0.5-p)); 
     z=(((c3*r+c2)*r+c1)*r+c0)/((d2*r+d1)*r+1); 
    } else { 
     r=p*p; 
     z=p*(((a3*r+a2)*r+a1)*r+a0)/((((b4*r+b3)*r+b2)*r+b1)*r+1); 
    } 
    return z; 
} 

von easycalculation.com Grabbed

+0

Ich schätze es wirklich! Es klappt. – Gregory

2

ich, dass dieser Code fand auch funktioniert. Verwenden Sie critz (p), um die Wahrscheinlichkeit in den z-Score zu konvertieren. Zum Beispiel können wir 1,65 von critz (0,95) erwarten, da 95% 1,65 Standardabweichung im z-Score entspricht.

/* 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. 
*/ 
    var Z_MAX = 6; 
function poz(z) { 

    var y, x, w; 

    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); 
} 


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

function critz(p) { 
    var Z_EPSILON = 0.000001;  /* Accuracy of z approximation */ 
    var minz = -Z_MAX; 
    var maxz = Z_MAX; 
    var zval = 0.0; 
    var pval; 
    if(p < 0.0) p = 0.0; 
    if(p > 1.0) p = 1.0; 

    while ((maxz - minz) > Z_EPSILON) { 
     pval = poz(zval); 
     if (pval > p) { 
      maxz = zval; 
     } else { 
      minz = zval; 
     } 
     zval = (maxz + minz) * 0.5; 
    } 
    return(zval); 
} 
Verwandte Themen