Examples

Cars

import pandas as pd

from pypbl.priors import Normal, Exponential
from pypbl.elicitation import BayesPreference

data = pd.read_csv('data/mtcars.csv')
print(data)

# set index of the data frame to be the item names
data.set_index('model', inplace=True)

p = BayesPreference(data=data)
p.set_priors([
    Exponential(1),  # MPG - high miles per gallon is preferred
    Normal(),  # number of cylinders
    Normal(),  # displacement
    Exponential(2),  # horsepower - high horsepower is preferred
    Normal(),  # real axle ratio
    Normal(),  # weight
    Exponential(-3),  # quarter mile time - high acceleration is preferred
    Normal(),  # engine type
    Normal(),  # transmission type
    Normal(),  # number of gears
    Normal()  # number of carburetors
])

# add some preferences and infer the weights for each parameter
p.add_strict_preference('Pontiac Firebird', 'Fiat 128')
p.add_strict_preference('Mazda RX4', 'Mazda RX4 Wag')
p.add_indifferent_preference('Merc 280', 'Merc 280C')
p.infer_weights(method='mean')

print('\ninferred weights')
for a, b in zip(data.columns.values.tolist(), p.weights.tolist()):
    print('{}: {}'.format(a, b))

# rank all the items and highlight the top five
print('\ntop 5 cars')
print(p.rank().head(5))

# suggest a new item to compare against the highest ranked solution - this may take some time to compute
print('\nsuggested pair to request new preference')
print(p.suggest())

Beers

import pandas as pd

from pypbl.priors import Normal
from pypbl.elicitation import BayesPreference

data = pd.read_csv('data/bdbeers.csv')
print(data)

# set index of the data frame to be the item names
data.set_index('name', inplace=True)

# drop some columns that are unlikely to influence preferences
data.drop(columns=['srm', 'target_fg'], inplace=True)

p = BayesPreference(data=data)
p.set_priors([
    Normal(),  # abv - alcohol strength
    Normal(),  # attenuation_level
    Normal(),  # ebc - beer colour
    Normal(),  # ibu - bitterness
    Normal(),  # n_hops - number of hops
    Normal(),  # n_malt - number of malts
    Normal(),  # ph - ph level
    Normal(),  # target_og - target gravity
])

# add some preferences and infer the weights for each parameter
p.add_strict_preference('Indie Pale Ale', 'Kingpin')
p.add_strict_preference('Dead Pony Club', 'Indie Pale Ale')
p.add_strict_preference('Dead Pony Club', 'Punk IPA 2010 - Current')
p.add_strict_preference('5am Saint', 'Dead Pony Club')
p.add_strict_preference('Hazy Jane', '5am Saint')
p.add_strict_preference('Hazy Jane', 'Quench Quake')
p.add_strict_preference('Hazy Jane', 'Zombie Cake')
p.infer_weights(method='mean')

print('\ninferred weights')
for a, b in zip(data.columns.values.tolist(), p.weights.tolist()):
    print('{}: {}'.format(a, b))

# rank all the items and highlight the top five
print('\ntop 5 beers')
print(p.rank().head(5))

# suggest a new item to compare against the highest ranked solution - this may take some time to compute
print('\nsuggested pair to request new preference')
print(p.suggest())