⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ststat.pas

📁 条码控件: 一维条码控件 二维条码控件 PDF417Barcode MaxiCodeBarcode
💻 PAS
📖 第 1 页 / 共 5 页
字号:
(* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is TurboPower SysTools
 *
 * The Initial Developer of the Original Code is
 * TurboPower Software
 *
 * Portions created by the Initial Developer are Copyright (C) 1996-2002
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** *)

{*********************************************************}
{* SysTools: StStat.pas 4.03                             *}
{*********************************************************}
{* SysTools: Statistical math functions modeled on       *}
{*           those in Excel                              *}
{*********************************************************}

{$I StDefine.inc}

unit StStat;

{ The statistical distribution functions return results in singles }
{ since the fractional accuracy of these is typically about 3e-7.  }

interface

uses
  Windows,
  {$IFDEF UseMathUnit}
  Math,
  {$ELSE}
  StMath,
  {$ENDIF}
  SysUtils, StConst, StBase;

{AVEDEV}
function AveDev(const Data: array of Double) : Double;
function AveDev16(const Data; NData : Integer) : Double;
  {-Returns the average of the absolute deviations of data points from their
    mean. AveDev is a measure of the variability in a data set.
  }

{CONFIDENCE}
function Confidence(Alpha, StandardDev : Double; Size : LongInt) : Double;
  {-Returns the confidence interval for a population mean.
    The confidence interval is a range on either side of a sample mean.
      Alpha is the significance level used to compute the confidence level.
        The confidence level equals 100*(1 - Alpha)%, or in other words,
        an Alpha of 0.05 indicates a 95 percent confidence level.
      StandardDev is the population standard deviation for the data range
        and is assumed to be known.
      Size is the sample Size.
  }

{CORREL}
function Correlation(const Data1, Data2 : array of Double) : Double;
function Correlation16(const Data1, Data2; NData : Integer) : Double;
  {-Returns the correlation coefficient of the Data1 and Data2 arrays.
    Use the correlation coefficient to determine the relationship between
    two data sets.
    This function also returns the same value as the PEARSON function
    in Excel.
  }

{COVAR}
function Covariance(const Data1, Data2 : array of Double) : Double;
function Covariance16(const Data1, Data2; NData : Integer) : Double;
  {-Returns covariance, the average of products of deviations, for the Data1
    and Data2 arrays. Use covariance to determine the relationship between
    two data sets.
  }

{DEVSQ}
function DevSq(const Data : array of Double) : Double;
function DevSq16(const Data; NData : Integer) : Double;
  {-Returns the sum of squares of deviations of data points from their
    sample mean.
  }

{FREQUENCY}
procedure Frequency(const Data : array of Double; const Bins : array of Double;
  var Counts : array of LongInt);
procedure Frequency16(const Data; NData : Integer; const Bins; NBins : Integer;
  var Counts);
  {-Calculates how often values occur within an array of data,
    and then returns an array of counts.
      Data is an array of values for which you want to count frequencies.
      Bins is an array of intervals into which you want to group the values in
        Data.
      Counts is an array into which the frequency counts are returned. Counts
        must have one more element than does Bins. The first element of Counts
        has all items less than the first number in Bins. The next element of
        Counts is the items that fall between Bins[0] and Bins[1]. The last
        element of Counts has all items greater than the last number in Bins.
  }

