Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working example for time series estimation #62

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions carl/learning/calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ def fit(self, X, y, sample_weight=None):
* `self` [object]:
`self`.
"""
# Check inputs
X, y = check_X_y(X, y)
# Check inputs. Avoid this in order to use RNNs
# X, y = check_X_y(X, y)

# Convert y
label_encoder = LabelEncoder()
Expand Down
73 changes: 73 additions & 0 deletions carl/ratios/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""This module implements some utility functions."""

# Carl is free software; you can redistribute it and/or modify it
# under the terms of the Revised BSD License; see LICENSE file for
# more details.

import numpy as np
from .classifier import ClassifierRatio
import matplotlib.pyplot as plt


def plot_score_(ratio, axis, reals, labels):
cal_num, cal_den = (ratio.classifier_.calibrators_[0].calibrator0,
ratio.classifier_.calibrators_[0].calibrator1)
axis.plot(reals, cal_num.pdf(reals.reshape(-1, 1)),
label="p(s_num), num~{0}".format(labels[0]))
axis.plot(reals, cal_den.pdf(reals.reshape(-1, 1)),
label="p(s_den), den~{0}".format(labels[1]))
axis.legend(frameon=False)


def plot_scores(classifier_ratios, num_den_labels=None, save_file=None):
"""
Plot score plots for a list of classifier ratios,
useful to check training and calibration quality

Parameters
----------
* `classifier_ratios` [list of `ClassifierRatio`]:
List of ClassifierRatio to plot.

* `num_den_labels` [list of (num_label, den_label) tuples]:
List of numeratod and denominator labels for each
ratio in `classifier_ratios`

"""

if len(classifier_ratios) == 0:
raise ValueError
for classifier in classifier_ratios:
if not isinstance(classifier, ClassifierRatio):
raise ValueError

num_ratios = len(classifier_ratios)
if num_den_labels is None:
num_den_labels = [(l, 0) for l in
range(len(classifier_ratios))]
else:
# Ensure format for numbers
def rounds(n): return map(lambda x: round(x, 1), n)
num_den_labels = [(rounds(x[0]), rounds(x[1])) for x in num_den_labels]

reals = np.linspace(0, 1)

if num_ratios <= 3:
f, axarr = plt.subplots(1, num_ratios, sharex=True, sharey=True,
figsize=(12, 5))
for k, (ratio, pos) in enumerate(zip(classifier_ratios,
num_den_labels)):
plot_score_(ratio, axarr[k], reals, labels=pos)
else:
f, axarr = plt.subplots((num_ratios // 3) + (num_ratios % 3 != 0), 3,
sharex=True, sharey=True)
for k, (ratio, pos) in enumerate(zip(classifier_ratios,
num_den_labels)):
plot_score_(ratio, axarr[k // 3, k % 3], reals, labels=pos)
f.subplots_adjust(hspace=0.05, wspace=0.05)
if save_file is None:
plt.show()
else:
plt.savefig(save_file)
plt.close()
plt.clf()
947 changes: 947 additions & 0 deletions examples/Likelihood ratio of VAR time series.ipynb

Large diffs are not rendered by default.