<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Idea
Результат (ожидаемый)
обучение происходит на своем родном канале на симулированных данных
учитываются различия симуляции и данных (см. ниже алгоритм)
оценка качества (как и калибровка) будут несмещенными
качество лучше, чем baseline
Алгоритм
используем инклюзивный подход
учим при помощи классификатора перевзвешивание различия данных (удаляем треки из симуляции, которые не появляются в данных и удаляем из данных треки, которые не умеем симулировать)
Step1:  Import
Step2:  Reading initial data
Step3:  Data preprocessing
Step4:  Define mask for non-B events
Step5:  Define features
Step6:  Test that B-events similar in MC and data
Step7:  Test that number of tracks is independent on Track description
Step8:  Define base estimator and B weights, labels
Step9:  B probability computation
Step10:  Inclusive tagging
Step11:  Inclusive tagging
Step12:  New method
Reweighting with classifier
combine data and MC together to train a classifier
Step13:  train classifier to distinguish data and MC
Step14:  quality
Step15:  calibrate probabilities (due to reweighting rule where probabilities are used)
Step16:  compute MC and data track weights
Step17:  reweighting plotting
Step18:  Check reweighting rule
train classifier to distinguish data vs MC with provided weights
Step19:  Classifier trained on MC
Step20:  Classifier trained on data
Step21:  
Step22:  Calibration
<ASSISTANT_TASK:>
Python Code:
%pylab inline
figsize(8, 6)
import sys
sys.path.insert(0, "../")
Explanation: Idea
Результат (ожидаемый)
обучение происходит на своем родном канале на симулированных данных
учитываются различия симуляции и данных (см. ниже алгоритм)
оценка качества (как и калибровка) будут несмещенными
качество лучше, чем baseline
Алгоритм
используем инклюзивный подход
учим при помощи классификатора перевзвешивание различия данных (удаляем треки из симуляции, которые не появляются в данных и удаляем из данных треки, которые не умеем симулировать):
классификатор предсказывает $p(MC)$ и $p(RD)$
для симуляции при $p(MC)>0.5$
$$w_{MC}=\frac{p(RD)}{p(MC)},$$
       иначе
$$w_{MC}=1$$
    - для данных при $p(MC)<0.5$
$$w_{RD}=\frac{p(MC)}{p(RD)},$$
       иначе
$$w_{RD}=1$$
    - нормируем веса в событии
    - в формуле комбинирования возводим в степень $w * sign$
End of explanation
import pandas
import numpy
from folding_group import FoldingGroupClassifier
from rep.data import LabeledDataStorage
from rep.report import ClassificationReport
from rep.report.metrics import RocAuc
from sklearn.metrics import roc_curve, roc_auc_score
from decisiontrain import DecisionTrainClassifier
from rep.estimators import SklearnClassifier
Explanation: Import
End of explanation
import root_numpy
MC = pandas.DataFrame(root_numpy.root2array('../datasets/MC/csv/WG/Bu_JPsiK/2012/Tracks.root', stop=5000000))
data = pandas.DataFrame(root_numpy.root2array('../datasets/data/csv/WG/Bu_JPsiK/2012/Tracks.root', stop=5000000))
data.head()
MC.head()
Explanation: Reading initial data
End of explanation
from utils import data_tracks_preprocessing
data = data_tracks_preprocessing(data, N_sig_sw=True)
MC = data_tracks_preprocessing(MC)
', '.join(data.columns)
print sum(data.signB == 1), sum(data.signB == -1)
print sum(MC.signB == 1), sum(MC.signB == -1)
Explanation: Data preprocessing:
Add necessary features:
- #### define label = signB * signTrack
* if &gt; 0 (same sign) - label **1**
* if &lt; 0 (different sign) - label **0**
diff pt, min/max PID
Apply selections:
remove ghost tracks
loose selection on PID
End of explanation
mask_sw_positive = (data.N_sig_sw.values > 1) * 1
data.head()
data['group_column'] = numpy.unique(data.event_id, return_inverse=True)[1]
MC['group_column'] = numpy.unique(MC.event_id, return_inverse=True)[1]
data.index = numpy.arange(len(data))
MC.index = numpy.arange(len(MC))
Explanation: Define mask for non-B events
End of explanation
# features = ['cos_diff_phi', 'diff_pt', 'partPt', 'partP', 'nnkrec', 'diff_eta', 'EOverP', 
#             'ptB', 'sum_PID_mu_k', 'proj', 'PIDNNe', 'sum_PID_k_e', 'PIDNNk', 'sum_PID_mu_e', 'PIDNNm',
#             'phi', 'IP', 'IPerr', 'IPs', 'veloch', 'max_PID_k_e', 'ghostProb', 
#             'IPPU', 'eta', 'max_PID_mu_e', 'max_PID_mu_k', 'partlcs']
features = ['cos_diff_phi', 'partPt', 'partP', 'nnkrec', 'diff_eta', 'EOverP', 
            'ptB', 'sum_PID_mu_k', 'proj', 'PIDNNe', 'sum_PID_k_e', 'PIDNNk', 'sum_PID_mu_e', 'PIDNNm',
            'phi', 'IP', 'IPerr', 'IPs', 'veloch', 'max_PID_k_e', 'ghostProb', 
            'IPPU', 'eta', 'max_PID_mu_e', 'max_PID_mu_k', 'partlcs']
