Example #1
0
        public static double Correlation(int i, int j, int k, Segment segment)
        {
            Regression R_ij = new Regression(i, j, segment);
            Regression R_ik = new Regression(i, k, segment);
            Regression R_jk = new Regression(j, k, segment);

            double formular1 = 0, formular2 = 0;
            double Cijj = R_ij.MSE(segment);
            double Cikk = R_ik.MSE(segment);

            double temp = 0; //EU_jk[(F_ji - F_ki)^2]
            int count_temp = 0;
            for (int u = 0; u < R_jk.Uij.Length; u++)
            {
                int u_index = R_jk.Uij[u];
                if (segment.data[u_index, i] != 0)
                {
                    temp += Math.Pow(R_ij.ExpertScore(segment.data[u_index, i], segment) - R_ik.ExpertScore(segment.data[u_index, i], segment), 2);
                    count_temp++;
                }
            }
            //temp = temp / R_jk.Uij.Length;
            temp = temp / count_temp;

            formular1 = Cijj + Cikk - temp;
            formular2 = 2 * Math.Sqrt(Cijj * Cikk);

            return formular1 / formular2;
        }
Example #2
0
        public static double Prediction(int active_user_id, int item_id, Segment segment)
        { 
            //index of item_id in segment data
            int item_id_index = -1;
            for (int i = 0; i < segment.item_id.Length; i++)
            {
                if (Convert.ToInt32(segment.item_id[i].Trim()) == item_id)
                {
                    item_id_index = i;
                    break;
                }
            }

            for(int a = 0; a < segment.data.rows; a++)
            {
                if (segment.user_id[a] == active_user_id)
                {
                    List<double> L_MSE_ij = new List<double>(); // i,i: item_id_index
                    List<int> Ia = new List<int>();// Item id index của active user a
                    double MSE_Min = -9999;
                    for (int j = 0; j < segment.data.cols - 1; j++) //Bỏ unknown item
                    {
                        if (segment.data[a, j] != 0)
                        {
                            Ia.Add(j);
                            Regression r = new Regression(item_id_index, j, segment);
                            double MSE_ij = r.MSE(segment);
                            L_MSE_ij.Add(MSE_ij);

                            if (MSE_Min == -9999 || MSE_ij < MSE_Min)
                                MSE_Min = MSE_ij;
                        }
                    }

                    List<double> L_MSE_Diag = new List<double>(); // {C*_i}_jj
                    double Inv_Sum_MSE_Diag = 0;
                    foreach (double MSE_ij in L_MSE_ij)
                    {
                        double MSE_Diag = MSE_ij - segment.Correlation_Avg * MSE_Min;
                        L_MSE_Diag.Add(MSE_Diag);

                        Inv_Sum_MSE_Diag += 1 / MSE_Diag;
                    }

                    int[] _Ia = Ia.ToArray();
                    double[] _L_MSE_Diag = L_MSE_Diag.ToArray();

                    double predict_ai = 0;
                    for(int j = 0; j < _Ia.Length; j++)
                    {
                        double wji = 1 / (_L_MSE_Diag[j] * Inv_Sum_MSE_Diag);
                        Regression r = new Regression(item_id_index, _Ia[j], segment);
                        predict_ai += wji * r.ExpertScore(segment.data[a, _Ia[j]], segment);
                    }

                    return predict_ai;
                }

            }

            return 0;
        }