<jupyter_start><jupyter_text>Plackett–Luce model inference using LSR and I-LSRThis short notebook gives an example of how to use the `inference.py` Python module to find the spectral estimate (using LSR) and the maximum-likelihood estimate (using I-LSR).<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
import inference

%matplotlib inline
nb_items = 10
nb_rankings = 500
size_of_ranking = 3<jupyter_output><empty_output><jupyter_text>Generate some data1. We generate random model parameters (uniformly on a logarithmic scale).2. We generate rankings over random subsets of the items using a Plackett–Luce model with the previously generated parameters.<jupyter_code>strengths = np.exp(np.random.rand(nb_items))
rankings = inference.generate_rankings(strengths, nb_rankings, size_of_ranking)<jupyter_output><empty_output><jupyter_text>Below we give an example of what the data (i.e., the rankings) look like. We only show the 10 first rankings.<jupyter_code>rankings[:10]<jupyter_output><empty_output><jupyter_text>Compute the spectral and maximum-likelihood estimatesBoth estimates are well-defined only if the comparison graph is strongly connected.If it is not, the algorithm raises an error.<jupyter_code># Spectral estimate using LSR.
spectral_estimate = inference.lsr(nb_items, rankings)

# ML estimate using I-LSR.
ml_estimate = inference.ilsr(nb_items, rankings)<jupyter_output><empty_output><jupyter_text>Visualize the estimatesWe plot the true parameters (the strengths that generated the rankings) and the two estimates using heat maps. The darker the cell is, the "stronger" the item is.<jupyter_code>def plot_strengths(ax, strengths):
    img = np.log(strengths)[np.newaxis,:]
    ax.imshow(img, interpolation='nearest', cmap=plt.get_cmap('YlGnBu'))
    ax.yaxis.set_visible(False)
fig, (ax1, ax2, ax3) = plt.subplots(nrows=3, figsize=(10.0, 5.0))

plot_strengths(ax1, strengths)
ax1.set_title("True parameters")

plot_strengths(ax2, ml_estimate)
ax2.set_title("ML estimate")

plot_strengths(ax3, spectral_estimate)
ax3.set_title("Spectral estimate")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08961593172119488, 'fraction_numerical': 0.012802275960170697, 'mean_word_length': 6.902621722846442, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '624854', 'license': 'MIT', 'n_tokens_mistral': 715, 'n_tokens_neox': 680, 'n_words': 199, 'path': 'simple-example.ipynb', 'repo_name': 'lucasmaystre/lsr', 'size': '2109'}