Explanation: Define features
End of explanation
b_ids_data = numpy.unique(data.group_column.values, return_index=True)[1]
b_ids_MC = numpy.unique(MC.group_column.values, return_index=True)[1]
Bdata = data.iloc[b_ids_data].copy()
BMC = MC.iloc[b_ids_MC].copy()
Bdata['Beta'] = Bdata.diff_eta + Bdata.eta
BMC['Beta'] = BMC.diff_eta + BMC.eta
Bdata['Bphi'] = Bdata.diff_phi + Bdata.phi
BMC['Bphi'] = BMC.diff_phi + BMC.phi
Bfeatures = ['Beta', 'Bphi', 'ptB']
hist(Bdata['ptB'].values, normed=True, alpha=0.5, bins=60, 
     weights=Bdata['N_sig_sw'].values)
hist(BMC['ptB'].values, normed=True, alpha=0.5, bins=60);
hist(Bdata['Beta'].values, normed=True, alpha=0.5, bins=60, 
     weights=Bdata['N_sig_sw'].values)
hist(BMC['Beta'].values, normed=True, alpha=0.5, bins=60);
hist(Bdata['Bphi'].values, normed=True, alpha=0.5, bins=60, 
     weights=Bdata['N_sig_sw'].values)
hist(BMC['Bphi'].values, normed=True, alpha=0.5, bins=60);
tt_base = DecisionTrainClassifier(learning_rate=0.02, n_estimators=1000,
                                  n_threads=16)
data_vs_MC_B = pandas.concat([Bdata, BMC])
label_data_vs_MC_B = [0] * len(Bdata) + [1] * len(BMC)
weights_data_vs_MC_B = numpy.concatenate([Bdata.N_sig_sw.values * (Bdata.N_sig_sw.values > 1) * 1, 
                                          numpy.ones(len(BMC))])
weights_data_vs_MC_B_all = numpy.concatenate([Bdata.N_sig_sw.values, numpy.ones(len(BMC))])
tt_B = FoldingGroupClassifier(SklearnClassifier(tt_base), n_folds=2, random_state=321, 
                              train_features=Bfeatures, group_feature='group_column')
%time tt_B.fit(data_vs_MC_B, label_data_vs_MC_B, sample_weight=weights_data_vs_MC_B)
pass
roc_auc_score(label_data_vs_MC_B, tt_B.predict_proba(data_vs_MC_B)[:, 1], sample_weight=weights_data_vs_MC_B)
roc_auc_score(label_data_vs_MC_B, tt_B.predict_proba(data_vs_MC_B)[:, 1], sample_weight=weights_data_vs_MC_B_all)
from hep_ml.reweight import GBReweighter, FoldingReweighter
reweighterB = FoldingReweighter(GBReweighter(), random_state=3444)
reweighterB.fit(BMC[Bfeatures], Bdata[Bfeatures], target_weight=Bdata.N_sig_sw)
BMC_weights = reweighterB.predict_weights(BMC[Bfeatures])
hist(Bdata['ptB'].values, normed=True, alpha=0.5, bins=60, 
     weights=Bdata['N_sig_sw'].values)
hist(BMC['ptB'].values, normed=True, alpha=0.5, bins=60, weights=BMC_weights);
weights_data_vs_MC_B_w = numpy.concatenate([Bdata.N_sig_sw.values * (Bdata.N_sig_sw.values > 1) * 1, 
                                            BMC_weights])
