Task

Include the curvature as an extra parameter to your likelihood


In [22]:
import numpy as np
import scipy.integrate as integrate
def E(z,OmDE,OmM):
    """
    This function computes the integrand for the computation of the luminosity distance for a flat universe
    z -> float
    OmDE -> float
    OmM -> float
    gives
    E -> float
    """
    Omk=1-OmDE-OmM
    return 1/np.sqrt(OmM*(1+z)**3+OmDE+Omk*(1+z)**2)

def dl(z,OmDE,OmM,h=0.7):
    """
    This function computes the luminosity distance
    z -> float
    OmDE -> float
    h ->float
    returns
    dl -> float
    """
    inte=integrate.quad(E,0,z,args=(OmDE,OmM))
    # Velocidad del sonido en km/s
    c = 299792.458
    # Factor de Hubble
    Ho = 100*h
    Omk=1-OmDE-OmM
    distance_factor = c*(1+z)/Ho 
    if Omk>1e-10:
        omsqrt = np.sqrt(Omk)
        return distance_factor / omsqrt * np.sinh(omsqrt * inte[0])
    elif Omk<-1e-10:
        omsqrt = np.sqrt(-Omk)
        return distance_factor / omsqrt * np.sin(omsqrt * inte[0])
    else:
        return distance_factor * inte[0]
        
zandmu = np.loadtxt('../data/SCPUnion2.1_mu_vs_z.txt', skiprows=5,usecols=(1,2))
covariance = np.loadtxt('../data/SCPUnion2.1_covmat_sys.txt')
inv_cov = np.linalg.inv(covariance)
dl = np.vectorize(dl)
def loglike(params,h=0.7):
    """
    This function computes the logarithm of the likelihood. It recieves a vector
    params-> vector with one component (Omega Dark Energy, Omega Matter)
    """
    OmDE = params[0]
    OmM = params[1]
    
# Ahora quiero calcular la diferencia entre el valor reportado y el calculado
    muteo = 5.*np.log10(dl(zandmu[:,0],OmDE,OmM,h))+25
    print dl(zandmu[:,0],OmDE,OmM,h)
    delta = muteo-zandmu[:,1]
    chisquare=np.dot(delta,np.dot(inv_cov,delta))
    return -chisquare/2

In [23]:
loglike([0.6,0.3])


