Esempio n. 1
0
        /// <summary>
        /// 行列 X に主成分分析を適用する.
        /// </summary>
        /// <param name="X">
        /// <para>[n, d]の行列(書き換えられることはない)</para>
        /// <para>データ数:n,次元数:dとしたとき,行列 X の形式は n×d でなければならない.</para>
        /// </param>
        /// <param name="varType">分散の種類</param>
        public PrincipalComponentAnalysis(Matrix X, VarianceType varType)
        {
            Matrix _X = new Matrix(X);

            // 各変数の平均値を0にする
            Vector colAvg = _X.ColumnAverages;

            _X.Columns.ForEach(delegate(int i, IVector v)
            {
                v.ForEach(delegate(ref double val)
                {
                    val -= colAvg[i];
                });
            });

            // 分散・共分散行列
            Matrix S = Matrix.Transpose(_X) * _X;

            if (varType == VarianceType.DivN)
            {
                S /= _X.RowSize;
            }
            else
            {
                S /= (_X.RowSize - 1);
            }

            // 固有値分解
            EigenvalueDecomposition evd = new EigenvalueDecomposition(S);

            evd.Sort(EigenvalueDecomposition.SortOrder.Descending);

            this.eigenvalues  = evd.RealEigenvalues;
            this.eigenvectors = evd.RealEigenvectors;
        }
Esempio n. 2
0
        /// <summary>
        /// Creates the structures parameter row.
        /// </summary>
        /// <param name="readText">The read text.</param>
        /// <returns>The created structures parameter row.</returns>
        /// <exception cref="LineParseException">Thrown when either:
        /// <list type="bullet">
        /// <item><paramref name="readText"/> does not contain the separator character.</item>
        /// <item>Location id field is empty or consists only of white-space characters.</item>
        /// <item>Parameter id field is empty or consists only of white-space characters.</item>
        /// <item>Numeric value field is not a number or too large/small to be represented as <see cref="double"/>.</item>
        /// <item>Variance value field is not a number or too large/small to be represented as <see cref="double"/>.</item>
        /// <item>Boolean field is not a valid value.</item>
        /// </list></exception>
        private StructuresParameterRow CreateStructuresParameterRow(string readText)
        {
            string[] tokenizedText = TokenizeString(readText);

            if (tokenizedText.Length != headerLength)
            {
                string message = string.Format(Resources.StructuresCharacteristicsCsvReader_CreateStructuresParameterRow_Line_should_have_NumberOfExpectedElements_0_but_has_ActualNumberOfElements_1_,
                                               headerLength, tokenizedText.Length);
                throw CreateLineParseException(lineNumber, message);
            }

            string       locationId        = ParseLocationId(tokenizedText);
            string       parameterId       = ParseParameterId(tokenizedText);
            string       alphanumericValue = ParseAlphanumericValue(tokenizedText);
            double       numericValue      = ParseNumericValue(tokenizedText);
            double       varianceValue     = ParseVarianceValue(tokenizedText);
            VarianceType varianceType      = ParseVarianceType(tokenizedText);

            return(new StructuresParameterRow
            {
                LocationId = locationId,
                ParameterId = parameterId,
                AlphanumericValue = alphanumericValue,
                NumericalValue = numericValue,
                VarianceValue = varianceValue,
                VarianceType = varianceType,
                LineNumber = lineNumber
            });
        }
        /// <summary>
        /// �s�� X �Ɏ听�����͂�K�p����D
        /// </summary>
        /// <param name="X">
        /// <para>[n, d]�̍s��i�����������邱�Ƃ͂Ȃ��j</para>
        /// <para>�f�[�^���Fn�C�������Fd�Ƃ����Ƃ��C�s�� X �̌`���� n�~d �łȂ���΂Ȃ�Ȃ��D</para>
        /// </param>
        /// <param name="varType">���U�̎��</param>
        public PrincipalComponentAnalysis(Matrix X, VarianceType varType)
        {
            Matrix _X = new Matrix(X);

            // �e�ϐ��̕��ϒl��0�ɂ���
            Vector colAvg = _X.ColumnAverages;
            _X.Columns.ForEach(delegate(int i, IVector v)
            {
                v.ForEach(delegate(ref double val)
                {
                    val -= colAvg[i];
                });
            });

            // ���U�E�����U�s��
            Matrix S = Matrix.Transpose(_X) * _X;
            if (varType == VarianceType.DivN)
            {
                S /= _X.RowSize;
            }
            else
            {
                S /= (_X.RowSize - 1);
            }

            // �ŗL�l����
            EigenvalueDecomposition evd = new EigenvalueDecomposition(S);
            evd.Sort(EigenvalueDecomposition.SortOrder.Descending);

            this.eigenvalues = evd.RealEigenvalues;
            this.eigenvectors = evd.RealEigenvectors;
        }
Esempio n. 4
0
        public static double StandardDeviation(this double[] input, VarianceType type)
        {
            if (!input.Any())
            {
                throw new InvalidOperationException(Consts.EMPTY_ARRAY);
            }

            return(Math.Sqrt(input.Variance(type)));
        }
        private static SyntaxKind GetVarianceToken(VarianceType variance)
        {
            switch (variance)
            {
            case VarianceType.Contravariant:
                return(SyntaxKind.InKeyword);

            case VarianceType.Covariant:
                return(SyntaxKind.OutKeyword);

            default:
                return(SyntaxKind.None);
            }
        }
Esempio n. 6
0
        public static double Variance(this double[] input, VarianceType type)
        {
            if (!input.Any())
            {
                throw new InvalidOperationException(Consts.EMPTY_ARRAY);
            }
            else if (input.Count() == 1 && type == VarianceType.Sample)
            {
                throw new InvalidOperationException(Consts.INVALID_SAMPLE_FOR_VARIANCE);
            }

            var mean    = input.Mean();
            int divisor = type == VarianceType.Population ? input.Count() : input.Count() - 1;

            return(input.Sum(q => Math.Pow(q - mean, 2)) / divisor);
        }
Esempio n. 7
0
        private static T _Standardize_T <T>(T vector, VarianceType varType) where T : IVector
        {
            VectorChecker.ZeroSize(vector);

            double avg = vector.Average;
            double std = Math.Sqrt(varType == VarianceType.DivN ? vector.Scatter / vector.Size : vector.Variance);

            if (clapack.CalculationChecker.IsLessThanLimit(std))
            {
                throw new Exception.ValueIsLessThanLimitException(
                          "The standard variation is less than \"Calculation Limit\".\n"
                          + "Values = " + vector.ToString());
            }

            for (int i = 0; i < vector.Size; ++i)
            {
                vector[i] = (vector[i] - avg) / std;
            }
            return(vector);
        }
Esempio n. 8
0
        /// <summary>
        /// 指定された行列を標準化する.
        /// </summary>
        /// <param name="X">標準化する行列(直接変更されない)</param>
        /// <param name="target">行ごとの標準化なのか,列ごとの標準化なのかを指定する</param>
        /// <param name="varType">標準化に使用される分散のタイプ</param>
        /// <returns>Xのコピーを標準化した行列</returns>
        ///
        /// <exception cref="Exception.ValueIsLessThanLimitException">
        /// 標準偏差値が設定されている精度の下限値未満の場合にthrowされる.
        /// </exception>
        public static Matrix Standardize(Matrix X, Target target, VarianceType varType)
        {
            Matrix ret = new Matrix(X);

            if (target == Target.EachRow)
            {
                for (int r = 0; r < ret.RowSize; ++r)
                {
                    ret.Rows[r] = Standardize(ret.Rows[r], varType);
                }
            }
            else
            {
                for (int c = 0; c < ret.ColumnSize; ++c)
                {
                    ret.Columns[c] = Standardize(ret.Columns[c], varType);
                }
            }
            return(ret);
        }
Esempio n. 9
0
		/// <summary>
		/// 分散
		/// </summary>
		/// <param name="data"></param>
		/// <param name="type"></param>
		/// <returns></returns>
		public static double Variance(IEnumerable<double> data, VarianceType type = VarianceType.Unbiased)
		{
			CheckArgument(data);

			var mean = Mean(data);
			double n = data.Count();

			if (type == VarianceType.Unbiased && n == 1)
			{
				throw new InvalidOperationException("標本数1の場合不偏分散は計算できません");
			}

			var temp = data.Select(x => (mean - x) * (mean - x)).Sum();
			if (type == VarianceType.Sample)
			{
				return temp / n;
			}
			else
			{
				return temp / (n - 1.0);
			}
		}
Esempio n. 10
0
        private static List <string> ValidateStochasticVariableParameters(StructuresParameterRow row, bool meanMustBeGreaterThanZero, bool variationAsStandardDeviation)
        {
            List <string> messages = ValidateStochasticVariableMeanParameter(row, meanMustBeGreaterThanZero);

            VarianceType type = row.VarianceType;

            if (type != VarianceType.StandardDeviation && type != VarianceType.CoefficientOfVariation)
            {
                messages.Add(string.Format(Resources.StructuresParameterRowsValidator_ParameterId_0_Line_1_ColumnName_2_invalid_variancetype_value,
                                           row.ParameterId, row.LineNumber, StructureFilesKeywords.VariationTypeColumnName.FirstToUpper()));
            }

            messages.AddRange(ValidatePositiveDoubleParameter(row, StructureFilesKeywords.VariationValueColumnName));

            double mean         = row.NumericalValue;
            double absoluteMean = Math.Abs(mean);

            if (variationAsStandardDeviation)
            {
                if (row.VarianceType == VarianceType.CoefficientOfVariation && absoluteMean < valueTooCloseToZero)
                {
                    messages.Add(string.Format(Resources.StructuresParameterRowsValidator_ParameterId_0_Line_1_ColumnName_2_mean_too_small_for_reliable_variation_value_conversion,
                                               row.ParameterId, row.LineNumber, StructureFilesKeywords.NumericalValueColumnName.FirstToUpper()));
                }
            }
            else
            {
                if (row.VarianceType == VarianceType.StandardDeviation && absoluteMean < valueTooCloseToZero)
                {
                    messages.Add(string.Format(Resources.StructuresParameterRowsValidator_ParameterId_0_Line_1_ColumnName_2_mean_too_small_for_reliable_variation_value_conversion,
                                               row.ParameterId, row.LineNumber, StructureFilesKeywords.NumericalValueColumnName.FirstToUpper()));
                }
            }

            return(messages);
        }
Esempio n. 11
0
        /// <summary>
        /// 共分散
        /// </summary>
        /// <param name="data1"></param>
        /// <param name="data2"></param>
        /// <returns></returns>
        public static double Covariance(IEnumerable<double> data1, IEnumerable<double> data2, VarianceType type = VarianceType.Unbiased)
        {
            CheckArgument(data1, "data1");
            CheckArgument(data2, "data2");
            if (data1.Count() != data2.Count())
            {
                throw new InvalidOperationException("data1とdata2の要素数が異なるため共分散を計算できません");
            }

            var n = data1.Count();
            var mean1 = Mean(data1);
            var mean2 = Mean(data2);

            var sum = data1.Select((x, i) => (x - mean1) * (data2.ElementAt(i) - mean2)).Sum();

            if (type == VarianceType.Unbiased)
            {
                return sum / (n - 1);
            }
            else
            {
                return sum / n;
            }
        }
Esempio n. 12
0
		/// <summary>
		/// 共分散
		/// </summary>
		/// <param name="data"></param>
		/// <param name="type"></param>
		/// <returns></returns>
		public static double Covariance(IEnumerable<VectorD> data, int index1, int index2, VarianceType type = VarianceType.Unbiased)
		{
			CheckArgument(data);

			return Covariance(data.Select(x => x[index1]), data.Select(x => x[index2]), type);
		}
Esempio n. 13
0
		/// <summary>
		/// 共分散
		/// </summary>
		/// <param name="data1"></param>
		/// <param name="data2"></param>
		/// <returns></returns>
		public static double Covariance(IEnumerable<double> data1, IEnumerable<double> data2, VarianceType type = VarianceType.Unbiased)
		{
			CheckArgument(data1, "data1");
			CheckArgument(data2, "data2");
			CheckArgumentLength(data1, data2);

			var n = data1.Count();
			var mean1 = Mean(data1);
			var mean2 = Mean(data2);

			var sum = data1.Select((x, i) => (x - mean1) * (data2.ElementAt(i) - mean2)).Sum();

			if (type == VarianceType.Unbiased)
			{
				return sum / (n - 1);
			}
			else
			{
				return sum / n;
			}
		}
Esempio n. 14
0
		/// <summary>
		/// 標準偏差
		/// </summary>
		/// <param name="data"></param>
		/// <param name="type"></param>
		/// <returns></returns>
		public static double StdDev(IEnumerable<double> data, VarianceType type = VarianceType.Unbiased)
		{
			CheckArgument(data);

			var variance = Variance(data, type);
			return Math.Sqrt(variance);
		}
        public void ReadLine_ValidFile_ReturnDataFromLine(int elementIndex, string expectedLocationId, string paramterId,
                                                          double expectedNumericValue, double expectedVarianceValue, VarianceType expectedType)
        {
            // Setup
            string filePath = TestHelper.GetTestDataPath(TestDataPath.Riskeer.Common.IO,
                                                         Path.Combine("Structures", "StructuresCharacteristicsCsvFiles", "ValidFile_2Locations_AllHeightStructureParameters.csv"));

            using (var reader = new StructuresCharacteristicsCsvReader(filePath))
            {
                // Call
                StructuresParameterRow parameter = null;
                for (var i = 0; i < elementIndex; i++)
                {
                    parameter = reader.ReadLine();
                }

                // Assert
                Assert.IsNotNull(parameter);
                Assert.AreEqual(expectedLocationId, parameter.LocationId);
                Assert.AreEqual(paramterId, parameter.ParameterId);
                Assert.AreEqual(expectedNumericValue, parameter.NumericalValue);
                Assert.AreEqual(expectedVarianceValue, parameter.VarianceValue);
                Assert.AreEqual(expectedType, parameter.VarianceType);
                Assert.AreEqual(elementIndex + 1, parameter.LineNumber);
            }
        }
Esempio n. 16
0
        public void SimpleProperties_SetNewValues_GetNewlySetValues(string locationId, string id, string alphanumeric, double numerical, double variance, VarianceType type, int lineNumber)
        {
            // Setup
            var parameter = new StructuresParameterRow();

            // Call
            parameter.LocationId        = locationId;
            parameter.ParameterId       = id;
            parameter.AlphanumericValue = alphanumeric;
            parameter.NumericalValue    = numerical;
            parameter.VarianceValue     = variance;
            parameter.VarianceType      = type;
            parameter.LineNumber        = lineNumber;

            // Assert
            Assert.AreEqual(locationId, parameter.LocationId);
            Assert.AreEqual(id, parameter.ParameterId);
            Assert.AreEqual(alphanumeric, parameter.AlphanumericValue);
            Assert.AreEqual(numerical, parameter.NumericalValue);
            Assert.AreEqual(variance, parameter.VarianceValue);
            Assert.AreEqual(type, parameter.VarianceType);
            Assert.AreEqual(lineNumber, parameter.LineNumber);
        }
Esempio n. 17
0
 /// <summary>
 /// 標準化された<see cref="IVector"/>インタフェース実装オブジェクトを作成する.
 /// このメソッドは,インタフェースで受け取り,インタフェースで返す.
 /// </summary>
 /// <param name="vector">
 /// 標準化される<see cref="IVector"/>インタフェース実装オブジェクト(実際は,この引数のコピーが標準化される)
 /// </param>
 /// <param name="varType">標準化に使用される分散の種類</param>
 /// <returns>標準化された<see cref="IVector"/>インタフェース実装オブジェクト</returns>
 ///
 /// <exception cref="Exception.ValueIsLessThanLimitException">
 /// 標準偏差値が設定されている精度の下限値未満の場合にthrowされる.
 /// </exception>
 public static IVector Standardize(IVector vector, VarianceType varType)
 {
     return(_Standardize_T(new Vector(vector), varType));
 }
        public int GetVariance(VarianceType varianceType)
        {
            switch (varianceType)
            {
            case VarianceType.All:
                //return Convert.ToInt16(_Variance.All);
                return(0);

            case VarianceType.Q1:
                return(Convert.ToInt16(_Variance.Q1));

            case VarianceType.Q2:
                return(Convert.ToInt16(_Variance.Q2));

            case VarianceType.Q3:
                return(Convert.ToInt16(_Variance.Q3));

            case VarianceType.Q4:
                return(Convert.ToInt16(_Variance.Q4));

            case VarianceType.QAverage:
                return(Convert.ToInt16(_Variance.QAverage));

            case VarianceType.S1:
                return(Convert.ToInt16(_Variance.S1));

            case VarianceType.S2:
                return(Convert.ToInt16(_Variance.S2));

            case VarianceType.S3:
                return(Convert.ToInt16(_Variance.S3));

            case VarianceType.S4:
                return(Convert.ToInt16(_Variance.S4));

            case VarianceType.S5:
                return(Convert.ToInt16(_Variance.S5));

            case VarianceType.S6:
                return(Convert.ToInt16(_Variance.S6));

            case VarianceType.S7:
                return(Convert.ToInt16(_Variance.S7));

            case VarianceType.S8:
                return(Convert.ToInt16(_Variance.S8));

            case VarianceType.S9:
                return(Convert.ToInt16(_Variance.S9));

            case VarianceType.Savg:
                return(Convert.ToInt16(_Variance.Savg));

            case VarianceType.L1:
                return(Convert.ToInt16(_Variance.L1));

            case VarianceType.L2:
                return(Convert.ToInt16(_Variance.L2));

            case VarianceType.L3:
                return(Convert.ToInt16(_Variance.L3));

            case VarianceType.L4:
                return(Convert.ToInt16(_Variance.L4));

            case VarianceType.L5:
                return(Convert.ToInt16(_Variance.L5));

            case VarianceType.L6:
                return(Convert.ToInt16(_Variance.L6));

            case VarianceType.L7:
                return(Convert.ToInt16(_Variance.L7));

            case VarianceType.L8:
                return(Convert.ToInt16(_Variance.L8));

            case VarianceType.L9:
                return(Convert.ToInt16(_Variance.L9));

            case VarianceType.L10:
                return(Convert.ToInt16(_Variance.L10));

            case VarianceType.L11:
                return(Convert.ToInt16(_Variance.L11));

            case VarianceType.L12:
                return(Convert.ToInt16(_Variance.L12));

            case VarianceType.L13:
                return(Convert.ToInt16(_Variance.L13));

            case VarianceType.L14:
                return(Convert.ToInt16(_Variance.L14));

            case VarianceType.L15:
                return(Convert.ToInt16(_Variance.L15));

            case VarianceType.L16:
                return(Convert.ToInt16(_Variance.L16));

            case VarianceType.Lavg:
                return(Convert.ToInt16(_Variance.Lavg));

            default:
                return(0);
            }
        }
Esempio n. 19
0
 /// <summary>
 /// 標準化された<see cref="ColumnVector"/>を作成する.
 /// </summary>
 /// <param name="vector">標準化される<see cref="ColumnVector"/>(実際は,この引数のコピーが標準化される)</param>
 /// <param name="varType">標準化に使用する分散の種類</param>
 /// <returns>標準化された<see cref="ColumnVector"/></returns>
 ///
 /// <exception cref="Exception.ValueIsLessThanLimitException">
 /// 標準偏差値が設定されている精度の下限値未満の場合にthrowされる.
 /// </exception>
 public static ColumnVector Standardize(ColumnVector vector, VarianceType varType)
 {
     return(_Standardize_T(new ColumnVector(vector), varType));
 }