weights_data_vs_MC_B_all_w = numpy.concatenate([Bdata.N_sig_sw.values, BMC_weights])
tt_B = FoldingGroupClassifier(SklearnClassifier(tt_base), n_folds=2, random_state=321, 
                              train_features=Bfeatures, group_feature='group_column')
%time tt_B.fit(data_vs_MC_B, label_data_vs_MC_B, sample_weight=weights_data_vs_MC_B_w)
roc_auc_score(label_data_vs_MC_B, tt_B.predict_proba(data_vs_MC_B)[:, 1], sample_weight=weights_data_vs_MC_B_all_w)
MC['N_sig_sw'] = BMC_weights[numpy.unique(MC.group_column.values, return_inverse=True)[1]]
Explanation: Test that B-events similar in MC and data
End of explanation
def compute_target_number_of_tracks(X):
    ids = numpy.unique(X.group_column, return_inverse=True)[1]
    number_of_tracks = numpy.bincount(X.group_column)
    target = number_of_tracks[ids]
    return target
from decisiontrain import DecisionTrainRegressor
from rep.estimators import SklearnRegressor
from rep.metaml import FoldingRegressor
tt_base_reg = DecisionTrainRegressor(learning_rate=0.02, n_estimators=1000,
                                     n_threads=16)
%%time
tt_data_NT = FoldingRegressor(SklearnRegressor(tt_base_reg), n_folds=2, random_state=321, 
                              features=features)
tt_data_NT.fit(data, compute_target_number_of_tracks(data), sample_weight=data.N_sig_sw.values * mask_sw_positive)
from sklearn.metrics import mean_squared_error
mean_squared_error(compute_target_number_of_tracks(data), tt_data_NT.predict(data),
                   sample_weight=data.N_sig_sw.values) ** 0.5
mean_squared_error(compute_target_number_of_tracks(data), 
                   [numpy.mean(compute_target_number_of_tracks(data))] * len(data), 
                   sample_weight=data.N_sig_sw.values) ** 0.5
%%time
tt_MC_NT = FoldingRegressor(SklearnRegressor(tt_base_reg), n_folds=2, random_state=321, 
                            features=features)
tt_MC_NT.fit(MC, compute_target_number_of_tracks(MC), sample_weight=MC.N_sig_sw.values)
mean_squared_error(compute_target_number_of_tracks(MC),
                   tt_MC_NT.predict(MC), sample_weight=MC.N_sig_sw.values) ** 0.5
mean_squared_error(compute_target_number_of_tracks(MC),
                   [numpy.mean(compute_target_number_of_tracks(MC))] * len(MC),
                   sample_weight=MC.N_sig_sw.values) ** 0.5
tt_MC_NT.get_feature_importances().sort_values(by='effect')[-5:]
Explanation: Test that number of tracks is independent on Track description
End of explanation
tt_base = DecisionTrainClassifier(learning_rate=0.02, n_estimators=1000,
                                  n_threads=16)
B_signs = data['signB'].groupby(data['group_column']).aggregate(numpy.mean)
B_weights = data['N_sig_sw'].groupby(data['group_column']).aggregate(numpy.mean)
B_signs_MC = MC['signB'].groupby(MC['group_column']).aggregate(numpy.mean)
B_weights_MC = MC['N_sig_sw'].groupby(MC['group_column']).aggregate(numpy.mean)
Explanation: Define base estimator and B weights, labels
End of explanation
from scipy.special import logit, expit
def compute_Bprobs(X, track_proba, weights=None, normed_weights=False):
    if weights is None:
        weights = numpy.ones(len(X))
    _, data_ids = numpy.unique(X['group_column'], return_inverse=True)
    
    track_proba[~numpy.isfinite(track_proba)] = 0.5
    track_proba[numpy.isnan(track_proba)] = 0.5
    
    if normed_weights:
        weights_per_events = numpy.bincount(data_ids, weights=weights)
        weights /= weights_per_events[data_ids]
    predictions = numpy.bincount(data_ids, weights=logit(track_proba) * X['signTrack'] * weights)
    return expit(predictions)
Explanation: B probability computation
End of explanation
tt_data = FoldingGroupClassifier(SklearnClassifier(tt_base), n_folds=2, random_state=321, 
                                 train_features=features, group_feature='group_column')
