<?php

#    (gH)   -_-  statobj2.php  ;  TimeStamp (unix) : 30 Juillet 2007 vers 18:31
#
#    Objets statistiques en php, implémentation soutenue
#
######################################################
######################################################

# fonctions hors classes

function pctg($a,$b,$c) {
  return( sprintf("%7.".$c."f",100*$a/$b) ) ;
} ; # fin de fonction pct

function cardif($a,$b) {
  return( ($a-$b)^2 ) ;
} ; # fin de fonction cardif


#######################################################
#######################################################

class VS {

#######################################################

  var    $nomCourt = "" ;
  var    $nomLong  = "" ;
  var    $tabVal   = array() ;

  function VS($nom="",$lstVal="",$nomdetail="") {
    $this->nomCourt = $nom ;
    $this->nomLong  = $nomdetail ;
    $lstVal = trim($lstVal) ;
    if (strlen($lstVal)>0) { $this->tabVal = preg_split("/ +/ ",$lstVal) ; } ;
  } # fin de fonction VS

  function nature() {
    return("non sue") ;
  } # fin de fonction nature

  function get_nomCourt() {
    return( $this->nomCourt ) ;
  } # fin de fonction get_nomCourt

  function set_nomCourt($nom) {
    $this->nomCourt  = $nom ;
  } # fin de fonction set_nomCourt

  function get_nomLong() {
    return( $this->nomLong ) ;
  } # fin de fonction get_nomLong

  function set_nomLong($nomdetail) {
    $this->nomLong  = $nomdetail ;
  } # fin de fonction set_nomLong

  function taille() {
    return( count($this->tabVal) ) ;
  } # fin de fonction taille

  function decritVS() {
    echo "Variable  : ".$this->get_nomCourt()."\n" ;
    echo "detail    : ".$this->get_nomLong()."\n" ;
    echo "nature    : ".$this->nature()."\n" ;
    echo "taille    : ".$this->taille()."\n" ;
  } # fin de fonction decritVS

# ajout de fonctions de base

function nbEgal($unev) {
  $nboc = 0 ;
  $ndv = $this->taille() ;
  $idv = 0 ;
  while ($idv<$ndv) {
    if ($this->tabVal[$idv]==$unev) { $nboc++ ; } ;
    $idv++ ;
  } ; # fin de tant que
  return($nboc) ;
} # fin de fonction nbEgal

function nbPlusPetit($unev) {
  $nboc = 0 ;
  $ndv = $this->taille() ;
  $idv = 0 ;
  while ($idv<$ndv) {
    if ($this->tabVal[$idv]<$unev) { $nboc++ ; } ;
    $idv++ ;
  } ; # fin de tant que
  return($nboc) ;
} # fin de fonction nbPlusPetit

function nbPlusGrand($unev) {
  $nboc = 0 ;
  $ndv = $this->taille() ;
  $idv = 0 ;
  while ($idv<$ndv) {
    if ($this->tabVal[$idv]>$unev) { $nboc++ ; } ;
    $idv++ ;
  } ; # fin de tant que
  return($nboc) ;
} # fin de fonction nbPlusGrand

function nbPlusPetitOuEgal($unev) {
  return(  $this->nbPlusPetit($unev) +  $this->nbEgal($unev) ) ;
} # fin de fonction nbPlusPetitOuEgal

function nbPlusGrandOuEgal($unev) {
  return(  $this->nbPlusGrand($unev) +  $this->nbEgal($unev) ) ;
} # fin de fonction nbPlusGrandOuEgal

function valMin() {
  $copieVal = $this->tabVal ;
  sort($copieVal) ;
  $lemin = $copieVal[0] ;
  return($lemin) ;
} # fin de fonction valMin

function valMax() {
  $copieVal = $this->tabVal ;
  rsort($copieVal) ;
  $lemax = $copieVal[0] ;
  return($lemax) ;
} # fin de fonction valMax

function mode() {
  $copieVal = $this->tabVal ;
  $tdo = array_count_values($copieVal) ;
  $nbo = 0 ;
  $lem = 0 ;
  while ( list($cle,$valeur) = each($tdo) ) {
    if ($valeur>$nbo) { $lem = $cle ; $nbo = $valeur ; } ;
  } ; # fin de tant que
  return( $lem ) ;
} # fin de fonction mode

} # fin de classe variable statistique

#######################################################

class QT extends VS {

#######################################################

  var $unite = "???" ;

  function QT($nom="",$lstVal="",$unit="???",$nomdetail="") {
    parent::VS($nom,$lstVal,$nomdetail) ;
    $this->unite = $unit ;
  } # fin de fonction QT

  function nature() {
    return("QT") ;
  } # fin de fonction nature

  function get_unite() {
    return( $this->unite ) ;
  } # fin de fonction get_unite

  function set_nomLong($unit) {
    $this->unite  = $unit ;
  } # fin de fonction set_unite

