# PLFIT fits a power-law distributional model to data.
#
# PLFIT(x) estimates x_min and alpha according to the goodness-of-fit
# based method described in Clauset, Shalizi, Newman (2007). x is a
# vector of observations of some quantity to which we wish to fit the
# power-law distribution p(x) ~ x^-alpha for x >= xmin.
# PLFIT automatically detects whether x is composed of real or integer
# values, and applies the appropriate method. For discrete data, if
# min(x) > 1000, PLFIT uses the continuous approximation, which is
# a reliable in this regime.
#
# The fitting procedure works as follows:
# 1) For each possible choice of x_min, we estimate alpha via the
# method of maximum likelihood, and calculate the Kolmogorov-Smirnov
# goodness-of-fit statistic D.
# 2) We then select as our estimate of x_min, the value that gives the
# minimum value D over all values of x_min.
#
# Note that this procedure gives no estimate of the uncertainty of the
# fitted parameters, nor of the validity of the fit.
#
# Example:
# x <- (1-runif(10000))^(-1/(2.5-1))
# plfit(x)
#
#
# Version 1.0 (2008 February)
# Version 1.1 (2008 February)
# - correction : division by zero if limit >= max(x) because the unique R function do no sort
# and the matlab function do...
# Version 1.1 (minor correction 2009 August)
# - correction : lines 230 zdiff calcul was wrong when xmin=0 (thanks to Naoki Masuda)
# - gpl version updated to v3.0 (asked by Felipe Ortega)
# Version 1.2 (2011 August)
# - correction for method "limit" thanks to David R. Pugh
# xmins <- xmins[xmins<=limit] is now xmins <- xmins[xmins>=limit]
# - "fixed" method added for xmins from David R. Pugh
# - modifications by Alan Di Vittorio:
# -