%time tt_data.fit(data, data.label, sample_weight=data.N_sig_sw.values * mask_sw_positive)
pass
pandas.DataFrame({'dataset': ['MC', 'data'], 
                  'quality': [roc_auc_score(
                B_signs_MC, compute_Bprobs(MC, tt_data.predict_proba(MC)[:, 1]), sample_weight=B_weights_MC),
                             roc_auc_score(
                B_signs, compute_Bprobs(data, tt_data.predict_proba(data)[:, 1]), sample_weight=B_weights)]})
Explanation: Inclusive tagging: training on data
End of explanation
tt_MC = FoldingGroupClassifier(SklearnClassifier(tt_base), n_folds=2, random_state=321, 
                               train_features=features, group_feature='group_column')
%time tt_MC.fit(MC, MC.label)
pass
pandas.DataFrame({'dataset': ['MC', 'data'], 
                  'quality': [roc_auc_score(
                B_signs_MC, compute_Bprobs(MC, tt_MC.predict_proba(MC)[:, 1]), sample_weight=B_weights_MC),
                             roc_auc_score(
                B_signs, compute_Bprobs(data, tt_MC.predict_proba(data)[:, 1]), sample_weight=B_weights)]})
Explanation: Inclusive tagging: training on MC
End of explanation
combined_data_MC = pandas.concat([data, MC])
combined_label = numpy.array([0] * len(data) + [1] * len(MC))
combined_weights_data = data.N_sig_sw.values #/ numpy.bincount(data.group_column)[data.group_column.values]
combined_weights_data_passed = combined_weights_data * mask_sw_positive
combined_weights_MC = MC.N_sig_sw.values# / numpy.bincount(MC.group_column)[MC.group_column.values]
combined_weights = numpy.concatenate([combined_weights_data_passed, 
                                      1. * combined_weights_MC / sum(combined_weights_MC) * sum(combined_weights_data_passed)])
combined_weights_all = numpy.concatenate([combined_weights_data, 
                                          1. * combined_weights_MC / sum(combined_weights_MC) * sum(combined_weights_data)])
Explanation: New method
Reweighting with classifier
combine data and MC together to train a classifier
End of explanation
%%time 
tt_base_large = DecisionTrainClassifier(learning_rate=0.3, n_estimators=1000,
                                        n_threads=20)
tt_data_vs_MC = FoldingGroupClassifier(SklearnClassifier(tt_base_large), n_folds=2, random_state=321, 
                                       train_features=features + ['label'], group_feature='group_column')
tt_data_vs_MC.fit(combined_data_MC, combined_label, sample_weight=combined_weights)
a = []
for n, p in enumerate(tt_data_vs_MC.staged_predict_proba(combined_data_MC)):
    a.append(roc_auc_score(combined_label, p[:, 1], sample_weight=combined_weights))
plot(a)
Explanation: train classifier to distinguish data and MC
End of explanation
combined_p = tt_data_vs_MC.predict_proba(combined_data_MC)[:, 1]
roc_auc_score(combined_label, combined_p, sample_weight=combined_weights)
roc_auc_score(combined_label, combined_p, sample_weight=combined_weights_all)
Explanation: quality
End of explanation
from utils import calibrate_probs, plot_calibration
combined_p_calib = calibrate_probs(combined_label, combined_weights, combined_p)[0]
plot_calibration(combined_p, combined_label, weight=combined_weights)
plot_calibration(combined_p_calib, combined_label, weight=combined_weights)
Explanation: calibrate probabilities (due to reweighting rule where probabilities are used)
End of explanation
# reweight data predicted as data to MC
used_probs = combined_p_calib
data_probs_to_be_MC = used_probs[combined_label == 0]
MC_probs_to_be_MC = used_probs[combined_label == 1]
track_weights_data = numpy.ones(len(data))
# take data with probability to be data
mask_data = data_probs_to_be_MC < 0.5
track_weights_data[mask_data] = (data_probs_to_be_MC[mask_data]) / (1 - data_probs_to_be_MC[mask_data])
# reweight MC predicted as MC to data
track_weights_MC = numpy.ones(len(MC))
mask_MC = MC_probs_to_be_MC > 0.5
track_weights_MC[mask_MC] = (1 - MC_probs_to_be_MC[mask_MC]) / (MC_probs_to_be_MC[mask_MC])
# simple approach, reweight only MC
track_weights_only_MC = (1 - MC_probs_to_be_MC) / MC_probs_to_be_MC
# data_ids = numpy.unique(data['group_column'], return_inverse=True)[1]
# MC_ids = numpy.unique(MC['group_column'], return_inverse=True)[1]
# # event_weight_data = (numpy.bincount(data_ids, weights=data.N_sig_sw) / numpy.bincount(data_ids))[data_ids]
# # event_weight_MC = (numpy.bincount(MC_ids, weights=MC.N_sig_sw) / numpy.bincount(MC_ids))[MC_ids]
# # normalize weights for tracks in a way that sum w_track = 1 per event
# track_weights_data /= numpy.bincount(data_ids, weights=track_weights_data)[data_ids]
# track_weights_MC /= numpy.bincount(MC_ids, weights=track_weights_MC)[MC_ids]
Explanation: compute MC and data track weights
End of explanation
hist(combined_p_calib[combined_label == 1], label='MC', normed=True, alpha=0.4, bins=60,
     weights=combined_weights_MC)
