In [21]:
import numpy as np
import astropy
from itertools import izip
from pearce.emulator import OriginalRecipe, ExtraCrispy

In [22]:
from matplotlib import pyplot as plt
%matplotlib inline
import seaborn as sns
sns.set()

In [23]:
training_dir = '/u/ki/swmclau2/des/PearceLHC_wp_z_sham_free_split/'
experts, overlap = 10, 2
fixed_params = {'z':0.0}

In [24]:
emu = OriginalRecipe(training_dir, fixed_params=fixed_params)
#emu = ExtraCrispy(training_dir, experts, overlap, fixed_params = fixed_params)

In [25]:
names = ['mean_occupation_centrals_assembias_param1','mean_occupation_satellites_assembias_param1',\
        'mean_occupation_centrals_assembias_split1','mean_occupation_satellites_assembias_split1']

In [31]:
#mock_wp = cat.calc_wp(rp_bins, RSD= False)
MAP = np.array([ 1.0, 0.0,0.5,0.5])


params = dict(zip(names, MAP))
#print params.keys()

emu_wps = []
#mock_nds = []
split = np.linspace(0.1, 0.9, 8)

#cat.model._input_model_dictionary['centrals_occupation']._split_abscissa = split_abcissa
#cat.model._input_model_dictionary['satellites_occupation']._split_abscissa = split_abcissa
for p in split:
    params['mean_occupation_centrals_assembias_split1'] = p
    params['mean_occupation_satellites_assembias_split1'] = p
    #print params.keys()
    #print cat.model.param_dict
    emu_wps.append(emu.emulate_wrt_r(params, emu.scale_bin_centers)[0])
    
emu_wps = np.array(emu_wps)

In [32]:
params = dict(zip(names, [0.0,0.0,0.5,0.5]))   

print params
noab_wp = emu.emulate_wrt_r(params, emu.scale_bin_centers)[0]


{'mean_occupation_satellites_assembias_split1': 0.5, 'mean_occupation_satellites_assembias_param1': 0.0, 'mean_occupation_centrals_assembias_split1': 0.5, 'mean_occupation_centrals_assembias_param1': 0.0}
sham_pos = np.c_[galaxy_catalog['halo_x'],\ galaxy_catalog['halo_y'],\ galaxy_catalog['halo_z']] sham_wp = wp(sham_pos, rp_bins , period=cat.Lbox, num_threads=1)

In [33]:
plt.figure(figsize=(10,8))
for p, emu_wp in zip(split, emu_wps):
    #print emu_wp
    plt.plot(emu.scale_bin_centers, emu_wp, label = p)
    
#plt.plot(bin_centers, sham_wp, ls='--', label = 'SHAM')
plt.plot(emu.scale_bin_centers, noab_wp, ls=':', label = 'No AB')


#plt.loglog()
plt.xscale('log')
plt.legend(loc='best',fontsize = 15)
plt.xlim([1e-1, 30e0]);
#plt.ylim([1,15000])
plt.xlabel(r'$r$',fontsize = 15)
plt.ylabel(r'$\xi(r)$',fontsize = 15)
plt.show()



In [34]:
plt.figure(figsize=(10,8))
for p, emu_wp in zip(split, emu_wps):
    plt.plot(emu.scale_bin_centers, emu_wp/noab_wp, label = p)
    
#plt.plot(bin_centers, sham_wp, ls='--', label = 'SHAM')
#plt.plot(bin_centers, noab_wp, ls=':', label = 'No AB')


#plt.loglog()
plt.xscale('log')
plt.legend(loc='best',fontsize = 15)
plt.xlim([1e-1, 15e0]);
plt.ylim([0.8, 1.2])
plt.xlabel(r'$r$',fontsize = 15)
plt.ylabel(r'$\xi(r)$',fontsize = 15)
plt.show()



In [24]:
import scipy.optimize as op
from itertools import izip

In [25]:
def nll(p):
    # Update the kernel parameters and compute the likelihood.
    # params are log(a) and log(m)
    #ll = 0
    #for emulator, _y in izip(self._emulators, self.y):
    #    emulator.kernel[:] = p
    #    ll += emulator.lnlikelihood(_y, quiet=True)
    emu._emulator.kernel[:] = p
    print p
    ll= emu._emulator.lnlikelihood(emu.y, quiet=False)

    # The scipy optimizer doesn't play well with infinities.
    return -ll if np.isfinite(ll) else 1e25

