Ejemplo n.º 1
0
def example1_run():
    data_file = "https://raw.githubusercontent.com/arteagac/xlogit/master/examples/data/fishing_long.csv"
    df = pd.read_csv(data_file)

    varnames = ['price', 'catch', 'income']
    X = df[varnames].values
    y = df['choice'].values
    transvars = ['catch']

    model = MixedLogit()
    model.fit(
        X,
        y,
        # isvars=['income'],
        transvars=transvars,
        transformation="boxcox",
        alts=['beach', 'boat', 'charter', 'pier'],
        # scipy_optimisation=True,
        # isvars=['income', 'price'],
        randvars={'catch': 'n'},
        # init_coeff=np.repeat(0, 11),
        # gtol=1e-4,
        grad=False,
        hess=False,
        # tol=1e-4,
        # method="L-BFGS-B",
        # fit_intercept=True,
        varnames=varnames)
    model.summary()
Ejemplo n.º 2
0
def example3_run():
    df = pd.read_csv("https://raw.githubusercontent.com/arteagac/xlogit/master/examples/data/fishing_long.csv")

    varnames = ['price', 'catch']
    X = df[varnames].values
    y = df['choice'].values

    model = MixedLogit()
    # initial_coeffs = [0.5, 0.5, 0.5, -0.1, 0.5, 1, 1, 1]
    model.fit(X, y, varnames=varnames,
            # isvars=['income'],
            alts=['beach', 'boat', 'charter', 'pier'],
            randvars={'price': 'n'},
            transvars=['price', 'catch'],
            #   verbose=0,
            n_draws=600,
            maxiter=2000,
            # grad=False,
            hess=False,
            halton=True,
            # init_coeff=np.repeat(0.2, 5),
            correlation=True,
            # weights=np.ones(1182),
            # method="L-BFGS-B",
            fit_intercept=True
            )
    model.summary()
Ejemplo n.º 3
0
def example8_run():
    df = pd.read_csv(
        "https://raw.githubusercontent.com/arteagac/xlogit/master/examples/data/electricity_long.csv"
    )
    varnames = ['cl', 'loc', 'pf', 'wk', 'tod', 'seas']
    y = df['choice'].values
    # choice_var = df['choice']
    df['wk'] = -df['wk']
    # df['tod'] = -df['tod']
    X = df[varnames].values

    # isvarnames = []

    randvars = {'wk': 'u', 'seas': 'n', 'tod': 'n'}
    corvars = ['tod', 'seas']
    alts = [1, 2, 3, 4]
    np.random.seed(123)
    model = MixedLogit()
    # init_coeff=[]
    model.fit(
        X,
        y,
        varnames=varnames,
        alts=alts,
        #   isvars=None,
        randvars=randvars,
        #   ids=df['chid'],
        panels=df.id.values,
        isvars=[],
        halton=True,
        # correlation=True,
        # transvars=['cl', 'loc'],
        # method="L-BFGS-B",
        correlation=corvars,
        # tol=1e-8,
        # hess=False,
        # grad=False,
        #   weights=None,
        #   avail=None,
        #   base_alt=None,
        #   fit_intercept=False,
        #   init_coeff=None,
        #   maxiter=2000,
        #   random_state=None,
        verbose=1,
        n_draws=200)
    model.summary()