hist(combined_p_calib[combined_label == 0], label='data', normed=True, alpha=0.4, bins=60, 
     weights=combined_weights_data);
legend(loc='best')
hist(track_weights_MC, normed=True, alpha=0.4, bins=60, label='MC')
hist(track_weights_data, normed=True, alpha=0.4, bins=60, label='RD');
legend(loc='best')
numpy.mean(track_weights_data), numpy.mean(track_weights_MC)
hist(combined_p_calib[combined_label == 1], label='MC', normed=True, alpha=0.4, bins=60, 
     weights=track_weights_MC * MC.N_sig_sw.values)
hist(combined_p_calib[combined_label == 0], label='data', normed=True, alpha=0.4, bins=60, 
     weights=track_weights_data * data.N_sig_sw.values);
legend(loc='best')
roc_auc_score(combined_label, combined_p_calib,
              sample_weight=numpy.concatenate([track_weights_data * data.N_sig_sw.values,
                                               track_weights_MC * MC.N_sig_sw.values]))
Explanation: reweighting plotting
End of explanation
%%time 
tt_check = FoldingGroupClassifier(SklearnClassifier(tt_base), n_folds=2, random_state=433, 
                                  train_features=features + ['label'], group_feature='group_column')
tt_check.fit(combined_data_MC, combined_label, 
             sample_weight=numpy.concatenate([track_weights_data * data.N_sig_sw.values * mask_sw_positive, 
                                              track_weights_MC * MC.N_sig_sw.values]))
roc_auc_score(combined_label, tt_check.predict_proba(combined_data_MC)[:, 1],
              sample_weight=numpy.concatenate([track_weights_data * data.N_sig_sw.values * mask_sw_positive, 
                                              track_weights_MC * MC.N_sig_sw.values]))
# * sum(track_weights_data * mask_sw_positive) / sum(track_weights_MC)
roc_auc_score(combined_label, tt_check.predict_proba(combined_data_MC)[:, 1],
              sample_weight=numpy.concatenate([track_weights_data * data.N_sig_sw.values, 
                                              track_weights_MC * MC.N_sig_sw.values]))
# * sum(track_weights_data) / sum(track_weights_MC)
Explanation: Check reweighting rule
train classifier to distinguish data vs MC with provided weights
End of explanation
tt_reweighted_MC = FoldingGroupClassifier(SklearnClassifier(tt_base), n_folds=2, random_state=321, 
                                          train_features=features, group_feature='group_column')
%time tt_reweighted_MC.fit(MC, MC.label, sample_weight=track_weights_MC * MC.N_sig_sw.values)
pass
pandas.DataFrame({'dataset': ['MC', 'data'], 
                  'quality': [roc_auc_score(
                B_signs_MC, 
                compute_Bprobs(MC, tt_reweighted_MC.predict_proba(MC)[:, 1], 
                               weights=track_weights_MC, normed_weights=False),
                sample_weight=B_weights_MC),
                             roc_auc_score(
                B_signs, 
                compute_Bprobs(data, tt_reweighted_MC.predict_proba(data)[:, 1], 
                               weights=track_weights_data, normed_weights=False),
                sample_weight=B_weights)]})
pandas.DataFrame({'dataset': ['MC', 'data'], 
                  'quality': [roc_auc_score(
                B_signs_MC, 
                compute_Bprobs(MC, tt_reweighted_MC.predict_proba(MC)[:, 1], 
                               weights=track_weights_MC, normed_weights=False),
                sample_weight=B_weights_MC),
                             roc_auc_score(
                B_signs, 
                compute_Bprobs(data, tt_reweighted_MC.predict_proba(data)[:, 1], 
                               weights=track_weights_data, normed_weights=False),
                sample_weight=B_weights)]})
