Пример #1
0
/*
 * Manual page at statistics.def
 */
INT16 CGEN_PUBLIC CStatistics_Setup
(
  CStatistics*    _this,
  INT32         nOrder,
  INT32         nDim,
  INT32         nCls,
  CData*          idLtb,
  INT32         nIcLtb
)
{
  INT32    c     = 0;                                                         /* Statistics class loop counter     */
  INT32    n     = 0;                                                         /* Dimension loop couner             */
  FLOAT64* lpMin = NULL;                                                      /* Ptr. to class' k minimum vector   */
  FLOAT64* lpMax = NULL;                                                      /* Ptr. to class' k maximum vector   */

  /* Validate */                                                                /* --------------------------------- */
  CHECK_THIS_RV(NOT_EXEC);                                                      /* Check this pointer                */
  if (nOrder<2) nOrder = 2;                                                     /* Default order is 2                */
  if (nDim  <1) nDim   = 1;                                                     /* Default dimensionality is 1       */
  if (nCls  <1) nCls   = 1;                                                     /* Default number of classes is 1    */

  /* Initialize statistics */                                                   /* --------------------------------- */
  CStatistics_Reset(_this,TRUE);                                                /* Start over                        */
  IFIELD_RESET(CData,"dat");                                                    /* Create/reset statistic data       */
  CData_Array(_this->m_idDat,T_DOUBLE,nDim,nCls*(nOrder+nDim+2));               /* Allocate statistic data           */
  CData_SetNBlocks(_this->m_idDat,nCls);                                        /* Set number of blocks              */
  if (CData_IsEmpty(_this->m_idDat)) return IERROR(_this,ERR_NOMEM,0,0,0);      /* Check if it worked out...         */
  for (c=0; c<nCls; c++)                                                        /* Loop over classes                 */
  {                                                                             /* >>                                */
    lpMin = CStatistics_GetPtr(_this,c,STA_DAI_MIN);                            /*   Get ptr. to class' k min. vec.  */
    lpMax = CStatistics_GetPtr(_this,c,STA_DAI_MAX);                            /*   Get ptr. to class' k max. vec.  */
    for (n=0; n<nDim; n++)                                                      /*   Loop over dimensions            */
    {                                                                           /*   >>                              */
      lpMin[n] = T_DOUBLE_MAX;                                                  /*     Initialize minimum vector     */
      lpMax[n] = T_DOUBLE_MIN;                                                  /*     Initialize maximum vector     */
    }                                                                           /*   <<                              */
  }                                                                             /* <<                                */

  /* Initialize label table */                                                  /* --------------------------------- */
  if (CData_IsEmpty(idLtb)) return O_K;                                         /* No label table -> that's it       */
  if (CData_GetNRecs(idLtb)!=nCls)                                              /* Bad number of labels              */
    return IERROR(_this,STA_NOTSETUP," (wrong no. of labels in idLtb)",0,0);    /*   -> Error                        */
  if (nIcLtb<0)                                                                 /* Label component not specified     */
    for (nIcLtb=0; nIcLtb<CData_GetNComps(idLtb); nIcLtb++)                     /*   Seek first symbolic component   */
      if (dlp_is_symbolic_type_code(CData_GetCompType(idLtb,nIcLtb)))           /*   ...                             */
        break;                                                                  /*   ...                             */
  if (!dlp_is_symbolic_type_code(CData_GetCompType(idLtb,nIcLtb)))              /* Label component not symbolic      */
    return IERROR(_this,STA_NOTSETUP," (label comp. not found in idLtb)",0,0);  /*   -> Error                        */
  IFIELD_RESET(CData,"ltb");                                                    /* Create/reset label table          */
  CData_SelectComps(_this->m_idLtb,idLtb,nIcLtb,1);                             /* Copy label table                  */

  return O_K;                                                                   /* Done                              */
}
Пример #2
0
/**
 * Fills a data table with the sample size (frequency) or the estimated a-priori
 * probability of the classes.
 *
 * @param _this
 *          Pointer to this CStatistics instance
 * @param idDst
 *          Pointer to the destination data instance
 * @param bProb
 *          If <code>TRUE</code> the method estimates class a-priori
 *          probabilities otherwise it stores the classes' sample sizes
 * @return <code>O_K</code> if successsfull, a (negative) error code otherwise
 */
