void LLExp(){ // Exponential model gStyle->SetOptStat(0); gStyle->SetOptFit(1111); gStyle->SetLabelSize(0.05,"XY"); gStyle->SetTitleSize(0.05,"XYZ"); gStyle->SetTitleOffset(1,"XYZ"); // Model double fmin = 0; double fmax = 20; TF1 *fg = new TF1("fg", "([1]/[0])*TMath::Exp(-x/[0])", fmin, fmax); double tau = 2; fg->SetParameter(0, tau); fg->SetParameter(1, 1); fg->SetNpx(1000); // hist TH1F *hg = new TH1F("hg", "", 100, fmin, fmax); hg->SetMarkerStyle(20); hg->GetXaxis()->SetTitle("x"); hg->GetYaxis()->SetTitle("Events"); // Generate sample int Nsample = 200; double tau_hat = 0; for (int j = 0; j < Nsample; j++){ double t = fg->GetRandom(); hg->Fill(t); tau_hat += t / Nsample; } // LL double fmin = 0; double fmax = 20; TF1 *fll = new TF1("fll", "-[0]*log(x) - [0]*[1]/x", 0.001, 6); fll->SetParameter(0, Nsample); fll->SetParameter(1, tau_hat); fll->SetNpx(1000); fll->GetXaxis()->SetTitle("#tau"); fll->GetYaxis()->SetTitle("Log L(#tau)"); cout << "Mean = " << tau_hat << endl; new TCanvas(); hg->Draw("e1"); fg->SetParameter(1, Nsample*hg->GetBinWidth(1)); fg->Draw("same"); new TCanvas(); fll->Draw(); } void LLGaus(){ // Gaus model gStyle->SetOptStat(0); gStyle->SetOptFit(1111); gStyle->SetLabelSize(0.05, "XY"); gStyle->SetTitleSize(0.05, "XYZ"); gStyle->SetTitleOffset(1, "XYZ"); // Model double mux = 0; double sigmax = 1; TF1 *fg = new TF1("fg", "[0]*TMath::Gaus(x,[1],[2])", -5, 5); fg->SetParameters(1. / (sqrt(2 * TMath::Pi())*sigmax), mux, sigmax); fg->GetXaxis()->SetTitle("x"); fg->GetYaxis()->SetTitle("f_{x}(x)"); fg->SetTitle(""); fg->SetLineColor(2); fg->SetNpx(1000); // hist TH1F *hg = new TH1F("hg", "", 100, -5, 5); hg->SetMarkerStyle(20); hg->GetXaxis()->SetTitle("x"); hg->GetYaxis()->SetTitle("Events"); // Generate sample int Nsample = 1000; double mu_hat = 0; double mu2_hat = 0; double sigma_hat = 0; for (int j = 0; j < Nsample; j++){ double t = fg->GetRandom(); hg->Fill(t); mu_hat += t / Nsample; mu2_hat += pow(t,2) / Nsample; } sigma_hat = sqrt(mu2_hat - pow(mu_hat, 2)); cout << "Mean = " << mu_hat << ", sigma = " << sigma_hat << endl; // LL double fxmin = -0.5; double fxmax = 0.5; double fymin = 0.8; double fymax = 1.2; TF2 *fll = new TF2("fll", "-1*[0]*log(sqrt(2*TMath::Pi())*y) - [0]/(2*y*y)*([1]-x*x)",fxmin,fxmax,fymin,fymax); fll->SetParameter(0, Nsample); fll->SetParameter(1, mu2_hat); fll->SetNpx(1000); fll->GetXaxis()->SetTitle("#mu"); fll->GetYaxis()->SetTitle("#sigma"); fll->GetZaxis()->SetTitle("Log L"); new TCanvas(); hg->Draw("e1"); fg->SetParameter(0, Nsample*hg->GetBinWidth(1) / (sqrt(2 * TMath::Pi())*sigmax)); fg->Draw("same"); new TCanvas(); fll->Draw("surf2"); new TCanvas(); fll->Draw("cont1z"); }