# *******************************************************
#   Mosel Example Problems
#   ======================
#
#   file folioqp_py.py
#   ``````````````````
#   Modeling a small QP problem
#    to perform portfolio optimization.
#    -- 1. QP: minimize variance
#       2. MIQP: limited number of assets --
#    -- Using Python to calculate covariance matrix --
#
#   (c) 2018 Fair Isaac Corporation
#       author: J. Müller, April 2018
# *******************************************************
import numpy as np


def covariance(dictionary, indices1, indices2):
    array1 = dict_to_array_2d(dictionary, indices1, indices2)

    covariance_array = np.cov(array1)

    covariance_dict = array_to_dict_2d(covariance_array, indices1, indices1)
    print_dict_as_table(covariance_dict, indices1, indices1)
    return covariance_dict


def covariance_of_mean(dict1, dict2, indices1, indices2):
    array1 = dict_to_array_2d(dict1, indices1, indices2)
    array2 = dict_to_array_2d(dict2, indices1, indices2)

    covariance_array = np.cov((array1 + array2) / 2)

    covariance_dict = array_to_dict_2d(covariance_array, indices1, indices1)
    print_dict_as_table(covariance_dict, indices1, indices1)
    return covariance_dict


if __name__ == "__main__":
    print('Functions imported from: folioqp_py.py')
