<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> import os . path <EOL> import numpy as np <EOL> import numpy . testing as npt <EOL> import pandas as pd <EOL> import pytest <EOL> from patsy import dmatrix <EOL> from . . import mnl <EOL> @ pytest . fixture <EOL> def num_alts ( ) : <EOL> return <NUM_LIT:4> <EOL> @ pytest . fixture ( scope = '<STR_LIT>' , params = [ <EOL> ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> pd . Series ( [ - <NUM_LIT> , <NUM_LIT> ] , index = [ '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> pd . DataFrame ( [ <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] , <EOL> columns = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) ) , <EOL> ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> pd . Series ( [ <EOL> <NUM_LIT> , - <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> , <EOL> <NUM_LIT> ] , <EOL> index = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> pd . DataFrame ( [ <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] , <EOL> columns = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) ) , <EOL> ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> pd . Series ( [ <EOL> - <NUM_LIT> , - <NUM_LIT> , <NUM_LIT> ] , <EOL> index = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> pd . DataFrame ( [ <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] , <EOL> columns = [ '<STR_LIT>' , '<STR_LIT:train>' , '<STR_LIT>' , '<STR_LIT>' ] ) ) , <EOL> ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> pd . Series ( [ <EOL> - <NUM_LIT> , - <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> index = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> pd . DataFrame ( [ <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] , <EOL> columns = [ '<STR_LIT>' , '<STR_LIT:train>' , '<STR_LIT>' , '<STR_LIT>' ] ) ) ] ) <EOL> def test_data ( request ) : <EOL> data , choosers , form , col , est_expected , sim_expected = request . param <EOL> return { <EOL> '<STR_LIT:data>' : data , <EOL> '<STR_LIT>' : choosers , <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT>' : col , <EOL> '<STR_LIT>' : est_expected , <EOL> '<STR_LIT>' : sim_expected <EOL> } <EOL> @ pytest . fixture <EOL> def df ( test_data ) : <EOL> filen = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT:data>' , test_data [ '<STR_LIT:data>' ] ) <EOL> return pd . read_csv ( filen ) <EOL> @ pytest . fixture <EOL> def choosers ( test_data ) : <EOL> filen = os . path . join ( <EOL> os . path . dirname ( __file__ ) , '<STR_LIT:data>' , test_data [ '<STR_LIT>' ] ) <EOL> return pd . read_csv ( filen ) <EOL> @ pytest . fixture <EOL> def chosen ( df , num_alts , test_data ) : <EOL> return df [ test_data [ '<STR_LIT>' ] ] . values . astype ( '<STR_LIT:int>' ) . reshape ( <EOL> ( len ( df ) / num_alts , num_alts ) ) <EOL> @ pytest . fixture <EOL> def dm ( df , test_data ) : <EOL> return dmatrix ( test_data [ '<STR_LIT>' ] , data = df , return_type = '<STR_LIT>' ) <EOL> @ pytest . fixture <EOL> def choosers_dm ( choosers , test_data ) : <EOL> return dmatrix ( <EOL> test_data [ '<STR_LIT>' ] , data = choosers , return_type = '<STR_LIT>' ) <EOL> @ pytest . fixture <EOL> def fit_coeffs ( dm , chosen , num_alts ) : <EOL> log_like , fit = mnl . mnl_estimate ( dm . as_matrix ( ) , chosen , num_alts ) <EOL> return fit . Coefficient . values <EOL> def test_mnl_estimate ( dm , chosen , num_alts , test_data ) : <EOL> log_like , fit = mnl . mnl_estimate ( dm . as_matrix ( ) , chosen , num_alts ) <EOL> result = pd . Series ( fit . Coefficient . values , index = dm . columns ) <EOL> result , expected = result . align ( test_data [ '<STR_LIT>' ] ) <EOL> npt . assert_allclose ( result . values , expected . values , rtol = <NUM_LIT> ) <EOL> def test_mnl_simulate ( dm , fit_coeffs , num_alts , test_data , choosers_dm ) : <EOL> data = np . array ( <EOL> [ [ <NUM_LIT:10> ** ( x + <NUM_LIT:1> ) for x in range ( len ( dm . columns ) ) ] ] * num_alts ) <EOL> probs = mnl . mnl_simulate ( <EOL> data , fit_coeffs , num_alts , returnprobs = True ) <EOL> npt . assert_allclose ( probs , [ [ <NUM_LIT:1> / num_alts ] * num_alts ] ) <EOL> probs = mnl . mnl_simulate ( <EOL> choosers_dm . as_matrix ( ) , fit_coeffs , num_alts , returnprobs = True ) <EOL> results = pd . DataFrame ( probs , columns = test_data [ '<STR_LIT>' ] . columns ) <EOL> results , expected = results . align ( test_data [ '<STR_LIT>' ] ) <EOL> npt . assert_allclose ( results . as_matrix ( ) , expected . as_matrix ( ) , rtol = <NUM_LIT> ) <EOL> def test_alternative_specific_coeffs ( num_alts ) : <EOL> template = np . array ( <EOL> [ [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ] ) <EOL> fish = df ( { '<STR_LIT:data>' : '<STR_LIT>' } ) <EOL> fish_choosers = choosers ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> fish_chosen = chosen ( fish , num_alts , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> num_choosers = len ( fish [ '<STR_LIT>' ] . unique ( ) ) <EOL> intercept_df = pd . DataFrame ( <EOL> np . tile ( template , ( num_choosers , <NUM_LIT:1> ) ) , <EOL> columns = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> income_df = pd . DataFrame ( <EOL> np . tile ( template , ( num_choosers , <NUM_LIT:1> ) ) , <EOL> columns = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> for idx , row in fish . iterrows ( ) : <EOL> income_df . loc [ idx ] = income_df . loc [ idx ] * row [ '<STR_LIT>' ] <EOL> dm = pd . concat ( [ intercept_df , income_df ] , axis = <NUM_LIT:1> ) <EOL> num_choosers = len ( fish_choosers [ '<STR_LIT>' ] . unique ( ) ) <EOL> intercept_df = pd . DataFrame ( <EOL> np . tile ( template , ( num_choosers , <NUM_LIT:1> ) ) , <EOL> columns = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> income_df = pd . DataFrame ( <EOL> np . tile ( template , ( num_choosers , <NUM_LIT:1> ) ) , <EOL> columns = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> for idx , row in fish_choosers . iterrows ( ) : <EOL> income_df . loc [ idx ] = income_df . loc [ idx ] * row [ '<STR_LIT>' ] <EOL> choosers_dm = pd . concat ( [ intercept_df , income_df ] , axis = <NUM_LIT:1> ) <EOL> expected = pd . Series ( [ <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> - <NUM_LIT> , - <NUM_LIT> ] , <EOL> index = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> log_like , fit = mnl . mnl_estimate ( dm . as_matrix ( ) , fish_chosen , num_alts ) <EOL> result = pd . Series ( fit . Coefficient . values , index = dm . columns ) <EOL> result , expected = result . align ( expected ) <EOL> npt . assert_allclose ( result . values , expected . values , rtol = <NUM_LIT> ) <EOL> expected = pd . DataFrame ( [ <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] , <EOL> columns = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> fit_coeffs = fit . Coefficient . values <EOL> probs = mnl . mnl_simulate ( <EOL> choosers_dm . as_matrix ( ) , fit_coeffs , num_alts , returnprobs = True ) <EOL> results = pd . DataFrame ( probs , columns = expected . columns ) <EOL> results , expected = results . align ( expected ) <EOL> npt . assert_allclose ( results . as_matrix ( ) , expected . as_matrix ( ) , rtol = <NUM_LIT> ) </s>