  function decritQT() {
    parent::decritVS() ;
    echo "unité     : ".$this->get_unite()."\n" ;
  } # fin de fonction decritQT

# fonctions d'analyse

function moyenne() {

  $copieVal = $this->tabVal ;
  $som = array_sum($copieVal) ;
  return( $som / $this->taille() ) ;

} # fin de fonction moyenne() {


function variance() {

  $copieVal = $this->tabVal ;
  $moy = $this->moyenne() ;
  $som  = 0 ;
  $ndv = $this->taille() ;
  $idv = 0 ;
  while ($idv<$ndv) {
    $vac  = $this->tabVal[$idv] ;
    $dif  = $vac-$moy ;
    $som += $dif*$dif ;
    $idv++ ;
  } ; # fin de tant que
  return( $som / ($ndv-1) ) ;

} # fin de fonction variance() {

function ect() {
  return( sqrt( $this->variance() ) ) ;
} # fin de fonction ect() {

function cdv() {
  return( 100*( $this->ect() ) / $this->moyenne() ) ;
} # fin de fonction cdv() {

function mediane() {

  $copieVal = $this->tabVal ;
  sort($copieVal) ;
  $ndv  = $this->taille() ;
  $mid  = $ndv/2 ;
  if (($ndv%2)==0) {
     $lamed = ($copieVal[$mid-1] +$copieVal[$mid])/2 ;
  } else {
    $lamed = $copieVal[round($mid+0.5)-1] ;
  } ; # fin de si
  return($lamed) ;

} # fin de fonction mediane {

function analyseQT() {

    $this->decritQT() ;
    echo "\n" ;
    echo "  Moyenne    : ".sprintf("%7.2f",$this->moyenne()) ."  ".$this->get_unite()."\n" ;
    echo "  Variance   : ".sprintf("%7.2f",$this->variance())."  ".$this->get_unite()."^2 \n" ;
    echo "  Ecart-type : ".sprintf("%7.2f",$this->ect())     ."  ".$this->get_unite()."\n" ;
    echo "  Cdv.       : ".sprintf("%7.2f",$this->cdv())     ."  "." %\n" ;
    echo "\n" ;
    echo "  Minimum    : ".sprintf("%7.2f",$this->valMin())     ."  "." %\n" ;
    echo "  M&eacute;diane    : ".sprintf("%7.2f",$this->mediane())     ."  "." %\n" ;
    echo "  Maximum    : ".sprintf("%7.2f",$this->valMax())     ."  "." %\n" ;}

# fin de fonction analyseQT

function coefcorr( $varx ) {

# calcul des deux moyennes et des deux éacart-types

$my = $this->moyenne() ;
$mx = $varx->moyenne() ;
$ey = $this->ect() ;
$ex = $varx->ect() ;

# boucle de calcul du numérateur de covariance

$ndv  = $this->taille() ;
$cov = 0 ;
$idv = 0 ;
while ($idv<$ndv) {
    $cov  += ($varx->tabVal[$idv] -$mx)*($this->tabVal[$idv] -$my);
    $idv++ ;
} ; # fin de tant que

# calcul du coefficient de corrélation

$corr = $cov /( ($ndv-1) * $ex * $ey ) ;

# calcul des coefficients de la régression linéaire

$coefa = $corr * $ey / $ex ;
$coefb = $my - $coefa * $mx ;

# préparation et renvoi du tableau résultat

$tcoef = array() ;
$tcoef[0] = $corr  ;
$tcoef[1] = $coefa ;
$tcoef[2] = $coefb ;

return($tcoef) ;

} # fin de fonction coefcorr


} # fin de classe variable statistique quantitative

#######################################################

class QL extends VS {

#######################################################

  var $tabModa = array() ;

  function QL($nom="",$lstVal="",$tmoda=array(),$nomdetail="") {
    parent::VS($nom,$lstVal,$nomdetail) ;
    $this->tabModa = $tmoda ;
  } # fin de fonction QL

  function nature() {
    return("QL") ;
  } # fin de fonction nature

  function get_modalites() {
    ksort($this->tabModa) ;
    $ret = "" ;
    $nbc = 0 ;
    $nbm = count($this->tabModa) ;
    foreach ($this->tabModa as $cle => $valeur) {
      $ret .= "$cle = $valeur" ;
      $nbc++ ;
      if ($nbc<$nbm) { $ret .= " ; " ; }
    } ; # fin de pour chaque modalité
    return( $ret ) ;
  } # fin de fonction get_modalites