{GEOMEAN}
function GeometricMean(const Data : array of Double) : Double;
function GeometricMean16(const Data; NData : Integer) : Double;
  {-Returns the geometric mean of an array of positive data. The geometric
    mean is the n'th root of the product of n positive numbers.}

{HARMEAN}
function HarmonicMean(const Data : array of Double) : Double;
function HarmonicMean16(const Data; NData : Integer) : Double;
  {-Returns the harmonic mean of an array of data. The harmonic mean is the
    reciprocal of the arithmetic mean of reciprocals.}

{LARGE}
function Largest(const Data : array of Double; K : Integer) : Double;
function Largest16(const Data; NData : Integer; K : Integer) : Double;
  {-Returns the K'th largest value in an array of data. You can use this
    function to select a value based on its relative standing.}

{MEDIAN}
function Median(const Data : array of Double) : Double;
function Median16(const Data; NData : Integer) : Double;
  {-Returns the median of the given numbers. The median is the number in the
    middle of a set of numbers; that is, half the numbers have values that
    are greater than the median, and half have values that are less.
    If there is an even number of numbers in the set, MEDIAN calculates
    the average of the two numbers in the middle.
  }

{MODE}
function Mode(const Data: array of Double) : Double;
function Mode16(const Data; NData : Integer) : Double;
  {-Returns the most frequently occurring, or repetitive, value in an array
    of data. In case of duplicate frequencies it returns the smallest
    such value.}

{PERCENTILE}
function Percentile(const Data : array of Double; K : Double) : Double;
function Percentile16(const Data; NData : Integer; K : Double) : Double;
  {-Returns the value of the K'th percentile of an array of data.
      K is the percentile value, a number between 0 and 1. If K is not a
        multiple of 1/(n-1) where n is the size of Data, Percentile
        interpolates between the closest bins.
  }

{PERCENTRANK}
function PercentRank(const Data : array of Double; X : Double) : Double;
function PercentRank16(const Data; NData : Integer; X : Double) : Double;
  {-Returns the percentile position of a value within an array of data.
      X is a data value. If X is not found within the array, PercentRank
        interpolates between the closest data points.
  }

{PERMUT}
function Permutations(Number, NumberChosen : Integer) : Extended;
  {-Returns the number of permutations for a given Number of objects that
    can be selected from Number objects. A permutation is any set or subset
    of objects or events where internal order is significant.
    Permutations are different from combinations, for which the internal order
    is not significant.
      Number is an Integer that describes the number of objects.
      NumberChosen is an Integer that describes the number of objects in
        each permutation.
  }

{COMBIN}
function Combinations(Number, NumberChosen : Integer) : Extended;
  {-Returns the number of combinations for a given Number of objects that
    can be selected from Number objects. A combination is any set or subset
    of objects or events where internal order is not significant.
      Number is an Integer that describes the number of objects.
      NumberChosen is an Integer that describes the number of objects in
        each permutation.
  }

{FACT}
function Factorial(N : Integer) : Extended;
  {-Returns N! as a floating point number.
    Extended is used for range, not accuracy.
  }

{RANK}
function Rank(Number : Double; const Data : array of Double;
  Ascending : Boolean) : Integer;
function Rank16(Number : Double; const Data; NData : Integer;
  Ascending : Boolean) : Integer;
  {-Returns the rank of a number in a list of numbers. If you were to sort
    a list that contained no duplicates, the rank of the Number would be its
    position within the sorted list.
      Number is the number whose rank you want.
      Data is the list of numbers.
      If Ascending is True the rank is measured from the beginning of the
        array; otherwise from the end of the array.
      If the Number is not found in the array, 0 is returned. Numbers that
        appear multiple times all have the same rank, but they affect the
        rank of numbers appearing after them. For example, in a list of
        Integers, if the number 10 appears twice and has a rank of 5,
        then 11 would have a rank of 7 (no number would have a rank of 6).
      Be sure to sort Data before calling this routine if you want an
        unambiguous ranking.
  }

{SMALL}
function Smallest(const Data : array of Double; K : Integer) : Double;
function Smallest16(const Data; NData : Integer; K : Integer) : Double;
  {-Returns the K'th smallest value in an array of data. You can use this
    function to select a value based on its relative standing.}

{TRIMMEAN}
function TrimMean(const Data : array of Double; Percent : Double) : Double;
function TrimMean16(const Data; NData : Integer; Percent : Double) : Double;
  {-Returns the mean of Data after trimming Percent points from the data set.
    If Percent is 0.2 and there are 20 points in Data, the 2 largest and 2
    smallest points would be dropped before computing the mean. Percent must
    be a number between 0 and 1.
  }

{--------------------------------------------------------------------------}

type
  {full statistics for a linear regression}
  TStLinEst = record
    B0, B1     : Double;   {model coefficients}
    seB0, seB1 : Double;   {standard error of model coefficients}
    R2         : Double;   {coefficient of determination}
    sigma      : Double;   {standard error of regression}
    SSr, SSe   : Double;   {elements for ANOVA table}
    F0         : Double;   {F-statistic to test B1=0}
    df         : Integer;  {denominator degrees of freedom for F-statistic}
  end;

{LINEST}
procedure LinEst(const KnownY : array of Double;
  const KnownX : array of Double; var LF : TStLinEst; ErrorStats : Boolean);
procedure LinEst16(const KnownY; const KnownX; NData : Integer;
  var LF : TStLinEst; ErrorStats : Boolean);
  {-Performs linear fit to data and returns coefficients and error
    statistics.
      KnownY is the dependent array of known data points.
      KnownX is the independent array of known data points.
      NData must be greater than 2.
      If ErrorStats is FALSE, only B0 and B1 are computed; the other fields
        of TStLinEst are set to 0.0.
      See declaration of TStLinEst for returned data.

  }

{LOGEST}
procedure LogEst(const KnownY : array of Double;
  const KnownX : array of Double; var LF : TStLinEst; ErrorStats : Boolean);
procedure LogEst16(const KnownY; const KnownX; NData : Integer;
  var LF : TStLinEst; ErrorStats : Boolean);
  {-Performs log-linear fit to data and returns coefficients and error
    statistics.
      KnownY is the dependent array of known data points.
      KnownX is the independent array of known data points.
      NData must be greater than 2.
      KnownY is transformed using ln(KnownY) before fitting:
        y = B0*B1^x implies that ln(y) = ln(B0)+X*ln(B1)
      In the returned LF, B0 and B1 are returned as shown. Other values in LF
      are in terms of the log transformation.
      If ErrorStats is FALSE, only B0 and B1 are computed; the other fields
        of TStLinEst are set to 0.0.
      See declaration of TStLinEst for returned data.
  }

{FORECAST}
function Forecast(X : Double; const KnownY: array of Double;
  const KnownX : array of Double) : Double;
function Forecast16(X : Double; const KnownY; const KnownX;
  NData : Integer) : Double;
  {-Calculates a future value by using existing values. The predicted value
    is a y-value for a given X-value. The known values are existing X-values
    and y-values, and the new value is predicted by using linear regression.
      X is the data point for which you want to predict a value.
      KnownY is the dependent array of known data points.
      KnownX is the independent array of known data points.
  }

{similar to GROWTH but more consistent with FORECAST}
function ForecastExponential(X : Double; const KnownY : array of Double;
  const KnownX : array of Double) : Double;
function ForecastExponential16(X : Double; const KnownY; const KnownX;
  NData : Integer) : Double;
  {-Calculates a future value by using existing values. The predicted value
    is a y-value for a given X-value. The known values are existing X-values
    and y-values, and the new value is predicted by using linear regression
    to an exponential growth model, y = B0*B1^X.
      X is the data point for which you want to predict a value.
      KnownY is the dependent array of known data points.
      KnownX is the independent array of known data points.
  }

{INTERCEPT}
function Intercept(const KnownY : array of Double;
  const KnownX : array of Double) : Double;
function Intercept16(const KnownY; const KnownX; NData : Integer) : Double;
  {-Calculates the point at which a line will intersect the y-axis by using
    existing X-values and y-values. The intercept point is based on a best-fit
    regression line plotted through the known X-values and known y-values.
    Use the intercept when you want to determine the value of the dependent
    variable when the independent variable is 0 (zero).
  }

{RSQ}
function RSquared(const KnownY : array of Double;
  const KnownX : array of Double) : Double;
function RSquared16(const KnownY; const KnownX; NData : Integer) : Double;
  {-Returns the square of the Pearson product moment correlation coefficient
    through data points in KnownY's and KnownX's. The r-squared value can
    be interpreted as the proportion of the variance in y attributable to
    the variance in X.
  }

{SLOPE}
function Slope(const KnownY : array of Double;
  const KnownX : array of Double) : Double;
function Slope16(const KnownY; const KnownX; NData : Integer) : Double;
  {-Returns the slope of the linear regression line through data points in
    KnownY's and KnownX's. The slope is the vertical distance divided by the
    horizontal distance between any two points on the line, which is the rate
    of change along the regression line.
  }

{STEYX}
function StandardErrorY(const KnownY : array of Double;
  const KnownX : array of Double) : Double;
function StandardErrorY16(const KnownY; const KnownX;
  NData : Integer) : Double;
  {-Returns the standard error of the predicted y-value for each X in a linear
    regression. The standard error is a measure of the amount of error in the
    prediction of y for an individual X.
  }

{--------------------------------------------------------------------------}

{BETADIST}
function BetaDist(X, Alpha, Beta, A, B : Single) : Single;
  {-Returns the cumulative beta probability density function.
    The cumulative beta probability density function is commonly used to
    study variation in the percentage of something across samples.
      X is the value at which to evaluate the function, A <= X <= B.
      Alpha is a parameter to the distribution.
      Beta is a parameter to the distribution.
      A is the lower bound to the interval of X.
      B is the upper bound to the interval of X.
      The standard beta distribution has A=0 and B=1.
    Fractional error less than 3.0e-7.
  }

{BETAINV}
function BetaInv(Probability, Alpha, Beta, A, B : Single) : Single;
  {-Returns the inverse of the cumulative beta probability density function.
    That is, if Probability = BetaDist(X,...), then
    BetaInv(Probability,...) = X.
      Probability is a probability (0 <= p <= 1) associated with the
        beta distribution.
      Alpha is a parameter to the distribution.
      Beta is a parameter to the distribution.
      A is the lower bound to the interval of the distribution.
      B is the upper bound to the interval of the distribution.
    Fractional error less than 3.0e-7.
  }

{BINOMDIST}
function BinomDist(NumberS, Trials : Integer; ProbabilityS : Single;
  Cumulative : Boolean) : Single;
  {-Returns the individual term binomial distribution probability.
    Use BinomDist in problems with a fixed number of tests or Trials,
    when the outcomes of any trial are only success or failure,
    when Trials are independent, and when the probability of success is
    constant throughout the experiment.
      NumberS is the number of successes in Trials.
      Trials is the number of independent trials.
      ProbabilityS is the probability of success on each trial.
      Cumulative is a logical value that determines the form of the function.
        If Cumulative is TRUE, then BinomDist returns the cumulative
        distribution function, which is the probability that there are at most
        NumberS successes; if FALSE, it returns the probability mass function,
        which is the probability that there are NumberS successes.
  }

{CRITBINOM}
function CritBinom(Trials : Integer; ProbabilityS, Alpha : Single) : Integer;
  {-Returns the smallest value for which the cumulative binomial distribution
    is greater than or equal to a criterion value.
      Trials is the number of Bernoulli trials.
      ProbabilityS is the probability of a success on each trial.
      Alpha is the criterion value.
  }

{CHIDIST}
function ChiDist(X : Single; DegreesFreedom : Integer) : Single;
  {-Returns the one-tailed probability of the chi-squared distribution.
    The chi-squared distribution is associated with a chi-squared test.
    Use the chi-squared test to compare observed and expected values.
      X is the value at which you want to evaluate the distribution.
      DegreesFreedom is the number of degrees of freedom.
  }

{CHIINV}
function ChiInv(Probability : Single; DegreesFreedom : Integer) : Single;
  {-Returns the inverse of the one-tailed probability of the chi-squared
    distribution. If Probability = ChiDist(X,...), then
    ChiInv(Probability,...) = X.
      Probability is a probability associated with the chi-squared
        distribution.
      DegreesFreedom is the number of degrees of freedom.
  }

{EXPONDIST}
function ExponDist(X, Lambda : Single; Cumulative : Boolean) : Single;
  {-Returns the exponential distribution. Use ExponDist to model the time
    between events.
      X is the value of the function.
      Lambda is the parameter value.
      Cumulative is a logical value that indicates which form of the
        exponential function to provide. If Cumulative is TRUE, ExponDist
        returns the cumulative distribution function; if FALSE, it returns
        the probability density function.
  }

{FDIST}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -