public static double TestStatic(IList <IList <double> > samples) { double sample_count = samples.Count; double total_count = ToolsCollection.CountElements(samples); double total_mean = ToolsMathStatistics.MeanAll(samples); IList <double> sample_means = ToolsMathStatistics.Means0(samples); double sstr = 0.0; for (int sample_index = 0; sample_index < samples.Count; sample_index++) { sstr += samples[sample_index].Count * ToolsMath.Sqr(sample_means[sample_index] - total_mean); } double sse = 0.0; for (int sample_index = 0; sample_index < samples.Count; sample_index++) { for (int measurement_index = 0; measurement_index < samples[sample_index].Count; measurement_index++) { sse += ToolsMath.Sqr(samples[sample_index][measurement_index] - sample_means[sample_index]); } } //FTransform double degrees_of_freedom_0 = (sample_count - 1.0); double degrees_of_freedom_1 = (total_count - sample_count); double summed_variance = sstr / degrees_of_freedom_0; double total_varaiance = sse / degrees_of_freedom_1; double f_statistic = summed_variance / total_varaiance; return(FisherSnedecor.CDF(degrees_of_freedom_0, degrees_of_freedom_1, f_statistic)); }
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 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)); }
//TODO implement 3 mean variants //http://www.itl.nist.gov/div898/handbook/eda/section3/eda35a.htm public static double TestStatic(IList <IList <double> > samples) { double total_count = ToolsCollection.CountElements(samples); double total_mean = ToolsMathStatistics.MeanAll(samples); //double[] sample_means = ToolsMathStatistics.Means0(samples); //double total_mean = ToolsMathStatistics.MedianAll(samples); double[] sample_means = ToolsMathStatistics.Medians0(samples); double summed_varriance = 0.0; for (int sample_index = 0; sample_index < samples.Count; sample_index++) { summed_varriance += samples[sample_index].Count * ToolsMath.Sqr(sample_means[sample_index] - total_mean); } double total_variance = 0.0; for (int sample_index = 0; sample_index < samples.Count; sample_index++) { for (int measurement_index = 0; measurement_index < samples[sample_index].Count; measurement_index++) { total_variance += ToolsMath.Sqr(samples[sample_index][measurement_index] - sample_means[sample_index]); } } double degrees_of_freedom_0 = samples.Count - 1; double degrees_of_freedom_1 = total_count - samples.Count; double f_statistic = (degrees_of_freedom_1 * summed_varriance) / (degrees_of_freedom_0 * total_variance); return(FisherSnedecor.CDF(degrees_of_freedom_0, degrees_of_freedom_1, f_statistic)); }
public override bool ComputeRBA(IMarketModelIndicator market_model, double[] result) { if (previous_1 == 0) { previous_1 = market_model.CurrentBid; previous_2 = market_model.CurrentBid; } double momentum = previous_1 - previous_2; double error_now = market_model.CurrentBid - (previous_1 + (momentum_weight * momentum)); error_now_array_sqr_sum = (error_now_array_sqr_sum + ToolsMath.Sqr(error_now) - ToolsMath.Sqr(this.error_now_array[index_error_now % this.error_now_array.Length])) / this.error_now_array.Length; error_now_array_sum = error_now_array_sum + error_now - this.error_now_array[index_error_now % this.error_now_array.Length]; this.error_now_array[index_error_now % this.error_now_array.Length] = error_now; index_error_now++; double error_now_sdev = Math.Sqrt(error_now_array_sqr_sum); if (error_now_sdev == 0) { error_now_sdev = double.Epsilon; } double error_now_z = error_now / error_now_sdev; double error_now_weight = max_error_weight * (1 - Math.Exp(-ToolsMath.Sqr(error_now_z) / expected_error_z)); result[0] = previous_1 + (momentum * momentum_weight) + (error_now * error_now_weight); result[1] = result[0] + (error_now_sdev * 2); result[2] = result[0] - (error_now_sdev * 2); result[3] = error_now; result[4] = error_now_sdev; result[5] = error_now_array_sum; result[6] = error_now_z; result[7] = error_now_weight; result[8] = momentum; previous_2 = previous_1; previous_1 = result[0]; return(this.error_now_array.Length < market_model.Second1.HistoryCount); }
public override double Test(IList <double> sample_0, IList <double> sample_1) { double mean_0 = ToolsMathStatistics.Mean(sample_0); double mean_1 = ToolsMathStatistics.Mean(sample_1); double variance_0 = ToolsMathStatistics.Variance(sample_0, mean_0); double variance_1 = ToolsMathStatistics.Variance(sample_0, mean_1); double t_statistic = (mean_0 - mean_1) / Math.Sqrt((variance_0 / sample_0.Count) + (variance_1 / sample_1.Count)); //Welch–Satterthwaite equation: double dof_nominator = ToolsMath.Sqr((variance_0 / sample_0.Count) + (variance_1 / sample_1.Count)); double dof_denominator = (Math.Pow(variance_0, 4) / (sample_0.Count * sample_0.Count * (sample_0.Count - 1))) + (Math.Pow(variance_1, 4) / (sample_1.Count * sample_1.Count * (sample_1.Count - 1))); double degrees_of_freedom = dof_nominator / dof_denominator; StudentT distribution = new StudentT(0.1, 1.0, degrees_of_freedom); return(distribution.CumulativeDistribution(-t_statistic)); }
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 static void DistanceTransform2DSlowRBA(ImageRaster2D <bool> mask_image, float[] voxel_size, ImageRaster2D <float> distance_image) { IRaster2DInteger raster = mask_image.Raster; int size_0 = mask_image.Raster.Size0; int size_1 = mask_image.Raster.Size1; // Apply the transform in the x-direction //for (int plane_index = 0; plane_index < size_z * size_y; plane_index++) //{ Parallel.For(0, size_0, index_0 => { for (int index_1 = 0; index_1 < size_1; index_1++) { float best_distance = float.MaxValue; if (mask_image.GetElementValue(index_0, index_1)) { best_distance = 0; } else { for (int index_0_1 = 0; index_0_1 < size_0; index_0_1++) { for (int index_1_1 = 0; index_1_1 < size_1; index_1_1++) { if (mask_image.GetElementValue(index_0_1, index_1_1)) { float distance = ToolsMath.Sqrt(ToolsMath.Sqr((index_0_1 - index_0) * voxel_size[0]) + ToolsMath.Sqr((index_1_1 - index_1) * voxel_size[0])); if (distance < best_distance) { best_distance = distance; } } } } } distance_image.SetElementValue(index_0, index_1, best_distance); } }); }
public override bool ComputeRBA(IMarketModelIndicator market_model, double[] result) { if (previous_1 == 0) { previous_1 = market_model.CurrentBid; previous_2 = market_model.CurrentBid; } double momentum = previous_1 - previous_2; double error_now = market_model.CurrentBid - (previous_1 + (momentum_weight * momentum)); indicator_error_now[index_indicator_error_now % indicator_error_now.Length] = error_now; index_indicator_error_now++; double error_now_s = ToolsMathStatistics.StandardDeviation(indicator_error_now); if (error_now_s == 0) { error_now_s = double.Epsilon; } double error_now_z = error_now / error_now_s; double error_now_weight = max_error_weight * (1 - Math.Exp(-ToolsMath.Sqr(error_now_z) / expected_error_z)); result[0] = previous_1 + (momentum * momentum_weight) + (error_now * error_now_weight); result[1] = result[0] + (error_now_s * 2); result[2] = result[0] - (error_now_s * 2); result[3] = error_now; result[4] = error_now_s; result[5] = error_now_z; result[6] = error_now_weight; result[7] = momentum; previous_2 = previous_1; previous_1 = result[0]; return(indicator_error_now.Length < market_model.Second1.HistoryCount); }
private static float Collide(float offset_0, float offset_1, float location_0, float location_1) { return((ToolsMath.Sqr(location_1) - ToolsMath.Sqr(location_0) + offset_1 - offset_0) / (2.0f * (location_1 - location_0))); }
private static int ComputeDistance(ImageRaster3D <float> distance_image_sqr, int index_0, int index_1, int index_2, int index_axis, float[] locations_axis, float[] start, float[] locations, float[] offsets, int curve_index) { float location = locations_axis[index_axis]; while ((curve_index != 0) && (location < start[curve_index])) { curve_index--; } distance_image_sqr.SetElementValue(index_0, index_1, index_2, offsets[curve_index] + ToolsMath.Sqr(location - locations[curve_index])); return(curve_index); }
//public static void DistanceTransformRBA(ImageRaster3D<bool> mask_image, ImageRaster3D<float> distance_image, float[] voxel_size) //{ // int size_x = mask_image.Raster.SizeX; // int size_y = mask_image.Raster.SizeY; // int size_z = mask_image.Raster.SizeZ; // /* Apply the transform in the x-direction. */ // Parallel.For(0, size_z * size_y, plane_index => // { // int z_index = plane_index / size_y; // int y_index = plane_index - z_index * size_y; // int element_index = size_x * ((size_y * z_index) + y_index); // /* Project distances forward. */ // float distance = float.MaxValue; // for (int x_index = 0; x_index < size_x; x_index++) // { // distance += voxel_size[0]; // /* The voxel value is true; the distance should be 0. */ // if (mask_image[element_index + x_index]) // { // distance = 0.0f; // } // distance_image[element_index + x_index] = distance; // } // /* Project distances backward. From this point on we don't have to // * read the source data anymore, since all object voxels now have a // * distance assigned. */ // distance = float.MaxValue; // for (int x_index = size_x - 1; x_index >= 0; x_index--) // { // distance += voxel_size[0]; // /* The voxel value is 0; the distance should be 0 too. */ // if (distance_image[element_index + x_index] == 0.0f) // { // distance = 0.0f; // } // /* Calculate the shortest distance in the row. */ // distance_image[element_index + x_index] = Math.Min(distance_image[element_index + x_index], distance); // } // }); // /* Apply the transform in the y-direction. */ // float size_1_sqr = ToolsMath.Sqr(voxel_size[1]); // float size_1_inv = 1.0f / voxel_size[1]; // Parallel.For(0, size_z * size_x, plane_index => // { // int z_index = plane_index / size_x; // int x_index = plane_index - z_index * size_x; // int element_index = z_index * size_y * size_x + x_index; // float[] temp_1 = new float[size_y]; // /* Copy the column and square the distances. */ // for (int y_index = 0; y_index < size_y; y_index++) // { // temp_1[y_index] = ToolsMath.Sqr(distance_image[element_index + y_index * size_x]); // } // /* Calculate the smallest squared distance in 2D. */ // for (int y_index = 0; y_index < size_y; y_index++) // { // /* Calculate the smallest search range, i.e. y-im to y+im. */ // float distance = temp_1[y_index]; // if (distance == 0) // { // continue; // } // int im = (int)(size_1_inv * ToolsMath.Sqrt(distance)); // if (im == 0) // { // continue; // } // for (int j = Math.Max(0, y_index - im); j < Math.Min(size_y, y_index + im); j++) // { // if (temp_1[j] < distance) distance = Math.Min(distance, temp_1[j] + size_1_sqr * ToolsMath.Sqr(j - y_index)); // } // distance_image[element_index + y_index * size_x] = distance; // } // }); // /* Apply the transform in the z-direction. */ // float size_2_sqr = ToolsMath.Sqr(voxel_size[2]); // float size_2_inv = 1.0f / voxel_size[2]; // Parallel.For(0, size_y * size_x, plane_index => // { // float[] temp_2 = new float[size_z]; // int y_index = plane_index / size_x; // int x_index = plane_index - (y_index * size_x); // int element_index = y_index * size_x + x_index; // /* Copy the column. */ // for (int z_index = 0; z_index < size_z; z_index++) // { // temp_2[z_index] = distance_image[element_index + z_index * size_y * size_x]; // } // /* Calculate the smallest squared distance in 3D. */ // for (int z_index = 0; z_index < size_z; z_index++) // { // /* Calculate smallest search range, i.e. z-im to z+im. */ // float distance = temp_2[z_index]; // if (distance == 0) // { // continue; // } // int im = (int)(size_2_inv * ToolsMath.Sqrt(distance)); // if (im == 0) // { // continue; // } // for (int j = Math.Max(0, z_index - im); j < Math.Min(size_z, z_index + im); j++) // { // if (temp_2[j] < distance) distance = Math.Min(distance, temp_2[j] + size_2_sqr * ToolsMath.Sqr(j - z_index)); // } // distance_image[element_index + z_index * size_x * size_y] = distance; // } // }); //} public static void DistanceTransform3DMediumRBA(ImageRaster3D <bool> mask_image, float[] voxel_size, ImageRaster3D <float> distance_image) { IRaster3DInteger raster = mask_image.Raster; int size_0 = mask_image.Raster.Size0; int size_1 = mask_image.Raster.Size1; int size_2 = mask_image.Raster.Size2; float[] locations_0 = new float[size_0]; float[] locations_1 = new float[size_1]; float[] locations_2 = new float[size_2]; Parallel.For(0, size_0, index_0 => { locations_0[index_0] = index_0 * voxel_size[0]; }); Parallel.For(0, size_1, index_1 => { locations_1[index_1] = index_1 * voxel_size[1]; }); Parallel.For(0, size_2, index_2 => { locations_2[index_2] = index_2 * voxel_size[2]; }); // Apply the transform in the x-direction //for (int plane_index = 0; plane_index < size_z * size_y; plane_index++) //{ Parallel.For(0, size_2 * size_1, plane_index => { int index_2 = plane_index / size_1; int index_1 = plane_index % size_1; // Upwards pass float added_distance = float.PositiveInfinity; for (int index_0 = 0; index_0 < size_0; index_0++) { int element_index = raster.GetElementIndex(index_0, index_1, index_2); added_distance += voxel_size[0]; /* The voxel value is true; the distance should be 0. */ if (mask_image[element_index]) { added_distance = 0.0f; } distance_image[element_index] = ToolsMath.Sqr(added_distance); } if (distance_image.GetElementValue(size_0 - 1, index_1, index_2) < float.PositiveInfinity) { // Downwards pass added_distance = float.PositiveInfinity; for (int index_0 = size_0 - 1; index_0 >= 0; index_0--) { int element_index = raster.GetElementIndex(index_0, index_1, index_2); added_distance += voxel_size[0]; /* The voxel value is 0; the distance should be 0 too. */ if (distance_image[element_index] == 0.0f) { added_distance = 0.0f; } /* Calculate the shortest distance in the row. */ distance_image[element_index] = Math.Min(distance_image[element_index], ToolsMath.Sqr(added_distance)); } } }); // Apply the transform in the y-direction //for (int plane_index = 0; plane_index < size_0 * size_2; plane_index++) //{ Parallel.For(0, size_2 * size_0, plane_index => { int index_0 = plane_index % size_0; int index_2 = plane_index / size_0; float[] temp_1 = new float[size_1]; for (int index_1 = 0; index_1 < size_1; index_1++) { temp_1[index_1] = distance_image.GetElementValue(index_0, index_1, index_2); } // Upwards pass for (int index_1 = 0; index_1 < size_1; index_1++) { int element_index = raster.GetElementIndex(index_0, index_1, index_2); for (int index_1_inner = index_1 + 1; index_1_inner < size_1; index_1_inner++) { if (distance_image.GetElementValue(index_0, index_1_inner, index_2) <= distance_image[element_index]) { break; } float distance = distance_image[element_index] + ToolsMath.Sqr((index_1_inner - index_1) * voxel_size[1]); if (distance < temp_1[index_1_inner]) { temp_1[index_1_inner] = distance; } } } //Downwards pass for (int index_1 = size_1 - 1; index_1 >= 0; index_1--) { int element_index = raster.GetElementIndex(index_0, index_1, index_2); for (int index_1_inner = index_1 - 1; index_1_inner >= 0; index_1_inner--) { if (distance_image.GetElementValue(index_0, index_1_inner, index_2) <= distance_image[element_index]) { break; } float distance = distance_image[element_index] + ToolsMath.Sqr((index_1 - index_1_inner) * voxel_size[1]); if (distance < temp_1[index_1_inner]) { temp_1[index_1_inner] = distance; } } } for (int index_1 = 0; index_1 < size_1; index_1++) { distance_image.SetElementValue(index_0, index_1, index_2, temp_1[index_1]); } }); // Apply the transform in the z-direction. */ //for (int plane_index = 0; plane_index < size_x * size_y; plane_index++) //{ Parallel.For(0, size_1 * size_0, plane_index => { int index_0 = plane_index % size_0; int index_1 = plane_index / size_0; float[] temp_2 = new float[size_2]; for (int index_2 = 0; index_2 < size_2; index_2++) { temp_2[index_2] = distance_image.GetElementValue(index_0, index_1, index_2); } // Upwards_pass for (int index_2 = 0; index_2 < size_2; index_2++) { int element_index = raster.GetElementIndex(index_0, index_1, index_2); for (int index_2_inner = index_2 + 1; index_2_inner < size_2; index_2_inner++) { if (distance_image.GetElementValue(index_0, index_1, index_2_inner) <= distance_image[element_index]) { break; } float distance = distance_image[element_index] + ToolsMath.Sqr((index_2_inner - index_2) * voxel_size[2]); if (distance < temp_2[index_2_inner]) { temp_2[index_2_inner] = distance; } } } // Downwards pass for (int index_2 = size_2 - 1; index_2 >= 0; index_2--) { int element_index = raster.GetElementIndex(index_0, index_1, index_2); for (int index_2_inner = index_2 - 1; index_2_inner >= 0; index_2_inner--) { if (distance_image.GetElementValue(index_0, index_1, index_2_inner) <= distance_image[element_index]) { break; } float distance = distance_image[element_index] + ToolsMath.Sqr((index_2 - index_2_inner) * voxel_size[2]); if (distance < temp_2[index_2_inner]) { temp_2[index_2_inner] = distance; } } } //Fill and root for (int index_2 = 0; index_2 < size_2; index_2++) { distance_image.SetElementValue(index_0, index_1, index_2, ToolsMath.Sqrt(temp_2[index_2])); } }); }