コード例 #1
0
        // the internal linear regression routine, which assumes inputs are entirely valid

        private MultiLinearRegressionResult LinearRegression_Internal(int outputIndex)
        {
            IReadOnlyList <double> yColumn = storage[outputIndex];

            IReadOnlyList <double>[] xColumns = new IReadOnlyList <double> [Dimension];
            string[] xNames = new string[Dimension];
            for (int c = 0; c < Dimension; c++)
            {
                if (c == outputIndex)
                {
                    xColumns[c] = null;
                    xNames[c]   = "Intercept";
                }
                else
                {
                    SampleStorage xColumn = storage[c];
                    xColumns[c] = xColumn;
                    if (String.IsNullOrWhiteSpace(xColumn.Name))
                    {
                        xNames[c] = c.ToString();
                    }
                    else
                    {
                        xNames[c] = xColumn.Name;
                    }
                }
            }

            return(new MultiLinearRegressionResult(yColumn, xColumns, xNames));
        }
コード例 #2
0
        /// <summary>
        /// Swaps the X and Y variables in the bivariate sample.
        /// </summary>
        public void TransposeXY()
        {
            SampleStorage t = yData;

            yData = xData;
            xData = t;
        }
コード例 #3
0
 /// <summary>
 /// Copies the multivariate sample.
 /// </summary>
 /// <returns>An independent copy of the multivariate sample.</returns>
 public MultivariateSample Copy()
 {
     SampleStorage[] columnCopies = new SampleStorage[storage.Length];
     for (int i = 0; i < columnCopies.Length; i++)
     {
         columnCopies[i] = storage[i].Copy();
     }
     return(new MultivariateSample(columnCopies, false));
 }
コード例 #4
0
        /// <summary>
        /// Performs a linear logistic regression analysis.
        /// </summary>
        /// <param name="outputIndex">The index of the column to predict.</param>
        /// <returns>A logistic multi-linear model fit. The kth parameter is the slope of the multi-linear model with respect to
        /// the kth column, except for k equal to the <paramref name="outputIndex"/>, for which it is the intercept.</returns>
        /// <remarks>Logistic linear regression is suited to situations where multiple input variables, either continuous or binary indicators, are used to predict
        /// the value of a binary output variable. Like a linear regression, a logistic linear regression tries to find a model that predicts the output variable using
        /// a linear combination of input variables. Unlike a simple linear regression, the model does not assume that this linear
        /// function predicts the output directly; instead it assumes that this function value is then fed into a logit link function, which
        /// maps the real numbers into the interval (0, 1), and interprets the value of this link function as the probability of obtaining success value
        /// for the output variable.</remarks>
        /// <exception cref="InvalidOperationException">The column to be predicted contains values other than 0 and 1.</exception>
        /// <exception cref="InsufficientDataException">There are not more rows in the sample than columns.</exception>
        /// <exception cref="DivideByZeroException">The curvature matrix is singular, indicating that the data is independent of
        /// one or more parameters, or that two or more parameters are linearly dependent.</exception>
        public MultiLinearLogisticRegressionResult LogisticLinearRegression(int outputIndex)
        {
            if ((outputIndex < 0) || (outputIndex >= this.Dimension))
            {
                throw new ArgumentOutOfRangeException(nameof(outputIndex));
            }
            if (this.Count <= this.Dimension)
            {
                throw new InsufficientDataException();
            }

            List <bool>   yColumn  = new List <bool>(this.Count);
            SampleStorage yStorage = storage[outputIndex];

            foreach (double yValue in yStorage)
            {
                if (yValue == 0.0)
                {
                    yColumn.Add(false);
                }
                else if (yValue == 1.0)
                {
                    yColumn.Add(true);
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }

            IReadOnlyList <double>[] xColumns = new IReadOnlyList <double> [Dimension];
            string[] xNames = new string[Dimension];
            for (int c = 0; c < Dimension; c++)
            {
                if (c == outputIndex)
                {
                    xColumns[c] = null;
                    xNames[c]   = "Intercept";
                }
                else
                {
                    SampleStorage xColumn = storage[c];
                    xColumns[c] = xColumn;
                    if (String.IsNullOrWhiteSpace(xColumn.Name))
                    {
                        xNames[c] = c.ToString();
                    }
                    else
                    {
                        xNames[c] = xColumn.Name;
                    }
                }
            }

            return(new MultiLinearLogisticRegressionResult(yColumn, xColumns, xNames));
        }
コード例 #5
0
 /// <summary>
 /// Initializes a new multivariate sample.
 /// </summary>
 /// <param name="dimension">The dimension of the sample space, that is, the number of variables
 /// recorded for each sample entry.</param>
 /// <exception cref="ArgumentOutOfRangeException"><paramref name="dimension"/> is less than one.</exception>
 public MultivariateSample(int dimension)
 {
     if (dimension < 1)
     {
         throw new ArgumentOutOfRangeException("dimension");
     }
     storage = new SampleStorage[dimension];
     for (int j = 0; j < storage.Length; j++)
     {
         storage[j] = new SampleStorage();
     }
     isReadOnly = false;
 }
コード例 #6
0
        /// <summary>
        /// Recomputes the time series as the differences between sequential values of the original series.
        /// </summary>
        /// <remarks>
        /// <para>Differencing decreases the number of values in the series by one.</para>
        /// </remarks>
        /// <seealso cref="Integrate"/>
        public void Difference()
        {
            if (data.Count < 2)
            {
                throw new InsufficientDataException();
            }

            SampleStorage newData = new SampleStorage();

            for (int i = 1; i < data.Count; i++)
            {
                newData.Add(data[i] - data[i - 1]);
            }

            data = newData;
        }
コード例 #7
0
        /// <summary>
        /// Recomputes the time series as the sums of seqential values of the original series.
        /// </summary>
        /// <param name="c">The constant to be added to the first value.</param>
        /// <seealso cref="Difference"/>
        public void Integrate(double c)
        {
            if (data.Count < 1)
            {
                throw new InsufficientDataException();
            }

            SampleStorage newData = new SampleStorage();

            newData.Add(data[0] + c);
            for (int i = 1; i < data.Count; i++)
            {
                newData.Add(data[i] + newData[i - 1]);
            }

            data = newData;
        }
コード例 #8
0
 /// <summary>
 /// Gets the indicated columns as a multivariate sample.
 /// </summary>
 /// <param name="columnIndexes">A list of column indexes.</param>
 /// <returns>A read-only <see cref="MultivariateSample"/> consisting of the indicated columns.</returns>
 /// <remarks>
 /// <para>Use this method to perform multivariate analyses, such as regression and principal component analyis, using
 /// only a subset of the variables in the original multivariate sample.</para>
 /// <para>Note that this is a fast operation, which does not create independent copies of the columns.</para>
 /// </remarks>
 public MultivariateSample Columns(IList <int> columnIndexes)
 {
     if (columnIndexes == null)
     {
         throw new ArgumentNullException("columnIndexes");
     }
     SampleStorage[] columns = new SampleStorage[columnIndexes.Count];
     for (int i = 0; i < columns.Length; i++)
     {
         int ci = columnIndexes[i];
         if ((ci < 0) || (ci >= Dimension))
         {
             throw new ArgumentOutOfRangeException("columnIndexes");
         }
         columns[i] = storage[ci];
     }
     return(new MultivariateSample(columns, true));
 }
コード例 #9
0
 internal BivariateSample(SampleStorage xData, SampleStorage yData, bool isReadOnly)
 {
     this.xData      = xData;
     this.yData      = yData;
     this.isReadOnly = isReadOnly;
 }
コード例 #10
0
 /// <summary>
 /// Initializes a new bivariate sample.
 /// </summary>
 public BivariateSample()
 {
     xData      = new SampleStorage();
     yData      = new SampleStorage();
     isReadOnly = false;
 }
コード例 #11
0
 public ProductsController(SampleStorage storage)
 {
     _storage = storage;
 }