# And the gradient of the objective function.
def grad_nll(p):
    # Update the kernel parameters and compute the likelihood.
    #gll = 0
    #for emulator, _y in izip(self._emulators, self.y):
    #    emulator.kernel[:] = p
    #    gll += emulator.grad_lnlikelihood(_y, quiet=True)
    emu._emulator.kernel[:] = p
    gll = emu._emulator.grad_lnlikelihood(emu.y, quiet=True)
    return -gll

In [26]:
emu.goodness_of_fit(training_dir)


Out[26]:
array([ 0.99992995,  0.99986322,  0.99986264,  0.999844  ,  0.9997624 ,
        0.99958338,  0.99930644,  0.99944203,  0.99883149,  0.99572952,
        0.97476404,  0.97082905,  0.87971156,  0.94471602,  0.92468451,
       -0.19442956, -0.37925496,  0.92957744])

In [27]:
p0 = emu._emulator.kernel.vector

In [28]:
p0 = np.log(np.random.rand(emu._emulator.kernel.vector.shape[0]))
results = op.minimize(nll, p0, jac=grad_nll)


[-3.51877894 -1.031018   -0.03814318 -1.12453113 -5.67319569 -1.57654881]
[-2.54866192 -1.20465382 -0.24510811 -1.15897529 -5.73876184 -1.59899366]
[-0.73571178 -0.2929829   0.7663019   0.15657296 -3.17872519  0.75055019]
[-2.29441917 -1.0768038  -0.10327098 -0.97448671 -5.3797499  -1.26950061]
[-1.90578933 -0.94877488  0.18587524 -0.16692819 -3.96293447 -2.32630763]
[-2.25483709 -1.06376401 -0.07382134 -0.8922366  -5.23544677 -1.37713676]
[-2.30632749 -1.37161234 -0.28350743 -0.6856578  -4.96381384 -1.38446668]
[-2.51228908 -2.60300567 -1.12225178  0.14065741 -3.8772821  -1.41378638]
[ -2.84046891 -11.12014018   1.86926408   1.23468974  -0.49898834
  -1.4966161 ]