Ejemplo n.º 4
0
def example12_run():
    try:
        df = pd.read_csv(
            "examples_prit/Final_RT_Dataset_weights_corrected.csv")
    except FileNotFoundError:
        df = pd.read_csv(
            "xlogitprit/examples_prit/Final_RT_Dataset_weights_corrected.csv")

    all_vars = [
        'cost', 'risk', 'seats', 'noise', 'crowdness', 'convloc', 'clientele'
    ]
    is_vars = ['gender', 'age']
    rand_vars = {'cost': 'n', 'risk': 'n'}  #, 'seats': 'ln'}
    choice_set = ['WaterTaxi', 'Ferry', 'Hovercraft', 'Helicopter']
    choice_var = df['choice']
    # alt_var = df['idx.alt']
    Tol = 1e-4
    # av = df['av']
    choice_id = df['idx.chid']
    ind_id = df['idx.id']
    weights = df['weight']

    R = 100
    X = df[all_vars]
    # X_zeros = np.zeros_like(X)
    model = MixedLogit()
    model.fit(
        X=X,
        y=choice_var,
        varnames=all_vars,
        alts=choice_set,
        #   isvars=is_vars,
        ids=choice_id,
        weights=weights,
        # init_coeff=np.repeat(20, 10),
        panels=ind_id,
        randvars=rand_vars,
        grad=False,
        hess=False,
        #   n_draws=R,
        #   correlation=['cost', 'risk'],
        #   fit_intercept=True,
        #   avail=av,
        gtol=Tol)
    model.summary()
Ejemplo n.º 5
0
def example7_run():
    df = pd.read_csv(
        "https://raw.githubusercontent.com/arteagac/xlogit/master/examples/data/electricity_long.csv"
    )

    varnames = ["pf", "cl", "loc", "wk", "tod", "seas"]

    df['tod'] = -df['tod']
    df['seas'] = -df['seas']

    X = df[varnames].values
    y = df['choice'].values
    choice_id = df['chid']
    alt = [1, 2, 3, 4]
    np.random.seed(123)
    model = MixedLogit()
    model.fit(
        X,
        y,
        varnames,
        alts=alt,
        randvars={
            'cl': 't',
            'loc': 'n',
            'wk': 'u',
            'tod': 'ln',
            'seas': 'ln'
        },
        fit_intercept=True,
        isvars=['pf'],
        #   transformation="boxcox",
        #   transvars=['cl', 'loc', 'wk'],
        correlation=True,
        # ids=choice_id,
        panels=df.id.values,
        #   isvars=[],
        #   grad=False,
        #   hess=False,
        halton=False,
        # tol=1e-4,
        #   method='L-BFGS-B',
        n_draws=400)
    model.summary()
Ejemplo n.º 6
0
from xlogitprit import MixedLogit
import numpy as np
import pandas as pd

data_file = "https://raw.githubusercontent.com/arteagac/xlogit/master/examples/data/fishing_long.csv"
df = pd.read_csv(data_file)

varnames = ['price', 'catch', 'income']
X = df[varnames].values
y = df['choice'].values
transvars = ['catch']