INT16 CGEN_PUBLIC CStatistics_FreqEx
(
  CStatistics* _this,
  CData*       idDst,
  BOOL         bProb
)
{
  INT32    c     = 0;                                                            /* Class loop counter                */
  INT32    C     = 0;                                                            /* Number of statistics classes      */
  INT32    nTsz  = 0;                                                            /* Total sample size                 */
  FLOAT64* lpSsz = NULL;                                                         /* Pointer to class' c sample size   */

  /* Validate */                                                                /* --------------------------------- */
  if (!idDst) return IERROR(_this,ERR_NULLARG,"idDst",0,0);                     /* Check output data instance        */
  CData_Reset(idDst,TRUE);                                                      /* Clear destination instance        */
  CHECK_THIS_RV(NOT_EXEC);                                                      /* Check this pointer                */
  IF_NOK(CStatistics_Check(_this))                                              /* Check instance data               */
    return IERROR(_this,STA_NOTSETUP," ( use -status for details)",0,0);        /* ...                               */

  /* Initialize */                                                              /* --------------------------------- */
  C = CStatistics_GetNClasses(_this);                                           /* Get number of statistics classes  */
  CData_Array(idDst,T_DOUBLE,1,C);                                              /* Allocate output data instance     */
  CData_SetNBlocks(idDst,C);                                                    /* Set block number                  */
  if (!CData_XAddr(idDst,0,0)) return IERROR(_this,ERR_NOMEM,0,0,0);            /* Should have been successfull ...  */

  /* Store sample sizes / estimated a-rpior probabilities */                    /* --------------------------------- */
  nTsz = bProb ? CStatistics_GetNSamples(_this) : 1;                            /* Get frequency divident            */
  for (c=0; c<C; c++)                                                           /* Loop over classes                 */
  {                                                                             /* >>                                */
    DLPASSERT((lpSsz = CStatistics_GetPtr(_this,c,STA_DAI_SSIZE)));             /*   Get ptr. to class' c sample size*/
    CData_Dstore(idDst,lpSsz[0]/(FLOAT64)nTsz,c,0);                              /*   Store sample size of class c    */
  }                                                                             /* <<                                */

  return O_K;
}
Пример #3
0
/**
 * Returns the total sample size over all classes.
 *
 * @param _this
 *          Pointer to CStatistics instance
 * @return The total sample size
 */
INT32 CGEN_PUBLIC CStatistics_GetNSamples(CStatistics* _this)
{
  INT32    c     = 0;                                                            /* Statistics class loop counter     */
  INT32    nSsz  = 0;                                                            /* Total sample size                 */
  FLOAT64* lpSsz = NULL;                                                         /* Ptr. to class' k sample size      */
  CHECK_THIS_RV(0);                                                             /* Check this pointer                */
  for (c=0; c<CStatistics_GetNClasses(_this); c++)                              /* Loop over statistics classes      */
  {                                                                             /* >>                                */
    lpSsz = CStatistics_GetPtr(_this,c,STA_DAI_SSIZE);                          /*   Get ptr. to class' k sample size*/
    if (lpSsz) nSsz += (INT32)*lpSsz;                                            /*   Sum up                          */
  }                                                                             /* <<                                */
  return nSsz;
}
Пример #4
0
/**
 * Updates the statistics with one vector. There are no checks performed!
 *
 * @param _this
 *          Pointer to this CStatistics instance
 * @param lpX
 *          Pointer to a buffer containing the vector to update the statistics
 *          with (is expected to contain <i>N</i> = <code>{@link dat}.dim</code>
 *          double values)
 * @param c
 *          Index of class this vector belongs to (0 &le; <code>k</code> &lt;
 *          <i>C</i> = <code>{@link dat}.nblock</code>)
 */
