/// <summary> /// Gets the average row of a parameter matrix excluding the feature contribution. /// </summary> /// <param name="entityParameterVectors"> /// The matrix of parameters. /// Each row of the matrix is expected to be the parameter vector of a given user or item. /// </param> /// <param name="weights">The feature weights.</param> /// <param name="features">The feature values.</param> /// <returns>The average user/item parameter vector excluding the feature contribution.</returns> private static GaussianArray GetAverageParameterVectorExcludingFeatureContribution( GaussianMatrix entityParameterVectors, GaussianMatrix weights, SparseFeatureMatrix features) { Debug.Assert(entityParameterVectors.Count > 0, "The number of entities must be positive."); return(new GaussianArray( entityParameterVectors[0].Count, i => GetAverageParameterExcludingFeatureContribution(entityParameterVectors.Select(x => x[i]), weights[i], features))); }
/// <summary> /// Computes the average parameter of an array of user/item parameters /// excluding the feature contribution for the users or items. /// </summary> /// <param name="entityParameters">The array of user/item parameters to get the average of.</param> /// <param name="weights">The feature weights of each user/item.</param> /// <param name="features">The feature values of each user/item.</param> /// <returns>The average parameter distribution excluding feature contributions.</returns> private static Gaussian GetAverageParameterExcludingFeatureContribution( IEnumerable <Gaussian> entityParameters, IList <Gaussian> weights, SparseFeatureMatrix features) { var adjustedEntityParameters = entityParameters.Select((gaussian, i) => DoublePlusOp.AAverageConditional(gaussian, ComputeFeatureContribution(weights, features.NonZeroFeatureValues[i], features.NonZeroFeatureIndices[i]))); return(GetAverageParameter(adjustedEntityParameters)); }
/// <summary> /// Gets the parameter distributions of the average item. /// </summary> /// <param name="parameterDistributions">The learned posterior parameter distributions.</param> /// <param name="itemFeatures">The item features.</param> /// <returns>The average item parameter distributions.</returns> public static ItemParameterDistribution GetAverageItemParameters( ParameterDistributions parameterDistributions, SparseFeatureMatrix itemFeatures) { GaussianArray traitDistribution; Gaussian biasDistribution; GetAverageTraitsAndBiases( parameterDistributions.ItemTraitDistribution, parameterDistributions.ItemBiasDistribution, parameterDistributions.ItemFeature.TraitWeights, parameterDistributions.ItemFeature.BiasWeights, itemFeatures, out traitDistribution, out biasDistribution); return(new ItemParameterDistribution(traitDistribution, biasDistribution)); }
/// <summary> /// Gets the parameter distributions of the average user. /// </summary> /// <param name="parameterDistributions">The learned posterior parameter distributions.</param> /// <param name="userFeatures">The user features.</param> /// <returns>The average user parameter distributions.</returns> public static UserParameterDistribution GetAverageUserParameters( ParameterDistributions parameterDistributions, SparseFeatureMatrix userFeatures) { GaussianArray traitDistribution; Gaussian biasDistribution; GaussianArray thresholdDistribution = GetAverageParameterVector(parameterDistributions.UserThresholdDistribution); GetAverageTraitsAndBiases( parameterDistributions.UserTraitDistribution, parameterDistributions.UserBiasDistribution, parameterDistributions.UserFeature.TraitWeights, parameterDistributions.UserFeature.BiasWeights, userFeatures, out traitDistribution, out biasDistribution); return(new UserParameterDistribution(traitDistribution, biasDistribution, thresholdDistribution)); }
/// <summary> /// Gets the average traits and biases amongst all users or items. /// </summary> /// <param name="traits">The user/item traits.</param> /// <param name="biases">The user/item biases.</param> /// <param name="traitFeatureWeights">The feature weights for the traits.</param> /// <param name="biasFeatureWeights">The feature weights for the biases.</param> /// <param name="featureValues">The feature values of all users/items.</param> /// <param name="averageTraits">The resulting average traits.</param> /// <param name="averageBias">The resulting average bias.</param> /// <remarks> /// When features are used, this method will exclude the feature contribution when computing the average. /// Therefore, the feature contribution for a given user/item has to be then added at prediction time. /// </remarks> private static void GetAverageTraitsAndBiases( GaussianMatrix traits, GaussianArray biases, GaussianMatrix traitFeatureWeights, GaussianArray biasFeatureWeights, SparseFeatureMatrix featureValues, out GaussianArray averageTraits, out Gaussian averageBias) { if (featureValues.FeatureCount == 0) { averageTraits = GetAverageParameterVector(traits); averageBias = GetAverageParameter(biases); } else { averageTraits = GetAverageParameterVectorExcludingFeatureContribution(traits, traitFeatureWeights, featureValues); averageBias = GetAverageParameterExcludingFeatureContribution(biases, biasFeatureWeights, featureValues); } }