model = MixedLogit()
model.fit(
    X,
    y,
    # isvars=['income'],
    transvars=transvars,
    transformation="boxcox",
    alts=['beach', 'boat', 'charter', 'pier'],
    # scipy_optimisation=True,
    isvars=['income', 'price'],
    randvars={'catch': 'n'},
    weights=np.ones(4782),
    # init_coeff=np.repeat(0, 11),
    # gtol=1e-4,
    # grad=False,
    # hess=False,
    # tol=1e-4,
    # method="L-BFGS-B",
    fit_intercept=True,
Ejemplo n.º 7
0
from xlogitprit import MixedLogit
df = pd.read_csv(
    "https://raw.githubusercontent.com/arteagac/xlogit/master/examples/data/electricity_long.csv"
)

varnames = ["pf", "cl", "loc", "wk", "tod", "seas"]

df['tod'] = -df['tod']
df['seas'] = -df['seas']

X = df[varnames].values
y = df['choice'].values
choice_id = df['chid']
alt = [1, 2, 3, 4]
np.random.seed(123)
model = MixedLogit()
model.fit(
    X,
    y,
    varnames,
    alts=alt,
    randvars={
        'cl': 't',
        'loc': 'n',
        'wk': 'u',
        'tod': 'ln',
        'seas': 'ln'
    },
    fit_intercept=True,
    isvars=['pf'],
    #   transformation="boxcox",
Ejemplo n.º 8
0
from xlogitprit import MixedLogit
df = pd.read_csv("https://raw.githubusercontent.com/arteagac/xlogit/master/examples/data/electricity_long.csv")

varnames = ["pf", "cl", "loc", "wk", "tod", "seas"]

df['tod'] = -df['tod']
df['seas'] = -df['seas']


X = df[varnames].values
y = df['choice'].values
choice_id = df['chid']
alt = [1, 2, 3, 4]
np.random.seed(123)
model = MixedLogit()
model.fit(X, y,
          varnames,
          alts=alt,
          randvars={'cl': 'n', 'loc': 'n', 'wk': 'u', 'tod': 'ln', 'seas': 'ln'},
          # fit_intercept=True,
          # isvars=['pf'],
          transformation="boxcox",
          transvars=['pf', 'cl'],
          # correlation=True,
          weights=np.ones(361),
          # ids=choice_id,
          panels=df.id.values,
          isvars=[],
          # grad=False,
          # hess=False,
Ejemplo n.º 9
0
import pandas as pd
import numpy as np

from xlogitprit import MixedLogit

df = pd.read_csv(
    "https://raw.githubusercontent.com/arteagac/xlogit/master/examples/data/fishing_long.csv"
)

varnames = ['price', 'catch']
X = df[varnames].values
y = df['choice'].values

model = MixedLogit()
# initial_coeffs = [0.5, 0.5, 0.5, -0.1, 0.5, 1, 1, 1]
model.fit(
    X,
    y,
    varnames=varnames,
    # isvars=['income'],
    alts=['beach', 'boat', 'charter', 'pier'],
    randvars={'price': 'n'},
    transvars=['price', 'catch'],
    #   verbose=0,
    n_draws=600,
    maxiter=2000,
    # grad=False,
    # hess=False,
    halton=True,
    # init_coeff=np.repeat(0.2, 5),
    correlation=True,
Ejemplo n.º 10
0
def example2_run():
    df = pd.read_csv(
        "https://raw.githubusercontent.com/arteagac/xlogit/master/examples/data/electricity_long.csv"
    )

    varnames = ["pf", "tod", "seas", "cl", "loc", "wk"]
    # tod = df['tod']
    # kde = ss.gaussian_kde(tod)
    # xs = np.linspace(min(tod), max(tod))
    # plt.plot(xs, kde(xs))
    # plt.savefig('testtod.png')
    # print(1/0)
    # Testing plots of tod and seas

    df['tod'] = -df['tod']
    df['seas'] = -df['seas']

    X = df[varnames].values
    y = df['choice'].values

    choice_id = df['chid']
    panels = df.id.values
    alt = [1, 2, 3, 4]

    randvars1 = {"cl": 'n', "loc": 'n', "wk": 'u', "tod": 'ln', "seas": 'ln'}
    randvars2 = {"wk": 'u', "tod": 'ln', "seas": 'ln', "cl": 'n', "loc": 'n'}
    randvars3 = {"wk": 'u', "tod": 'ln', "seas": 'ln', "cl": 'n', "loc": 'n'}
    randvars4 = {"wk": 'u', "tod": 'ln', "seas": 'ln', "cl": 'n', "loc": 'n'}
    # randvars4 = {"cl": 'n', "loc": 'n', "wk": 'n', "tod": 'ln', "seas": 'ln'}

    # init_coeff = np.array([-0.93415106, 2.18043941, 2.19710561, -0.24598737, 2.55557668, 1.89018881,
    #   0.40421956,  0.15024535,  0.23448914, -0.01404247,  0.06802344,  0.36800581,
    #  -0.21865647,  0.22454976,  0.13975384,  2.35863656, -0.1631371,   0.15818687,
    #   0.10194485,  1.71571673,  0.8943477])
    np.random.seed(123)
    model = MixedLogit()
    model.check_if_gpu_available()
    N = len(np.unique(df['id'].values))
    training_size = int(0.8 * N)
    ids = np.random.choice(N, training_size, replace=False)
    train_idx = [ii for ii, id_val in enumerate(df['id']) if id_val in ids]
    test_idx = [ii for ii, id_val in enumerate(df['id']) if id_val not in ids]

    X_train = X[train_idx, :]
    y_train = y[train_idx]
    panels_train = panels[train_idx]
    X_test = X[test_idx, :]
    y_test = y[test_idx]
    panels_test = panels[test_idx]

    model.fit(
        X,
        y,
        varnames,
        alts=alt,
        randvars=randvars1,
        # init_coeff=init_coeff,
        # fit_intercept=True,
        # isvars=['pf'],
        # transformation="boxcox",
        # transvars=['pf'],
        #   correlation=True,
        #   correlation=['wk', 'tod', 'loc'],
        # weights=np.ones(361),
        # ids=choice_id,
        panels=panels,
        # isvars=[],
        # grad=False,
        # hess=False,
        # ftol=1e-7,
        # gtol=1e-7,
        # halton=True,
        # method='L-BFGS-B',
        # maxiter=100,
        #   n_draws=80,
        # verbose=False
    )
    model.summary()
    model.validation_loglik(X_test, y_test, panels=panels_test)
    model.corr()
    model.cov()
    model.stddev()
Ejemplo n.º 11
0
from xlogitprit import MixedLogit
df = pd.read_csv(
    "https://raw.githubusercontent.com/arteagac/xlogit/master/examples/data/electricity_long.csv"
)

varnames = ["pf", "cl", "loc", "wk", "tod", "seas"]

df['tod'] = -df['tod']
df['seas'] = -df['seas']

X = df[varnames].values
y = df['choice'].values
alt = [1, 2, 3, 4]
np.random.seed(123)
model = MixedLogit()
# init_coeff = np.repeat(.1, 13)
model.fit(
    X,
    y,
    varnames,
    alts=alt,
    randvars={
        'cl': 'n',
        'loc': 'n',
        'wk': 'u',
        'tod': 'ln',
        'seas': 'ln'
    },
    # fit_intercept=True,
    # transformation="boxcox",
Ejemplo n.º 12
0
all_vars = [
    'cost', 'risk', 'seats', 'noise', 'crowdness', 'convloc', 'clientele'
]
is_vars = ['gender', 'age']
rand_vars = {'cost': 'n', 'risk': 'n', 'seats': 'n'}
choice_set = ['WaterTaxi', 'Ferry', 'Hovercraft', 'Helicopter']
choice_var = df['choice']
alt_var = df['idx.alt']
Tol = 1e-4
# av = df['av']
choice_id = df['idx.chid']
ind_id = df['idx.id']
weights = df['weight']

R = 100
model = MixedLogit()
model.fit(
    X=df[all_vars],
    y=choice_var,
    varnames=all_vars,
    alts=alt_var,
    #   isvars=is_vars,
    ids=choice_id,
    weights=weights,
    #   panels=ind_id,
    randvars=rand_vars,
    #   n_draws=R,
    #   correlation=['cost', 'risk'],
    #   fit_intercept=True,
    #   avail=av,
    gtol=Tol)
Ejemplo n.º 13
0
    "https://raw.githubusercontent.com/arteagac/xlogit/master/examples/data/electricity_long.csv"
)
varnames = ['cl', 'loc', 'pf', 'wk', 'tod', 'seas']
y = df['choice'].values
# choice_var = df['choice']
df['wk'] = -df['wk']
# df['tod'] = -df['tod']
X = df[varnames].values

# isvarnames = []

randvars = {'wk': 'u', 'seas': 'n', 'tod': 'n'}
corvars = ['tod', 'seas']
alts = [1, 2, 3, 4]
np.random.seed(123)
model = MixedLogit()
# init_coeff=[]
model.fit(
    X,
    y,
    varnames=varnames,
    alts=alts,
    #   isvars=None,
    randvars=randvars,
    #   ids=df['chid'],
    panels=df.id.values,
    isvars=[],
    halton=True,
    # correlation=True,
    # transvars=['cl', 'loc'],
    # method="L-BFGS-B",