  function decritQL() {
    parent::decritVS() ;
    echo "modalités : ".$this->get_modalites()."\n" ;
  } # fin de fonction decritQT

# fonctions d'analyse

function triAplat() {
  $copieVal = $this->tabVal ;
  $tmod = $this->tabModa ;
  $tdo = array_count_values($copieVal) ;
  $tap = array() ;
  while ( list($cle,$valeur) = each($tdo) ) {
      $label = $tmod[$cle] ;
      $tap[$label] = $valeur ;
  } ; # fin de tant que
  return($tap) ;
} # fin de fonction triAplat

function afficheTriAplat($opt) {
  # opt = 1 : ordre décroissant des effectifs
  # opt = 2 : ordre des codes
  # opt = 3 : ordre des labels
  $etot = $this->taille() ;
  $copieVal = $this->tabVal ;
  $teff = array_count_values($copieVal) ;
  $tmod = $this->tabModa ;
  $tap  = $this->triAplat() ;

$teffl = array() ;
$tmodl = array() ;
while ( list($label,$effectif) = each($tap) ) {
   $teffl[$label] = $effectif."_".$label ;
   $tmodl[$label] = $label."_".$effectif ;
} ; # fin de tant que

  echo "     Label     Effectif     Frequence \n" ;
  echo "     -----     --------     --------- \n" ;
  echo "\n" ;

  if ($opt==1) { # ordre décroissant des effectifs
    rsort($teffl) ;
    while ( list($cle,$efff) = each($teffl) ) {
      $tmp = preg_split("/_/",$efff) ;
      $effectif = $tmp[0] ;
      $label    = $tmp[1] ;
      echo "      ".sprintf("%-12s",$label).sprintf("%4d",$effectif)."    ".pctg($effectif,$etot,1)." %\n" ;
    } ; # fin de tant que
  } ; # fin de si

  if ($opt==2) { # ordre des codes
    ksort($teff) ;
    while ( list($code,$effectif) = each($teff) ) {
      $label = $tmod[$code] ;
      echo "      ".sprintf("%-12s",$label).sprintf("%4d",$effectif)."    ".pctg($effectif,$etot,1)." %\n" ;
    } ; # fin de tant que
  } ; # fin de si

  if ($opt==3) {  # ordre des labels
    sort($tmodl) ;
    while ( list($ind,$labeff) = each($tmodl) ) {
      $tmp = preg_split("/_/",$labeff) ;
      $label    = $tmp[0] ;
      $effectif = $tmp[1] ;
      echo "      ".sprintf("%-12s",$label).sprintf("%4d",$effectif)."    ".pctg($effectif,$etot,1)." %\n" ;
    } ; # fin de tant que
  } ; # fin de si
} # fin de function afficheTriAplat

function triCroise($lotreql) {

  $copieVal1 = $this->tabVal ;
  $tmod1     = $this->tabModa ;
  $tdo1      = array_count_values($copieVal1) ;

  $copieVal2 = $lotreql->tabVal ;
  $tmod2     = $lotreql->tabModa ;
  $tdo2      = array_count_values($copieVal2) ;

  $eff = array() ;
  $ndv = $this->taille() ;
  $idv = 0 ;
  while ($idv<$ndv) {
    $val1 = $copieVal1[$idv] ;
    $val2 = $copieVal2[$idv] ;
    $eff[$val1][$val2]++ ;
    $idv++ ;
  } ; # fin de tant que

  $tcr = array() ;

  while ( list($cle1,$valeur1) = each($tdo1) ) {
      $label1 = $tmod1[$cle1] ;
      while ( list($cle2,$valeur2) = each($tdo2) ) {
          $label2 = $tmod2[$cle2] ;
          $tcr[$label1][$label2] = $eff[$cle1][$cle2] ;
       } ; # fin de tant que
       reset( $tdo2) ;
  } ; # fin de tant que
  return($tcr) ;

} # fin de function triCroise

function afficheTriCroise($lotreql,$num=1) {

  $letcr = $this->triCroise($lotreql) ;
  echo " Tri croisé de ".$this->get_nomCourt()." (en ligne) " ;
  echo " et de ".$lotreql->get_nomCourt()." (en colonne) \n" ;

  $tmod1     = $this->tabModa ;
  $tmod2     = $lotreql->tabModa ;

  $label = " " ;
  echo "  ".sprintf("%-15s",$label)." " ;
  while ( list($cle2,$valeur2) = each($tmod2) ) {
      $label2 = $tmod2[$cle2] ;
      echo "  ".sprintf("%-5s",$label2)." " ;
  } ; # fin de tant que
  reset( $tmod2) ;
  echo " \n" ;
  while ( list($cle1,$valeur1) = each($tmod1) ) {
      $label1 = $tmod1[$cle1] ;
      echo "  ".sprintf("%-15s",$label1)." " ;
      while ( list($cle2,$valeur2) = each($tmod2) ) {
          $label2 = $tmod2[$cle2] ;
          $veff = $letcr[$label1][$label2] ;
          if ($veff=="") { $veff = 0 ; } ;
          echo "  ".sprintf("%5d",$veff)." " ;
       } ; # fin de tant que
       reset( $tmod2) ;
       echo " \n" ;
  } ; # fin de tant que

} # fin de function afficheTriCroise

} # fin de classe variable statistique qualitative

#######################################################
#######################################################

?>