Exemplo n.º 1
0
def test_mag_func_omegaK_is_pos():
    """ Apparent magnitude calculator test function.

    This is a test function that tests the apparent
    magnitude calculator function when Omega_K > 0.0.
    We use the following link to check if our answer
    is correct:
    http://www.astro.ucla.edu/~wright/CosmoCalc.html

    The apparent magnitude function outputs the apparent
    magnitude but in the website they calculate the 
    luminosity distance. Therefore, we calculate the 
    luminosity distance from the apparent magnitude output
    of our function to compare it with the one on the
    website.
    """

    # We choose the parameters such the Omega_M + Omega_lambda < 1.

    test_param = [0.1, 0.714, 69.6, -19.23]

    # Our apparent magnitude function takes the cosmological parameters
    # and the redshift data array as input. For the test, we input an
    # array containing one redshift data point.

    dist_modulus = mag_model(test_param, [3])[0]

    # Calculating the luminosity distance in units of 10^4 Mpc.

    lum_distance = round(
        10**((dist_modulus - test_param[3]) / 5) * 10 / 10**10, 2)

    # The expected output for the same parameter set in units of 10^4 Mpc.

    expected_output = 3.30

    assert (lum_distance == expected_output
            ), "apparent magnitude function does not work when OmegaK > 0.0"

    print("output of the magnitude function is correct")
Exemplo n.º 2
0
def log_likelihood_test_fake_data():
    """ Testing the likelihood function on fake Supernovae data.

    This function test the likelihood function on fake Supernovae
    data that is created for a given set of the cosmological 
    parameters in the same format as the real SN data file. We
    calculate the likelihood over a range of Omega_M and Omega_lambda
    parameters and then verify that the maximum of the log likelihood
    function corresponds to the Omega_M and Omega_lambda values used
    to create the fake data.
    """
    # The cosmological parameters used to create the fake data.

    test_params = [0.3, 0.6, 75, -19.23]

    # We create a redshift array with 200 datapoints that takes
    # values starting at redshift of 0.002 and goes up to 2.

    fake_data = pd.DataFrame(np.linspace(0.002, 2.0, 200), columns=["zcmb"])

    # Adding the 'dmb' column to the fake data that mimics
    # the statistical error. We choose the error to be very low.

    fake_data["dmb"] = np.random.uniform(0, 0.01, len(fake_data.zcmb))

    mean = mag_model(test_params, fake_data.zcmb)

    cov = np.diag(pow(pd.Series.to_numpy(fake_data.dmb), 2))

    # Calculate the apparent magnitude values for each redshift in the
    # fake data using a normal distribution such that mean is calculated
    # using the apparent magnitude function and the variance is given by
    # the square of the fake statistical error given by the 'dmb' column.

    fake_data["mb"] = np.random.multivariate_normal(mean, cov)

    # Create an array of the Omega_M and Omega_lambda values over which
    # the log likelihood values are calculated.

    omega_arr = np.arange(0.1, 1.0, 0.01)

    likelihood_mat = np.zeros((len(omega_arr), len(omega_arr)))

    # Calculate the log likelihood values over all values of Omega_M and
    # Omega_lambda.

    for i, omega_m_item in enumerate(omega_arr):
        for j, omega_l_item in enumerate(omega_arr):
            likelihood_mat[i, j] = log_likelihood(
                [omega_m_item, omega_l_item, test_params[2], test_params[3]],
                fake_data)
            print("{:2.1%} done".format(i / len(omega_arr)), end="\r")

    # Find the value of Omega_M and Omega_lambda for which the log likelihood
    # is the highest.

    max_omega_m = omega_arr[int(np.argmax(likelihood_mat) / len(omega_arr))]
    max_omega_l = omega_arr[int(np.argmax(likelihood_mat) % len(omega_arr))]

    # The max values found above should be close to the Omega_M and Omega_lambda
    # values used to create a fake data.

    assert all(
        np.isclose([max_omega_m, max_omega_l],
                   [test_params[0], test_params[1]],
                   atol=0.01)), "Log likelihood function does not work"

    print("Log likelihood function works well on the fake data.")