[-2.54980728 -3.57670217 -0.78025548  0.26572946 -3.49106855 -1.42325565]
[-2.58773319 -3.76557123 -1.04251328 -1.02861603 -2.51160925 -1.42351584]
[-2.56347872 -3.64478525 -0.87479356 -0.20085318 -3.13799535 -1.42334944]
[-2.55707573 -3.64188036 -0.87851208 -0.1829293  -3.13818215 -1.42116457]
[-2.53146375 -3.63026079 -0.89338616 -0.11123379 -3.13892934 -1.41242508]
[-2.42901584 -3.58378252 -0.95288247  0.17554824 -3.1419181  -1.37746711]
[-2.10285447 -3.60015337 -0.69386037  0.31915205 -3.10358396 -1.29918388]
[-1.51957683 -4.38660261 -0.46517878  0.93951959 -2.22986675 -1.13192076]
[-0.24347272 -6.80171336  0.38183469  0.9194749   0.7845268  -0.83002592]
[-0.24051607 -6.55368495  0.01131107  1.71074856  0.64850849 -0.84451821]
[-0.3194272  -5.73296402 -0.15285792  1.89336016  0.06005056 -0.92560859]
[ 0.06456015 -4.50515016 -0.45810562  2.92141935 -0.11771211 -0.97850654]
[ 0.86364382 -4.33387217 -1.09285096  4.97644362  1.05011832 -0.93975226]
[ 1.47505064 -4.26427314 -0.93067628  5.92923467  1.72210891 -0.92259167]
[ 2.62538208 -4.62376382 -0.16206115  7.26248534  3.13060213 -0.89272899]
[ 3.1142781  -4.73072303  0.29182103  7.57825148  3.72621936 -0.87072544]
[ 3.06635692 -4.74816233  0.24729192  7.51041792  3.73629824 -0.86954529]
[ 2.97809748 -4.74621392  0.16486738  7.35528732  3.76537138 -0.86786421]
[ 2.83461554 -4.74416375  0.03531968  7.0556909   3.86184633 -0.86699597]
[ 2.26068779 -4.73596307 -0.48287113  5.85730523  4.24774613 -0.86352302]
[ 1.76677578 -4.80175223 -0.91871599  4.53936314  5.01537201 -0.86904905]
[ 2.12790933 -4.75364918 -0.60003938  5.50300262  4.45410713 -0.86500859]
[ 2.14330425 -4.79197874 -0.56073671  5.41997628  4.61814349 -0.8696357 ]
[ 2.15231409 -4.80383177 -0.50956105  5.29443194  4.8452548  -0.88648779]
[ 2.13348806 -4.80362352 -0.39431526  5.04859959  5.24075108 -0.9352853 ]
[ 2.08866333 -4.82179669 -0.3634583   4.99778626  5.27693149 -0.95057275]
[ 1.95459994 -4.86626651 -0.33353691  4.90785383  5.267805   -0.97586174]
[ 1.66322517 -4.93773478 -0.328695    4.78583878  5.15871512 -1.01581914]
[ 1.28511965 -4.99740178 -0.3970877   4.68462664  4.95927206 -1.05376079]
[ 0.93411358 -5.0288266  -0.54153059  4.67566511  4.73331359 -1.07634569]
[ 0.9369705  -5.01708806 -0.60738811  4.80118801  4.69701078 -1.06541638]
[ 0.97061058 -5.01548576 -0.59893042  4.81947682  4.7203192  -1.06094607]
[ 0.96774612 -5.01540749 -0.60081723  4.81660365  4.72223816 -1.0601514 ]
[ 0.96694957 -5.01529482 -0.60076227  4.81774755  4.72163412 -1.05951302]
[ 0.96673456 -5.01546466 -0.60072981  4.81759906  4.72167814 -1.0594985 ]
[ 0.96674296 -5.01546641 -0.60071879  4.81766457  4.72162396 -1.05949926]
[ 0.96674199 -5.01546658 -0.60071879  4.81766454  4.7216221  -1.05949938]
[ 0.96674295 -5.01546642 -0.60071879  4.81766457  4.72162395 -1.05949927]
[ 0.96674295 -5.01546642 -0.60071879  4.81766457  4.72162395 -1.05949927]
[ 0.96674295 -5.01546642 -0.60071879  4.81766457  4.72162395 -1.05949927]
[ 0.96674295 -5.01546642 -0.60071879  4.81766457  4.72162395 -1.05949927]
[ 0.96674295 -5.01546642 -0.60071879  4.81766457  4.72162395 -1.05949927]
[ 0.96674295 -5.01546642 -0.60071879  4.81766457  4.72162395 -1.05949927]
[ 0.96674295 -5.01546642 -0.60071879  4.81766457  4.72162395 -1.05949927]
[ 0.96674295 -5.01546642 -0.60071879  4.81766457  4.72162395 -1.05949927]
[ 0.96674295 -5.01546642 -0.60071879  4.81766457  4.72162395 -1.05949927]
[ 0.96674295 -5.01546642 -0.60071879  4.81766457  4.72162395 -1.05949927]
[ 0.96674295 -5.01546642 -0.60071879  4.81766457  4.72162395 -1.05949927]
[ 0.96674295 -5.01546642 -0.60071879  4.81766457  4.72162395 -1.05949927]
[ 0.96674295 -5.01546642 -0.60071879  4.81766457  4.72162395 -1.05949927]
[ 0.96674295 -5.01546642 -0.60071879  4.81766457  4.72162395 -1.05949927]
[ 0.96674295 -5.01546642 -0.60071879  4.81766457  4.72162395 -1.05949927]
[ 0.96674199 -5.01546658 -0.60071879  4.81766454  4.7216221  -1.05949938]
[ 0.96674248 -5.0154665  -0.60071879  4.81766455  4.72162303 -1.05949932]
[ 0.96674272 -5.01546646 -0.60071879  4.81766456  4.72162349 -1.05949929]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.966742   -5.01546657 -0.60071878  4.81766443  4.72162204 -1.05949937]
[ 0.96674283 -5.01546644 -0.60071879  4.81766456  4.72162372 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.96674284 -5.01546644 -0.60071879  4.81766456  4.72162373 -1.05949928]
[ 0.966742   -5.01546657 -0.60071878  4.81766443  4.72162204 -1.05949937]
[ 0.96674242 -5.0154665  -0.60071879  4.8176645   4.72162288 -1.05949933]
[ 0.96674202 -5.01546657 -0.60071878  4.81766442  4.72162205 -1.05949937]
[ 0.96674242 -5.0154665  -0.60071879  4.8176645   4.72162288 -1.05949933]
[ 0.96674242 -5.0154665  -0.60071879  4.8176645   4.72162288 -1.05949933]
[ 0.96674242 -5.0154665  -0.60071879  4.8176645   4.72162288 -1.05949933]
[ 0.96674242 -5.0154665  -0.60071879  4.8176645   4.72162288 -1.05949933]
[ 0.96674242 -5.0154665  -0.60071879  4.8176645   4.72162288 -1.05949933]
[ 0.96674242 -5.0154665  -0.60071879  4.8176645   4.72162288 -1.05949933]
[ 0.96674242 -5.0154665  -0.60071879  4.8176645   4.72162288 -1.05949933]
[ 0.96674242 -5.0154665  -0.60071879  4.8176645   4.72162288 -1.05949933]
[ 0.96674242 -5.0154665  -0.60071879  4.8176645   4.72162288 -1.05949933]
[ 0.96674242 -5.0154665  -0.60071879  4.8176645   4.72162288 -1.05949933]
[ 0.96674242 -5.0154665  -0.60071879  4.8176645   4.72162288 -1.05949933]
[ 0.96674242 -5.0154665  -0.60071879  4.8176645   4.72162288 -1.05949933]
[ 0.96674242 -5.0154665  -0.60071879  4.8176645   4.72162288 -1.05949933]
[ 0.96674242 -5.0154665  -0.60071879  4.8176645   4.72162288 -1.05949933]
[ 0.96674242 -5.0154665  -0.60071879  4.8176645   4.72162288 -1.05949933]
[ 0.96674242 -5.0154665  -0.60071879  4.8176645   4.72162288 -1.05949933]
[ 0.96674242 -5.0154665  -0.60071879  4.8176645   4.72162288 -1.05949933]
[ 0.96674242 -5.0154665  -0.60071879  4.8176645   4.72162288 -1.05949933]
[ 0.96674242 -5.0154665  -0.60071879  4.8176645   4.72162288 -1.05949933]
[ 0.96674242 -5.0154665  -0.60071879  4.8176645   4.72162288 -1.05949933]
[ 0.96674242 -5.0154665  -0.60071879  4.8176645   4.72162288 -1.05949933]
[ 0.96674202 -5.01546657 -0.60071878  4.81766442  4.72162205 -1.05949937]
[ 0.96674222 -5.01546654 -0.60071878  4.81766446  4.72162247 -1.05949935]
[ 0.96674232 -5.01546652 -0.60071878  4.81766448  4.72162268 -1.05949934]
[ 0.96674237 -5.01546651 -0.60071878  4.81766449  4.72162278 -1.05949933]
[ 0.96674239 -5.01546651 -0.60071878  4.81766449  4.72162283 -1.05949933]
[ 0.9667424  -5.01546651 -0.60071878  4.8176645   4.72162286 -1.05949933]
[ 0.96674241 -5.0154665  -0.60071878  4.8176645   4.72162287 -1.05949933]
[ 0.96674241 -5.0154665  -0.60071878  4.8176645   4.72162288 -1.05949933]
[ 0.96674242 -5.0154665  -0.60071878  4.8176645   4.72162288 -1.05949933]
[ 0.96674242 -5.0154665  -0.60071879  4.8176645   4.72162288 -1.05949933]
[ 0.96674242 -5.0154665  -0.60071879  4.8176645   4.72162288 -1.05949933]
[ 0.96674242 -5.0154665  -0.60071879  4.8176645   4.72162288 -1.05949933]

In [30]:
results.x


Out[30]:
array([ 0.96674242, -5.0154665 , -0.60071879,  4.8176645 ,  4.72162288,
       -1.05949933])

In [31]:
p0


Out[31]:
array([-3.51877894, -1.031018  , -0.03814318, -1.12453113, -5.67319569,
       -1.57654881])

In [32]:
emu._emulator.kernel[:] = results.x
emu._emulator.recompute()


Out[32]:
True

In [34]:
print results.x
print emu.get_param_names()


[ 0.96674242 -5.0154665  -0.60071879  4.8176645   4.72162288 -1.05949933]
['mean_occupation_satellites_assembias_split1', 'mean_occupation_satellites_assembias_param1', 'mean_occupation_centrals_assembias_split1', 'mean_occupation_centrals_assembias_param1', 'r']

In [38]:
emu._emulator.kernel.pars


Out[38]:
array([  2.62936512e+00,   6.63453628e-03,   5.48417300e-01,
         1.23675908e+02,   1.12350437e+02,   3.46629315e-01])

In [ ]: