//Select a sample from a range of images and organise it into and array public RangeType[][] Sample(IList <IImageRaster <IRaster4DInteger, RangeType> > images, IImageRaster <IRaster4DInteger, bool> mask) { List <int> selected_indexes = mask.GetElementIndexesWithValue(true); if (this.random) { ToolsMathCollection.ShuffleIP(selected_indexes); } int sample_size = count; if (!this.fixed_count) { sample_size = (int)(selected_indexes.Count * fraction); } RangeType[][] sample = new RangeType[this.count][]; for (int sample_index = 0; sample_index < this.count; sample_index++) { sample[sample_index] = new RangeType[images.Count]; for (int feature_index = 0; feature_index < images.Count; feature_index++) { int sample_value_index = selected_indexes[(int)((selected_indexes.Count / ((double)sample_size)) * sample_index)]; sample_value_index = ToolsMath.Clamp(sample_value_index, 0, selected_indexes.Count - 1); sample[sample_index][feature_index] = images[feature_index].GetElementValue(sample_value_index); } } return(sample); }
public Bitmap Render(IImageSpace3D <float, float> source_image) { Bitmap destination_image = new Bitmap(resolution_x, resolution_y); float[] coordinates_y = ToolsCollection.Copy(render_origen); for (int index_y = 0; index_y < resolution_x; index_y++) { ToolsMathCollection.AddRBA <float>(algebra, coordinates_y, render_stride_y, coordinates_y); float[] coordinates_x = ToolsCollection.Copy(coordinates_y); for (int index_x = 0; index_x < resolution_y; index_x++) { float[] coordinates_z = ToolsCollection.Copy(coordinates_x); float max_value = source_image.GetLocationValue(coordinates_z); ToolsMathCollection.AddRBA(algebra, coordinates_z, render_stride_z, coordinates_z); for (int index_z = 1; index_z < resolution_z; index_z++) { float value = source_image.GetLocationValue(coordinates_z); if (this.comparer.Compare(max_value, value) == -1) { max_value = value; } ToolsMathCollection.AddRBA(algebra, coordinates_z, render_stride_z, coordinates_z); } destination_image.SetPixel(index_x, index_y, this.converter.Compute(max_value)); ToolsMathCollection.AddRBA(algebra, coordinates_x, render_stride_x, coordinates_x); } } return(destination_image); }
public static Tuple <double, double> TestStatic(IList <IList <double> > samples) { int sample_size = samples[0].Count; foreach (IList <double> sample in samples) { if (sample.Count != sample_size) { throw new Exception("samples must be of equal size"); } } // Larsen Marx 4Th editiopn P779 double sample_count = samples.Count; double total_count = ToolsCollection.CountElements(samples); double total_mean = ToolsMathStatistics.MeanAll(samples); double sum_squared_all = ToolsMathStatistics.SumSquaredAll(samples); IList <double> sample_sums = ToolsMathCollection.Sums0(samples); IList <double> measurement_sums = ToolsMathCollection.Sums1(samples); // compute C double c = ToolsMath.Sqr(total_mean * total_count) / (sample_size * sample_count); double sstot = sum_squared_all - c; double ssb = 0; for (int measurement_index = 0; measurement_index < sample_size; measurement_index++) { ssb += ToolsMath.Sqr(measurement_sums[measurement_index]) / sample_count; } ssb -= c; double sstr = 0.0; for (int sample_index = 0; sample_index < samples.Count; sample_index++) { sstr += ToolsMath.Sqr(sample_sums[sample_index]) / sample_size; } sstr -= c; double sse = sstot - ssb - sstr; double degrees_of_freedom_0_samples = (sample_count - 1.0); double degrees_of_freedom_0_measurements = (sample_size - 1.0); double degrees_of_freedom_1 = degrees_of_freedom_0_samples * degrees_of_freedom_0_measurements; //F-Transform samples double f_statistic_samples = (sstr / degrees_of_freedom_0_samples) / (sse / degrees_of_freedom_1); //F-Transform measurements double f_statistic_measurements = (ssb / degrees_of_freedom_0_measurements) / (sse / degrees_of_freedom_1); return(new Tuple <double, double>( FisherSnedecor.CDF(degrees_of_freedom_0_samples, degrees_of_freedom_1, f_statistic_samples), FisherSnedecor.CDF(degrees_of_freedom_0_measurements, degrees_of_freedom_1, f_statistic_measurements))); }
public double GetAUC(LabelType label_value) { int label_index = this.Model.DataContext.GetLabelDescriptor(0).GetValueIndex(label_value); double[] label_scores = new double[label_values.Length]; double[] second_scores = new double[label_values.Length]; bool[] labels = new bool[label_values.Length]; for (int index = 0; index < label_values.Length; index++) { label_scores[index] = likelihoods[index][label_index]; labels[index] = (this.Model.DataContext.GetLabelDescriptor(0).GetValueIndex(label_values[index]) == label_index); List <int> ordering_indexes = ToolsMathCollection.Ordering(likelihoods[index]); if (ordering_indexes[0] == label_index) { second_scores[index] = likelihoods[index][label_index]; } else { second_scores[index] = likelihoods[index][ordering_indexes[0]]; } } double[] scores = ToolsMathCollection.DivideList(label_scores, second_scores); return(ToolsMathStatistics.ComputeROCAUCTrapeziod(labels, scores)); }
public Bitmap Render(IImageSpace3D <float, float> source_image) { float [,] image = new float [this.resolution[0], this.resolution[1]]; Parallel.For(0, this.resolution[1], index_y => { float[] coordinates_y = ToolsMathCollection.AddMultiple <float>(algebra, coordinates_origen, render_stride_y, index_y); for (int index_x = 0; index_x < this.resolution[0]; index_x++) { float[] coordinates_x = ToolsMathCollection.AddMultiple <float>(algebra, coordinates_y, render_stride_x, index_x); image[index_x, index_y] = Projection(coordinates_x, this.render_stride_z, this.resolution[2], source_image); } }); //for (int index_y = 0; index_y < this.resolution[1]; index_y++) //{ // float[] coordinates_y = ToolsMathArray.AddMultiple<float>(algebra, coordinates_origen, render_stride_y, index_y); // for (int index_x = 0; index_x < this.resolution[0]; index_x++) // { // float[] coordinates_x = ToolsMathArray.AddMultiple<float>(algebra, coordinates_y, render_stride_x, index_x); // float value = Projection(coordinates_x, this.render_stride_z, this.resolution[2], source_image); // destination_image.SetPixel(index_x, index_y, this.converter.Compute(value)); // } //} return(ConvertToBitmap(image)); }
public static double TestStatic(IList <IList <double> > samples) { int sample_size = samples[0].Count; foreach (IList <double> sample in samples) { if (sample.Count != sample_size) { throw new Exception("samples must be of equal size"); } } double[,] measurement_ranks = ToolsMathStatistics.Ranks1(samples); double[] rank_sums = ToolsMathCollection.Sums0(measurement_ranks); double chi_square_statistic_part = 0.0; for (int treatment_index = 0; treatment_index < rank_sums.Length; treatment_index++) { chi_square_statistic_part += ToolsMath.Sqr(rank_sums[treatment_index]); } double treatment_count = samples.Count; double block_count = sample_size; double chi_square_statistic = ((12.0 / (block_count * treatment_count * (treatment_count + 1.0))) * chi_square_statistic_part) - (3 * block_count * (treatment_count + 1.0)); return(ChiSquared.CDF(rank_sums.Length, chi_square_statistic)); }
public DomainType[] Minimize(DomainType[] initial_state) { DomainType[,] population = new DomainType[PopulationCount, initial_state.Length]; float[] fitness_values = new float[PopulationCount]; for (int iteration_index = 0; iteration_index < IterationCount; iteration_index++) { d_mutator.Repopulate(null, null, null, null); } return(population.Select1DIndex0(ToolsMathCollection.MaxIndex(fitness_values))); }
public Tuple <IDataSet <FeatureType, LabelType>, IDataSet <FeatureType, LabelType> > Split(double fraction) { List <int> instances = new List <int>(ToolsMathSeries.RangeInt32(InstanceCount)); ToolsMathCollection.ShuffleIP(instances); int set_0_size = (int)(instances.Count * fraction); List <int> selected_instance_indexes_0 = ToolsCollection.Crop(instances, 0, set_0_size); List <int> selected_instance_indexes_1 = ToolsCollection.Crop(instances, set_0_size, instances.Count); IDataSet <FeatureType, LabelType> data_set_0 = SelectInstances(selected_instance_indexes_0); IDataSet <FeatureType, LabelType> data_set_1 = SelectInstances(selected_instance_indexes_1); return(new Tuple <IDataSet <FeatureType, LabelType>, IDataSet <FeatureType, LabelType> >(data_set_0, data_set_1)); }
public double Evaluate(ITemplateModelDiscrete <DomainType, LabelType> template, IDataSet <DomainType, LabelType> data_set, ISet <int> feature_set) { IDataSet <DomainType, LabelType> selected_data_set = data_set.SelectFeatures(new List <int>(feature_set)); double[] scores = new double[this.FoldCount]; Parallel.For(0, this.FoldCount, fold_index => { Tuple <IDataSet <DomainType, LabelType>, IDataSet <DomainType, LabelType> > split = selected_data_set.Split(this.TrainingSetFraction); ReportDiscrete <DomainType, LabelType> report = template.GenerateAndTestDiscrete(split.Item1, split.Item2); scores[fold_index] = report.CorrectLabelRate; }); return(ToolsMathCollection.Sum(scores) / ((double)this.FoldCount)); }
protected override float Projection(float[] origen, float[] stride_size, int stride_count, IImageSpace3D <float, float> source_image) { float[] coordinates = ToolsCollection.Copy(origen); for (int index_z = 0; index_z < stride_count; index_z++) { float value = source_image.GetLocationValue(coordinates); if (this.comparer.Compare(value, min_value) == 1) { return(value); } ToolsMathCollection.AddRBA(algebra, coordinates, stride_size, coordinates); } return(min_value); }
public static double TestStatic(IList <double> sample_0, IList <double> sample_1) { if (sample_0.Count != sample_1.Count) { throw new Exception("Samples not of equal size"); } double[] difference = ToolsMathCollection.SubtractElements(sample_0, sample_1); double mean_difference = ToolsMathStatistics.Mean(difference); double variance = ToolsMathStatistics.Variance(difference); double t_statistic = mean_difference / (Math.Sqrt(variance) / Math.Sqrt(sample_0.Count)); double degrees_of_freedom = (sample_0.Count - 1); StudentT distribution = new StudentT(0.1, 1.0, degrees_of_freedom); return(distribution.CumulativeDistribution(t_statistic)); }
private void InitMembers(double [] factors) { Debug.Assert(factors.Length == 4); argb_integer_weigths = new int[4]; argb_integer_temp = new int[4]; //reduced so it will not go out of uint32 range double sum = ToolsMathCollection.Sum(factors); double multiplier = (int.MaxValue) / (sum * 255); for (int index = 0; index < factors.Length; index++) { argb_integer_weigths[index] = (ushort)Math.Floor(factors[index] * multiplier); } argb_integer_divisor = ToolsMathCollectionInteger.Sum(argb_integer_weigths); }
public static double TestStatic(IList <IList <double> > samples) { double total_size = 0.0f; double squared_rank_mean_sum = 0.0f; double[][] rank_samples = ToolsMathStatistics.ConvertToAccendingRanks(samples); foreach (double[] sample in rank_samples) { total_size += sample.Length; double sum = ToolsMathCollection.Sum(sample); squared_rank_mean_sum += (sum * sum) / ((double)sample.Length); } double statistic = (12.0 * squared_rank_mean_sum) / (total_size * (total_size + 1)) - 3 * (total_size + 1); return(1 - ChiSquared.CDF(samples.Count - 1, statistic)); }
public static void PlotHistogram(string file_path, double[] values, int bincount, double lower_quantile, double upper_quantile) { Array.Sort(values); double lowerbound = ToolsMathStatistics.QuantileSorted(values, lower_quantile); double upperbound = ToolsMathStatistics.QuantileSorted(values, upper_quantile); double[] selected_values = ToolsMathCollection.Select(values, lowerbound, upperbound); double stride = (upperbound - lowerbound) / bincount; double[] bin_limits = new double[bincount - 1]; bin_limits[0] = lowerbound + stride; for (int bin_limit_index = 1; bin_limit_index < bin_limits.Length; bin_limit_index++) { bin_limits[bin_limit_index] = bin_limits[bin_limit_index - 1] + stride; } PlotModel model = new HistrogramPlot(selected_values, bin_limits).PlotModel; WriteToFile(file_path, model, 800, 800); }
public static double TestStatic(IList <IList <double> > samples, IList <double> limits) { // Create to blocks according to limits double[,] table = new double[samples.Count, limits.Count + 1]; for (int index_0 = 0; index_0 < samples.Count; index_0++) { for (int index_1 = 0; index_1 < samples[index_0].Count; index_1++) { double value = samples[index_0][index_1]; //for any of the other bins int limit_index = 0; while ((limit_index < limits.Count) && (limits[limit_index] <= value)) { limit_index++; } table[index_0, limit_index]++; } } // Compute test for independance double[] sums_0 = ToolsMathCollection.Sums0(table); double[] sums_1 = ToolsMathCollection.Sums1(table); double total = ToolsMathCollection.Sum(sums_0); double chi_square_statistic = 0; for (int index_0 = 0; index_0 < sums_0.Length; index_0++) { for (int index_1 = 0; index_1 < sums_1.Length; index_1++) { double expectation = (sums_0[index_0] * sums_1[index_1]) / total; chi_square_statistic += (ToolsMath.Sqr(table[index_0, index_1] - expectation) / expectation); } } double degrees_of_freedom = (sums_0.Length - 1) * (sums_1.Length - 1); return(ChiSquared.CDF(degrees_of_freedom, chi_square_statistic)); }
public RangeType Compute(DomainType domain_value_0) { int index = -1; if (ResultIncludesThreshold) { index = ToolsMathCollection.FirstEqualOrLargerIndex(this.thresholds, domain_value_0); } else { index = ToolsMathCollection.FirstLargerIndex(this.thresholds, domain_value_0); } if (index == -1) { return(values[values.Length - 1]); } else { return(values[index]); } }
// based on first bigger than second means positive public static double ComputeRankSumStatistic(IList <double> sample_0, IList <double> sample_1) { // compute ranks DictionaryCount <double> counts = new DictionaryCount <double>(); foreach (double item in sample_0) { counts.Increment(item); } foreach (double item in sample_1) { counts.Increment(item); } Dictionary <double, double> ranks = new Dictionary <double, double>(); List <double> keys = new List <double>(counts.Keys); keys.Sort(); double rank = 1; foreach (double key in keys) { int count = counts[key]; ranks[key] = (rank + rank + count - 1) / 2.0; rank += count; } List <double> rank_list = new List <double>(); foreach (double item in sample_0) { rank_list.Add(ranks[item]); } return(ToolsMathCollection.Sum(rank_list)); }
// based on first bigger than second means positive public static double ComputeSingedRankPairedStatistic(IList <double> sample_0, IList <double> sample_1) { double[] absolute_difference = ToolsMathCollection.AbsoluteDifference(sample_0, sample_1); // compute ranks DictionaryCount <double> counts = new DictionaryCount <double>(); foreach (double item in absolute_difference) { counts.Increment(item); } Dictionary <double, double> ranks = new Dictionary <double, double>(); List <double> keys = new List <double>(counts.Keys); keys.Sort(); double rank = 1; foreach (double key in keys) { int count = counts[key]; ranks[key] = (rank + rank + count - 1) / 2.0; rank += count; } double statistic = 0; for (int index = 0; index < sample_0.Count; index++) { if (sample_1[index] < sample_0[index]) { statistic += ranks[absolute_difference[index]]; } } return(statistic); }
public override int GetHashCode() { return(ToolsMathCollection.Sum(new AlgebraIntegerInt32(), size)); }