Esempi: Lezione 8
Contents
Esempi: Lezione 8¶

8.0 Generazione di tanti toy experiment¶
statistiche.h
#ifndef statistiche_h
#define statistiche_h
#include <cmath>
class statistiche
{
public:
statistiche () :
m_sum (0.),
m_sumSq (0.),
m_N (0)
{}
~statistiche () {}
// aggiunge un elemento al campione
long long int addEvent (double value)
{
m_sum += value ;
m_sumSq += value * value ;
++m_N ;
return m_N ;
}
// restituisce la media
double getMean () const
{
if (m_N == 0) return 0. ;
return m_sum / static_cast<double> (m_N) ;
}
// restituisce la varianza
double getVariance (bool correct = false) const
{
if (m_N == 0) return 0. ;
double sigma = m_sumSq / static_cast<double> (m_N)
- (m_sum * m_sum) / static_cast<double> (m_N * m_N) ;
if (correct && m_N > 1) sigma = sigma * m_N / static_cast<double> (m_N - 1) ;
return sigma ;
}
// restituisce la sigma
double getSigma (bool correct = false) const
{
return sqrt (getVariance (correct)) ;
}
// restituisce la varianza della media
double getVarianceMean (bool correct = false) const
{
return getVariance (correct) / m_N ;
}
// restituisce la sigma della media
double getSigmaMean (bool correct = false) const
{
return sqrt (getVarianceMean (correct)) ;
}
// restituisce il numero di eventi
double getN () const
{
return m_N ;
}
// annulla tutti i membri
void reset ()
{
m_sum = 0. ;
m_sumSq = 0. ;
m_N = 0 ;
return ;
}
private:
// somma dei numeri del campione
double m_sum ;
// somma quadrata dei numeri del campione
double m_sumSq ;
// numero di elementi del campione
long long int m_N ;
} ;
#endif
main_00.cpp
/*
c++ -o main_00 main_00.cpp
*/
#include <cstdlib>
#include <iostream>
#include "../../Lezione_03/programmi/statistiche.h"
float rand_range (float min, float max)
{
return min + (max - min) * rand () / static_cast<float> (RAND_MAX) ;
}
int main (int argc, char ** argv)
{
// numero di eventi generati per esperimento
int NMAX = 500 ;
// numero di toy per construire la pdf della media per campionamento
int NToys = 100 ;
statistiche s_singleToy ;
statistiche s_tot ;
// loop sui toy experiment
for (int iToy = 0 ; iToy < NToys ; ++iToy)
{
int i = 0 ;
// il loop seguente e' un singolo toy experiment
while (i++ < NMAX) s_singleToy.addEvent (rand_range (-3., 3.)) ;
s_tot.addEvent (s_singleToy.getMean ()) ;
s_singleToy.reset () ;
} // loop sui toy experiment
std::cout << "prodotti " << s_tot.getN () << " toy" << std::endl ;
return 0 ;
}

8.1 Generazione di tanti toy experiment e visualizzazione della media¶
main_01.cpp
/*
c++ -o main_01 `root-config --cflags --glibs` main_01.cpp
*/
#include <cstdlib>
#include <iostream>
#include "TH1F.h"
#include "TCanvas.h"
#include "../../Lezione_05/programmi/statistiche.h"
float rand_range (float min, float max)
{
return min + (max - min) * rand () / static_cast<float> (RAND_MAX) ;
}
int main (int argc, char ** argv)
{
// numero di eventi generati per esperimento
int NMAX = 500 ;
// numero di toy per construire la pdf della media per campionamento
int NToys = 40000 ;
statistiche s_singleToy ;
statistiche s_tot ;
TH1F h_medie ("h_medie", "distribuzione delle medie", 41, -0.5, 0.5) ;
// loop sui toy experiment
for (int iToy = 0 ; iToy < NToys ; ++iToy)
{
int i = 0 ;
// il loop seguente e' un singolo toy experiment
while (i++ < NMAX) s_singleToy.addEvent (rand_range (-3., 3.)) ;
s_tot.addEvent (s_singleToy.getMean ()) ;
h_medie.Fill (s_singleToy.getMean ()) ;
s_singleToy.reset () ;
} // loop sui toy experiment
std::cout << "prodotti " << s_tot.getN () << " toy" << std::endl ;
TCanvas c1 ("c1", "c1", 100, 100, 1000, 1000) ;
h_medie.SetFillColor (kOrange + 1) ;
h_medie.GetXaxis ()->SetTitle ("media del campione") ;
h_medie.Draw () ;
c1.Print ("medie.png", "png") ;
return 0 ;
}

8.2 Confronto fra la larghezza della distribuzione e l’errore sulla media¶
/*
c++ -o main_02 main_02.cpp
*/
#include <cstdlib>
#include <iostream>
#include "../../Lezione_05/programmi/statistiche.h"
float rand_range (float min, float max)
{
return min + (max - min) * rand () / static_cast<float> (RAND_MAX) ;
}
int main (int argc, char ** argv)
{
// numero di eventi generati per esperimento
int NMAX = 500 ;
// numero di toy per construire la pdf della media per campionamento
int NToys = 40000 ;
statistiche s_singleToy ;
statistiche s_tot ;
statistiche s_incertezzaMedia ;
// loop sui toy experiment
for (int iToy = 0 ; iToy < NToys ; ++iToy)
{
int i = 0 ;
// il loop seguente e' un singolo toy experiment
while (i++ < NMAX) s_singleToy.addEvent (rand_range (-3., 3.)) ;
s_tot.addEvent (s_singleToy.getMean ()) ;
s_incertezzaMedia.addEvent (s_singleToy.getSigmaMean ()) ;
s_singleToy.reset () ;
} // loop sui toy experiment
std::cout << "media delle deviazioni standard della media per i singoli toy: "
<< s_incertezzaMedia.getMean () << " \n" ;
std::cout << "deviazione standard della distribuzione delle medie dei singoli toy: "
<< s_tot.getSigma () << " \n" ;
return 0 ;
}

8.3 Implementazione di hit-or-miss¶
/*
c++ -o main_03 main_03.cpp
*/
#include <cstdlib>
#include <iostream>
#include <cmath>
float rand_range (float min, float max)
{
return min + (max - min) * rand () / static_cast<float> (RAND_MAX) ;
}
double fsin (double x)
{
return 1. + sin (x) ;
}
bool isBelow (double g (double), double xMin, double xMax,
double yMin, double yMax)
{
double x = rand_range (xMin, xMax) ;
double y = rand_range (yMin, yMax) ;
if (y < g (x)) return true ;
return false ;
}
int main (int argc, char ** argv)
{
srand (time (NULL)) ;
int N = 10000 ;
int nHit = 0 ;
double xMin = 0. ;
double xMax = 2*M_PI ;
double yMin = 0. ;
double yMax = 2. ;
for (int i = 0 ; i < N ; ++i)
{
if (isBelow (fsin, xMin, xMax, yMin, yMax) == true) ++nHit ;
}
double Area = (xMax - xMin) * (yMax - yMin) ;
double Integral = nHit * Area / static_cast<double> (N) ;
double p = nHit / static_cast<double> (N) ;
double Var = Area * Area / static_cast<double> (N) * p * (1. - p) ;
double StdDev = sqrt (Var) ;
std::cout << "Integral = " << Integral
<< " +/- " << StdDev << std::endl ;
return 0 ;
}