[  124.49991645   221.9519845    235.19437877   315.01464654   280.30054274
   398.16872828   355.13700562    74.69441739   186.32391817   200.24947027
   160.31028142    85.12150513   462.63402574   119.39634308   336.31701323
   115.6027417    221.39726375   133.94400866    70.82877222    66.84536093
   133.60881064   106.88479466   104.35013699   113.59875216   216.93512001
   105.94957723    65.66227159   156.88846553   216.33997794    95.62254675
   578.62507067   158.14873404    70.72076469    72.51399085   120.09950749
    94.79654368    71.28624429   101.05184314   160.20667596    83.64680252
    76.35377708   138.07641535   102.50356443    72.57100498   238.80391119
    73.65885622   121.72448069    74.45743702   131.04454584    71.76413169
    64.93591907   241.98363811   739.88139973   173.03706576   577.12450115
   678.2145442    606.22116269   354.29644133   260.00817551   135.26725278
   178.91993387    65.81106334    97.47842798    69.31408134   159.04921193
    75.01480796   136.60891456    96.15267087    69.31408134   108.54757444
   132.58554814   123.66213937    65.81106334   151.40275304   158.14873404
   108.10408348   127.67470011    70.62871947    81.16555999    84.68562653
   111.65370101   147.810388     242.90581756   104.55832978   147.36161122
   149.60609265   113.87417249    91.73741787   140.63713548    96.15267087
   146.46423697   149.60609265   185.72030853   256.76648397    89.09117576
   177.10875269   113.4299576    112.985803     104.11538242   309.91270981
   291.646689      99.68923383   137.95111573    93.50279192   111.20978763
   142.42901282   382.41085669   134.82015209   143.32531057   186.62803185
   307.09686209    66.24872703   101.45896761   217.63110428   187.53599186
    83.36514585   134.82015209    92.17867046   120.98980448   146.91289421
    90.4140239     75.01480796   158.14873404   101.45896761   262.78903405
   260.00817551   308.97386431   140.63713548   231.85508216   134.82015209
   144.22184764   100.13157654    64.93591907   140.63713548   287.91063655
   140.18931581    90.85509458    95.26913558   140.18931581    65.37346068
    83.36514585   116.09615104   165.81039008   107.66065289   105.44440578
   160.85088275    99.68923383   136.60891456    64.93591907   149.60609265
   109.43473745    82.045212     130.36198468   140.78948333   120.4054127
   207.87837389    79.47393441   362.13027269   105.37926689    65.05423639
   124.09003336   198.79918281   144.2773112    339.84063549    88.53893596
   100.00403222   117.02626264    77.78680838   214.39133277   252.52587625
   285.87643277   689.41408372   603.09320453   471.41741329  1205.32976692
  1525.36224334   193.07472034   522.15613412  1283.97195778  1508.86061321
  2019.08503099   686.23236385  1380.75641658  1520.15479029  2010.45666743
  2019.4382528   1544.95993377  1822.86830658   389.06044544  1307.28100665
  1056.1662386    544.85718942   876.66779507  1878.59660843   666.42006775
  1306.98205386  1151.13561613   717.995757     428.61692126  1456.69264348
   941.19801771   693.17715885  1034.57108192   865.54958272  1323.80100803
   929.58843531  1765.19862979   543.1294816    669.56311907   764.55964832
  1467.13410321   570.77196464  1324.69452092   589.00804341   826.63791801
   779.82353489  1244.65472515  1291.13474255   490.98642519   759.51318325
   998.67581891  1215.8266736   1239.01152959  1127.57425908   389.85169961
   276.4257152   1406.04346751  1392.15947163   735.47483304  1715.40687865
  1905.64650407  1716.13859869   680.87670442  2074.55058419   832.779044
  1536.50547652   538.52075554  1072.82138579   555.03023084   732.29769258
  2193.80969263   852.23211156  1251.16828901  1257.21421826  2088.95366399
   600.32073604  2139.00115394  1590.47022388   815.53976451  1412.21510862
  1585.09387139   895.76231258   297.9143015   1631.49360671  1289.76107191
  1527.51638843  1363.44376387  1415.27074406  1490.69294261   911.96696675
  1336.74545547   577.99241538   878.42181688  1610.495382    1590.7928312
   405.04100369  2156.93854273   975.06340063  1690.79382187  1034.80329646
   863.2521647    424.94713001  1548.75298491   500.44624414   389.09233364
   985.84697358   954.08345015   590.08527802  1050.83694459  1655.34393988
  1071.51805856   916.52316189   423.92016324  2014.76770324  1031.03810716
  1934.25961297  2273.59493266  1292.76688911   883.83305304  1039.81386505
  1894.02655694  2103.78293173   530.41539861   676.20212276  1261.42180837
  2062.1515158   1824.31658299  1278.39709024  1061.85279383  2331.3890377
  3608.71790219  3260.81915292  1534.67376537  2016.8586857   2331.3890377
  1191.32739172  2395.4475418   2787.51504671  6240.32505204  2648.8136285
  5149.83587241  2242.34382452  3336.67438878  2459.88027711  3322.85348042
  1652.68668184  3871.15153564  2331.3890377   2602.92933865  1979.77654374
   864.91562504  3123.91134708  3412.91837742   822.73843127  2961.37750126
  4644.99728324  3329.76232652  2331.3890377   2459.88027711  3864.00457527
  2754.349431    2721.27187942  3260.81915292  2066.52552869  2459.88027711
  2655.38289717  3573.57428625  2141.50109062  3856.86058805  2774.23826338
  2557.222052    2479.28245423  2299.50142849  2880.84321872  2286.77299223
  5371.88444815  5971.29237027  2974.8477419   6304.00155806  5572.71136566
  5035.8078897   4166.7000982   3247.06941159  4261.42049425  1770.0031024
  2119.57466614  5013.07076092  4914.809501    5549.45597277  5172.70949024
  5487.5470368   4712.13003371   854.34095186  1303.95255535   896.7604833
  1355.23596172  1053.19609945  3076.30591194  4548.41068766  3750.05962352
  2331.3890377   3750.05962352  2767.60513642  2395.4475418   1863.31163752
  4771.98393943  3055.95368338  5379.57826577  2576.78912786  5226.16875884
  6160.92875548  5087.43009191  5924.0874171   2466.34397395  3538.5081912
  5149.83587241  4232.22482573  2236.01214135  3171.67945927  4854.55775059
  4144.90972408  3700.45124103  1239.52825985  3001.82898202  1718.32714526
  1808.66096998  6168.85836935  3559.53852324  1773.62000445  6343.87088175
  4254.11734966  4400.71168718  2589.85199279  3608.71790219  2927.76157446
  1948.98554394  3267.69890065  3496.531772    5901.30124478  1447.31326744
  1483.30151855  3110.2931414   5441.21650642  2760.97552435  5005.49681985
  4592.93259167  5050.97870625  4563.24051748  3134.81547435  1880.38225802
  6560.10493724  4481.81081386  2331.3890377   2961.37750126  3412.91837742
  5728.2933894   6073.85079769  2529.23307029  1960.67421894  4922.35262356
  4029.13081006  3341.51474417  3123.91134708  4997.92543369  6081.75574326
  1756.15229746  5767.33604386  1321.00587012  3771.36575166  4115.89592741
  1875.50164945  4334.60623416  3336.67438878  3656.6369073   5058.56792657
  2542.24280853  2453.42028126  4094.16570655  5456.65034605  2816.10754553
  3405.97115931  2305.8713567   1706.35423288  3350.50814828  2914.33915091
  2147.77459682   998.78585487  1772.41420285  2369.77899929  1912.17014369
  2369.77899929  1587.56486742  1784.47975296   754.89665378  1724.31995267
  2583.31874821  1185.74514275  1845.05751659  3552.52530101  3686.30476502
  3785.58490619  2325.00393069  2767.60513642  3049.17631351  3199.04809717
  2204.41153312  2217.0401976   3461.63764487  3601.68299656  2280.41449268
  3055.95368338  2147.77459682  1069.61382883  3700.45124103  2035.4538553
  1546.39677025  1772.41420285  2854.10856303  2274.0598112   2135.23149785
  2741.10781581  4086.92805331  4086.92805331  2754.349431    3489.54666064
  2274.0598112   1820.77656066  1042.27514182  1796.56202139  1366.68241832
  3219.60904185  1383.88610778   870.21052876  3343.58966383  4036.34532898
  2147.77459682  2242.34382452  1453.10625303  3205.8984687   1338.10038663
  1617.10097744  3336.67438878  2544.19552911  1778.44488573  3686.30476502
  2934.47792515  1942.83948455  1587.56486742  2974.8477419   1058.66384249
  1441.52474461  2840.76201408  4779.47754409  3559.53852324  1406.88761532
  2635.68584906  6081.75574326  6940.21186703  5043.39202649  2492.23568943
  6640.58927666  7622.45586029  5333.45205809  9579.79444509  6280.10632866
  6240.32505204  4474.42445251  9754.26392016  2524.68311658  6640.58927666
  7456.8141454   8377.19447561  8039.90010207  5218.52424187  6560.10493724
  2927.76157446  2622.57243971  6081.75574326  8974.31828961  9017.29461729
  1058.66384249  4437.53371918  7622.45586029  9034.49704134  8674.68550799
  3964.33121004  3750.05962352  9319.3088321   5226.16875884  5963.41916586
  6105.48408632  7489.88035052  3137.54283244  3971.51947967  2860.78703785
  6721.28573001  8056.6952708   7226.23183306  6272.14565769  7374.2856457
  9405.97336136  5302.7503188   8470.45749096  9964.47053852  8023.11233302
  6616.42162155  9103.37445199  5081.3508072   8250.36791281  3629.84111534]
