private void CheckCofiGradient(double lambda)
        {
            Matrix X_s = Matrix.Rand(4, 3);
            Matrix T_s = Matrix.Rand(5, 3);

            Matrix Y_s = (X_s * T_s.T);
            Matrix R_s = Matrix.Zeros(Y_s.Rows, Y_s.Cols);

            Y_s[Matrix.Rand(Y_s.Rows, Y_s.Cols) > 0.5] = 0.0;
            R_s[Y_s == 0] = 1.0;

            Matrix X_ts = Matrix.NormRand(X_s.Rows, X_s.Cols);
            Matrix T_ts = Matrix.NormRand(T_s.Rows, T_s.Cols);

            ICostFunction costFunction = new CofiCostFunction()
            {
                R = R_s, X = X_ts, Y = Y_s.Unshape(), Lambda = 0, Regularizer = null, CollaborativeFeatures = 3
            };

            costFunction.Initialize();

            Vector grad = costFunction.ComputeGradient(Vector.Combine(X_ts.Unshape(), T_ts.Unshape()));

            Vector numericalGrad = this.ComputeNumericalGradient(
                f => costFunction.ComputeCost(f),
                Vector.Combine(X_ts.Unshape(), T_ts.Unshape()));

            Assert.True(this.CheckNumericalGradient(numericalGrad, grad) < 0.0000000001);
        }
        public void Test_Cofi_CostFunction()
        {
            Matrix rMat = Y.ToBinary(i => i > 0d);

            ICostFunction costFunction = new CofiCostFunction()
            {
                R = rMat, X = X, Y = Y.Unshape(), Lambda = 0, Regularizer = null, CollaborativeFeatures = X.Cols
            };

            costFunction.Initialize();
            double cost = costFunction.ComputeCost(Vector.Combine(X.Unshape(), Theta.Unshape()));
            Vector grad = costFunction.ComputeGradient(Vector.Combine(X.Unshape(), Theta.Unshape()));

            Almost.Equal(39.796d, System.Math.Round(cost, 3), 0.001);

            this.CheckCofiGradient(0);
        }
        /// <summary>
        /// Generates a new Collaborative Filtering model.
        /// </summary>
        /// <param name="X">Training matrix values.</param>
        /// <param name="y">Vector of entity identifiers.</param>
        /// <returns></returns>
        public override IModel Generate(Matrix X, Vector y)
        {
            this.Preprocess(X.Copy());

            // inputs are ratings from each user (X = entities x ratings), y = entity id.
            // create rating range in case we don't have one already
            if (this.Ratings == null)
            {
                this.Ratings = new Range(X.Min(), X.Max());
            }

            // indicator matrix of 1's where rating was provided otherwise 0's.
            Matrix R = X.ToBinary(f => this.Ratings.Test(f));

            // The mean needs to be values within rating range only.
            Vector mean = X.GetRows().Select(s =>
                                             s.Where(w => this.Ratings.Test(w)).Sum() /
                                             s.Where(w => this.Ratings.Test(w)).Count()
                                             ).ToVector();

            // update feature averages before preprocessing features.
            this.FeatureProperties.Average = mean;

            this.Preprocess(X);

            // where references could be user ratings and entities are movies / books, etc.
            int references = X.Cols, entities = X.Rows;

            // initialize Theta parameters
            Matrix ThetaX = Matrix.Rand(entities, this.CollaborativeFeatures, -1d);
            Matrix ThetaY = Matrix.Rand(references, this.CollaborativeFeatures, -1d);

            ICostFunction costFunction = new CofiCostFunction()
            {
                CollaborativeFeatures = this.CollaborativeFeatures,
                Lambda      = this.Lambda,
                R           = R,
                Regularizer = null,
                X           = ThetaX,
                Y           = X.Unshape()
            };

            // we're optimising two params so combine them
            Vector Theta = Vector.Combine(ThetaX.Unshape(), ThetaY.Unshape());

            Optimizer optimizer = new Optimizer(Theta, this.MaxIterations, this.LearningRate)
            {
                CostFunction = costFunction
            };

            optimizer.Run();

            // extract the optimised parameter Theta
            ThetaX = optimizer.Properties.Theta.Slice(0, (ThetaX.Rows * ThetaX.Cols) - 1).Reshape(entities, VectorType.Row);
            ThetaY = optimizer.Properties.Theta.Slice(ThetaX.Rows * ThetaX.Cols, Theta.Length - 1).Reshape(references, VectorType.Row);

            // create reference mappings, each value is the original index.
            this.ReferenceFeatureMap = (this.ReferenceFeatureMap == null ? Vector.Create(references, i => i) : this.ReferenceFeatureMap);
            this.EntityFeatureMap    = (this.EntityFeatureMap == null ? Vector.Create(entities, i => i) : this.EntityFeatureMap);

            return(new CofiRecommenderModel()
            {
                Descriptor = this.Descriptor,
                NormalizeFeatures = this.NormalizeFeatures,
                FeatureNormalizer = this.FeatureNormalizer,
                FeatureProperties = this.FeatureProperties,
                Ratings = this.Ratings,
                ReferenceFeatureMap = this.ReferenceFeatureMap,
                EntityFeatureMap = this.EntityFeatureMap,
                Mu = mean,
                Y = y,
                Reference = X,
                ThetaX = ThetaX,
                ThetaY = ThetaY
            });
        }