From f73c9b14b3bc10d4f0cbedaf32c1e78f800a8bbb Mon Sep 17 00:00:00 2001 From: rnhmjoj Date: Sun, 24 May 2020 22:04:31 +0200 Subject: [PATCH] ex-5: add header to the results table --- README.md | 27 +++++---------------------- ex-5/main.c | 20 +++++++++++++------- ex-5/plot.py | 17 ++++++++++++----- 3 files changed, 30 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index d595bed..037d499 100644 --- a/README.md +++ b/README.md @@ -155,27 +155,10 @@ and `-b`. ### Exercise 5 `ex-5/main` compute estimations of the integral of exp(x) between 0 and 1 -with the methods plain MC, MISER and VEGAS with different number of points. -It takes no arguments in input. -It prints out eight columns: - -1. the number of points; - -2. the integral estimation with plain MC; - -3. its uncertainty; - -4. the integral estimation with MISER; - -5. its uncertainty; - -6. the integral estimation with VEGAS; - -7. its uncertainty; - -8. its χ². - -To plot the results do: +using several methods: a plain Monte Carlo, the MISER and VEGAS algorithms +with different number of samples. The program takes no arguments and prints +a table of the result and its error for each method. +To visualise the results, you can plot the table by doing: $ ex-5/main | ex-5/plot.py @@ -214,7 +197,7 @@ The program accepts some parameters to control the histogram and number of events, run it with `-h` to see their usage. (optional) `ex-6/plots/emd.py` makes the plots of the EMD statistics of the RL -deconvolution (shown in excercies.pdf, section 6.6) as a function of the number +deconvolution (shown in exercises.pdf, section 6.6) as a function of the number of rounds. The programs sources its data from two files in the same directory, these were obtained by running `ex-6/bin/test`. Do: diff --git a/ex-5/main.c b/ex-5/main.c index 39931b5..115cb1c 100644 --- a/ex-5/main.c +++ b/ex-5/main.c @@ -5,7 +5,7 @@ #include -// Wrapper for the gsl_function. +// exp() wrapper for the gsl_function structure // double function (double * x, size_t dim, void * params) { @@ -19,13 +19,13 @@ void results (size_t calls, double result, double error, double chi) { if (calls != 0) { - printf ("%.0e\t", (double)calls); + printf ("%6.0e | ", (double)calls); } - printf ("%.10f\t", result); - printf ("%.10f\t", error); + printf ("%5f | ", result); + printf ("%5f | ", error); if (chi != 0) { - printf ("%.3f\t", chi); + printf ("%5f", chi); } } @@ -52,6 +52,12 @@ int main(int argc, char** argv) expo.dim = 1; expo.params = NULL; + // Print the results table header + printf(" Calls | Plain | Error | MISER |" + " Error | VEGAS | Error | χ²\n"); + printf(" ------|----------|----------|----------|" + "----------|----------|----------|---------\n"); + // Compute the integral for the following number of function calls: // 50 // 500 @@ -62,7 +68,7 @@ int main(int argc, char** argv) // 50'000'000 // with the three MC methods: plain MC, MISER and VEGAS. // - while (calls < 50000001) + while (calls <= 5000000) { // Plain MC gsl_monte_plain_state *sMC = gsl_monte_plain_alloc (dims); @@ -82,7 +88,7 @@ int main(int argc, char** argv) gsl_monte_vegas_state *sVE = gsl_monte_vegas_alloc (dims); gsl_monte_vegas_integrate (&expo, lower, upper, dims, calls, r, sVE, &integral, &error); - + // In order to see the parameters of the VEGAS integration, decomment this // paragraph: // diff --git a/ex-5/plot.py b/ex-5/plot.py index d297e70..d581431 100755 --- a/ex-5/plot.py +++ b/ex-5/plot.py @@ -4,19 +4,26 @@ import matplotlib.pyplot as plt import numpy as np import sys -calls, I_MC, σ_MC, I_MI, σ_MI, I_VE, σ_VE, chi = np.loadtxt(sys.stdin, unpack=True) +table = np.loadtxt(sys.stdin, unpack=True, skiprows=2, delimiter='|') +calls, I_MC, σ_MC, I_MI, σ_MI, I_VE, σ_VE, chi = table exact = 1.7182818285 plt.rcParams['font.size'] = 17 + plt.figure() plt.title('Plain MC', loc='right') plt.ylabel('$I^{oss}$') plt.xlabel('calls') -plt.axhline(y=exact, color='#c556ea', linestyle='-', label='Exact value') +plt.xscale('log') -plt.errorbar(calls, I_MC, linestyle='', marker='o', yerr=σ_MC, color='#92182b', label='Plain MC') -plt.errorbar(calls, I_MI, linestyle='', marker='o', yerr=σ_MI, color='black', label='MISER') -plt.errorbar(calls, I_VE, linestyle='', marker='o', yerr=σ_VE, color='gray', label='VEGAS') +plt.axhline(y=exact, color='#c556ea', linestyle='-', + label='Exact value') +plt.errorbar(calls, I_MC, linestyle='', marker='o', + yerr=σ_MC, color='#92182b', label='Plain MC') +plt.errorbar(calls, I_MI, linestyle='', marker='o', + yerr=σ_MI, color='black', label='MISER') +plt.errorbar(calls, I_VE, linestyle='', marker='o', + yerr=σ_VE, color='gray', label='VEGAS') plt.legend() plt.show()