Out[23]:
-274.10488550524985

In [2]:
def markovchain(steps, step_width, pasoinicial):
    chain=[pasoinicial]
    likechain=[loglike(chain[0])]
    accepted = 0
    for i in range(steps):
        rand = np.random.normal(0.,1.,len(pasoinicial))
        newpoint = chain[i] + step_width*rand
        liketry = loglike(newpoint)
        if np.isnan(liketry) :
            print 'Paso algo raro'
            liketry = -1E50
            accept_prob = 0
        elif liketry > likechain[i]:
            accept_prob = 1
        else:
            accept_prob = np.exp(liketry - likechain[i])

        if accept_prob >= np.random.uniform(0.,1.):
            chain.append(newpoint)
            likechain.append(liketry)
            accepted += 1
        else:
            chain.append(chain[i])
            likechain.append(likechain[i])
    chain = np.array(chain)
    likechain = np.array(likechain)

    print "Razon de aceptacion =",float(accepted)/float(steps)

    return chain, likechain

In [3]:
chain1, likechain1 = markovchain(100,[0.1,0.1],[0.7,0.3])


Razon de aceptacion = 0.44

In [4]:
import matplotlib.pyplot as plt
%matplotlib inline

In [5]:
plt.plot(chain1[:,0],'o')
plt.plot(chain1[:,1],'o')


Out[5]:
[<matplotlib.lines.Line2D at 0x7f3d8f14c290>]

In [6]:
columna1=chain1[:,0]
np.sqrt(np.mean(columna1**2)-np.mean(columna1)**2)


Out[6]:
0.094182964516509399

Now let's analize the chains

First just an histogram


In [7]:
import seaborn as sns

In [8]:
omegade=chain1[:,0]
omegadm=chain1[:,1]

In [9]:
sns.distplot(omegade)


Out[9]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f3d8791ca10>

In [10]:
sns.distplot(omegadm)


Out[10]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f737157f9d0>

In [10]:
sns.jointplot(x=omegadm,y=omegade)


Out[10]:
<seaborn.axisgrid.JointGrid at 0x7f3d8769c2d0>

In [11]:
import corner

In [12]:
corner.corner(chain1)


Out[12]:

How to obtain the confidence regions of a variable?


In [13]:
mean_de=np.mean(omegade)
print(mean_de)


0.723519010223

You can use the "central credible interval"


In [14]:
dimde=len(omegade)
points_outside = np.int((1-0.68)*dimde/2)
de_sorted=np.sort(omegade)
print de_sorted[points_outside], de_sorted[dimde-points_outside]


0.629694556722 0.840268026527

In [28]:
dimde=len(omegade)
points_outside = np.int((1-0.95)*dimde/2)
de_sorted=np.sort(omegade)
print de_sorted[points_outside], de_sorted[dimde-points_outside]


0.441054715189 0.981563215272