Explanation: Classifier trained on MC
End of explanation
%%time 
tt_reweighted_data = FoldingGroupClassifier(SklearnClassifier(tt_base), n_folds=2, random_state=321, 
                                            train_features=features, group_feature='group_column')
tt_reweighted_data.fit(data, data.label, 
                       sample_weight=track_weights_data * data.N_sig_sw.values * mask_sw_positive)
pass
pandas.DataFrame({'dataset': ['MC', 'data'], 
                  'quality': [roc_auc_score(
                B_signs_MC, 
                compute_Bprobs(MC, tt_reweighted_data.predict_proba(MC)[:, 1], 
                               weights=track_weights_MC, normed_weights=False),
                sample_weight=B_weights_MC),
                             roc_auc_score(
                B_signs, 
                compute_Bprobs(data, tt_reweighted_data.predict_proba(data)[:, 1], 
                               weights=track_weights_data, normed_weights=False),
                sample_weight=B_weights)]})
pandas.DataFrame({'dataset': ['MC', 'data'], 
                  'quality': [roc_auc_score(
                B_signs_MC, 
                compute_Bprobs(MC, tt_reweighted_data.predict_proba(MC)[:, 1], 
                               weights=track_weights_MC, normed_weights=False),
                sample_weight=B_weights_MC),
                             roc_auc_score(
                B_signs, 
                compute_Bprobs(data, tt_reweighted_data.predict_proba(data)[:, 1], 
                               weights=track_weights_data, normed_weights=False),
                sample_weight=B_weights)]})
Explanation: Classifier trained on data
End of explanation
numpy.mean(mc_sum_weights_per_event), numpy.mean(data_sum_weights_per_event)
_, data_ids = numpy.unique(data['group_column'], return_inverse=True)
mc_sum_weights_per_event = numpy.bincount(MC.group_column.values, weights=track_weights_MC)
data_sum_weights_per_event = numpy.bincount(data_ids, weights=track_weights_data)
hist(mc_sum_weights_per_event, bins=60, normed=True, alpha=0.5)
hist(data_sum_weights_per_event, bins=60, normed=True, alpha=0.5, weights=B_weights);
hist(mc_sum_weights_per_event, bins=60, normed=True, alpha=0.5)
hist(data_sum_weights_per_event, bins=60, normed=True, alpha=0.5, weights=B_weights);
hist(numpy.bincount(MC.group_column), bins=81, normed=True, alpha=0.5, range=(0, 80))
hist(numpy.bincount(data.group_column), bins=81, normed=True, alpha=0.5, range=(0, 80));
hist(expit(p_tt_mc) - expit(p_data), bins=60, weights=B_weights, normed=True, label='standard approach',
     alpha=0.5);
hist(expit(p_data_w_MC) - expit(p_data_w), bins=60, weights=B_weights, normed=True, label='compensate method',
     alpha=0.5);
legend()
xlabel('$p_{MC}-p_{data}$')
Explanation: 
End of explanation
from utils import compute_mistag
bins_perc = [10, 20, 30, 40, 50, 60, 70, 80, 90]
compute_mistag(expit(p_data), B_signs, B_weights, chosen=numpy.ones(len(B_signs), dtype=bool),
               bins=bins_perc,
               uniform=False, label='data')
compute_mistag(expit(p_tt_mc), B_signs, B_weights, chosen=numpy.ones(len(B_signs), dtype=bool),
               bins=bins_perc,
               uniform=False, label='MC')
compute_mistag(expit(p_data_w), B_signs, B_weights, chosen=numpy.ones(len(B_signs), dtype=bool),
               bins=bins_perc,
               uniform=False, label='new')
legend(loc='best')
xlim(0.3, 0.5)
ylim(0.2, 0.5)
bins_edg = numpy.linspace(0.3, 0.9, 10)
compute_mistag(expit(p_data), B_signs, B_weights, chosen=numpy.ones(len(B_signs), dtype=bool),
               bins=bins_edg,
               uniform=True, label='data')
compute_mistag(expit(p_tt_mc), B_signs, B_weights, chosen=numpy.ones(len(B_signs), dtype=bool),
               bins=bins_edg,
               uniform=True, label='MC')
compute_mistag(expit(p_data_w), B_signs, B_weights, chosen=numpy.ones(len(B_signs), dtype=bool),
               bins=bins_edg,
               uniform=True, label='new')
legend(loc='best')
Explanation: Calibration
End of explanation
<END_TASK>