| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#include <esl_config.h> |
| 10 |
|
| 11 |
#include <stdlib.h> |
| 12 |
#include <stdio.h> |
| 13 |
#include <string.h> |
| 14 |
|
| 15 |
#include "easel.h" |
| 16 |
#include "esl_getopts.h" |
| 17 |
#include "esl_histogram.h" |
| 18 |
#include "esl_exponential.h" |
| 19 |
#include "esl_gev.h" |
| 20 |
#include "esl_gumbel.h" |
| 21 |
#include "esl_normal.h" |
| 22 |
#include "esl_stats.h" |
| 23 |
|
| 24 |
static char banner[] = "collate a data histogram, output xmgrace datafile"; |
| 25 |
|
| 26 |
static char usage[] = "[-options] <datafile>"; |
| 27 |
|
| 28 |
static ESL_OPTIONS options[] = { |
| 29 |
|
| 30 |
{"-h", eslARG_NONE, FALSE,NULL,NULL, NULL,NULL,NULL,"show help and usage", 1 }, |
| 31 |
{"-o", eslARG_OUTFILE, NULL,NULL,NULL, NULL,NULL,NULL,"output file for plot (default is stdout)", 1 }, |
| 32 |
|
| 33 |
{"-b", eslARG_NONE, FALSE,NULL,NULL, NULL,NULL,NULL,"input file is binary, array of doubles", 2 }, |
| 34 |
{"-f", eslARG_INT, "1",NULL,"n>0",NULL,NULL,"-b","which field to read on text line (1..n)", 2 }, |
| 35 |
|
| 36 |
{"-w", eslARG_REAL, "1.0",NULL,NULL, NULL,NULL,NULL,"bin size for histogram", 3 }, |
| 37 |
{"--min", eslARG_REAL, "-100.",NULL,NULL, NULL,NULL,NULL,"initial lower bound of histogram", 3 }, |
| 38 |
{"--max", eslARG_REAL, "100.",NULL,NULL, NULL,NULL,NULL,"initial upper bound of histogram", 3 }, |
| 39 |
{"--surv", eslARG_NONE, FALSE,NULL,NULL, NULL,NULL,NULL,"output survival plot, not histogram", 3 }, |
| 40 |
|
| 41 |
{"--gumbel", eslARG_NONE, FALSE, NULL,NULL, NULL,NULL,NULL,"fit data to a Gumbel distribution", 4 }, |
| 42 |
{"--exptail", eslARG_NONE, FALSE, NULL,NULL, NULL,NULL,NULL,"fit tail to an exponential distribution", 4 }, |
| 43 |
{"--gev", eslARG_NONE, FALSE, NULL,NULL, NULL,NULL,NULL,"fit data to a generalized EVD (Frechet or Weibull)", 4 }, |
| 44 |
{"--normal", eslARG_NONE, FALSE, NULL,NULL, NULL,NULL,NULL,"fit data to a normal (Gaussian) distribution", 4 }, |
| 45 |
{"--trunc", eslARG_REAL, NULL, NULL,NULL, NULL,"--gumbel",NULL,"with --gumbel, specify data are truncated, min value is <x>", 4 }, |
| 46 |
{"--gumloc", eslARG_NONE, FALSE, NULL,NULL, NULL,NULL,NULL,"fit data to a Gumbel distribution w/ known lambda", 4 }, |
| 47 |
{"--exptailloc",eslARG_NONE, FALSE, NULL,NULL, NULL,NULL,NULL,"fit tail to an exponential tail w/ known lambda", 4 }, |
| 48 |
{"--showgum", eslARG_NONE, FALSE, NULL,NULL, NULL,"--mu",NULL,"plot a known Gumbel for comparison", 4 }, |
| 49 |
{"--showexp", eslARG_NONE, FALSE, NULL,NULL, NULL,"--mu",NULL,"plot a known exponential tail for comparison", 4 }, |
| 50 |
{"--showgev", eslARG_NONE, FALSE, NULL,NULL, NULL,"--mu",NULL,"plot a known GEV for comparison", 4 }, |
| 51 |
{"--alpha", eslARG_REAL, "0.0", NULL,NULL, NULL,NULL,NULL,"set known alpha (GEV shape parameter)", 4 }, |
| 52 |
{"--lambda", eslARG_REAL,"0.693", NULL,NULL, NULL,NULL,NULL,"set known lambda", 4 }, |
| 53 |
{"--mu", eslARG_REAL, "0.0", NULL,NULL, NULL,NULL,NULL,"set known mu", 4 }, |
| 54 |
{"-t", eslARG_REAL, "0.01", NULL,NULL, NULL,NULL,NULL,"set tail mass to fit to", 4 }, |
| 55 |
{ 0,0,0,0,0,0,0,0,0,0}, |
| 56 |
}; |
| 57 |
|
| 58 |
|
| 59 |
int |
| 60 |
main(int argc, char **argv) |
| 61 |
{ |
| 62 |
ESL_HISTOGRAM *h; |
| 63 |
ESL_GETOPTS *go; |
| 64 |
char *datafile; |
| 65 |
FILE *ifp; |
| 66 |
double x; |
| 67 |
|
| 68 |
int which_field; |
| 69 |
char *outfile; |
| 70 |
FILE *ofp; |
| 71 |
double hmin; |
| 72 |
double hmax; |
| 73 |
double hbinsize; |
| 74 |
|
| 75 |
double *xv; |
| 76 |
int n; |
| 77 |
double params[3]; |
| 78 |
double lambda; |
| 79 |
double mu; |
| 80 |
double alpha; |
| 81 |
double tailp; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
go = esl_getopts_Create(options); |
| 88 |
if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK || |
| 89 |
esl_opt_VerifyConfig(go) != eslOK) |
| 90 |
{ |
| 91 |
printf("Failed to parse command line: %s\n", go->errbuf); |
| 92 |
esl_usage(stdout, argv[0], usage); |
| 93 |
printf("\nTo see more help on available options, do %s -h\n\n", argv[0]); |
| 94 |
exit(1); |
| 95 |
} |
| 96 |
|
| 97 |
if (esl_opt_GetBoolean(go, "-h")) { |
| 98 |
esl_banner(stdout, argv[0], banner); |
| 99 |
esl_usage (stdout, argv[0], usage); |
| 100 |
puts("\nGeneral options are:"); |
| 101 |
esl_opt_DisplayHelp(stdout, go, 1, 2, 80); |
| 102 |
puts("\nOptions that control how to read the input file:"); |
| 103 |
esl_opt_DisplayHelp(stdout, go, 2, 2, 80); |
| 104 |
puts("\nOptions that control how to display the output XY file:"); |
| 105 |
esl_opt_DisplayHelp(stdout, go, 3, 2, 80); |
| 106 |
puts("\nOptional ML fitting or plotting of distributions for comparison:"); |
| 107 |
esl_opt_DisplayHelp(stdout, go, 4, 2, 80); |
| 108 |
return eslOK; |
| 109 |
} |
| 110 |
which_field = esl_opt_GetInteger(go, "-f"); |
| 111 |
outfile = esl_opt_GetString (go, "-o"); |
| 112 |
tailp = esl_opt_GetReal (go, "-t"); |
| 113 |
hbinsize = esl_opt_GetReal (go, "-w"); |
| 114 |
hmin = esl_opt_GetReal (go, "--min"); |
| 115 |
hmax = esl_opt_GetReal (go, "--max"); |
| 116 |
lambda = esl_opt_GetReal (go, "--lambda"); |
| 117 |
mu = esl_opt_GetReal (go, "--mu"); |
| 118 |
alpha = esl_opt_GetReal (go, "--alpha"); |
| 119 |
|
| 120 |
ESL_DASSERT1(( which_field >= 1 )); |
| 121 |
|
| 122 |
if (esl_opt_ArgNumber(go) != 1) |
| 123 |
{ |
| 124 |
printf("Incorrect number of command line arguments.\n"); |
| 125 |
esl_usage(stdout, argv[0], usage); |
| 126 |
printf("\nTo see more help on available options, do %s -h\n\n", argv[0]); |
| 127 |
exit(1); |
| 128 |
} |
| 129 |
|
| 130 |
datafile = esl_opt_GetArg(go, 1); |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
if (strcmp(datafile, "-") == 0) ifp = stdin; |
| 137 |
else { |
| 138 |
ifp = fopen(datafile, "rb"); |
| 139 |
if (ifp == NULL) esl_fatal("Failed to open input data file %s\n", datafile); |
| 140 |
} |
| 141 |
|
| 142 |
if (outfile == NULL) ofp = stdout; |
| 143 |
else { |
| 144 |
ofp = fopen(outfile, "w"); |
| 145 |
if (ofp == NULL) esl_fatal("Failed to open output xmgrace data file %s\n", outfile); |
| 146 |
} |
| 147 |
|
| 148 |
h = esl_histogram_CreateFull(hmin,hmax,hbinsize); |
| 149 |
if (h == NULL) esl_fatal("Failed to create histogram"); |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
if (esl_opt_GetBoolean(go, "-b")) |
| 157 |
{ |
| 158 |
while (fread(&x, sizeof(double), 1, ifp) == 1) |
| 159 |
esl_histogram_Add(h, x); |
| 160 |
} |
| 161 |
else |
| 162 |
{ |
| 163 |
char *buf; |
| 164 |
int nbuf; |
| 165 |
char *s; |
| 166 |
char *tok; |
| 167 |
int i; |
| 168 |
|
| 169 |
buf = NULL; |
| 170 |
nbuf = 0; |
| 171 |
while (esl_fgets(&buf, &nbuf, ifp) == eslOK) |
| 172 |
{ |
| 173 |
s = buf; |
| 174 |
for (i = 0; i < which_field; i++) |
| 175 |
{ |
| 176 |
esl_strtok(&s, " \t\n", &tok); |
| 177 |
if (tok == NULL) break; |
| 178 |
} |
| 179 |
if (tok != NULL) { |
| 180 |
x = atof(tok); |
| 181 |
esl_histogram_Add(h, x); |
| 182 |
} |
| 183 |
} |
| 184 |
free(buf); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
if (esl_opt_GetBoolean(go, "--gumbel")) |
| 192 |
{ |
| 193 |
esl_histogram_GetData(h, &xv, &n); |
| 194 |
if(! esl_opt_IsDefault(go, "--trunc")) { |
| 195 |
if (esl_gumbel_FitTruncated(xv, n, esl_opt_GetReal(go, "--trunc"), &(params[0]), &(params[1])) != eslOK) |
| 196 |
esl_fatal("gumbel truncated fit failed"); |
| 197 |
} else { |
| 198 |
if (esl_gumbel_FitComplete(xv, n, &(params[0]), &(params[1])) != eslOK) |
| 199 |
esl_fatal("gumbel complete fit failed"); |
| 200 |
} |
| 201 |
esl_histogram_SetExpect(h, &esl_gumbel_generic_cdf, ¶ms); |
| 202 |
|
| 203 |
printf("# Gumbel fit: mu = %f lambda = %f\n", params[0], params[1]); |
| 204 |
} |
| 205 |
else if (esl_opt_GetBoolean(go, "--gumloc")) |
| 206 |
{ |
| 207 |
params[1] = lambda; |
| 208 |
esl_histogram_GetData(h, &xv, &n); |
| 209 |
if (esl_gumbel_FitCompleteLoc(xv, n, params[1], &(params[0])) != eslOK) |
| 210 |
esl_fatal("gumbel location-only complete fit failed"); |
| 211 |
esl_histogram_SetExpect(h, &esl_gumbel_generic_cdf, ¶ms); |
| 212 |
|
| 213 |
printf("# Gumbel fit with forced lambda = %f: mu = %f\n", params[1], params[0]); |
| 214 |
} |
| 215 |
else if (esl_opt_GetBoolean(go, "--exptail")) |
| 216 |
{ |
| 217 |
esl_histogram_GetTailByMass(h, tailp, &xv, &n, NULL); |
| 218 |
if (esl_exp_FitComplete(xv, n, &(params[0]), &(params[1])) != eslOK) |
| 219 |
esl_fatal("exponential complete fit failed"); |
| 220 |
esl_histogram_SetExpectedTail(h, params[0], tailp, &esl_exp_generic_cdf, ¶ms); |
| 221 |
|
| 222 |
printf("# Exponential fit to %.2f%% tail: lambda = %f\n", tailp*100.0, params[1]); |
| 223 |
} |
| 224 |
else if (esl_opt_GetBoolean(go, "--exptailloc")) |
| 225 |
{ |
| 226 |
params[1] = lambda; |
| 227 |
esl_histogram_GetTailByMass(h, tailp, &xv, &n, NULL); |
| 228 |
params[0] = xv[0]; |
| 229 |
esl_histogram_SetExpectedTail(h, params[0], tailp, &esl_exp_generic_cdf, ¶ms); |
| 230 |
} |
| 231 |
else if (esl_opt_GetBoolean(go, "--gev")) |
| 232 |
{ |
| 233 |
esl_histogram_GetData(h, &xv, &n); |
| 234 |
if (esl_gev_FitComplete(xv, n, &(params[0]), &(params[1]), &(params[2])) != eslOK) |
| 235 |
esl_fatal("generalized EVD complete data fit failed"); |
| 236 |
esl_histogram_SetExpect(h, &esl_gev_generic_cdf, ¶ms); |
| 237 |
|
| 238 |
printf("# generalized EVD fit: mu = %f lambda = %f alpha = %f\n", params[0], params[1], params[2]); |
| 239 |
} |
| 240 |
else if (esl_opt_GetBoolean(go, "--normal")) |
| 241 |
{ |
| 242 |
esl_histogram_GetData(h, &xv, &n); |
| 243 |
esl_stats_DMean(xv, n, &(params[0]), &(params[1])); |
| 244 |
params[1] = sqrt(params[1]); |
| 245 |
esl_histogram_SetExpect(h, &esl_normal_generic_cdf, ¶ms); |
| 246 |
} |
| 247 |
else if (esl_opt_GetBoolean(go, "--showgum")) |
| 248 |
{ |
| 249 |
params[0] = mu; |
| 250 |
params[1] = lambda; |
| 251 |
esl_histogram_SetExpect(h, &esl_gumbel_generic_cdf, ¶ms); |
| 252 |
} |
| 253 |
else if (esl_opt_GetBoolean(go, "--showexp")) |
| 254 |
{ |
| 255 |
params[0] = mu; |
| 256 |
params[1] = lambda; |
| 257 |
esl_histogram_SetExpectedTail(h, mu, tailp, &esl_exp_generic_cdf, ¶ms); |
| 258 |
} |
| 259 |
else if (esl_opt_GetBoolean(go, "--showgev")) |
| 260 |
{ |
| 261 |
params[0] = mu; |
| 262 |
params[1] = lambda; |
| 263 |
params[2] = alpha; |
| 264 |
esl_histogram_SetExpect(h, &esl_gev_generic_cdf, ¶ms); |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
if (esl_opt_GetBoolean(go, "--surv")) esl_histogram_PlotSurvival(ofp, h); |
| 271 |
else esl_histogram_Plot(ofp, h); |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
if (outfile != NULL) fclose(ofp); |
| 279 |
if (strcmp(datafile, "-") != 0) fclose(ifp); |
| 280 |
esl_histogram_Destroy(h); |
| 281 |
esl_getopts_Destroy(go); |
| 282 |
return 0; |
| 283 |
} |