INT16 CGEN_PROTECTED CStatistics_UpdateVector
(
  CStatistics* _this,
  FLOAT64*      lpX,
  INT32         c,
  FLOAT64       w
)
{
  INT32    i     = 0;                                                            /* Mixed sum index                   */
  INT32    k     = 0;                                                            /* Order loop counter (for k>2)      */
  INT32    n     = 0;                                                            /* 1st dimension loop couner         */
  INT32    m     = 0;                                                            /* 2nd dimension loop couner         */
  INT32    K     = 0;                                                            /* Statistics order                  */
  INT32    N     = 0;                                                            /* Statistics dimensionality         */
  FLOAT64* lpSsz = NULL;                                                         /* Ptr. to class' c sample size      */
  FLOAT64* lpMin = NULL;                                                         /* Ptr. to class' c minimum vector   */
  FLOAT64* lpMax = NULL;                                                         /* Ptr. to class' c maximum vector   */
  FLOAT64* lpSum = NULL;                                                         /* Ptr. to class' c sum vector       */
  FLOAT64* lpMsm = NULL;                                                         /* Ptr. to class' c mixed sum matrix */
  FLOAT64* lpKsm = NULL;                                                         /* Ptr. to class' c k-th ord.sum vec.*/

  /* Validate */                                                                /* --- DEBUG ONLY ------------------ */
  DLPASSERT(_this);                                                             /* Check this pointer                */
  DLPASSERT(_this->m_idDat);                                                    /* Check statistics data table       */
  DLPASSERT((INT32)(dlp_size(lpX)/sizeof(FLOAT64)) == CStatistics_GetDim(_this)); /* Check update vector buffer        */
  DLPASSERT(c>=0 && c<CStatistics_GetNClasses(_this));                          /* Check class index                 */

  /* Initialize */                                                              /* --------------------------------- */
  K = CStatistics_GetOrder(_this);                                              /* Get statistics order              */
  N = CStatistics_GetDim(_this);                                                /* Get statistics dimensionality     */
  DLPASSERT((lpSsz = CStatistics_GetPtr(_this,c,STA_DAI_SSIZE)));               /* Get ptr. to class' c sample size  */
  DLPASSERT((lpMin = CStatistics_GetPtr(_this,c,STA_DAI_MIN  )));               /* Get ptr. to class' c min. vector  */
  DLPASSERT((lpMax = CStatistics_GetPtr(_this,c,STA_DAI_MAX  )));               /* Get ptr. to class' c max. vector  */
  DLPASSERT((lpSum = CStatistics_GetPtr(_this,c,STA_DAI_SUM  )));               /* Get ptr. to class' c sum vector   */
  DLPASSERT((lpMsm = CStatistics_GetPtr(_this,c,STA_DAI_MSUM )));               /* Get ptr. to class' c mix.sum.matr.*/
  DLPASSERT((lpKsm = CStatistics_GetPtr(_this,c,STA_DAI_KSUM )) || K<=2);       /* Get ptr. to class' c k-th ord.sum.*/

  /* Update */                                                                  /* --------------------------------- */
  (*lpSsz)+=w;                                                                  /* Increment sample size             */
  for (n=0,i=0; n<N; n++)                                                       /* Loop over dimensions              */
  {                                                                             /* >>                                */
    if (lpMin[n] > lpX[n]) lpMin[n] = lpX[n];                                   /*   Track minimum                   */
    if (lpMax[n] < lpX[n]) lpMax[n] = lpX[n];                                   /*   Track maximum                   */
    lpSum[n] += lpX[n]*w;                                                       /*   Update sum                      */
    for (m=0; m<N; m++,i++) lpMsm[i] += lpX[n]*lpX[m]*w;                        /*   Update mixed sum                */
    for (k=3; k<=K; k++) lpKsm[(k-3)*N+n] += dlm_pow(lpX[n],k)*w;               /*   Update k-th order sums          */
  }                                                                             /* <<                                */

  return O_K;                                                                   /* Done                              */
}