public void new_method() { #region doc_learn // Suppose we would like to compute descriptive // statistics from the following data samples: double[][] data = { new double[] { 1, 52, 5 }, new double[] { 2, 12, 5 }, new double[] { 1, 65, 5 }, new double[] { 1, 25, 5 }, new double[] { 2, 62, 5 }, }; // Create the descriptive analysis var analysis = new DescriptiveAnalysis(); // Learn the data analysis.Learn(data); // Query different measures double[] means = analysis.Means; double[] variance = analysis.Variances; DoubleRange[] quartiles = analysis.Quartiles; #endregion test(analysis); }
public void DescriptiveAnalysisConstructorTest1() { // Suppose we would like to compute descriptive // statistics from the following data samples: double[,] data = { { 1, 52, 5 }, { 2, 12, 5 }, { 1, 65, 5 }, { 1, 25, 5 }, { 2, 62, 5 }, }; // Create the analysis DescriptiveAnalysis analysis = new DescriptiveAnalysis(data); // Compute analysis.Compute(); // Retrieve interest measures double[] means = analysis.Means; // { 1.4, 43.2, 5.0 } double[] modes = analysis.Modes; // { 1.0, 52.0, 5.0 } Assert.AreEqual(3, means.Length); Assert.AreEqual(1.4, means[0]); Assert.AreEqual(43.2, means[1]); Assert.AreEqual(5.0, means[2]); Assert.AreEqual(1.0, modes[0]); Assert.AreEqual(52.0, modes[1]); Assert.AreEqual(5.0, modes[2]); }
public void DescriptiveAnalysisConstructorTest4() { double[][] data = { new double[] { 1, 52, 5 }, new double[] { 2, 12, 5 }, new double[] { 1, 65, 5 }, new double[] { 1, 25, 5 }, new double[] { 2, 62, 5 }, }; // Create the analysis DescriptiveAnalysis analysis = new DescriptiveAnalysis(data); analysis.Compute(); double[] means = analysis.Means; double[] modes = analysis.Modes; Assert.AreEqual(3, means.Length); Assert.AreEqual(1.4, means[0]); Assert.AreEqual(43.2, means[1]); Assert.AreEqual(5.0, means[2]); Assert.AreEqual(1.0, modes[0]); Assert.AreEqual(52.0, modes[1]); Assert.AreEqual(5.0, modes[2]); Assert.AreEqual(3, analysis.ColumnNames.Length); Assert.AreEqual("Column 0", analysis.ColumnNames[0]); Assert.AreEqual("Column 1", analysis.ColumnNames[1]); Assert.AreEqual("Column 2", analysis.ColumnNames[2]); }
/// <summary> /// Creates a Multinomial Logistic Regression Analysis. /// </summary> /// private void btnCreate_Click(object sender, EventArgs e) { if (dgvLearningSource.DataSource == null) { MessageBox.Show("Please load some data first."); return; } // Finishes and save any pending changes to the given data dgvLearningSource.EndEdit(); // Creates a matrix from the entire source data table double[,] table = (dgvLearningSource.DataSource as DataTable).ToMatrix(out columnNames); // Get only the input vector values (first two columns) double[][] inputs = table.GetColumns(new int[] { 0, 1 }).ToArray(); // Get only the output labels (last column) int[] outputs = table.GetColumn(2).Subtract(1).ToInt32(); // Create and compute a new Simple Descriptive Analysis var sda = new DescriptiveAnalysis(table, columnNames); sda.Compute(); // Show the descriptive analysis on the screen dgvDistributionMeasures.DataSource = sda.Measures; // Creates the Support Vector Machine for 2 input variables mlr = new MultinomialLogisticRegressionAnalysis(inputs, outputs); try { // Run mlr.Compute(); lbStatus.Text = "Analysis complete!"; } catch (ConvergenceException) { lbStatus.Text = "Convergence could not be attained. " + "The learned machine might still be usable."; } createSurface(table); // Populate details about the fitted model tbChiSquare.Text = mlr.ChiSquare.Statistic.ToString("N5"); tbPValue.Text = mlr.ChiSquare.PValue.ToString("N5"); checkBox1.Checked = mlr.ChiSquare.Significant; tbDeviance.Text = mlr.Deviance.ToString("N5"); tbLogLikelihood.Text = mlr.LogLikelihood.ToString("N5"); dgvCoefficients.DataSource = mlr.Coefficients; }
public void DataBindTest() { double[,] data = { { 1, 52, 5 }, { 2, 12, 5 }, { 1, 65, 5 }, { 1, 25, 5 }, { 2, 62, 5 }, }; DescriptiveAnalysis analysis = new DescriptiveAnalysis(data); analysis.Compute(); var m0 = analysis.Measures[0]; var m1 = analysis.Measures[1]; var m2 = analysis.Measures[2]; Assert.AreEqual(0, m0.Index); Assert.AreEqual(1, m1.Index); Assert.AreEqual(2, m2.Index); Assert.AreEqual("Column 0", m0.Name); Assert.AreEqual("Column 1", m1.Name); Assert.AreEqual("Column 2", m2.Name); Assert.AreEqual(m0, analysis.Measures["Column 0"]); Assert.AreEqual(m1, analysis.Measures["Column 1"]); Assert.AreEqual(m2, analysis.Measures["Column 2"]); // var box = Accord.Controls.DataGridBox.Show(analysis.Measures); // Assert.AreEqual(21, box.DataGridView.Columns.Count); }
private ArrayDataView AnalysisData(DataTable dataTable) { DescriptiveAnalysis analysis; string[] columnNames; double[,] arrayData = dataTable.ToMatrix(out columnNames); // Analysis Data analysis = new DescriptiveAnalysis(arrayData, columnNames); analysis.Compute(); double[,] analysisData = new double[0, arrayData.GetLength(1)]; analysisData = analysisData.InsertRow <double>(analysis.Distinct.ToDouble(), analysisData.GetLength(0)); analysisData = analysisData.InsertRow <double>(analysis.Means, analysisData.GetLength(0)); analysisData = analysisData.InsertRow <double>(analysis.Medians, analysisData.GetLength(0)); analysisData = analysisData.InsertRow <double>(analysis.Modes, analysisData.GetLength(0)); analysisData = analysisData.InsertRow <double>(analysis.StandardDeviations, analysisData.GetLength(0)); analysisData = analysisData.InsertRow <double>(analysis.Variances, analysisData.GetLength(0)); analysisData = analysisData.InsertRow <double>(analysis.Sums, analysisData.GetLength(0)); string[] rowNames = { "Distinct", "Means", "Medians", "Modes", "StandardDeviations", "Variances", "Sums" }; return(new ArrayDataView(analysisData, columnNames, rowNames)); }
public void DescriptiveAnalysisConstructorTest() { double[,] data = Matrix.Magic(3); string[] columnNames = { "x", "y", "z" }; DescriptiveAnalysis target = new DescriptiveAnalysis(data, columnNames); target.Compute(); Assert.AreEqual("x", target.ColumnNames[0]); Assert.AreEqual("y", target.ColumnNames[1]); Assert.AreEqual("z", target.ColumnNames[2]); Assert.IsTrue(target.CorrelationMatrix.IsEqual(new double[, ] { { 1.0000, -0.7559, 0.1429 }, { -0.7559, 1.0000, -0.7559 }, { 0.1429, -0.7559, 1.0000 }, }, 0.0001)); Assert.IsTrue(target.CovarianceMatrix.IsEqual(new double[, ] { { 7, -8, 1 }, { -8, 16, -8 }, { 1, -8, 7 }, }, 0.00000001)); Assert.IsTrue(target.StandardScores.IsEqual(new double[, ] { { 1.1339, -1.0000, 0.3780 }, { -0.7559, 0, 0.7559 }, { -0.3780, 1.0000, -1.1339 }, }, 0.001)); Assert.IsTrue(target.Means.IsEqual(new double[] { 5, 5, 5 })); Assert.IsTrue(target.StandardDeviations.IsEqual(new double[] { 2.6458, 4.0000, 2.6458 }, 0.001)); Assert.IsTrue(target.Medians.IsEqual(new double[] { 4, 5, 6 })); Assert.AreEqual(3, target.Ranges[0].Min); Assert.AreEqual(8, target.Ranges[0].Max); Assert.AreEqual(1, target.Ranges[1].Min); Assert.AreEqual(9, target.Ranges[1].Max); Assert.AreEqual(2, target.Ranges[2].Min); Assert.AreEqual(7, target.Ranges[2].Max); Assert.AreEqual(3, target.Samples); Assert.AreEqual(3, target.Variables); Assert.IsTrue(target.Source.IsEqual(Matrix.Magic(3))); Assert.IsTrue(target.Sums.IsEqual(new double[] { 15, 15, 15 })); Assert.IsTrue(target.Variances.IsEqual(new double[] { 7, 16, 7 })); }
protected override void EndProcessing() { var d = new DescriptiveAnalysis(_data.ColumnNames.ToArray()); d.Learn(_data.ToDoubleJaggedArray()); // TODO: Convert output to prettyprint or define format data WriteObject(d); }
public static PerformanceResult RunConcurrentPerformanceTest(int numIterations, int degreeParallelism, Action operation) { int i; var taskList = new Task <PerformanceResult> [degreeParallelism]; long startTime = HiResTimer.Ticks; int subIterations = numIterations / degreeParallelism; for (i = 0; i < degreeParallelism; i++) { var t = new Task <PerformanceResult>(() => RunPerformanceTest(subIterations, operation, true)); taskList[i] = t; } for (i = 0; i < degreeParallelism; i++) { taskList[i].Start(); } Task.WaitAll(taskList); long stopTime = HiResTimer.Ticks; var rawData = new List <double>(); for (i = 0; i < degreeParallelism; i++) { rawData.AddRange(taskList[i].Result.DescriptiveResult.RawData); } var desc = new DescriptiveAnalysis(rawData); desc.Analyze(false); desc.AnalyzeHistogram(cHistogramBuckets); var res = new PerformanceResult { IsValid = true, Iterations = taskList.Sum(p => p.Result.Iterations), DegreeOfParallelism = degreeParallelism, TotalMilliseconds = ConvertToMs(startTime, stopTime), TotalSeconds = ConvertToSeconds(startTime, stopTime), TotalTicks = stopTime - startTime, DescriptiveResult = desc.Result }; for (i = 0; i < degreeParallelism; i++) { taskList[i].Dispose(); } return(res); }
public static PerformanceResult RunConcurrentPerformanceTest(int numIterations, int degreeParallelism, Func <bool> operation) { int i; var taskList = new Task <PerformanceResult> [degreeParallelism]; int subIterations = numIterations / degreeParallelism; for (i = 0; i < degreeParallelism; i++) { var t = new Task <PerformanceResult>(() => RunPerformanceTest(subIterations, operation, true)); taskList[i] = t; } for (i = 0; i < degreeParallelism; i++) { taskList[i].Start(); } Task.WaitAll(taskList); var rawData = new List <double>(); bool valid = true; for (i = 0; i < degreeParallelism; i++) { valid &= taskList[i].Result.IsValid; rawData.AddRange(taskList[i].Result.DescriptiveResult.RawData); } var desc = new DescriptiveAnalysis(rawData); desc.Analyze(false); desc.AnalyzeHistogram(cHistogramBuckets); var res = new PerformanceResult { IsValid = valid, TotalMilliseconds = taskList.Max(p => p.Result.TotalMilliseconds), TotalSeconds = taskList.Max(p => p.Result.TotalSeconds), TotalTicks = taskList.Max(p => p.Result.TotalTicks), DescriptiveResult = desc.Result }; for (i = 0; i < degreeParallelism; i++) { taskList[i].Dispose(); } return(res); }
private void btnRunAnalysis_Click(object sender, EventArgs e) { if (dgvAnalysisSource.DataSource == null) { MessageBox.Show("Please load some data first."); return; } // Finishes and save any pending changes to the given data dgvAnalysisSource.EndEdit(); // Creates a matrix from the source data table double[,] sourceMatrix = (dgvAnalysisSource.DataSource as DataTable).ToMatrix(out sourceColumnNames); // Creates the Simple Descriptive Analysis of the given source sda = new DescriptiveAnalysis(sourceMatrix, sourceColumnNames); sda.Compute(); // Populates statistics overview tab with analysis data dgvStatisticCenter.DataSource = new ArrayDataView(sda.DeviationScores, sourceColumnNames); dgvStatisticStandard.DataSource = new ArrayDataView(sda.StandardScores, sourceColumnNames); dgvStatisticCovariance.DataSource = new ArrayDataView(sda.CovarianceMatrix, sourceColumnNames); dgvStatisticCorrelation.DataSource = new ArrayDataView(sda.CorrelationMatrix, sourceColumnNames); dgvDistributionMeasures.DataSource = sda.Measures; // Creates the Principal Component Analysis of the given source pca = new PrincipalComponentAnalysis(sda.Source, (AnalysisMethod)cbMethod.SelectedValue); // Compute the Principal Component Analysis pca.Compute(); // Populates components overview with analysis data dgvFeatureVectors.DataSource = new ArrayDataView(pca.ComponentMatrix); dgvPrincipalComponents.DataSource = pca.Components; dgvProjectionComponents.DataSource = pca.Components; numComponents.Maximum = pca.Components.Count; numComponents.Value = 1; numThreshold.Value = (decimal)pca.Components[0].CumulativeProportion * 100; CreateComponentCumulativeDistributionGraph(graphCurve); CreateComponentDistributionGraph(graphShare); }
/// <summary> /// Launched when the user clicks the "Run analysis" button. /// </summary> /// private void btnCompute_Click(object sender, EventArgs e) { // Save any pending changes dgvAnalysisSource.EndEdit(); if (dgvAnalysisSource.DataSource == null) { MessageBox.Show("Please load some data using File > Open!"); return; } // Create a matrix from the source data table double[,] sourceMatrix = (dgvAnalysisSource.DataSource as DataTable).ToMatrix(out columnNames); // Create and compute a new Simple Descriptive Analysis sda = new DescriptiveAnalysis(sourceMatrix, columnNames); sda.Compute(); // Show the descriptive analysis on the screen dgvDistributionMeasures.DataSource = sda.Measures; // Populates statistics overview tab with analysis data dgvStatisticCenter.DataSource = new ArrayDataView(sda.DeviationScores, columnNames); dgvStatisticStandard.DataSource = new ArrayDataView(sda.StandardScores, columnNames); dgvStatisticCovariance.DataSource = new ArrayDataView(sda.CovarianceMatrix, columnNames); dgvStatisticCorrelation.DataSource = new ArrayDataView(sda.CorrelationMatrix, columnNames); AnalysisMethod method = (AnalysisMethod)cbMethod.SelectedValue; // Create the Principal Component Analysis of the data pca = new PrincipalComponentAnalysis(sda.Source, method); pca.Compute(); // Finally, compute the analysis! // Populate components overview with analysis data dgvFeatureVectors.DataSource = new ArrayDataView(pca.ComponentMatrix); dgvPrincipalComponents.DataSource = pca.Components; dgvProjectionComponents.DataSource = pca.Components; distributionView.DataSource = pca.Components; cumulativeView.DataSource = pca.Components; numComponents.Maximum = pca.Components.Count; numComponents.Value = 1; numThreshold.Value = (decimal)pca.Components[0].CumulativeProportion * 100; }
private string outDescripAnalysis(Sitio s, DescriptiveAnalysis da) { StringBuilder sb = new StringBuilder(); sb.Append(s.name + ","); sb.Append(s.gridPoints.Count + ","); sb.Append(da.Means[0] + ","); sb.Append(da.Medians[0] + ","); sb.Append(da.Modes[0] + ","); sb.Append(da.Ranges[0].Min + ","); sb.Append(da.Ranges[0].Max + ","); return(sb.ToString()); }
public KPCAResult Analyze() { string[] columnNames; double[,] inputDataMatrix = options.InputData.ToMatrix(out columnNames); var da = new DescriptiveAnalysis(inputDataMatrix, columnNames); da.Compute(); IKernel kernel = null; switch (options.KpcaKernel) { case KPCAKernelKind.Gaussian: kernel = new Gaussian(options.Sigma.Value); break; case KPCAKernelKind.Polynomial: kernel = new Polynomial(options.Degree.Value, options.Constant.Value); break; } double[,] inputs = inputDataMatrix.GetColumns(0, 1); var kpca = new KernelPrincipalComponentAnalysis(inputs, kernel, options.Method); kpca.Center = options.Center; kpca.Compute(); double[,] result; if (kpca.Components.Count >= 2) { result = kpca.Transform(inputs, 2); } else { result = kpca.Transform(inputs, 1); result = result.InsertColumn(Matrix.Vector(result.GetLength(0), 0.0)); } //double[,] points = result.InsertColumn(inputDataMatrix.GetColumn(2)); return(new KPCAResult(options.InputData, da.DeviationScores, da.StandardScores, columnNames, options.Method, da.Measures, inputDataMatrix, kpca.ComponentMatrix, kpca.Components)); }
public void DescriptiveAnalysisConstructorTest4() { double[][] data = { new double[] { 1, 52, 5 }, new double[] { 2, 12, 5 }, new double[] { 1, 65, 5 }, new double[] { 1, 25, 5 }, new double[] { 2, 62, 5 }, }; // Create the analysis DescriptiveAnalysis analysis = new DescriptiveAnalysis(data); analysis.Compute(); test(analysis); }
/// <summary> /// Tests whether the means of two samples are different. /// </summary> /// <param name="sample1">The first sample.</param> /// <param name="sample2">The second sample.</param> /// <param name="hypothesizedDifference">The hypothesized sample difference.</param> /// <param name="assumeEqualVariances">True to assume equal variances, false otherwise. Default is true.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> public TwoSampleTTest(List <double> sample1, List <double> sample2, bool assumeEqualVariances = true, double hypothesizedDifference = 0, TwoSampleHypothesis alternate = TwoSampleHypothesis.ValuesAreDifferent) { // References: http://en.wikipedia.org/wiki/Student's_t-test#Worked_examples var s1 = new DescriptiveAnalysis(sample1); var s2 = new DescriptiveAnalysis(sample2); s1.Analyze(false); s2.Analyze(false); var r1 = s1.Result; var r2 = s2.Result; Compute(r1.Mean, r1.Variance, r1.Count, r2.Mean, r2.Variance, r2.Count, hypothesizedDifference, assumeEqualVariances, alternate); }
public static PerformanceResult RunPerformanceTest(int numIterations, Func <bool> operation, bool isParallel = false) { bool isResultValid = true; var measures = new List <double>(numIterations); // grab the start time long startTime = HiResTimer.Ticks; // Same operation as the functional test, we just // do a lot of them. for (int i = 0; ((i < numIterations) && (isResultValid)); i++) { long aStart = HiResTimer.Ticks; isResultValid &= (operation()); long aStop = HiResTimer.Ticks; if (isResultValid) { measures.Add(ConvertToMs(aStart, aStop)); } } // grab the stop time long stopTime = HiResTimer.Ticks; var descriptive = new DescriptiveAnalysis(measures); descriptive.Analyze(isParallel); if (!isParallel) { descriptive.AnalyzeHistogram(cHistogramBuckets); } // If they all worked, we can report a valid result. // If they didn't then we call the perf test inconclusive. return(new PerformanceResult { IsValid = isResultValid, TotalSeconds = ConvertToSeconds(startTime, stopTime), TotalMilliseconds = ConvertToMs(startTime, stopTime), TotalTicks = (stopTime - startTime), DescriptiveResult = descriptive.Result }); }
public void DescriptiveAnalysisConstructorTest1() { // Suppose we would like to compute descriptive // statistics from the following data samples: double[,] data = { { 1, 52, 5 }, { 2, 12, 5 }, { 1, 65, 5 }, { 1, 25, 5 }, { 2, 62, 5 }, }; // Create the analysis DescriptiveAnalysis analysis = new DescriptiveAnalysis(data); // Compute analysis.Compute(); test(analysis); }
public PCAResult Analyze() { string[] columnNames; double[,] inputMatrix = options.InputData.ToMatrix(out columnNames); var da = new DescriptiveAnalysis(inputMatrix, columnNames); da.Compute(); var pca = new PrincipalComponentAnalysis(da.Source, options.Method); pca.Compute(); return(new PCAResult(options.InputData, da.DeviationScores, da.StandardScores, columnNames, options.Method, da.Measures, da.CorrelationMatrix, da.CovarianceMatrix, pca.ComponentMatrix, pca.Components)); }
private void zTest(List <double> scores, double hypothesismean) { // Creates the Simple Descriptive Analysis of the given source DescriptiveAnalysis da = new DescriptiveAnalysis(scores.ToArray()); int sampleSize = scores.Count; double sampleMean = da.Means[0]; double standardDeviation = da.StandardDeviations[0]; double hypothesizedMean = hypothesismean; ZTest test = new ZTest(sampleMean, standardDeviation, sampleSize, hypothesizedMean, OneSampleHypothesis.ValueIsSmallerThanHypothesis); // Now, we can check whether this result would be // unlikely under a standard significance level: bool significant = test.Significant; // We can also check the test statistic and its P-Value double statistic = test.Statistic; double pvalue = test.PValue; }
internal void compute() { // Create and compute a new Simple Descriptive Analysis var sda = new DescriptiveAnalysis(_data, _columnNames); sda.Compute(); // Create the Principal Component Analysis of the data _pca = new PrincipalComponentAnalysis(sda.Source, _configuration.Method); _pca.Compute(); if (_configuration.PCAExportUseForcedNumComponents && (_pca.Components.Count > _configuration.PCAExportForcedNumComponents)) { _transformedData = _pca.Transform(_data, _configuration.PCAExportForcedNumComponents); } else { _transformedData = _pca.Transform(_data, _pca.Components.Count); } }
/// <summary> /// Constructs a Z test. /// </summary> /// <param name="sample1">The first data sample.</param> /// <param name="sample2">The second data sample.</param> /// <param name="hypothesizedDifference">The hypothesized sample difference.</param> /// <param name="alternate">The alternative hypothesis (research hypothesis) to test.</param> public TwoSampleZTest(List <double> sample1, List <double> sample2, double hypothesizedDifference = 0, TwoSampleHypothesis alternate = TwoSampleHypothesis.ValuesAreDifferent) { int samples1 = sample1.Count; int samples2 = sample2.Count; if (samples1 < 30 || samples2 < 30) { Trace.TraceWarning( "Warning: running a Z test for less than 30 samples. Consider running a Student's T Test instead."); } var s1 = new DescriptiveAnalysis(sample1); var s2 = new DescriptiveAnalysis(sample2); s1.Analyze(false); s2.Analyze(false); var r1 = s1.Result; var r2 = s2.Result; Compute(r1.Mean, r2.Mean, r1.Variance / r1.Count, r2.Variance / r2.Count, hypothesizedDifference, alternate); }
public void DescriptiveAnalysisConstructorTest3() { // Suppose we would like to compute descriptive // statistics from the following data samples: double[] data = { 52, 12, 65, 25, 62, 12 }; // Create the analysis DescriptiveAnalysis analysis = new DescriptiveAnalysis(data); // Compute analysis.Compute(); var columnNames = analysis.ColumnNames; var correlation = analysis.CorrelationMatrix; var covariance = analysis.CovarianceMatrix; var deviationScores = analysis.DeviationScores; var distinct = analysis.Distinct; var kurtosis = analysis.Kurtosis; var means = analysis.Means; var medians = analysis.Medians; var modes = analysis.Modes; var ranges = analysis.Ranges; var samples = analysis.Samples; var skewness = analysis.Skewness; var source = analysis.Source; var standardDeviation = analysis.StandardDeviations; var standardErrors = analysis.StandardErrors; var standardScores = analysis.StandardScores; var sums = analysis.Sums; var variables = analysis.Variables; var variances = analysis.Variances; var confidence = analysis.Confidence; var quartiles = analysis.Quartiles; Assert.IsTrue(columnNames.IsEqual(new string[] { "Column 0", })); Assert.IsTrue(correlation.IsEqual(new double[, ] { { 1 } })); Assert.IsTrue(covariance.IsEqual(new double[, ] { { 604.39999999999998 } })); Assert.IsTrue(deviationScores.IsEqual(new double[, ] { { 14.0 }, { -26.0 }, { 27.0 }, { -13.0 }, { 24.0 }, { -26.0 } })); Assert.IsTrue(distinct.IsEqual(new int[] { 5 })); Assert.IsTrue(kurtosis.IsEqual(new double[] { -2.7161799571726601 })); Assert.IsTrue(means.IsEqual(new double[] { 38.0 })); Assert.IsTrue(medians.IsEqual(new double[] { 38.5 })); Assert.IsTrue(modes.IsEqual(new double[] { 12.0 })); Assert.IsTrue(ranges.Apply(p => p.Min).IsEqual(new double[] { 12 })); Assert.IsTrue(ranges.Apply(p => p.Max).IsEqual(new double[] { 65 })); Assert.IsTrue(samples == 6); Assert.IsTrue(skewness.IsEqual(new double[] { -0.022168530787350427 })); Assert.IsTrue(source.IsEqual(new double[, ] { { 52 }, { 12 }, { 65 }, { 25 }, { 62 }, { 12 }, })); Assert.IsTrue(standardDeviation.IsEqual(new double[] { 24.584547992590792 })); Assert.IsTrue(standardErrors.IsEqual(new double[] { 10.036599689801987 })); Assert.IsTrue(standardScores.IsEqual(new double[, ] { { 0.5694633883128245 }, { -1.0575748640095313 }, { 1.09825082031759 }, { -0.52878743200476563 }, { 0.97622295139341342 }, { -1.0575748640095313 }, })); Assert.IsTrue(sums.IsEqual(new double[] { 228.0 })); Assert.IsTrue(variables == 1); Assert.IsTrue(variances.IsEqual(new double[] { 604.39999999999998 })); Assert.AreEqual(1, confidence.Length); Assert.AreEqual(18.328626080742229, confidence[0].Min); Assert.AreEqual(57.671373919257775, confidence[0].Max); DoubleRange q; double q2 = Measures.Quartiles(data, out q, alreadySorted: false); Assert.AreEqual(1, quartiles.Length); Assert.AreEqual(q.Min, quartiles[0].Min); Assert.AreEqual(q.Max, quartiles[0].Max); Assert.AreEqual(12, quartiles[0].Min); Assert.AreEqual(62, quartiles[0].Max); }
private void btnGenerateInputData_Click(object sender, EventArgs e) { //if (this.strProjectPath.Trim() == "") // return; //if (this.cmbBoundary.Text.Trim() == "") // return; //if (this.cmbRstraint.Text.Trim() == "") // return; //if (this.txtParameter.Text.Trim() == "") // return; if (lsbLayerLandUse.Items.Count == 0) return; //if (lsbLayerDriverFactor.Items.Count == 0) // return; int iLanduseType = 5; ILayer pLayer = null; IRaster pRaster =new RasterClass(); IGeoDataset pGdsMask = null; IGeoDataset pGdsRstraint = null; IGeoDataset pGdsLanduse = null; IGeoDataset pGdsDriverFactor = null; IRasterBandCollection pRasterBandColection=(new RasterClass()) as IRasterBandCollection; //try //{ string sLyrMask = this.cmbBoundary.Text; //boundaty-->mask for (int i = 0; i < pMap.LayerCount; i++) { ILayer pLyr = pMap.get_Layer(i); if (pLyr is IRasterLayer) { if (pLyr.Name == sLyrMask) { pRaster = (pLyr as IRasterLayer).Raster; pGdsMask = pRaster as IGeoDataset; } } } //data //限制区 string sLyrRstraint = this.cmbBoundary.Text; //土地利用数据 string sLyrLanduse = this.lsbLayerLandUse.Items[0].ToString(); //驱动因子 string[] arr = new string[this.lsbLayerDriverFactor.Items.Count]; for (int i = 0; i < this.lsbLayerDriverFactor.Items.Count; i++) { arr[i]=this.lsbLayerDriverFactor.Items[i].ToString(); } for (int i = 0; i < pMap.LayerCount; i++) { ILayer pLyr = pMap.get_Layer(i); if (pLyr is IRasterLayer) { if (pLyr.Name == sLyrRstraint) { pRaster = (pLyr as IRasterLayer).Raster; pGdsRstraint = pRaster as IGeoDataset; } //土地利用数据 if (pLyr.Name == sLyrLanduse) { this.rtxtState.AppendText("读取土地利用参数数据...\n"); this.rtxtState.ScrollToCaret(); pRaster = (pLyr as IRasterLayer).Raster; pGdsLanduse = pRaster as IGeoDataset; //land use 添加到 IRasterBandCollection pRasterBandColection=pRaster as IRasterBandCollection; //pRasterBandColection.AppendBand(pRaster as IRasterBand); string ascFileNameLanduse = strProjectPath + "\\cov1_all.0"; //cov1_0.0;cov1_1.0; Rater2Ascii(pGdsMask, 100, pGdsLanduse, ascFileNameLanduse); //将土地利用数据拆分 StreamReader sr = new StreamReader(ascFileNameLanduse, System.Text.Encoding.Default); //try //{ //使用StreamReader类来读取文件 sr.BaseStream.Seek(0, SeekOrigin.Begin); // 从数据流中读取每一行,直到文件的最后一行,并在richTextBox1中显示出内容 //读取头文件 string[] header = new string[6]; for (int j = 0; j < 6; j++) { header[j] = sr.ReadLine(); } //行列数 string[] ncols = header[0].Split(' '); string[] nrows = header[1].Split(' '); int icol = int.Parse(ncols[ncols.Length - 1]); int irow = int.Parse(nrows[nrows.Length - 1]); int[,] iLanduse = new int[irow, icol]; // string strLine = sr.ReadLine(); string[] strData; int ir = 0; while (ir < irow) //while (strLine != null) { strData = strLine.Split(' '); for (int ic = 0; ic < icol; ic++) { iLanduse[ir, ic] = int.Parse(strData[ic]); } strLine = sr.ReadLine(); ir++; } //关闭此StreamReader对象 sr.Close(); //输出相应的土地利用数据 DataTable2Txt(header, iLanduseType, iLanduse, strProjectPath); //} //catch (Exception ex) //{ //MessageBox.Show(ex.Message); sr.Close(); //} } } } this.rtxtState.AppendText("输出土地利用参数数据成功。\n"); this.rtxtState.AppendText("读取驱动因子数据...\n"); this.rtxtState.ScrollToCaret(); for (int ifac = 0; ifac < lsbLayerDriverFactor.Items.Count; ifac++) { for (int i = 0; i < pMap.LayerCount; i++) { ILayer pLyr = pMap.get_Layer(i); if (pLyr is IRasterLayer) { //输出驱动因子数据 sc1gr0.grid string sFacName = lsbLayerDriverFactor.Items[ifac].ToString(); if (pLyr.Name == sFacName) { pRaster = (pLyr as IRasterLayer).Raster; pGdsDriverFactor = pRaster as IGeoDataset; string ascFileNameFac = strProjectPath + "\\sc1gr"+ifac.ToString()+".grid"; //cov1_0.0;cov1_1.0; this.rtxtState.AppendText("输出驱动因子数据【" + sFacName + "】\n"); Rater2Ascii(pGdsMask, 100, pGdsDriverFactor, ascFileNameFac); this.rtxtState.AppendText("输出驱动因子数据【" + sFacName + "】成功。\n"); this.rtxtState.ScrollToCaret(); //mask 添加到 IRasterBandCollection IRasterBandCollection rasterbands = (IRasterBandCollection)pRaster; IRasterBand rasterband = rasterbands.Item(0); pRasterBandColection.AppendBand(rasterband); //pRasterBandColection.Add(pRaster as IRasterBand,ifac+1); } } } } this.rtxtState.AppendText("开始制备驱动因子参数...\n"); this.rtxtState.ScrollToCaret(); //sample data ITable itFactors=ExportSample(pGdsMask,pRasterBandColection); //logistic 回归分析 DataTable dtFactors = ITable2DTable(itFactors); itFactors = null; //制备驱动力参数文件 this.rtxtState.AppendText("读取驱动因子数据表格数据...\n"); this.rtxtState.ScrollToCaret(); LogisticRegressionAnalysis lra; // Gets the columns of the independent variables List<string> names = new List<string>(); foreach (string name in lsbLayerDriverFactor.Items) names.Add(name); String[] independentNames = names.ToArray(); DataTable independent = dtFactors.DefaultView.ToTable(false, independentNames); // Creates the input and output matrices from the source data table double[][] input = independent.ToArray(); double[,] sourceMatrix = dtFactors.ToMatrix(independentNames); for(int ild=0;ild<iLanduseType;ild++) { String landuseName = (string)this.lsbLayerLandUse.Items[0].ToString(); this.rtxtState.AppendText("开始制备土地利用类型【" + ild.ToString() + "】驱动因子参数...\n"); this.rtxtState.ScrollToCaret(); DataColumn taxColumn =new DataColumn(); taxColumn.DataType = System.Type.GetType("System.Int32"); taxColumn.ColumnName ="sysland"+ild.ToString();//列名 taxColumn.Expression = "iif("+landuseName+" = "+ild.ToString()+",1,0)";//设置该列得表达式,用于计算列中的值或创建聚合列 dtFactors.Columns.Add(taxColumn); string dependentName = "sysland" + ild.ToString(); DataTable dependent = dtFactors.DefaultView.ToTable(false, dependentName); double[] output = dependent.Columns[dependentName].ToArray(); // Creates the Simple Descriptive Analysis of the given source DescriptiveAnalysis sda = new DescriptiveAnalysis(sourceMatrix, independentNames); sda.Compute(); // Populates statistics overview tab with analysis data //dgvDistributionMeasures.DataSource = sda.Measures; // Creates the Logistic Regression Analysis of the given source lra = new LogisticRegressionAnalysis(input, output, independentNames, dependentName); // Compute the Logistic Regression Analysis lra.Compute(); // Populates coefficient overview with analysis data //lra.Coefficients; MessageBox.Show(lra.Coefficients.Count.ToString()); //// Populate details about the fitted model //tbChiSquare.Text = lra.ChiSquare.Statistic.ToString("N5"); //tbPValue.Text = lra.ChiSquare.PValue.ToString("N5"); //checkBox1.Checked = lra.ChiSquare.Significant; //tbDeviance.Text = lra.Deviance.ToString("N5"); //tbLogLikelihood.Text = lra.LogLikelihood.ToString("N5"); } this.rtxtState.AppendText("制备驱动因子参数成功。\n"); this.rtxtState.ScrollToCaret(); MessageBox.Show(dtFactors.Rows.Count.ToString()); //} //catch (Exception ex) //{ // MessageBox.Show(ex.Message); //} }
/// <summary> /// Creates a Multinomial Logistic Regression Analysis. /// </summary> /// private void btnCreate_Click(object sender, EventArgs e) { if (dgvLearningSource.DataSource == null) { MessageBox.Show("Please load some data first."); return; } // Finishes and save any pending changes to the given data dgvLearningSource.EndEdit(); // Creates a matrix from the entire source data table double[,] table = (dgvLearningSource.DataSource as DataTable).ToMatrix(out columnNames); // Get only the input vector values (first two columns) double[][] inputs = table.GetColumns(0, 1).ToArray(); // Get only the output labels (last column) int[] outputs = table.GetColumn(2).Subtract(1).ToInt32(); // Create and compute a new Simple Descriptive Analysis var sda = new DescriptiveAnalysis(table, columnNames); sda.Compute(); // Show the descriptive analysis on the screen dgvDistributionMeasures.DataSource = sda.Measures; // Creates the Support Vector Machine for 2 input variables mlr = new MultinomialLogisticRegressionAnalysis(inputs, outputs); try { // Run mlr.Compute(); lbStatus.Text = "Analysis complete!"; } catch (ConvergenceException) { lbStatus.Text = "Convergence could not be attained. " + "The learned machine might still be usable."; } createSurface(table); // Populate details about the fitted model tbChiSquare.Text = mlr.ChiSquare.Statistic.ToString("N5"); tbPValue.Text = mlr.ChiSquare.PValue.ToString("N5"); checkBox1.Checked = mlr.ChiSquare.Significant; tbDeviance.Text = mlr.Deviance.ToString("N5"); tbLogLikelihood.Text = mlr.LogLikelihood.ToString("N5"); dgvCoefficients.DataSource = mlr.Coefficients; }
private void button1_Click(object sender, EventArgs e) { // Finishes and save any pending changes to the given data dgvAnalysisSource.EndEdit(); if (dgvAnalysisSource.DataSource == null) return; // Creates a matrix from the source data table double[,] sourceMatrix = (dgvAnalysisSource.DataSource as DataTable).ToMatrix(out sourceColumns); int rows = sourceMatrix.GetLength(0); int cols = sourceMatrix.GetLength(1); // Creates a new Simple Descriptive Analysis sda = new DescriptiveAnalysis(sourceMatrix, sourceColumns); sda.Compute(); // Populates statistics overview tab with analysis data dgvDistributionMeasures.DataSource = sda.Measures; // Get only the input values (exclude the class label indicator column) double[,] data = sourceMatrix.Submatrix(null, startColumn: 0, endColumn: 1); // Get only the associated labels int[] labels = sourceMatrix.GetColumn(2).ToInt32(); // Creates the Linear Discriminant Analysis of the given source lda = new LinearDiscriminantAnalysis(data, labels); // Computes the analysis lda.Compute(); // Performs the transformation of the data using two dimensions double[,] result = lda.Transform(data, 2); // Create a new plot with the original Z column double[,] points = result.InsertColumn(sourceMatrix.GetColumn(2)); // Create output scatter plot outputScatterplot.DataSource = points; // Create output table dgvProjectionResult.DataSource = new ArrayDataView(points, sourceColumns); // Populates discriminants overview with analysis data dgvPrincipalComponents.DataSource = lda.Discriminants; dgvFeatureVectors.DataSource = new ArrayDataView(lda.DiscriminantMatrix); dgvScatterBetween.DataSource = new ArrayDataView(lda.ScatterBetweenClass); dgvScatterWithin.DataSource = new ArrayDataView(lda.ScatterWithinClass); dgvScatterTotal.DataSource = new ArrayDataView(lda.ScatterMatrix); // Populates classes information dgvClasses.DataSource = lda.Classes; CreateComponentCumulativeDistributionGraph(graphCurve); CreateComponentDistributionGraph(graphShare); }
public void DescriptiveAnalysisOneSampleTest() { double[] data = { 52 }; // Create the analysis DescriptiveAnalysis analysis = new DescriptiveAnalysis(data); // Compute analysis.Compute(); var columnNames = analysis.ColumnNames; var correlation = analysis.CorrelationMatrix; var covariance = analysis.CovarianceMatrix; var deviationScores = analysis.DeviationScores; var distinct = analysis.Distinct; var kurtosis = analysis.Kurtosis; var means = analysis.Means; var medians = analysis.Medians; var modes = analysis.Modes; var ranges = analysis.Ranges; var samples = analysis.Samples; var skewness = analysis.Skewness; var source = analysis.Source; var standardDeviation = analysis.StandardDeviations; var standardErrors = analysis.StandardErrors; var standardScores = analysis.StandardScores; var sums = analysis.Sums; var variables = analysis.Variables; var variances = analysis.Variances; var confidence = analysis.Confidence; var quartiles = analysis.Quartiles; var innerFence = analysis.InnerFences; var outerFence = analysis.OuterFences; Assert.IsTrue(columnNames.IsEqual(new string[] { "Column 0", })); Assert.IsTrue(correlation.IsEqual(new double[, ] { { Double.NaN } })); Assert.IsTrue(covariance.IsEqual(new double[, ] { { Double.NaN } })); Assert.IsTrue(deviationScores.IsEqual(0)); Assert.IsTrue(distinct.IsEqual(new int[] { 1 })); Assert.IsTrue(kurtosis.IsEqual(new double[] { Double.NaN })); Assert.IsTrue(means.IsEqual(new double[] { 52 })); Assert.IsTrue(medians.IsEqual(new double[] { 52.0 })); Assert.IsTrue(modes.IsEqual(new double[] { 52.0 })); Assert.IsTrue(samples == 1); Assert.IsTrue(skewness.IsEqual(new double[] { Double.NaN })); Assert.IsTrue(source.IsEqual(new double[, ] { { 52 }, })); Assert.IsTrue(standardDeviation.IsEqual(new double[] { double.NaN })); Assert.IsTrue(standardErrors.IsEqual(new double[] { double.NaN })); Assert.IsTrue(standardScores.IsEqual(new double[, ] { { 0.0 }, })); Assert.IsTrue(sums.IsEqual(new double[] { 52 })); Assert.IsTrue(variables == 1); Assert.IsTrue(variances.IsEqual(new double[] { double.NaN })); Assert.AreEqual(1, ranges.Length); Assert.AreEqual(52, ranges[0].Min); Assert.AreEqual(52, ranges[0].Max); Assert.AreEqual(1, confidence.Length); Assert.AreEqual(double.NaN, confidence[0].Min); Assert.AreEqual(double.NaN, confidence[0].Max); Assert.AreEqual(1, quartiles.Length); Assert.AreEqual(52, quartiles[0].Min); Assert.AreEqual(52, quartiles[0].Max); Assert.AreEqual(1, innerFence.Length); Assert.AreEqual(52, innerFence[0].Min); Assert.AreEqual(52, innerFence[0].Max); Assert.AreEqual(1, outerFence.Length); Assert.AreEqual(52, outerFence[0].Min); Assert.AreEqual(52, outerFence[0].Max); }
/// <summary> /// Launched when the user clicks the "Run analysis" button. /// </summary> /// private void btnCompute_Click(object sender, EventArgs e) { // Save any pending changes dgvAnalysisSource.EndEdit(); if (dgvAnalysisSource.DataSource == null) { MessageBox.Show("Please load some data using File > Open!"); return; } // Create a matrix from the source data table double[][] sourceMatrix = (dgvAnalysisSource.DataSource as DataTable).ToArray(out columnNames); // Create and compute a new Simple Descriptive Analysis sda = new DescriptiveAnalysis(columnNames); sda.Learn(sourceMatrix); // Show the descriptive analysis on the screen dgvDistributionMeasures.DataSource = sda.Measures; // Get the input values (the two first columns) double[][] inputs = sourceMatrix.GetColumns(0, 1); // Get only the associated labels (last column) int[] outputs = sourceMatrix.GetColumn(2).ToInt32(); outputs = outputs.Subtract(outputs.Min()); // start at 0 // Create a Linear Discriminant Analysis for the data lda = new LinearDiscriminantAnalysis() { NumberOfOutputs = 2 }; // Compute the analysis! lda.Learn(inputs, outputs); // Perform the transformation of the data double[][] result = lda.Transform(inputs); // Create a new plot with the original Z column double[][] points = result.InsertColumn(sourceMatrix.GetColumn(2)); // Create output scatter plot outputScatterplot.DataSource = points; // Create the output table dgvProjectionResult.DataSource = new ArrayDataView(points, columnNames); // Populate discriminants overview with analysis data dgvFeatureVectors.DataSource = new ArrayDataView(lda.DiscriminantVectors); dgvScatterBetween.DataSource = new ArrayDataView(lda.ScatterBetweenClass); dgvScatterWithin.DataSource = new ArrayDataView(lda.ScatterWithinClass); dgvScatterTotal.DataSource = new ArrayDataView(lda.ScatterMatrix); dgvPrincipalComponents.DataSource = lda.Discriminants; distributionView.DataSource = lda.Discriminants; cumulativeView.DataSource = lda.Discriminants; // Populate classes information dgvClasses.DataSource = lda.Classes; lbStatus.Text = "Good! Feel free to browse the other tabs to see what has been found."; }
private void btnSampleRunAnalysis_Click(object sender, EventArgs e) { // Check requirements if (sourceTable == null) { MessageBox.Show("A sample spreadsheet can be found in the " + "Resources folder in the same directory as this application.", "Please load some data before attempting an analysis"); return; } // Finishes and save any pending changes to the given data dgvAnalysisSource.EndEdit(); sourceTable.AcceptChanges(); // Gets the column of the dependent variable String dependentName = (string)cbTimeName.SelectedItem; String censorName = (string)cbEventName.SelectedItem; DataTable timeTable = sourceTable.DefaultView.ToTable(false, dependentName); DataTable censorTable = sourceTable.DefaultView.ToTable(false, censorName); // Gets the columns of the independent variables List<string> names = new List<string>(); foreach (string name in checkedListBox1.CheckedItems) names.Add(name); String[] independentNames = names.ToArray(); // Creates the input and output matrices from the source data table double[][] input; double[] time = timeTable.Columns[dependentName].ToArray(); int[] censor = censorTable.Columns[censorName].ToArray().ToInt32(); if (independentNames.Length == 0) { input = new double[time.Length][]; for (int i = 0; i < input.Length; i++) input[i] = new double[0]; } else { DataTable independent = sourceTable.DefaultView.ToTable(false, independentNames); input = independent.ToArray(); } String[] sourceColumns; double[,] sourceMatrix = sourceTable.ToMatrix(out sourceColumns); // Creates the Simple Descriptive Analysis of the given source DescriptiveAnalysis sda = new DescriptiveAnalysis(sourceMatrix, sourceColumns); sda.Compute(); // Populates statistics overview tab with analysis data dgvDistributionMeasures.DataSource = sda.Measures; // Creates the Logistic Regression Analysis of the given source pha = new ProportionalHazardsAnalysis(input, time, censor, independentNames, dependentName, censorName); // Compute the Logistic Regression Analysis pha.Compute(); // Populates coefficient overview with analysis data dgvLogisticCoefficients.DataSource = pha.Coefficients; // Populate details about the fitted model tbChiSquare.Text = pha.ChiSquare.Statistic.ToString("N5"); tbPValue.Text = pha.ChiSquare.PValue.ToString("N5"); checkBox1.Checked = pha.ChiSquare.Significant; tbDeviance.Text = pha.Deviance.ToString("N5"); tbLogLikelihood.Text = pha.LogLikelihood.ToString("N5"); // Populate projection source table string[] cols = independentNames; if (!independentNames.Contains(dependentName)) cols = cols.Concatenate(dependentName); if (!independentNames.Contains(censorName)) cols = cols.Concatenate(censorName); DataTable projSource = sourceTable.DefaultView.ToTable(false, cols); dgvProjectionSource.DataSource = projSource; }
public void DescriptiveAnalysisTwoSampleTest() { double[][] data = { new [] { 52.0 }, new [] { 42.0 } }; // Create the analysis DescriptiveAnalysis analysis = new DescriptiveAnalysis(data); // Compute analysis.Compute(); var columnNames = analysis.ColumnNames; var correlation = analysis.CorrelationMatrix; var covariance = analysis.CovarianceMatrix; var deviationScores = analysis.DeviationScores; var distinct = analysis.Distinct; var kurtosis = analysis.Kurtosis; var means = analysis.Means; var medians = analysis.Medians; var modes = analysis.Modes; var ranges = analysis.Ranges; var samples = analysis.Samples; var skewness = analysis.Skewness; var source = analysis.Source; var standardDeviation = analysis.StandardDeviations; var standardErrors = analysis.StandardErrors; var standardScores = analysis.StandardScores; var sums = analysis.Sums; var variables = analysis.Variables; var variances = analysis.Variances; var confidence = analysis.Confidence; var quartiles = analysis.Quartiles; var innerFence = analysis.InnerFences; var outerFence = analysis.OuterFences; Assert.IsTrue(columnNames.IsEqual(new string[] { "Column 0" })); Assert.IsTrue(correlation.IsEqual(Matrix.Create(1, 1, 0.99999999999999978))); Assert.IsTrue(covariance.IsEqual(Matrix.Create(1, 1, 50.0))); Assert.IsTrue(deviationScores.IsEqual(new double[,] { { 5 }, { -5 } })); Assert.IsTrue(distinct.IsEqual(new int[] { 2 })); Assert.IsTrue(kurtosis.IsEqual(new double[] { Double.NaN })); Assert.IsTrue(means.IsEqual(new double[] { 47 })); Assert.IsTrue(medians.IsEqual(new double[] { 94 })); Assert.IsTrue(modes.IsEqual(new double[] { 52.0 }) || modes.IsEqual(new double[] { 42.0 })); Assert.IsTrue(samples == 2); Assert.IsTrue(skewness.IsEqual(new double[] { Double.NaN })); Assert.IsTrue(source.IsEqual(new double[,] { { 52 }, { 42 }, })); Assert.IsTrue(standardDeviation.IsEqual(new double[] { 7.0710678118654755 })); Assert.IsTrue(standardErrors.IsEqual(new double[] { 5.0 })); Assert.IsTrue(standardScores.IsEqual(new double[,] { { 0.70710678118654746 }, { -0.70710678118654746 }, })); Assert.IsTrue(sums.IsEqual(new double[] { 94 })); Assert.IsTrue(variables == 1); Assert.IsTrue(variances.IsEqual(new double[] { 50 })); Assert.AreEqual(1, ranges.Length); Assert.AreEqual(42, ranges[0].Min); Assert.AreEqual(52, ranges[0].Max); Assert.AreEqual(1, confidence.Length); Assert.AreEqual(37.200180077299734, confidence[0].Min); Assert.AreEqual(56.799819922700266, confidence[0].Max); Assert.AreEqual(1, quartiles.Length); Assert.AreEqual(73, quartiles[0].Min); Assert.AreEqual(68, quartiles[0].Max); Assert.AreEqual(1, innerFence.Length); Assert.AreEqual(80.5, innerFence[0].Min); Assert.AreEqual(60.5, innerFence[0].Max); Assert.AreEqual(1, outerFence.Length); Assert.AreEqual(88, outerFence[0].Min); Assert.AreEqual(53, outerFence[0].Max); }
public void DescriptiveAnalysisNSampleTest() { for (int i = 1; i < 100; i++) { double[] data = Matrix.Random(i, 0.0, 1.0); // Create the analysis DescriptiveAnalysis analysis = new DescriptiveAnalysis(data); // Compute analysis.Compute(); var columnNames = analysis.ColumnNames; var correlation = analysis.CorrelationMatrix; var covariance = analysis.CovarianceMatrix; var deviationScores = analysis.DeviationScores; var distinct = analysis.Distinct; var kurtosis = analysis.Kurtosis; var means = analysis.Means; var medians = analysis.Medians; var modes = analysis.Modes; var ranges = analysis.Ranges; var samples = analysis.Samples; var skewness = analysis.Skewness; var source = analysis.Source; var standardDeviation = analysis.StandardDeviations; var standardErrors = analysis.StandardErrors; var standardScores = analysis.StandardScores; var sums = analysis.Sums; var variables = analysis.Variables; var variances = analysis.Variances; var confidence = analysis.Confidence; var quartiles = analysis.Quartiles; var innerFence = analysis.InnerFences; var outerFence = analysis.OuterFences; Assert.AreEqual(1, columnNames.Length); Assert.AreEqual(1, correlation.Length); Assert.AreEqual(1, covariance.Length); Assert.AreEqual(i, deviationScores.Length); Assert.AreEqual(1, distinct.Length); Assert.AreEqual(1, kurtosis.Length); Assert.AreEqual(1, means.Length); Assert.AreEqual(1, medians.Length); Assert.AreEqual(1, modes.Length); Assert.AreEqual(1, ranges.Length); Assert.AreEqual(i, samples); Assert.AreEqual(1, skewness.Length); Assert.AreEqual(i, source.Length); Assert.AreEqual(1, standardDeviation.Length); Assert.AreEqual(1, standardErrors.Length); Assert.AreEqual(i, standardScores.Length); Assert.AreEqual(1, sums.Length); Assert.AreEqual(1, variables); Assert.AreEqual(1, variances.Length); Assert.AreEqual(1, confidence.Length); Assert.AreEqual(1, innerFence.Length); Assert.AreEqual(1, outerFence.Length); Assert.AreEqual(1, quartiles.Length); } }
private static void test(DescriptiveAnalysis analysis) { var columnNames = analysis.ColumnNames; var correlation = analysis.CorrelationMatrix; var covariance = analysis.CovarianceMatrix; var deviationScores = analysis.DeviationScores; var distinct = analysis.Distinct; var kurtosis = analysis.Kurtosis; var means = analysis.Means; var medians = analysis.Medians; var modes = analysis.Modes; var ranges = analysis.Ranges; var samples = analysis.Samples; var skewness = analysis.Skewness; var source = analysis.Source; var standardDeviation = analysis.StandardDeviations; var standardErrors = analysis.StandardErrors; var standardScores = analysis.StandardScores; var sums = analysis.Sums; var variables = analysis.Variables; var variances = analysis.Variances; Assert.IsTrue(columnNames.IsEqual(new string[] { "Column 0", "Column 1", "Column 2", })); Assert.IsTrue(correlation.IsEqual(new double[,] { { 1.00000000000000000, -0.24074447786944278, 0 }, { -0.24074447786944278, 1.00000000000000000, 0 }, { 0.00000000000000000, 0.00000000000000000, 0 } })); Assert.IsTrue(covariance.IsEqual(new double[,] { { 0.3, -3.1, 0.0 }, { -3.1, 552.7, 0.0 }, { 0.0, 0.0, 0.0 } })); Assert.IsTrue(deviationScores.IsEqual(new double[,] { { -0.39999999999999991, 8.7999999999999972, 0.0 }, { 0.60000000000000009, -31.200000000000003, 0.0 }, { -0.39999999999999991, 21.799999999999997, 0.0 }, { -0.39999999999999991, -18.200000000000003, 0.0 }, { 0.60000000000000009, 18.799999999999997, 0.0 } })); Assert.IsTrue(distinct.IsEqual(new int[] { 2, 5, 1 })); Assert.IsTrue(kurtosis.IsEqual(new double[] { -3.3333333333333321, -2.213664721197441, double.NaN })); Assert.IsTrue(means.IsEqual(new double[] { 1.4, 43.2, 5.0 })); Assert.IsTrue(medians.IsEqual(new double[] { 1.0, 52.0, 5.0 })); Assert.IsTrue(modes.IsEqual(new double[] { 1.0, 52.0, 5.0 })); Assert.IsTrue(ranges.Apply(p => p.Min).IsEqual(new double[] { 1, 12, 5 })); Assert.IsTrue(ranges.Apply(p => p.Max).IsEqual(new double[] { 2, 65, 5 })); Assert.IsTrue(samples == 5); Assert.IsTrue(skewness.IsEqual(new double[] { 0.60858061945018527, -0.60008123614710385, double.NaN })); Assert.IsTrue(source.IsEqual(new double[,] { { 1, 52, 5 }, { 2, 12, 5 }, { 1, 65, 5 }, { 1, 25, 5 }, { 2, 62, 5 }, })); Assert.IsTrue(standardDeviation.IsEqual(new double[] { 0.54772255750516607, 23.509572518444482, 0.0 })); Assert.IsTrue(standardErrors.IsEqual(new double[] { 0.24494897427831777, 10.513800454640558, 0.0 })); Assert.IsTrue(standardScores.IsEqual(new double[,] { { -0.73029674334022132, 0.37431561093235277, 0.0 }, { 1.0954451150103324, -1.3271189842147058, 0.0 }, { -0.73029674334022132, 0.92728185435514676, 0.0 }, { -0.73029674334022132, -0.77415274079191176, 0.0 }, { 1.0954451150103324, 0.79967425971911732, 0.0 }, })); Assert.IsTrue(sums.IsEqual(new double[] { 7, 216.0, 25 })); Assert.IsTrue(variables == 3); Assert.IsTrue(variances.IsEqual(new double[] { 0.3, 552.7, 0.0 })); }
private void btnGenerateInputData_Click(object sender, EventArgs e) { //if (this.strProjectPath.Trim() == "") // return; if (this.cmbBoundary.Text.Trim() == "") { MessageBox.Show("请添加边界数据!","提示!",MessageBoxButtons.OK); return; } if (this.cmbRstraint.Text.Trim() == "") { MessageBox.Show("请添加限制区域数据!", "提示!", MessageBoxButtons.OK); return; } //if (this.txtParameter.Text.Trim() == "") // return; if (lsbLayerLandUse.Items.Count == 0) return; //if (lsbLayerDriverFactor.Items.Count == 0) // return; int iLanduseType = 6; //ILayer pLayer = null; IRaster pRaster = new RasterClass(); IGeoDataset pGdsMask = null; IGeoDataset pGdsRstraint = null; IGeoDataset pGdsLanduse = null; IGeoDataset pGdsDriverFactor = null; IRasterBandCollection pRasterBandColection=(new RasterClass()) as IRasterBandCollection; //读取 double cellSize = 1000; //try //{ string sLyrMask = this.cmbBoundary.Text; //boundaty-->mask for (int i = 0; i < pMap.LayerCount; i++) { ILayer pLyr = pMap.get_Layer(i); if (pLyr is IRasterLayer) { if (pLyr.Name == sLyrMask) { IRasterProps pRasterP = (pLyr as IRasterLayer).Raster as IRasterProps; cellSize = pRasterP.MeanCellSize().X; pGdsMask = (pLyr as IRasterLayer).Raster as IGeoDataset; } } } //data //限制区 string sLyrRstraint = this.cmbRstraint.Text; //土地利用数据 string sLyrLanduse = this.lsbLayerLandUse.Items[0].ToString(); // 土地利用数据与驱动因子 数据名称(去除格式名) // 顺序不能改变 后面 ITable2DTable 使用这个列表进行了名称替换 lsbNames.Add(sLyrLanduse.Remove(sLyrLanduse.LastIndexOf("."))); foreach (string name in lsbLayerDriverFactor.Items) { lsbNames.Add(name.Remove(name.LastIndexOf("."))); //去除文件格式 } //驱动因子 //string[] arr = new string[this.lsbLayerDriverFactor.Items.Count]; //for (int i = 0; i < this.lsbLayerDriverFactor.Items.Count; i++) //{ // arr[i]=this.lsbLayerDriverFactor.Items[i].ToString(); //} for (int i = 0; i < pMap.LayerCount; i++) { ILayer pLyr = pMap.get_Layer(i); if (pLyr is IRasterLayer) { //IRaster curRaster = new RasterClass(); if (pLyr.Name == sLyrRstraint) { //curRaster = (pLyr as IRasterLayer).Raster; //(pLyr as IRasterLayer).Raster. pGdsRstraint = (pLyr as IRasterLayer).Raster as IGeoDataset; //限制数据 region.grid string ascFileNameRstraint = strProjectPath + "\\region.grid"; Rater2Ascii(pGdsMask,cellSize , pGdsRstraint, ascFileNameRstraint); } //土地利用数据 if (pLyr.Name == sLyrLanduse) { this.rtxtState.AppendText("读取土地利用参数数据...\n"); this.rtxtState.ScrollToCaret(); //curRaster = (pLyr as IRasterLayer).Raster; pGdsLanduse = (pLyr as IRasterLayer).Raster as IGeoDataset; //land use 添加到 IRasterBandCollection IRasterBandCollection rasterbands = (IRasterBandCollection)(pLyr as IRasterLayer).Raster; IRasterBand rasterband = rasterbands.Item(0); pRasterBandColection.AppendBand(rasterband); //pRasterBandColection = curRaster as IRasterBandCollection; //pRasterBandColection.AppendBand(pRaster as IRasterBand); string ascFileNameLanduse = strProjectPath + "\\cov1_all.0"; //cov1_0.0;cov1_1.0; Rater2Ascii(pGdsMask, cellSize, pGdsLanduse, ascFileNameLanduse); // //将土地利用数据拆分 StreamReader sr = new StreamReader(ascFileNameLanduse, System.Text.Encoding.Default); //try //{ //使用StreamReader类来读取文件 sr.BaseStream.Seek(0, SeekOrigin.Begin); // 从数据流中读取每一行,直到文件的最后一行,并在richTextBox1中显示出内容 //读取头文件 string[] header = new string[6]; for (int j = 0; j < 6; j++) { header[j] = sr.ReadLine(); } //行列数 string[] ncols = header[0].Split(' '); string[] nrows = header[1].Split(' '); int icol = int.Parse(ncols[ncols.Length - 1]); int irow = int.Parse(nrows[nrows.Length - 1]); int[,] iLanduse = new int[irow, icol]; // string strLine = sr.ReadLine(); string[] strData; int ir = 0; while (ir < irow) //while (strLine != null) { strData = strLine.Split(' '); for (int ic = 0; ic < icol; ic++) { iLanduse[ir, ic] = int.Parse(strData[ic]); } strLine = sr.ReadLine(); ir++; } //关闭此StreamReader对象 sr.Close(); //输出相应的土地利用数据 DataTable2Txt(header, iLanduseType, iLanduse, strProjectPath); //} //catch (Exception ex) //{ //MessageBox.Show(ex.Message); sr.Close(); //} } } } this.rtxtState.AppendText("输出土地利用参数数据成功。\n"); this.rtxtState.AppendText("读取驱动因子数据...\n"); this.rtxtState.ScrollToCaret(); for (int ifac = 0; ifac < lsbLayerDriverFactor.Items.Count; ifac++) { for (int i = 0; i < pMap.LayerCount; i++) { ILayer pLyr = pMap.get_Layer(i); if (pLyr is IRasterLayer) { //输出驱动因子数据 sc1gr0.grid string sFacName = lsbLayerDriverFactor.Items[ifac].ToString(); if (pLyr.Name == sFacName) { //IRaster curRaster = new RasterClass(); //curRaster = (pLyr as IRasterLayer).Raster; pGdsDriverFactor = (pLyr as IRasterLayer).Raster as IGeoDataset; string ascFileNameFac = strProjectPath + "\\sc1gr"+ifac.ToString()+".grid"; //cov1_0.0;cov1_1.0; this.rtxtState.AppendText("输出驱动因子数据【" + sFacName + "】\n"); //IGeoDataset curMask = null; //curMask = pGdsMask; Rater2Ascii(pGdsMask, cellSize, pGdsDriverFactor, ascFileNameFac); this.rtxtState.AppendText("输出驱动因子数据【" + sFacName + "】成功。\n"); this.rtxtState.ScrollToCaret(); //mask 添加到 IRasterBandCollection IRasterBandCollection rasterbands = (IRasterBandCollection)(pLyr as IRasterLayer).Raster; IRasterBand rasterband = rasterbands.Item(0); pRasterBandColection.AppendBand(rasterband); //pRasterBandColection.Add(pRaster as IRasterBand,ifac+1); } } } } this.rtxtState.AppendText("开始制备驱动因子参数...\n"); this.rtxtState.ScrollToCaret(); //IGeoDataset curtestGeo = null; ////boundaty-->mask //for (int i = 0; i < pMap.LayerCount; i++) //{ // ILayer pLyr = pMap.get_Layer(i); // if (pLyr is IRasterLayer) // { // if (pLyr.Name == sLyrMask) // { // curtestGeo = ((pLyr as IRasterLayer).Raster) as IGeoDataset; // } // } //} //sample data ITable itFactors = ExportSample(pGdsMask, pRasterBandColection); //logistic 回归分析 //lsbNames.AddRange(names); DataTable dtFactors = ITable2DTable(itFactors); itFactors = null; //MessageBox.Show(dtFactors.Columns[0].ColumnName + ";" + dtFactors.Columns[1].ColumnName + ";" + dtFactors.Columns[2].ColumnName); //制备驱动力参数文件 this.rtxtState.AppendText("读取驱动因子数据表格数据...\n"); this.rtxtState.ScrollToCaret(); LogisticRegressionAnalysis lra; // Gets the columns of the independent variables List<string> names = new List<string>(); foreach (string name in lsbLayerDriverFactor.Items) { names.Add(name.Remove(name.LastIndexOf("."))); //去除文件格式 } String[] independentNames = names.ToArray(); DataTable independent = dtFactors.DefaultView.ToTable(false, independentNames); // Creates the input and output matrices from the source data table double[][] input = independent.ToArray(); double[,] sourceMatrix = dtFactors.ToMatrix(independentNames); StreamWriter sw = new StreamWriter(strProjectPath + "\\alloc1.reg", false); for(int ild=1; ild< iLanduseType; ild++) { String landuseName = (string)this.lsbLayerLandUse.Items[0].ToString(); this.rtxtState.AppendText("开始制备土地利用类型【" + ild.ToString() + "】驱动因子参数...\n"); this.rtxtState.ScrollToCaret(); DataColumn taxColumn =new DataColumn(); taxColumn.DataType = System.Type.GetType("System.Int32"); taxColumn.ColumnName ="sysland"+ild.ToString();//列名 taxColumn.Expression = "iif("+lsbNames[0]+" = "+ild.ToString()+",1,0)";//设置该列得表达式,用于计算列中的值或创建聚合列 dtFactors.Columns.Add(taxColumn); string dependentName = "sysland" + ild.ToString(); DataTable dependent = dtFactors.DefaultView.ToTable(false, dependentName); double[] output = dependent.Columns[dependentName].ToArray(); // Creates the Simple Descriptive Analysis of the given source DescriptiveAnalysis sda = new DescriptiveAnalysis(sourceMatrix, independentNames); sda.Compute(); // Populates statistics overview tab with analysis data //dgvDistributionMeasures.DataSource = sda.Measures; // Creates the Logistic Regression Analysis of the given source lra = new LogisticRegressionAnalysis(input, output, independentNames, dependentName); // Compute the Logistic Regression Analysis lra.Compute(); // Populates coefficient overview with analysis data //lra.Coefficients; //MessageBox.Show(lra.Coefficients.Count.ToString()); //MessageBox.Show(lra.CoefficientValues[0].ToString()); //string str_check = listBox_deVar.Items[var_count].ToString().ToLower(); string st = ild.ToString(); int Relength = lra.CoefficientValues.Length; int number = 0; for (int i = 1; i < Relength; i++) { //if (< 0.05) //{ number++; //} } RegressionResult.Items.Add(st); RegressionResult.Items.Add("\t" + Math.Round(lra.CoefficientValues[1], 6)); RegressionResult.Items.Add(number); int var_number = 0; for (int i = 0; i < Relength; i++)//改过了0=1 { //if ( < 0.05) //{ RegressionResult.Items.Add("\t" + Math.Round(lra.CoefficientValues[i], 6) + "\t" + var_number); //} var_number = var_number + 1; } // 保存alloc1.reg 文件 sw.WriteLine(st); sw.WriteLine("\t" + Math.Round(lra.CoefficientValues[1], 6));//改过了1=0 sw.WriteLine(number); int var_number2 = 0; for (int i = 0; i < Relength; i++)//改过了0=1 { //if ( < 0.05) //{ sw.WriteLine("\t" + Math.Round(lra.CoefficientValues[i],6) + "\t" + var_number2); //} var_number2 = var_number2 + 1; } //progressBar1.Value = var_count + 1; } sw.Close(); this.rtxtState.AppendText("制备驱动因子参数成功。\n"); this.rtxtState.ScrollToCaret(); MessageBox.Show("制备驱动因子参数成功!","提示",MessageBoxButtons.OK); //} //catch (Exception ex) //{ // MessageBox.Show(ex.Message); //} }
private void btnSampleRunAnalysis_Click(object sender, EventArgs e) { // Check requirements if (sourceTable == null) { MessageBox.Show("A sample spreadsheet can be found in the " + "Resources folder in the same directory as this application.", "Please load some data before attempting an analysis"); return; } if (checkedListBox1.CheckedItems.Count == 0) { MessageBox.Show("Please select the dependent input variables to be used in the regression model.", "Please choose at least one input variable"); } // Finishes and save any pending changes to the given data dgvAnalysisSource.EndEdit(); sourceTable.AcceptChanges(); // Gets the column of the dependent variable String dependentName = (string)comboBox1.SelectedItem; DataTable dependent = sourceTable.DefaultView.ToTable(false, dependentName); // Gets the columns of the independent variables List <string> names = new List <string>(); foreach (string name in checkedListBox1.CheckedItems) { names.Add(name); } String[] independentNames = names.ToArray(); DataTable independent = sourceTable.DefaultView.ToTable(false, independentNames); // Creates the input and output matrices from the source data table double[][] input = independent.ToArray(); double[] output = dependent.Columns[dependentName].ToArray(); double[,] sourceMatrix = sourceTable.ToMatrix(independentNames); // Creates the Simple Descriptive Analysis of the given source var sda = new DescriptiveAnalysis(sourceMatrix, independentNames); sda.Compute(); // Populates statistics overview tab with analysis data dgvDistributionMeasures.DataSource = sda.Measures; // Creates the Logistic Regression Analysis of the given source lra = new LogisticRegressionAnalysis(input, output, independentNames, dependentName); // Compute the Logistic Regression Analysis lra.Compute(); // Populates coefficient overview with analysis data dgvLogisticCoefficients.DataSource = lra.Coefficients; // Populate details about the fitted model tbChiSquare.Text = lra.ChiSquare.Statistic.ToString("N5"); tbPValue.Text = lra.ChiSquare.PValue.ToString("N5"); checkBox1.Checked = lra.ChiSquare.Significant; tbDeviance.Text = lra.Deviance.ToString("N5"); tbLogLikelihood.Text = lra.LogLikelihood.ToString("N5"); // Create the Multiple Linear Regression Analysis of the given source mlr = new MultipleLinearRegressionAnalysis(input, output, independentNames, dependentName, true); // Compute the Linear Regression Analysis mlr.Compute(); dgvLinearCoefficients.DataSource = mlr.Coefficients; dgvRegressionAnova.DataSource = mlr.Table; tbRSquared.Text = mlr.RSquared.ToString("N5"); tbRSquaredAdj.Text = mlr.RSquareAdjusted.ToString("N5"); tbChiPValue.Text = mlr.ChiSquareTest.PValue.ToString("N5"); tbFPValue.Text = mlr.FTest.PValue.ToString("N5"); tbZPValue.Text = mlr.ZTest.PValue.ToString("N5"); tbChiStatistic.Text = mlr.ChiSquareTest.Statistic.ToString("N5"); tbFStatistic.Text = mlr.FTest.Statistic.ToString("N5"); tbZStatistic.Text = mlr.ZTest.Statistic.ToString("N5"); cbChiSignificant.Checked = mlr.ChiSquareTest.Significant; cbFSignificant.Checked = mlr.FTest.Significant; cbZSignificant.Checked = mlr.ZTest.Significant; // Populate projection source table string[] cols = independentNames; if (!independentNames.Contains(dependentName)) { cols = independentNames.Concatenate(dependentName); } DataTable projSource = sourceTable.DefaultView.ToTable(false, cols); dgvProjectionSource.DataSource = projSource; }
public void DescriptiveAnalysisConstructorTest3() { // Suppose we would like to compute descriptive // statistics from the following data samples: double[] data = { 52, 12, 65, 25, 62 }; // Create the analysis DescriptiveAnalysis analysis = new DescriptiveAnalysis(data); // Compute analysis.Compute(); var columnNames = analysis.ColumnNames; var correlation = analysis.CorrelationMatrix; var covariance = analysis.CovarianceMatrix; var deviationScores = analysis.DeviationScores; var distinct = analysis.Distinct; var kurtosis = analysis.Kurtosis; var means = analysis.Means; var medians = analysis.Medians; var modes = analysis.Modes; var ranges = analysis.Ranges; var samples = analysis.Samples; var skewness = analysis.Skewness; var source = analysis.Source; var standardDeviation = analysis.StandardDeviations; var standardErrors = analysis.StandardErrors; var standardScores = analysis.StandardScores; var sums = analysis.Sums; var variables = analysis.Variables; var variances = analysis.Variances; var confidence = analysis.Confidence; Assert.IsTrue(columnNames.IsEqual(new string[] { "Column 0", })); Assert.IsTrue(correlation.IsEqual(new double[,] { { 1 } })); Assert.IsTrue(covariance.IsEqual(new double[,] { { 552.7 } })); Assert.IsTrue(deviationScores.IsEqual(new double[,] { { 8.7999999999999972 }, { -31.200000000000003 }, { 21.799999999999997 }, { -18.200000000000003 }, { 18.799999999999997 } })); Assert.IsTrue(distinct.IsEqual(new int[] { 5 })); Assert.IsTrue(kurtosis.IsEqual(new double[] { -2.213664721197441 })); Assert.IsTrue(means.IsEqual(new double[] { 43.2 })); Assert.IsTrue(medians.IsEqual(new double[] { 52.0 })); Assert.IsTrue(modes.IsEqual(new double[] { 52.0 })); Assert.IsTrue(ranges.Apply(p => p.Min).IsEqual(new double[] { 12 })); Assert.IsTrue(ranges.Apply(p => p.Max).IsEqual(new double[] { 65 })); Assert.IsTrue(samples == 5); Assert.IsTrue(skewness.IsEqual(new double[] { -0.60008123614710385 })); Assert.IsTrue(source.IsEqual(new double[,] { { 52 }, { 12 }, { 65 }, { 25 }, { 62 }, })); Assert.IsTrue(standardDeviation.IsEqual(new double[] { 23.509572518444482 })); Assert.IsTrue(standardErrors.IsEqual(new double[] { 10.513800454640558 })); Assert.IsTrue(standardScores.IsEqual(new double[,] { { 0.37431561093235277 }, { -1.3271189842147058 }, { 0.92728185435514676 }, { -0.77415274079191176 }, { 0.79967425971911732 }, })); Assert.IsTrue(sums.IsEqual(new double[] { 216.0 })); Assert.IsTrue(variables == 1); Assert.IsTrue(variances.IsEqual(new double[] { 552.7 })); Assert.AreEqual(1, confidence.Length); Assert.AreEqual(22.593329768263665, confidence[0].Min); Assert.AreEqual(63.806670231736341, confidence[0].Max); }
private void btnSampleRunAnalysis_Click(object sender, EventArgs e) { // Check requirements if (sourceTable == null) { MessageBox.Show("A sample spreadsheet can be found in the " + "Resources folder in the same directory as this application.", "Please load some data before attempting an analysis"); return; } // Finishes and save any pending changes to the given data dgvAnalysisSource.EndEdit(); sourceTable.AcceptChanges(); // Gets the column of the dependent variable String dependentName = (string)cbTimeName.SelectedItem; String censorName = (string)cbEventName.SelectedItem; DataTable timeTable = sourceTable.DefaultView.ToTable(false, dependentName); DataTable censorTable = sourceTable.DefaultView.ToTable(false, censorName); // Gets the columns of the independent variables List <string> names = new List <string>(); foreach (string name in checkedListBox1.CheckedItems) { names.Add(name); } String[] independentNames = names.ToArray(); // Creates the input and output matrices from the source data table this.time = timeTable.Columns[dependentName].ToArray(); this.censor = censorTable.Columns[censorName].ToArray().ToInt32(); if (independentNames.Length == 0) { this.inputs = Jagged.Zeros(time.Length, 0); } else { DataTable independent = sourceTable.DefaultView.ToTable(false, independentNames); this.inputs = independent.ToJagged(); } String[] sourceColumns; this.sourceMatrix = sourceTable.ToJagged(out sourceColumns); // Creates the Simple Descriptive Analysis of the given source var sda = new DescriptiveAnalysis(sourceColumns).Learn(sourceMatrix); // Populates statistics overview tab with analysis data dgvDistributionMeasures.DataSource = sda.Measures; // Creates the Logistic Regression Analysis of the given source pha = new ProportionalHazardsAnalysis(independentNames, dependentName, censorName); // Compute the Logistic Regression Analysis ProportionalHazards model = pha.Learn(inputs, time, censor); // Populates coefficient overview with analysis data dgvLogisticCoefficients.DataSource = pha.Coefficients; // Populate details about the fitted model tbChiSquare.Text = pha.ChiSquare.Statistic.ToString("N5"); tbPValue.Text = pha.ChiSquare.PValue.ToString("N5"); checkBox1.Checked = pha.ChiSquare.Significant; tbDeviance.Text = pha.Deviance.ToString("N5"); tbLogLikelihood.Text = pha.LogLikelihood.ToString("N5"); // Populate projection source table string[] cols = independentNames; if (!independentNames.Contains(dependentName)) { cols = cols.Concatenate(dependentName); } if (!independentNames.Contains(censorName)) { cols = cols.Concatenate(censorName); } DataTable projSource = sourceTable.DefaultView.ToTable(false, cols); dgvProjectionSource.DataSource = projSource; }
public void DescriptiveAnalysisOneSampleTest() { double[] data = { 52 }; // Create the analysis DescriptiveAnalysis analysis = new DescriptiveAnalysis(data); // Compute analysis.Compute(); var columnNames = analysis.ColumnNames; var correlation = analysis.CorrelationMatrix; var covariance = analysis.CovarianceMatrix; var deviationScores = analysis.DeviationScores; var distinct = analysis.Distinct; var kurtosis = analysis.Kurtosis; var means = analysis.Means; var medians = analysis.Medians; var modes = analysis.Modes; var ranges = analysis.Ranges; var samples = analysis.Samples; var skewness = analysis.Skewness; var source = analysis.Source; var standardDeviation = analysis.StandardDeviations; var standardErrors = analysis.StandardErrors; var standardScores = analysis.StandardScores; var sums = analysis.Sums; var variables = analysis.Variables; var variances = analysis.Variances; var confidence = analysis.Confidence; var quartiles = analysis.Quartiles; var innerFence = analysis.InnerFences; var outerFence = analysis.OuterFences; Assert.IsTrue(columnNames.IsEqual(new string[] { "Column 0", })); Assert.IsTrue(correlation.IsEqual(new double[,] { { Double.NaN } })); Assert.IsTrue(covariance.IsEqual(new double[,] { { Double.NaN } })); Assert.IsTrue(deviationScores.IsEqual(0)); Assert.IsTrue(distinct.IsEqual(new int[] { 1 })); Assert.IsTrue(kurtosis.IsEqual(new double[] { Double.NaN })); Assert.IsTrue(means.IsEqual(new double[] { 52 })); Assert.IsTrue(medians.IsEqual(new double[] { 52.0 })); Assert.IsTrue(modes.IsEqual(new double[] { 52.0 })); Assert.IsTrue(samples == 1); Assert.IsTrue(skewness.IsEqual(new double[] { Double.NaN })); Assert.IsTrue(source.IsEqual(new double[,] { { 52 }, })); Assert.IsTrue(standardDeviation.IsEqual(new double[] { double.NaN })); Assert.IsTrue(standardErrors.IsEqual(new double[] { double.NaN })); Assert.IsTrue(standardScores.IsEqual(new double[,] { { 0.0 }, })); Assert.IsTrue(sums.IsEqual(new double[] { 52 })); Assert.IsTrue(variables == 1); Assert.IsTrue(variances.IsEqual(new double[] { double.NaN })); Assert.AreEqual(1, ranges.Length); Assert.AreEqual(52, ranges[0].Min); Assert.AreEqual(52, ranges[0].Max); Assert.AreEqual(1, confidence.Length); Assert.AreEqual(double.NaN, confidence[0].Min); Assert.AreEqual(double.NaN, confidence[0].Max); Assert.AreEqual(1, quartiles.Length); Assert.AreEqual(52, quartiles[0].Min); Assert.AreEqual(52, quartiles[0].Max); Assert.AreEqual(1, innerFence.Length); Assert.AreEqual(52, innerFence[0].Min); Assert.AreEqual(52, innerFence[0].Max); Assert.AreEqual(1, outerFence.Length); Assert.AreEqual(52, outerFence[0].Min); Assert.AreEqual(52, outerFence[0].Max); }
/// <summary> /// Launched when the user clicks the "Run analysis" button. /// </summary> /// private void btnCompute_Click(object sender, EventArgs e) { // Save any pending changes dgvAnalysisSource.EndEdit(); if (dgvAnalysisSource.DataSource == null) { MessageBox.Show("Please load some data using File > Open!"); return; } // Creates a matrix from the source data table double[,] sourceMatrix = (dgvAnalysisSource.DataSource as DataTable).ToMatrix(out columnNames); // Create and compute a new Simple Descriptive Analysis sda = new DescriptiveAnalysis(sourceMatrix, columnNames); sda.Compute(); // Show the descriptive analysis on the screen dgvDistributionMeasures.DataSource = sda.Measures; // Create the kernel function IKernel kernel = createKernel(); // Get the input values (the two first columns) double[,] inputs = sourceMatrix.GetColumns(0, 1); // Get only the associated labels (last column) int[] outputs = sourceMatrix.GetColumn(2).ToInt32(); // Creates the Kernel Discriminant Analysis of the given data kda = new KernelDiscriminantAnalysis(inputs, outputs, kernel); // Keep only the important components kda.Threshold = (double)numThreshold.Value; kda.Compute(); // Finally, compute the analysis! if (kda.Discriminants.Count < 2) { MessageBox.Show("Could not gather enough components to create" + "create a 2D plot. Please try a smaller threshold value."); return; } // Perform the transformation of the data using two components double[,] result = kda.Transform(inputs, 2); // Create a new plot with the original Z column double[,] points = result.InsertColumn(sourceMatrix.GetColumn(2)); // Create output scatter plot outputScatterplot.DataSource = points; createMappingScatterplot(graphMapFeature, points); // Create output table dgvProjectionResult.DataSource = new ArrayDataView(points, columnNames); // Populates components overview with analysis data dgvFeatureVectors.DataSource = new ArrayDataView(kda.DiscriminantMatrix); dgvScatterBetween.DataSource = new ArrayDataView(kda.ScatterBetweenClass); dgvScatterWithin.DataSource = new ArrayDataView(kda.ScatterWithinClass); dgvScatterTotal.DataSource = new ArrayDataView(kda.ScatterMatrix); dgvPrincipalComponents.DataSource = kda.Discriminants; distributionView.DataSource = kda.Discriminants; cumulativeView.DataSource = kda.Discriminants; // Populates classes information dgvClasses.DataSource = kda.Classes; lbStatus.Text = "Good! Feel free to browse the other tabs to see what has been found."; }
/// <summary> /// Launched when the user clicks the "Run analysis" button. /// </summary> /// private void btnCompute_Click(object sender, EventArgs e) { // Save any pending changes dgvAnalysisSource.EndEdit(); if (dgvAnalysisSource.DataSource == null) { MessageBox.Show("Please load some data using File > Open!"); return; } // Create a matrix from the source data table double[,] sourceMatrix = (dgvAnalysisSource.DataSource as DataTable).ToMatrix(out columnNames); int rows = sourceMatrix.GetLength(0); int cols = sourceMatrix.GetLength(1); // Create and compute a new Simple Descriptive Analysis sda = new DescriptiveAnalysis(sourceMatrix, columnNames); sda.Compute(); // Show the descriptive analysis on the screen dgvDistributionMeasures.DataSource = sda.Measures; // Create the kernel function IKernel kernel = createKernel(); // Get the input values (the two first columns) double[,] inputs = sourceMatrix.GetColumns(0, 1); // Get only the associated labels (last column) int[] outputs = sourceMatrix.GetColumn(2).ToInt32(); AnalysisMethod method = (AnalysisMethod)cbMethod.SelectedValue; // Creates the Kernel Principal Component Analysis of the given source kpca = new KernelPrincipalComponentAnalysis(inputs, kernel, method); // Whether to center in space kpca.Center = cbCenter.Checked; kpca.Compute(); // Finally, compute the analysis! double[,] result; if (kpca.Components.Count >= 2) { // Perform the transformation of the data using two components result = kpca.Transform(inputs, 2); } else { result = kpca.Transform(inputs, 1); result = result.InsertColumn(Matrix.Vector(result.GetLength(0), 0.0)); } // Create a new plot with the original Z column double[,] points = result.InsertColumn(sourceMatrix.GetColumn(2)); // Create output scatter plot outputScatterplot.DataSource = points; CreateScatterplot(graphMapFeature, points); // Create output table dgvProjectionResult.DataSource = new ArrayDataView(points, columnNames); dgvReversionSource.DataSource = new ArrayDataView(kpca.Result); // Populates components overview with analysis data dgvFeatureVectors.DataSource = new ArrayDataView(kpca.ComponentMatrix); dgvPrincipalComponents.DataSource = kpca.Components; dgvProjectionComponents.DataSource = kpca.Components; dgvReversionComponents.DataSource = kpca.Components; cumulativeView.DataSource = kpca.Components; distributionView.DataSource = kpca.Components; numComponents.Maximum = kpca.Components.Count; numNeighbor.Maximum = kpca.Result.GetLength(0); numNeighbor.Value = System.Math.Min(10, numNeighbor.Maximum); lbStatus.Text = "Good! Feel free to browse the other tabs to see what has been found."; }
/// <summary> /// Launched when the user clicks the "Run analysis" button. /// </summary> /// private void btnCompute_Click(object sender, EventArgs e) { // Save any pending changes dgvAnalysisSource.EndEdit(); if (dgvAnalysisSource.DataSource == null) { MessageBox.Show("Please load some data using File > Open!"); return; } // Create a matrix from the source data table double[][] sourceMatrix = (dgvAnalysisSource.DataSource as DataTable).ToJagged(out columnNames); // Create and compute a new Simple Descriptive Analysis sda = new DescriptiveAnalysis() { ColumnNames = columnNames }; sda.Learn(sourceMatrix); // Show the descriptive analysis on the screen dgvDistributionMeasures.DataSource = sda.Measures; // Create the kernel function IKernel kernel = createKernel(); // Get the input values (the two first columns) this.inputs = sourceMatrix.GetColumns(0, 1); // Get only the associated labels (last column) this.outputs = sourceMatrix.GetColumn(2).ToMulticlass(); var method = (PrincipalComponentMethod)cbMethod.SelectedValue; // Creates the Kernel Principal Component Analysis of the given source kpca = new KernelPrincipalComponentAnalysis() { Kernel = kernel, Method = method }; // Whether to center in space kpca.Center = cbCenter.Checked; var classifier = kpca.Learn(inputs); // Finally, compute the analysis! double[][] result = kpca.Transform(inputs); double[][] reduced; if (kpca.Components.Count >= 2) { // Perform the transformation of the data using two components kpca.NumberOfOutputs = 2; reduced = kpca.Transform(inputs); } else { kpca.NumberOfOutputs = 1; reduced = kpca.Transform(inputs); reduced = reduced.InsertColumn(Vector.Zeros(reduced.GetLength(0))); } // Create a new plot with the original Z column double[][] points = reduced.InsertColumn(sourceMatrix.GetColumn(2)); // Create output scatter plot outputScatterplot.DataSource = points; CreateScatterplot(graphMapFeature, points); // Create output table dgvProjectionResult.DataSource = new ArrayDataView(points, columnNames); dgvReversionSource.DataSource = new ArrayDataView(result); // Populates components overview with analysis data dgvFeatureVectors.DataSource = new ArrayDataView(kpca.ComponentVectors); dgvPrincipalComponents.DataSource = kpca.Components; cumulativeView.DataSource = kpca.Components; distributionView.DataSource = kpca.Components; numNeighbor.Maximum = result.Rows(); numNeighbor.Value = System.Math.Min(10, numNeighbor.Maximum); lbStatus.Text = "Good! Feel free to browse the other tabs to see what has been found."; }
private void button1_Click(object sender, EventArgs e) { // Finishes and save any pending changes to the given data dgvAnalysisSource.EndEdit(); if (dgvAnalysisSource.DataSource == null) return; // Creates a matrix from the source data table double[,] sourceMatrix = (dgvAnalysisSource.DataSource as DataTable).ToMatrix(out sourceColumns); int rows = sourceMatrix.GetLength(0); int cols = sourceMatrix.GetLength(1); // Creates a new Simple Descriptive Analysis sda = new DescriptiveAnalysis(sourceMatrix, sourceColumns); sda.Compute(); // Populates statistics overview tab with analysis data dgvDistributionMeasures.DataSource = sda.Measures; IKernel kernel; if (rbGaussian.Checked) { kernel = new Gaussian((double)numSigma.Value); } else { kernel = new Polynomial((int)numDegree.Value, (double)numConstant.Value); } // Get only the input values (exclude the class label indicator column) double[,] data = sourceMatrix.Submatrix(null, startColumn: 0, endColumn: 1); // Get only the associated labels int[] labels = sourceMatrix.GetColumn(2).ToInt32(); // Creates the Kernel Principal Component Analysis of the given source kpca = new KernelPrincipalComponentAnalysis(data, kernel, (AnalysisMethod)cbMethod.SelectedValue); kpca.Center = cbCenter.Checked; // Compute the analysis kpca.Compute(); double[,] result; if (kpca.Components.Count >= 2) { // Perform the transformation of the data using two components result = kpca.Transform(data, 2); } else { result = kpca.Transform(data, 1); result = result.InsertColumn(Matrix.Vector(result.GetLength(0), 0.0)); } // Create a new plot with the original Z column double[,] points = result.InsertColumn(sourceMatrix.GetColumn(2)); // Create output scatter plot outputScatterplot.DataSource = points; CreateScatterplot(graphMapFeature, points); // Create output table dgvProjectionResult.DataSource = new ArrayDataView(points, sourceColumns); dgvReversionSource.DataSource = new ArrayDataView(kpca.Result); // Populates components overview with analysis data dgvFeatureVectors.DataSource = new ArrayDataView(kpca.ComponentMatrix); dgvPrincipalComponents.DataSource = kpca.Components; dgvProjectionComponents.DataSource = kpca.Components; dgvReversionComponents.DataSource = kpca.Components; numComponents.Maximum = kpca.Components.Count; numNeighbor.Maximum = kpca.Result.GetLength(0); numNeighbor.Value = System.Math.Min(10, numNeighbor.Maximum); CreateComponentCumulativeDistributionGraph(graphCurve); CreateComponentDistributionGraph(graphShare); }
private static void test(DescriptiveAnalysis analysis) { var columnNames = analysis.ColumnNames; var correlation = analysis.CorrelationMatrix; var covariance = analysis.CovarianceMatrix; var deviationScores = analysis.DeviationScores; var distinct = analysis.Distinct; var kurtosis = analysis.Kurtosis; var means = analysis.Means; var medians = analysis.Medians; var modes = analysis.Modes; var ranges = analysis.Ranges; var samples = analysis.Samples; var skewness = analysis.Skewness; var source = analysis.Source; var standardDeviation = analysis.StandardDeviations; var standardErrors = analysis.StandardErrors; var standardScores = analysis.StandardScores; var sums = analysis.Sums; var variables = analysis.Variables; var variances = analysis.Variances; var quartiles = analysis.Quartiles; var inner = analysis.InnerFences; var outer = analysis.OuterFences; Assert.IsTrue(columnNames.IsEqual(new string[] { "Column 0", "Column 1", "Column 2", })); Assert.IsTrue(correlation.IsEqual(new double[, ] { { 1.00000000000000000, -0.24074447786944278, 0 }, { -0.24074447786944278, 1.00000000000000000, 0 }, { 0.00000000000000000, 0.00000000000000000, 0 } })); Assert.IsTrue(covariance.IsEqual(new double[, ] { { 0.3, -3.1, 0.0 }, { -3.1, 552.7, 0.0 }, { 0.0, 0.0, 0.0 } })); Assert.IsTrue(deviationScores.IsEqual(new double[, ] { { -0.39999999999999991, 8.7999999999999972, 0.0 }, { 0.60000000000000009, -31.200000000000003, 0.0 }, { -0.39999999999999991, 21.799999999999997, 0.0 }, { -0.39999999999999991, -18.200000000000003, 0.0 }, { 0.60000000000000009, 18.799999999999997, 0.0 } })); Assert.IsTrue(distinct.IsEqual(new int[] { 2, 5, 1 })); Assert.IsTrue(kurtosis.IsEqual(new double[] { -3.3333333333333321, -2.213664721197441, double.NaN })); Assert.IsTrue(means.IsEqual(new double[] { 1.4, 43.2, 5.0 })); Assert.IsTrue(medians.IsEqual(new double[] { 1.0, 52.0, 5.0 })); Assert.IsTrue(modes.IsEqual(new double[] { 1.0, 12.0, 5.0 })); Assert.IsTrue(ranges.Apply(p => p.Min).IsEqual(new double[] { 1, 12, 5 })); Assert.IsTrue(ranges.Apply(p => p.Max).IsEqual(new double[] { 2, 65, 5 })); Assert.IsTrue(samples == 5); Assert.IsTrue(skewness.IsEqual(new double[] { 0.60858061945018527, -0.60008123614710385, double.NaN })); Assert.IsTrue(source.IsEqual(new double[, ] { { 1, 52, 5 }, { 2, 12, 5 }, { 1, 65, 5 }, { 1, 25, 5 }, { 2, 62, 5 }, })); Assert.IsTrue(standardDeviation.IsEqual(new double[] { 0.54772255750516607, 23.509572518444482, 0.0 })); Assert.IsTrue(standardErrors.IsEqual(new double[] { 0.24494897427831777, 10.513800454640558, 0.0 })); Assert.IsTrue(standardScores.IsEqual(new double[, ] { { -0.73029674334022132, 0.37431561093235277, 0.0 }, { 1.0954451150103324, -1.3271189842147058, 0.0 }, { -0.73029674334022132, 0.92728185435514676, 0.0 }, { -0.73029674334022132, -0.77415274079191176, 0.0 }, { 1.0954451150103324, 0.79967425971911732, 0.0 }, })); Assert.IsTrue(sums.IsEqual(new double[] { 7, 216.0, 25 })); Assert.IsTrue(variables == 3); Assert.IsTrue(variances.IsEqual(new double[] { 0.3, 552.7, 0.0 })); Assert.AreEqual(3, quartiles.Length); Assert.AreEqual(1, quartiles[0].Min); Assert.AreEqual(2, quartiles[0].Max); Assert.AreEqual(18.5, quartiles[1].Min); Assert.AreEqual(63.5, quartiles[1].Max); Assert.AreEqual(18.5 - 1.5 * (63.5 - 18.5), inner[1].Min); Assert.AreEqual(63.5 + 1.5 * (63.5 - 18.5), inner[1].Max); Assert.AreEqual(18.5 - 3 * (63.5 - 18.5), outer[1].Min); Assert.AreEqual(63.5 + 3 * (63.5 - 18.5), outer[1].Max); Assert.AreEqual(5, quartiles[2].Min); Assert.AreEqual(5, quartiles[2].Max); }
private void button1_Click(object sender, EventArgs e) { // Finishes and save any pending changes to the given data dgvAnalysisSource.EndEdit(); if (dgvAnalysisSource.DataSource == null) return; // Creates a matrix from the source data table double[,] sourceMatrix = (dgvAnalysisSource.DataSource as DataTable).ToMatrix(out sourceColumns); // Creates a new Simple Descriptive Analysis sda = new DescriptiveAnalysis(sourceMatrix, sourceColumns); sda.Compute(); dgvDistributionMeasures.DataSource = sda.Measures; // Populates statistics overview tab with analysis data dgvDistributionMeasures.DataSource = sda.Measures; IKernel kernel; if (rbGaussian.Checked) { kernel = new Gaussian((double)numSigma.Value); } else { kernel = new Polynomial((int)numDegree.Value, (double)numConstant.Value); } // Get only the input values (exclude the class label indicator column) double[,] data = sourceMatrix.Submatrix(null, startColumn: 0, endColumn: 1); // Get only the associated labels int[] labels = sourceMatrix.GetColumn(2).ToInt32(); // Creates the Kernel Discriminant Analysis of the given source kda = new KernelDiscriminantAnalysis(data, labels, kernel); // Keep all components kda.Threshold = (double)numThreshold.Value; // Computes the analysis kda.Compute(); // Perform the transformation of the data using two components double[,] result = kda.Transform(data, 2); // Create a new plot with the original Z column double[,] points = result.InsertColumn(sourceMatrix.GetColumn(2)); // Create output scatter plot outputScatterplot.DataSource = points; CreateScatterplot(graphMapFeature, points); // Create output table dgvProjectionResult.DataSource = new ArrayDataView(points, sourceColumns); // Populates components overview with analysis data dgvFeatureVectors.DataSource = new ArrayDataView(kda.DiscriminantMatrix); dgvPrincipalComponents.DataSource = kda.Discriminants; dgvScatterBetween.DataSource = new ArrayDataView(kda.ScatterBetweenClass); dgvScatterWithin.DataSource = new ArrayDataView(kda.ScatterWithinClass); dgvScatterTotal.DataSource = new ArrayDataView(kda.ScatterMatrix); // Populates classes information dgvClasses.DataSource = kda.Classes; CreateComponentCumulativeDistributionGraph(graphCurve); CreateComponentDistributionGraph(graphShare); }
public void DescriptiveAnalysisTwoSampleTest() { double[][] data = { new [] { 52.0 }, new [] { 42.0 } }; // Create the analysis DescriptiveAnalysis analysis = new DescriptiveAnalysis(data); // Compute analysis.Compute(); var columnNames = analysis.ColumnNames; var correlation = analysis.CorrelationMatrix; var covariance = analysis.CovarianceMatrix; var deviationScores = analysis.DeviationScores; var distinct = analysis.Distinct; var kurtosis = analysis.Kurtosis; var means = analysis.Means; var medians = analysis.Medians; var modes = analysis.Modes; var ranges = analysis.Ranges; var samples = analysis.Samples; var skewness = analysis.Skewness; var source = analysis.Source; var standardDeviation = analysis.StandardDeviations; var standardErrors = analysis.StandardErrors; var standardScores = analysis.StandardScores; var sums = analysis.Sums; var variables = analysis.Variables; var variances = analysis.Variances; var confidence = analysis.Confidence; var quartiles = analysis.Quartiles; var innerFence = analysis.InnerFences; var outerFence = analysis.OuterFences; Assert.IsTrue(columnNames.IsEqual(new string[] { "Column 0" })); Assert.IsTrue(correlation.IsEqual(Matrix.Create(1, 1, 0.99999999999999978))); Assert.IsTrue(covariance.IsEqual(Matrix.Create(1, 1, 50.0))); Assert.IsTrue(deviationScores.IsEqual(new double[, ] { { 5 }, { -5 } })); Assert.IsTrue(distinct.IsEqual(new int[] { 2 })); Assert.IsTrue(kurtosis.IsEqual(new double[] { Double.NaN })); Assert.IsTrue(means.IsEqual(new double[] { 47 })); Assert.IsTrue(medians.IsEqual(new double[] { 94 })); Assert.IsTrue(modes.IsEqual(new double[] { 52.0 }) || modes.IsEqual(new double[] { 42.0 })); Assert.IsTrue(samples == 2); Assert.IsTrue(skewness.IsEqual(new double[] { Double.NaN })); Assert.IsTrue(source.IsEqual(new double[, ] { { 52 }, { 42 }, })); Assert.IsTrue(standardDeviation.IsEqual(new double[] { 7.0710678118654755 })); Assert.IsTrue(standardErrors.IsEqual(new double[] { 5.0 })); Assert.IsTrue(standardScores.IsEqual(new double[, ] { { 0.70710678118654746 }, { -0.70710678118654746 }, })); Assert.IsTrue(sums.IsEqual(new double[] { 94 })); Assert.IsTrue(variables == 1); Assert.IsTrue(variances.IsEqual(new double[] { 50 })); Assert.AreEqual(1, ranges.Length); Assert.AreEqual(42, ranges[0].Min); Assert.AreEqual(52, ranges[0].Max); Assert.AreEqual(1, confidence.Length); Assert.AreEqual(37.200180077299734, confidence[0].Min); Assert.AreEqual(56.799819922700266, confidence[0].Max); Assert.AreEqual(1, quartiles.Length); Assert.AreEqual(73, quartiles[0].Min); Assert.AreEqual(68, quartiles[0].Max); Assert.AreEqual(1, innerFence.Length); Assert.AreEqual(80.5, innerFence[0].Min); Assert.AreEqual(60.5, innerFence[0].Max); Assert.AreEqual(1, outerFence.Length); Assert.AreEqual(88, outerFence[0].Min); Assert.AreEqual(53, outerFence[0].Max); }
public void DescriptiveAnalysisConstructorTest() { double[,] data = Matrix.Magic(3); string[] columnNames = { "x", "y", "z" }; DescriptiveAnalysis target = new DescriptiveAnalysis(data, columnNames); target.Compute(); Assert.AreEqual("x", target.ColumnNames[0]); Assert.AreEqual("y", target.ColumnNames[1]); Assert.AreEqual("z", target.ColumnNames[2]); Assert.IsTrue(target.CorrelationMatrix.IsEqual(new double[,] { { 1.0000, -0.7559, 0.1429 }, { -0.7559, 1.0000, -0.7559 }, { 0.1429, -0.7559, 1.0000 }, }, 0.0001)); Assert.IsTrue(target.CovarianceMatrix.IsEqual(new double[,] { { 7, -8, 1 }, { -8, 16, -8 }, { 1, -8, 7 }, }, 0.00000001)); Assert.IsTrue(target.StandardScores.IsEqual(new double[,] { { 1.1339, -1.0000, 0.3780 }, { -0.7559, 0, 0.7559 }, { -0.3780, 1.0000, -1.1339 }, }, 0.001)); Assert.IsTrue(target.Means.IsEqual(new double[] { 5, 5, 5 })); Assert.IsTrue(target.StandardDeviations.IsEqual(new double[] { 2.6458, 4.0000, 2.6458 }, 0.001)); Assert.IsTrue(target.Medians.IsEqual(new double[] { 4, 5, 6 })); Assert.AreEqual(3, target.Ranges[0].Min); Assert.AreEqual(8, target.Ranges[0].Max); Assert.AreEqual(1, target.Ranges[1].Min); Assert.AreEqual(9, target.Ranges[1].Max); Assert.AreEqual(2, target.Ranges[2].Min); Assert.AreEqual(7, target.Ranges[2].Max); Assert.AreEqual(3, target.Samples); Assert.AreEqual(3, target.Variables); Assert.IsTrue(target.Source.IsEqual(Matrix.Magic(3))); Assert.IsTrue(target.Sums.IsEqual(new double[] { 15, 15, 15 })); Assert.IsTrue(target.Variances.IsEqual(new double[] { 7, 16, 7 })); }
public DistributionAnalysis(DescriptiveAnalysis descriptiveAnalysis) { }
/// <summary> /// Launched when the user clicks the "Run analysis" button. /// </summary> /// private void btnCompute_Click(object sender, EventArgs e) { // Save any pending changes dgvAnalysisSource.EndEdit(); if (dgvAnalysisSource.DataSource == null) { MessageBox.Show("Please load some data using File > Open!"); return; } // Create a matrix from the source data table double[][] sourceMatrix = (dgvAnalysisSource.DataSource as DataTable).ToArray(out columnNames); // Create and compute a new Simple Descriptive Analysis sda = new DescriptiveAnalysis(columnNames).Learn(sourceMatrix); // Show the descriptive analysis on the screen dgvDistributionMeasures.DataSource = sda.Measures; // Populates statistics overview tab with analysis data dgvStatisticCenter.DataSource = new ArrayDataView(sda.DeviationScores, columnNames); dgvStatisticStandard.DataSource = new ArrayDataView(sda.StandardScores, columnNames); dgvStatisticCovariance.DataSource = new ArrayDataView(sda.CovarianceMatrix, columnNames); dgvStatisticCorrelation.DataSource = new ArrayDataView(sda.CorrelationMatrix, columnNames); var method = (PrincipalComponentMethod)cbMethod.SelectedValue; // Create the Principal Component Analysis of the data pca = new PrincipalComponentAnalysis(method); pca.Learn(sourceMatrix); // Finally, compute the analysis! // Populate components overview with analysis data dgvFeatureVectors.DataSource = new ArrayDataView(pca.ComponentVectors); dgvPrincipalComponents.DataSource = pca.Components; dgvProjectionComponents.DataSource = pca.Components; distributionView.DataSource = pca.Components; cumulativeView.DataSource = pca.Components; numComponents.Maximum = pca.Components.Count; numComponents.Value = 1; numThreshold.Value = (decimal)pca.Components[0].CumulativeProportion * 100; }
private void btnRunAnalysis_Click(object sender, EventArgs e) { if (dgvAnalysisSource.DataSource == null) { MessageBox.Show("Please load some data first."); return; } // Finishes and save any pending changes to the given data dgvAnalysisSource.EndEdit(); DataTable table = dgvAnalysisSource.DataSource as DataTable; // Creates a matrix from the source data table double[,] sourceMatrix = table.ToMatrix(out inputColumnNames); // Creates the Simple Descriptive Analysis of the given source sda = new DescriptiveAnalysis(sourceMatrix, inputColumnNames); sda.Compute(); // Populates statistics overview tab with analysis data dgvDistributionMeasures.DataSource = sda.Measures; // Extract variables List<string> inputNames = new List<string>(); foreach (string name in clbInput.CheckedItems) inputNames.Add(name); this.inputColumnNames = inputNames.ToArray(); List<string> outputNames = new List<string>(); foreach (string name in clbOutput.CheckedItems) outputNames.Add(name); this.outputColumnNames = outputNames.ToArray(); DataTable inputTable = table.DefaultView.ToTable(false, inputColumnNames); DataTable outputTable = table.DefaultView.ToTable(false, outputColumnNames); double[,] inputs = inputTable.ToMatrix(); double[,] outputs = outputTable.ToMatrix(); // Creates the Partial Least Squares of the given source pls = new PartialLeastSquaresAnalysis(inputs, outputs, (AnalysisMethod)cbMethod.SelectedValue, (PartialLeastSquaresAlgorithm)cbAlgorithm.SelectedValue); // Computes the Partial Least Squares pls.Compute(); // Populates components overview with analysis data dgvWeightMatrix.DataSource = new ArrayDataView(pls.Weights); dgvFactors.DataSource = pls.Factors; dgvAnalysisLoadingsInput.DataSource = new ArrayDataView(pls.Predictors.FactorMatrix); dgvAnalysisLoadingsOutput.DataSource = new ArrayDataView(pls.Dependents.FactorMatrix); this.regression = pls.CreateRegression(); dgvRegressionCoefficients.DataSource = new ArrayDataView(regression.Coefficients, outputColumnNames); dgvRegressionIntercept.DataSource = new ArrayDataView(regression.Intercepts, outputColumnNames); dgvProjectionComponents.DataSource = pls.Factors; numComponents.Maximum = pls.Factors.Count; numComponents.Value = 1; dgvRegressionComponents.DataSource = pls.Factors; numComponentsRegression.Maximum = pls.Factors.Count; numComponentsRegression.Value = 1; CreateComponentCumulativeDistributionGraph(graphCurve); CreateComponentDistributionGraph(graphShare); dgvProjectionSourceX.DataSource = inputTable; dgvProjectionSourceY.DataSource = outputTable; dgvRegressionInput.DataSource = table.DefaultView.ToTable(false, inputColumnNames.Concatenate(outputColumnNames)); }
public void DescriptiveAnalysisConstructorTest3() { // Suppose we would like to compute descriptive // statistics from the following data samples: double[] data = { 52, 12, 65, 25, 62, 12 }; // Create the analysis DescriptiveAnalysis analysis = new DescriptiveAnalysis(data); // Compute analysis.Compute(); var columnNames = analysis.ColumnNames; var correlation = analysis.CorrelationMatrix; var covariance = analysis.CovarianceMatrix; var deviationScores = analysis.DeviationScores; var distinct = analysis.Distinct; var kurtosis = analysis.Kurtosis; var means = analysis.Means; var medians = analysis.Medians; var modes = analysis.Modes; var ranges = analysis.Ranges; var samples = analysis.Samples; var skewness = analysis.Skewness; var source = analysis.Source; var standardDeviation = analysis.StandardDeviations; var standardErrors = analysis.StandardErrors; var standardScores = analysis.StandardScores; var sums = analysis.Sums; var variables = analysis.Variables; var variances = analysis.Variances; var confidence = analysis.Confidence; var quartiles = analysis.Quartiles; Assert.IsTrue(columnNames.IsEqual(new string[] { "Column 0", })); Assert.IsTrue(correlation.IsEqual(new double[,] { { 1 } })); Assert.IsTrue(covariance.IsEqual(new double[,] { { 604.39999999999998 } })); Assert.IsTrue(deviationScores.IsEqual(new double[,] { { 14.0 }, { -26.0 }, { 27.0 }, { -13.0 }, { 24.0 }, { -26.0 } })); Assert.IsTrue(distinct.IsEqual(new int[] { 5 })); Assert.IsTrue(kurtosis.IsEqual(new double[] { -2.7161799571726601 })); Assert.IsTrue(means.IsEqual(new double[] { 38.0 })); Assert.IsTrue(medians.IsEqual(new double[] { 38.5 })); Assert.IsTrue(modes.IsEqual(new double[] { 12.0 })); Assert.IsTrue(ranges.Apply(p => p.Min).IsEqual(new double[] { 12 })); Assert.IsTrue(ranges.Apply(p => p.Max).IsEqual(new double[] { 65 })); Assert.IsTrue(samples == 6); Assert.IsTrue(skewness.IsEqual(new double[] { -0.022168530787350427 })); Assert.IsTrue(source.IsEqual(new double[,] { { 52 }, { 12 }, { 65 }, { 25 }, { 62 }, { 12 }, })); Assert.IsTrue(standardDeviation.IsEqual(new double[] { 24.584547992590792 })); Assert.IsTrue(standardErrors.IsEqual(new double[] { 10.036599689801987 })); Assert.IsTrue(standardScores.IsEqual(new double[,] { { 0.5694633883128245 }, { -1.0575748640095313 }, { 1.09825082031759 }, { -0.52878743200476563 }, { 0.97622295139341342 }, { -1.0575748640095313 }, })); Assert.IsTrue(sums.IsEqual(new double[] { 228.0 })); Assert.IsTrue(variables == 1); Assert.IsTrue(variances.IsEqual(new double[] { 604.39999999999998 })); Assert.AreEqual(1, confidence.Length); Assert.AreEqual(18.328626080742229, confidence[0].Min); Assert.AreEqual(57.671373919257775, confidence[0].Max); DoubleRange q; double q2 = Accord.Statistics.Tools.Quartiles(data, out q, alreadySorted: false); Assert.AreEqual(1, quartiles.Length); Assert.AreEqual(q.Min, quartiles[0].Min); Assert.AreEqual(q.Max, quartiles[0].Max); Assert.AreEqual(12, quartiles[0].Min); Assert.AreEqual(62, quartiles[0].Max); }
private void btnSampleRunAnalysis_Click(object sender, EventArgs e) { // Check requirements if (sourceTable == null) { MessageBox.Show("A sample spreadsheet can be found in the " + "Resources folder in the same directory as this application.", "Please load some data before attempting an analysis"); return; } if (checkedListBox1.CheckedItems.Count == 0) { MessageBox.Show("Please select the dependent input variables to be used in the regression model.", "Please choose at least one input variable"); } // Finishes and save any pending changes to the given data dgvAnalysisSource.EndEdit(); sourceTable.AcceptChanges(); // Gets the column of the dependent variable String dependentName = (string)comboBox1.SelectedItem; DataTable dependent = sourceTable.DefaultView.ToTable(false, dependentName); // Gets the columns of the independent variables List<string> names = new List<string>(); foreach (string name in checkedListBox1.CheckedItems) names.Add(name); String[] independentNames = names.ToArray(); DataTable independent = sourceTable.DefaultView.ToTable(false, independentNames); // Creates the input and output matrices from the source data table double[][] input = independent.ToArray(); double[] output = dependent.Columns[dependentName].ToArray(); double[,] sourceMatrix = sourceTable.ToMatrix(independentNames); // Creates the Simple Descriptive Analysis of the given source DescriptiveAnalysis sda = new DescriptiveAnalysis(sourceMatrix, independentNames); sda.Compute(); // Populates statistics overview tab with analysis data dgvDistributionMeasures.DataSource = sda.Measures; // Creates the Logistic Regression Analysis of the given source lra = new LogisticRegressionAnalysis(input, output, independentNames, dependentName); // Compute the Logistic Regression Analysis lra.Compute(); // Populates coefficient overview with analysis data dgvLogisticCoefficients.DataSource = lra.Coefficients; // Populate details about the fitted model tbChiSquare.Text = lra.ChiSquare.Statistic.ToString("N5"); tbPValue.Text = lra.ChiSquare.PValue.ToString("N5"); checkBox1.Checked = lra.ChiSquare.Significant; tbDeviance.Text = lra.Deviance.ToString("N5"); tbLogLikelihood.Text = lra.LogLikelihood.ToString("N5"); // Create the Multiple Linear Regression Analysis of the given source mlr = new MultipleLinearRegressionAnalysis(input, output, independentNames, dependentName, true); // Compute the Linear Regression Analysis mlr.Compute(); dgvLinearCoefficients.DataSource = mlr.Coefficients; dgvRegressionAnova.DataSource = mlr.Table; tbRSquared.Text = mlr.RSquared.ToString("N5"); tbRSquaredAdj.Text = mlr.RSquareAdjusted.ToString("N5"); tbChiPValue.Text = mlr.ChiSquareTest.PValue.ToString("N5"); tbFPValue.Text = mlr.FTest.PValue.ToString("N5"); tbZPValue.Text = mlr.ZTest.PValue.ToString("N5"); tbChiStatistic.Text = mlr.ChiSquareTest.Statistic.ToString("N5"); tbFStatistic.Text = mlr.FTest.Statistic.ToString("N5"); tbZStatistic.Text = mlr.ZTest.Statistic.ToString("N5"); cbChiSignificant.Checked = mlr.ChiSquareTest.Significant; cbFSignificant.Checked = mlr.FTest.Significant; cbZSignificant.Checked = mlr.ZTest.Significant; // Populate projection source table string[] cols = independentNames; if (!independentNames.Contains(dependentName)) cols = independentNames.Concatenate(dependentName); DataTable projSource = sourceTable.DefaultView.ToTable(false, cols); dgvProjectionSource.DataSource = projSource; }
private ArrayDataView AnalysisData(DataTable dataTable) { DescriptiveAnalysis analysis; string[] columnNames; double[,] arrayData = dataTable.ToMatrix(out columnNames); // Analysis Data analysis = new DescriptiveAnalysis(arrayData, columnNames); analysis.Compute(); double[,] analysisData = new double[0, arrayData.GetLength(1)]; analysisData = analysisData.InsertRow<double>(analysis.Distinct.ToDouble(), analysisData.GetLength(0)); analysisData = analysisData.InsertRow<double>(analysis.Means, analysisData.GetLength(0)); analysisData = analysisData.InsertRow<double>(analysis.Medians, analysisData.GetLength(0)); analysisData = analysisData.InsertRow<double>(analysis.Modes, analysisData.GetLength(0)); analysisData = analysisData.InsertRow<double>(analysis.StandardDeviations, analysisData.GetLength(0)); analysisData = analysisData.InsertRow<double>(analysis.Variances, analysisData.GetLength(0)); analysisData = analysisData.InsertRow<double>(analysis.Sums, analysisData.GetLength(0)); string[] rowNames = { "Distinct", "Means", "Medians", "Modes", "StandardDeviations", "Variances", "Sums" }; return new ArrayDataView(analysisData, columnNames, rowNames); }