public void WriteBadMatrixThrowsArgumentException() { var matrix = Matrix <float> .Build.Dense(1, 1); Assert.Throws <ArgumentException>(() => MatlabWriter.Write("somefile1", matrix, string.Empty)); Assert.Throws <ArgumentException>(() => MatlabWriter.Write("somefile1", matrix, null)); }
public void WriteRecords(string filePath, Dictionary <string, object> extra = null) { MatlabWriter writer = new MatlabWriter(filePath); WriteGammaList(writer, InPrecision, "inShape", "inRate"); WriteGammaList(writer, OutPrecision, "outShape", "outRate"); // MatlabWritter cannot write boolean writer.Write("consultOracle", MatrixUtils.ToDouble(ConsultOracle)); Matrix uncertaintyMat = MatrixUtils.StackColumnsIgnoreNull(Uncertainty); // write a Matrix writer.Write("uncertainty", uncertaintyMat); WriteGammaList(writer, OracleOut, "oraOutShape", "oraOutRate"); if (extra != null) { foreach (var kv in extra) { writer.Write(kv.Key, kv.Value); } } writer.Dispose(); }
private void SaveAsButton_Click(object sender, EventArgs e) { Readjust(); if (TFadjustedZ == null) { MessageBox.Show(this, "Cannot save an adjusted TF while in an error state.", "Error saving", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "Matlab MAT (*.mat)|*.mat|All Files (*.*)|*.*"; sfd.FileName = Path.GetFileNameWithoutExtension(TFFilename) + "_adj.mat"; if (sfd.ShowDialog() != DialogResult.OK) { return; } var Z = CreateMatrix.DenseOfColumns(new IEnumerable <double>[] { TFadjustedZ }); //var Sr = CreateMatrix.DenseOfColumnVectors(new Vector<Complex>[] { TFadjustedSr }); var Sr = CreateMatrix.DenseOfColumns(new Vector <Complex>[] { TFadjustedSr }); var matrices = new List <MatlabMatrix>(); matrices.Add(MatlabWriter.Pack(Z, "z")); matrices.Add(MatlabWriter.Pack(Sr, "Sr")); MatlabWriter.Store(sfd.FileName, matrices); }
static void Main(string[] args) { var data = Matrix <double> .Build.Random(2, 2); DelimitedWriter.Write("123", data); MatlabWriter.Write("123", Matrix <double> .Build.Random(2, 2), "a"); }
public static void TestWritingMat() { string p = Config.PathToSavedFile("test_save_mat.mat"); MatlabWriter w = new MatlabWriter(p); w.Write("b", 2); double[] arr = new double[] { 1, 2, 3 }; w.Write("arr", arr); Vector vec = Vector.FromArray(new double[] { 4, 5, 6, 7 }); w.Write("vec", vec); List <double> list = new List <double>(arr); w.Write("list", list); Matrix m = Matrix.Parse("1 2\n 3 4"); w.Write("m", m); long time = 1329L; w.Write("longNum", time); List <Matrix> mats = new List <Matrix>(); mats.Add(Matrix.IdentityScaledBy(2, 3.0)); mats.Add(Matrix.IdentityScaledBy(3, 4.0)); w.Write("list_mats", mats); w.Dispose(); }
public static void WriteGammaList(MatlabWriter writer, List <Gamma?> gList, string shapeName, string rateName) { double[] shapes, rates; GammaListToArrays(gList, out shapes, out rates); writer.Write(shapeName, shapes); writer.Write(rateName, rates); }
internal void StudentIsPositiveTest4() { double shape = 1; Gamma precPrior = Gamma.FromShapeAndRate(shape, shape); // mean=-1 causes improper messages double mean = -1; Gaussian meanPrior = Gaussian.PointMass(mean); double evExpected; Gaussian xExpected = StudentIsPositiveExact(mean, precPrior, out evExpected); GaussianOp.ForceProper = false; GaussianOp_Laplace.modified = true; GaussianOp_Laplace.modified2 = true; Gaussian xF = Gaussian.Uniform(); Gaussian xB = Gaussian.Uniform(); Gamma q = GaussianOp_Laplace.QInit(); double r0 = 0.38; r0 = 0.1; for (int iter = 0; iter < 20; iter++) { q = GaussianOp_Laplace.Q(xB, meanPrior, precPrior, q); //xF = GaussianOp_Laplace.SampleAverageConditional(xB, meanPrior, precPrior, q); xF = Gaussian.FromMeanAndPrecision(mean, r0); xB = IsPositiveOp.XAverageConditional(true, xF); Console.WriteLine("xF = {0} xB = {1}", xF, xB); } Console.WriteLine("x = {0} should be {1}", xF * xB, xExpected); double[] precs = EpTests.linspace(1e-3, 5, 100); double[] evTrue = new double[precs.Length]; double[] evApprox = new double[precs.Length]; double[] evApprox2 = new double[precs.Length]; //r0 = q.GetMean(); double sum = 0, sum2 = 0; for (int i = 0; i < precs.Length; i++) { double r = precs[i]; Gaussian xFt = Gaussian.FromMeanAndPrecision(mean, r); evTrue[i] = IsPositiveOp.LogAverageFactor(true, xFt) + precPrior.GetLogProb(r); evApprox[i] = IsPositiveOp.LogAverageFactor(true, xF) + precPrior.GetLogProb(r) + xB.GetLogAverageOf(xFt) - xB.GetLogAverageOf(xF); evApprox2[i] = IsPositiveOp.LogAverageFactor(true, xF) + precPrior.GetLogProb(r0) + q.GetLogProb(r) - q.GetLogProb(r0); sum += System.Math.Exp(evApprox[i]); sum2 += System.Math.Exp(evApprox2[i]); } Console.WriteLine("r0 = {0}: {1} {2} {3}", r0, sum, sum2, q.GetVariance() + System.Math.Pow(r0 - q.GetMean(), 2)); //TODO: change path for cross platform using using (var writer = new MatlabWriter(@"..\..\..\Tests\student.mat")) { writer.Write("z", evTrue); writer.Write("z2", evApprox); writer.Write("z3", evApprox2); writer.Write("precs", precs); } }
public void CanWriteComplexMatrices() { var mat1 = Matrix <Complex> .Build.Dense(5, 3); for (var i = 0; i < mat1.ColumnCount; i++) { mat1[i, i] = new Complex(i + .1, i + .1); } var mat2 = Matrix <Complex> .Build.Dense(4, 5); for (var i = 0; i < mat2.RowCount; i++) { mat2[i, i] = new Complex(i + .1, i + .1); } var mat3 = Matrix <Complex> .Build.Sparse(5, 4); mat3[0, 0] = new Complex(1.1, 1.1); mat3[0, 2] = new Complex(2.2, 2.2); mat3[4, 3] = new Complex(3.3, 3.3); var mat4 = Matrix <Complex> .Build.Sparse(3, 5); mat4[0, 0] = new Complex(1.1, 1.1); mat4[0, 2] = new Complex(2.2, 2.2); mat4[2, 4] = new Complex(3.3, 3.3); Matrix <Complex>[] write = { mat1, mat2, mat3, mat4 }; string[] names = { "mat1", "dense_matrix_2", "s1", "sparse2" }; if (File.Exists("testz.mat")) { File.Delete("testz.mat"); } MatlabWriter.Write("testz.mat", write, names); var read = MatlabReader.ReadAll <Complex>("testz.mat", names); Assert.AreEqual(write.Length, read.Count); for (var i = 0; i < write.Length; i++) { var w = write[i]; var r = read[names[i]]; Assert.AreEqual(w.RowCount, r.RowCount); Assert.AreEqual(w.ColumnCount, r.ColumnCount); Assert.IsTrue(w.Equals(r)); } File.Delete("testz.mat"); }
public void WriteBadMatricesThrowsArgumentException() { Matrix <float> matrix = Matrix <float> .Build.Dense(1, 1); Assert.Throws <ArgumentException>(() => MatlabWriter.Write("somefile3", matrix, string.Empty)); Assert.Throws <ArgumentException>(() => MatlabWriter.Write("somefile3", matrix, null)); Assert.Throws <ArgumentException>(() => MatlabWriter.Write("somefile3", matrix, "some matrix")); Assert.Throws <ArgumentException>(() => MatlabWriter.Write("somefile3", new[] { matrix }, new[] { string.Empty })); Assert.Throws <ArgumentException>(() => MatlabWriter.Write("somefile3", new[] { matrix }, new string[] { null })); Assert.Throws <ArgumentException>(() => MatlabWriter.Write("somefile3", new[] { matrix, matrix }, new[] { "matrix" })); Assert.Throws <ArgumentException>(() => MatlabWriter.Write("somefile3", new[] { matrix }, new[] { "some matrix" })); }
public void CanWriteDoubleMatrices() { Matrix <double> mat1 = Matrix <double> .Build.Dense(5, 5); for (var i = 0; i < mat1.ColumnCount; i++) { mat1[i, i] = i + .1; } Matrix <double> mat2 = Matrix <double> .Build.Dense(4, 5); for (var i = 0; i < mat2.RowCount; i++) { mat2[i, i] = i + .1; } Matrix <double> mat3 = Matrix <double> .Build.Sparse(5, 4); mat3[0, 0] = 1.1; mat3[0, 2] = 2.2; mat3[4, 3] = 3.3; Matrix <double> mat4 = Matrix <double> .Build.Sparse(3, 5); mat4[0, 0] = 1.1; mat4[0, 2] = 2.2; mat4[2, 4] = 3.3; Matrix <double>[] write = { mat1, mat2, mat3, mat4 }; string[] names = { "mat1", "dense_matrix_2", "s1", "sparse2" }; if (File.Exists("testd.mat")) { File.Delete("testd.mat"); } MatlabWriter.Write("testd.mat", write, names); var read = MatlabReader.ReadAll <double>("testd.mat", names); Assert.AreEqual(write.Length, read.Count); for (var i = 0; i < write.Length; i++) { var w = write[i]; var r = read[names[i]]; Assert.AreEqual(w.RowCount, r.RowCount); Assert.AreEqual(w.ColumnCount, r.ColumnCount); Assert.IsTrue(w.Equals(r)); } File.Delete("testd.mat"); }
public void MatlabWriteNumericNameTest() { string fileName = $"{System.IO.Path.GetTempPath()}MatlabWriteNumericNameTest{Environment.CurrentManagedThreadId}.mat"; using (MatlabWriter writer = new MatlabWriter(fileName)) { writer.Write("24", new int[0]); } Dictionary <string, object> vars = MatlabReader.Read(fileName); int[] ints = (int[])vars["24"]; Assert.Empty(ints); }
public static void ToLogisticSerializeToMat(string file, List <Tuple <Beta, Gaussian, Beta> > msgs, Dictionary <string, object> extra) { int n = msgs.Count; double[] outBetaA = new double[n]; double[] outBetaB = new double[n]; double[] inNormalMeans = new double[n]; double[] inNormalVariances = new double[n]; // alpha parameters double[] inBetaA = new double[n]; double[] inBetaB = new double[n]; for (int i = 0; i < msgs.Count; i++) { Tuple <Beta, Gaussian, Beta> pairI = msgs[i]; Beta toBeta = pairI.Item1; Gaussian fromX = pairI.Item2; Beta fromLogistic = pairI.Item3; fromX.GetMeanAndVariance(out inNormalMeans[i], out inNormalVariances[i]); outBetaA[i] = toBeta.TrueCount; outBetaB[i] = toBeta.FalseCount; double alpha = fromLogistic.TrueCount; double beta = fromLogistic.FalseCount; inBetaA[i] = alpha; inBetaB[i] = beta; } // write to .mat file MatlabWriter matWriter = new MatlabWriter(file); matWriter.Write("outBetaA", outBetaA); matWriter.Write("outBetaB", outBetaB); matWriter.Write("inNormalMeans", inNormalMeans); matWriter.Write("inNormalVariances", inNormalVariances); matWriter.Write("inBetaA", inBetaA); matWriter.Write("inBetaB", inBetaB); if (extra != null) { foreach (var kv in extra) { matWriter.Write(kv.Key, kv.Value); } } matWriter.Dispose(); }
//[DeploymentItem(@"Data\test.mat", "Data")] public void MatlabWriterTest() { Dictionary <string, object> dict = MatlabReader.Read(Path.Combine(TestUtils.DataFolderPath, "test.mat")); string fileName = $"{System.IO.Path.GetTempPath()}MatlabWriterTest{Environment.CurrentManagedThreadId}.mat"; using (MatlabWriter writer = new MatlabWriter(fileName)) { foreach (var entry in dict) { writer.Write(entry.Key, entry.Value); } } MatlabReaderTester(fileName); }
//保存数据 private void button1_Click(object sender, EventArgs e) { double[,] matdata = new double[2, dataList[0].Count]; for (var i = 0; i < 2; i++) { for (var j = 0; j < dataList.ElementAt(i).Count; j++) { matdata[i, j] = dataList.ElementAt(i).ElementAt(j); } } Matrix <double> matrix = Matrix <double> .Build.DenseOfArray(matdata); String path = "D:\\TestDataFile\\thmat\\" + DateTime.Now.ToString("MM -dd-H-mm-ss_") + ".mat"; MatlabWriter.Write(path, matrix, "data"); }
//[DeploymentItem(@"Data\test.mat", "Data")] public void MatlabWriterTest() { Dictionary <string, object> dict = MatlabReader.Read(Path.Combine( #if NETCORE Path.GetDirectoryName(typeof(PsychTests).Assembly.Location), // work dir is not the one with Microsoft.ML.Probabilistic.Tests.dll on netcore and neither is .Location on netfull #endif "Data", "test.mat")); string fileName = $"{System.IO.Path.GetTempPath()}MatlabWriterTest{Environment.CurrentManagedThreadId}.mat"; using (MatlabWriter writer = new MatlabWriter(fileName)) { foreach (var entry in dict) { writer.Write(entry.Key, entry.Value); } } MatlabReaderTester(fileName); }
public void SaveToFile(string fileName) { // add simulation date and time // add simulation configuration // add add initial state of plane var matrices = new List <MatlabMatrix> { MatlabWriter.Pack(mb.DenseOfRowVectors(position_record), "position_record"), MatlabWriter.Pack(mb.DenseOfRowMajor(Vk_record.Count, 1, Vk_record), "Vk_record"), MatlabWriter.Pack(mb.DenseOfRowMajor(phi_record.Count, 1, phi_record), "phi_record"), MatlabWriter.Pack(mb.DenseOfRowMajor(psi_record.Count, 1, psi_record), "psi_record"), MatlabWriter.Pack(mb.DenseOfRowMajor(theta_record.Count, 1, theta_record), "theta_record"), MatlabWriter.Pack(mb.DenseOfRowMajor(current_T_record.Count, 1, current_T_record), "current_T_record"), MatlabWriter.Pack(mb.DenseOfRowMajor(time_record.Count, 1, time_record), "time_record"), }; MatlabWriter.Store(fileName, matrices); }
public void MatlabMatrixRoundtrip() { var denseDouble = Matrix <double> .Build.Random(20, 20); var denseComplex = Matrix <Complex> .Build.Random(10, 10); var diagonalDouble = Matrix <double> .Build.DiagonalOfDiagonalArray(new[] { 1.0, 2.0, 3.0 }); var sparseDouble = Matrix <double> .Build.Sparse(20, 20, (i, j) => i % (j + 1) == 2?i + 10 *j : 0); var denseDoubleP = MatlabWriter.Pack(denseDouble, "denseDouble"); var denseComplexP = MatlabWriter.Pack(denseComplex, "denseComplex"); var diagonalDoubleP = MatlabWriter.Pack(diagonalDouble, "diagonalDouble"); var sparseDoubleP = MatlabWriter.Pack(sparseDouble, "sparseDouble"); Assert.That(MatlabReader.Unpack <double>(denseDoubleP).Equals(denseDouble)); Assert.That(MatlabReader.Unpack <Complex>(denseComplexP).Equals(denseComplex)); Assert.That(MatlabReader.Unpack <double>(diagonalDoubleP).Equals(diagonalDouble)); Assert.That(MatlabReader.Unpack <double>(sparseDoubleP).Equals(sparseDouble)); Assert.That(MatlabReader.Unpack <double>(denseDoubleP).Storage, Is.TypeOf <DenseColumnMajorMatrixStorage <double> >()); Assert.That(MatlabReader.Unpack <Complex>(denseComplexP).Storage, Is.TypeOf <DenseColumnMajorMatrixStorage <Complex> >()); Assert.That(MatlabReader.Unpack <double>(diagonalDoubleP).Storage, Is.TypeOf <SparseCompressedRowMatrixStorage <double> >()); Assert.That(MatlabReader.Unpack <double>(sparseDoubleP).Storage, Is.TypeOf <SparseCompressedRowMatrixStorage <double> >()); if (File.Exists("testrt.mat")) { File.Delete("testrt.mat"); } MatlabWriter.Store("testrt.mat", new[] { denseDoubleP, denseComplexP, diagonalDoubleP, sparseDoubleP }); Assert.That(MatlabReader.Read <double>("testrt.mat", "denseDouble").Equals(denseDouble)); Assert.That(MatlabReader.Read <Complex>("testrt.mat", "denseComplex").Equals(denseComplex)); Assert.That(MatlabReader.Read <double>("testrt.mat", "diagonalDouble").Equals(diagonalDouble)); Assert.That(MatlabReader.Read <double>("testrt.mat", "sparseDouble").Equals(sparseDouble)); Assert.That(MatlabReader.Read <double>("testrt.mat", "denseDouble").Storage, Is.TypeOf <DenseColumnMajorMatrixStorage <double> >()); Assert.That(MatlabReader.Read <Complex>("testrt.mat", "denseComplex").Storage, Is.TypeOf <DenseColumnMajorMatrixStorage <Complex> >()); Assert.That(MatlabReader.Read <double>("testrt.mat", "diagonalDouble").Storage, Is.TypeOf <SparseCompressedRowMatrixStorage <double> >()); Assert.That(MatlabReader.Read <double>("testrt.mat", "sparseDouble").Storage, Is.TypeOf <SparseCompressedRowMatrixStorage <double> >()); File.Delete("testrt.mat"); }
/// <summary> /// Escribe la salida del clasificador como una matriz /// </summary> /// <param name="list"> lista de objetos OutputAndTarget o salidas del clasificador cntk</param> /// <param name="path"> direccion de destino del archivo .mat a crear</param> public static void saveDataMatlabMatrix(List <NetEvaluation.OutputAndTarget> list, string path) { Matrix <float> target = Matrix <float> .Build.Dense(list.Count, 1); //rows igual a el numero de registros //colums igual al numero de valores en el registro Matrix <float> salida = Matrix <float> .Build.Dense(list.Count, list[0].Output.Length); for (int i = 0; i < list.Count; i++) { target.At(i, 0, list[i].Target); salida.SetRow(i, list[i].Output); } var dict = new Dictionary <string, Matrix <float> >(); dict.Add("target", target); dict.Add("output", salida); MatlabWriter.Write(path, dict); }
public void Save(string file) { if (file.EndsWith(".mat")) { MatlabWriter.Write <Complex>(file, matrix, "A"); } else if (file.EndsWith(".mtx")) { MatrixMarketWriter.WriteMatrix <Complex>(file, matrix); } else if (file.EndsWith(".csv")) { DelimitedWriter.Write <Complex>(file, matrix, ";"); } else { throw new NotImplementedException(); } }
public void MatlabWriteStringListTest() { List <string> strings = new List <string>(); strings.Add("a"); strings.Add("b"); string fileName = $"{System.IO.Path.GetTempPath()}MatlabWriteStringListTest{Environment.CurrentManagedThreadId}.mat"; using (MatlabWriter writer = new MatlabWriter(fileName)) { writer.Write("strings", strings); } Dictionary <string, object> vars = MatlabReader.Read(fileName); string[] array = (string[])vars["strings"]; for (int i = 0; i < array.Length; i++) { Assert.Equal(strings[i], array[i]); } }
public void MatlabWriteStringDictionaryTest() { Dictionary <string, string> dictString = new Dictionary <string, string>(); dictString["a"] = "a"; dictString["b"] = "b"; string fileName = $"{System.IO.Path.GetTempPath()}MatlabWriteStringDictionaryTest{Environment.CurrentManagedThreadId}.mat"; using (MatlabWriter writer = new MatlabWriter(fileName)) { writer.Write("dictString", dictString); } Dictionary <string, object> vars = MatlabReader.Read(fileName); Dictionary <string, object> dict = (Dictionary <string, object>)vars["dictString"]; foreach (var entry in dictString) { Assert.Equal(dictString[entry.Key], dict[entry.Key]); } }
private void TestCollocationPointCreation() { var model = new CollocationModel(); ModelCreator modelCreator = new ModelCreator(model); string filename = "..\\..\\..\\InputFiles\\PlateWithHole.txt"; IsogeometricReader modelReader = new IsogeometricReader(modelCreator, filename); modelReader.CreateCollocationModelFromFile(); //var solverBuilder = new SuiteSparseSolver.Builder(); //solverBuilder.DofOrderer = new DofOrderer( // new NodeMajorDofOrderingStrategy(), new NullReordering()); var solverBuilder = new GmresSolver.Builder(); ISolver solver = new GmresSolver(model, new AsymmetricDofOrderer(new RowDofOrderingStrategy()), new DofOrderer(new NodeMajorDofOrderingStrategy(), new NullReordering())); // Structural problem provider var provider = new ProblemStructural(model, solver); // Linear static analysis var childAnalyzer = new LinearAnalyzer(model, solver, provider); var parentAnalyzer = new StaticAnalyzer(model, solver, provider, childAnalyzer); // Run the analysis parentAnalyzer.Initialize(); parentAnalyzer.BuildMatrices(); var k = solver.LinearSystems[0].Matrix; Matrix <double> kmatlab = MathNet.Numerics.LinearAlgebra.CreateMatrix.Dense <double>(k.NumRows, k.NumColumns); for (int i = 0; i < k.NumRows; i++) { for (int j = 0; j < k.NumColumns; j++) { kmatlab[i, j] = k[i, j]; } } MatlabWriter.Write("..\\..\\..\\InputFiles\\KcolMsolve.mat", kmatlab, "Ktotal"); }
public void WriteBadMatricesThrowsArgumentException() { var matrix = Matrix <float> .Build.Dense(1, 1); var filePath = Path.GetTempFileName(); try { Assert.Throws <ArgumentException>(() => MatlabWriter.Write(filePath, matrix, string.Empty)); Assert.Throws <ArgumentException>(() => MatlabWriter.Write(filePath, matrix, null)); Assert.Throws <ArgumentException>(() => MatlabWriter.Write(filePath, matrix, "some matrix")); Assert.Throws <ArgumentException>(() => MatlabWriter.Write(filePath, new[] { matrix }, new[] { string.Empty })); Assert.Throws <ArgumentException>(() => MatlabWriter.Write(filePath, new[] { matrix }, new string[] { null })); Assert.Throws <ArgumentException>(() => MatlabWriter.Write(filePath, new[] { matrix, matrix }, new[] { "matrix" })); Assert.Throws <ArgumentException>(() => MatlabWriter.Write(filePath, new[] { matrix }, new[] { "some matrix" })); } finally { File.Delete(filePath); } }
protected void SaveScaledTF(double scaleFactor) { var TFSrScaled = TFSr.Divide(scaleFactor); SaveFileDialog sfd = new SaveFileDialog(); //sfd.InitialDirectory = @"C:\Users\ConraN01\Documents\Spyder_WS\MRI_RF_TF_Tool_Project\Test Files for Python Utility\Raw Neuro Header Voltage Data Files"; sfd.Filter = "MAT (*.mat)|*.mat|All Files (*.*)|*.*"; sfd.Title = "Select output scaled Etan file..."; if (sfd.ShowDialog() != DialogResult.OK) { return; } var Z = CreateMatrix.DenseOfColumns(new IEnumerable <double>[] { TFz }); var Sr = CreateMatrix.DenseOfColumnVectors(new Vector <Complex>[] { TFSrScaled }); var matrices = new List <MatlabMatrix>(); matrices.Add(MatlabWriter.Pack(Z, "z")); matrices.Add(MatlabWriter.Pack(Sr, "Sr")); MatlabWriter.Store(sfd.FileName, matrices); }
static void Main() { Func <double, double> Result = ((double result) => { return(Math.Pow(Math.E, -result)); }); Console.WriteLine("...bayes_estimation2 Start!!!..."); Class_p pre1 = new Class_p(); MWArray max_m1 = (MWNumericArray)pre1.pre_process(); f_method fsolve_r1 = new f_method(); f_method1 fmincon_r = new f_method1(); Classi integratef = new Classi(); Classi1 integratef1 = new Classi1(); MWCharArray str2 = "theta"; MWCharArray str2f = "n"; string str1tt = "(theta^" + 0.ToString() + "/factorial(" + 0.ToString() + ")*exp(1)^(-theta))", str1t; Console.WriteLine("please assign the number of points you want deal with:"); int i, j, data_range = int.Parse(Console.ReadLine()); for (i = 0; i <= data_range; i++) { str1tt = str1tt + "*" + "(theta^" + i.ToString() + "/factorial(" + i.ToString() + ")*exp(1)^(-theta))"; } string str1ttf = "(theta^n/factorial(n)*exp(1)^(-theta))" + "*" + str1tt; double[,] max_m2 = (double[, ])max_m1.ToArray(); var max_m3 = DenseMatrix.OfArray(max_m2); int s1 = max_m2.GetLength(0), s2 = max_m2.GetLength(1); var result_p = new DenseMatrix((int)Math.Ceiling((double)(s1 / data_range)), s2); var result_p1 = new DenseVector(s2); double[] std_r; long stop_Value = 0, start_Value = 0, freq = 0; QueryPerformanceFrequency(ref freq); //获取CPU频率 QueryPerformanceCounter(ref start_Value); //获取初始前值 var count1f = new DenseMatrix((int)Math.Ceiling((double)(s1 / data_range)), s2); double[] count1t = new double[s2]; MathNet.Numerics.LinearAlgebra.Matrix <double> tmp_m1; MWArray[] argsOut = new MWArray[3]; MWArray[] argsOut1 = new MWArray[2]; MWArray[] argsIn = new MWArray[6]; MWArray[] argsIn1 = new MWArray[5]; MWArray[] argsIn1f = new MWArray[4]; MWArray[] argsOutf1 = new MWArray[1]; MWArray[] argsIn1f1 = new MWArray[7]; MWArray[] argsOutf2 = new MWArray[1]; int exitflag = 0; int allmins = 0; for (i = 0; i < s2; i++) { //s2 / data_range??? for (j = 0; j < (int)Math.Ceiling((double)(s1 / data_range)); j++) { int count1 = 0; if ((j + 1) * data_range - 1 > max_m3.RowCount) { tmp_m1 = max_m3.SubMatrix(j * data_range, data_range - ((j + 1) * data_range - max_m3.ColumnCount), i, 1); } else { tmp_m1 = max_m3.SubMatrix(j * data_range, data_range, i, 1); } for (int ii = 0; ii < tmp_m1.RowCount; ii++) { for (int jj = 0; jj < tmp_m1.ColumnCount; jj++) { if (Math.Abs(tmp_m1[ii, jj]) > 0.9 * max_m3.SubMatrix(0, max_m3.RowCount, i, 1).Enumerate().Max()) { count1++; } } } count1f[j, i] = count1; count1t[i] = count1t[i] + count1; } count1t[i] = count1t[i] / Math.Ceiling((double)(s1 / data_range)); } for (i = 0; i < s2; i++) { //s2 / data_range??? for (j = 0; j < (int)Math.Ceiling((double)(s1 / data_range)); j++) { str1t = "theta^" + count1f[j, i].ToString() + "/factorial(" + ((int)Math.Round(count1f[j, i])).ToString() + ")*exp(1)^(-theta)-" + (count1f[j, i] / data_range).ToString(); MWCharArray str1 = new MWCharArray(str1t); argsIn[0] = str1; argsIn[1] = str2; argsIn[2] = 1; argsIn[5] = 100; argsOut[0] = result_p[j, i]; argsOut[1] = exitflag; argsOut[2] = allmins; if (count1f[j, i] <= count1t[i]) { argsIn[3] = 0; argsIn[4] = count1t[i]; fsolve_r1.fsolve_r1(3, ref argsOut, argsIn); result_p[j, i] = ((MWNumericArray)argsOut[0]).ToScalarDouble(); } else { argsIn[3] = count1t[i]; argsIn[4] = data_range; fsolve_r1.fsolve_r1(3, ref argsOut, argsIn); result_p[j, i] = ((MWNumericArray)argsOut[0]).ToScalarDouble(); } } } std_r = (double[])Std_t1(result_p); for (i = 0; i < s2; i++) { string str3 = "((1/(sqrt(2*pi)*" + std_r[i].ToString() + "))*exp(((theta-" + count1t[i].ToString() + ")^2)/" + "(-2*" + std_r[i].ToString() + "^2)))"; str1tt = str1tt + "*" + str3; argsIn1f[0] = str1tt; argsIn1f[1] = str2; argsIn1f[2] = 0; argsIn1f[3] = data_range; integratef.integrate(1, ref argsOutf1, argsIn1f); result_p1[i] = ((MWNumericArray)argsOutf1[0]).ToScalarDouble(); str1tt = str1tt + "/" + result_p1[i].ToString(); str1tt = str1tt + "*" + str1ttf + "*" + str2f;; argsIn1f1[0] = str1tt; argsIn1f1[1] = str2; argsIn1f1[2] = str2f; argsIn1f1[3] = 0; argsIn1f1[4] = data_range; argsIn1f1[5] = 0; argsIn1f1[6] = data_range; integratef1.integrate1(1, ref argsOutf2, argsIn1f1); result_p1[i] = ((MWNumericArray)argsOutf2[0]).ToScalarDouble(); } //for (i = 0; i < s2; i++) //{ // MWCharArray str1tt1; // //s2 / data_range??? // for (j = 0; j < (int)Math.Ceiling((double)(s1 / data_range)); j++) // { // string str3 = "((1/(sqrt(2*pi)*" + std_r[i].ToString() + "))*exp(((theta-" + count1t[i].ToString() + ")^2)/" + "(-2*" + std_r[i].ToString() + "^2)))"; // str1tt = str1tt + "*" + str3; // str1tt1 = new MWCharArray(str1tt); // argsIn1[0] = str1tt1; // argsIn1[1] = str2; // argsIn1[2] = result_p[j, i]; // argsIn1[3] = 0; // argsIn1[4] = data_range; // argsOut1[0] = result_p[j, i]; // argsOut1[1] = exitflag; // fmincon_r.fmincon_r(2, ref argsOut1, argsIn1); // result_p[j, i] = Result(((MWNumericArray)argsOut1[0]).ToScalarDouble()); // } //} MatlabWriter.Write("result_p1.mat", result_p, "result_p1"); QueryPerformanceCounter(ref stop_Value);//获取终止变量值 var times2 = (stop_Value - start_Value) / (double)freq * 1000; Console.WriteLine("RunTime " + times2 / 1000 + "s"); }
internal void HeteroscedasticGPR() { // This model is based on the paper "Most Likely Heteroscedastic Gaussian Process Regression" by Kersting et al, ICML 2007 // Silverman's motorcycle benchmark dataset double[] inputs = new double[] { 2.4, 2.6, 3.2, 3.6, 4, 6.2, 6.6, 6.8, 7.8, 8.199999999999999, 8.800000000000001, 8.800000000000001, 9.6, 10, 10.2, 10.6, 11, 11.4, 13.2, 13.6, 13.8, 14.6, 14.6, 14.6, 14.6, 14.6, 14.6, 14.8, 15.4, 15.4, 15.4, 15.4, 15.6, 15.6, 15.8, 15.8, 16, 16, 16.2, 16.2, 16.2, 16.4, 16.4, 16.6, 16.8, 16.8, 16.8, 17.6, 17.6, 17.6, 17.6, 17.8, 17.8, 18.6, 18.6, 19.2, 19.4, 19.4, 19.6, 20.2, 20.4, 21.2, 21.4, 21.8, 22, 23.2, 23.4, 24, 24.2, 24.2, 24.6, 25, 25, 25.4, 25.4, 25.6, 26, 26.2, 26.2, 26.4, 27, 27.2, 27.2, 27.2, 27.6, 28.2, 28.4, 28.4, 28.6, 29.4, 30.2, 31, 31.2, 32, 32, 32.8, 33.4, 33.8, 34.4, 34.8, 35.2, 35.2, 35.4, 35.6, 35.6, 36.2, 36.2, 38, 38, 39.2, 39.4, 40, 40.4, 41.6, 41.6, 42.4, 42.8, 42.8, 43, 44, 44.4, 45, 46.6, 47.8, 47.8, 48.8, 50.6, 52, 53.2, 55, 55, 55.4, 57.6 }; double[] outputs = new double[] { 0, -1.3, -2.7, 0, -2.7, -2.7, -2.7, -1.3, -2.7, -2.7, -1.3, -2.7, -2.7, -2.7, -5.4, -2.7, -5.4, 0, -2.7, -2.7, 0, -13.3, -5.4, -5.4, -9.300000000000001, -16, -22.8, -2.7, -22.8, -32.1, -53.5, -54.9, -40.2, -21.5, -21.5, -50.8, -42.9, -26.8, -21.5, -50.8, -61.7, -5.4, -80.40000000000001, -59, -71, -91.09999999999999, -77.7, -37.5, -85.59999999999999, -123.1, -101.9, -99.09999999999999, -104.4, -112.5, -50.8, -123.1, -85.59999999999999, -72.3, -127.2, -123.1, -117.9, -134, -101.9, -108.4, -123.1, -123.1, -128.5, -112.5, -95.09999999999999, -81.8, -53.5, -64.40000000000001, -57.6, -72.3, -44.3, -26.8, -5.4, -107.1, -21.5, -65.59999999999999, -16, -45.6, -24.2, 9.5, 4, 12, -21.5, 37.5, 46.9, -17.4, 36.2, 75, 8.1, 54.9, 48.2, 46.9, 16, 45.6, 1.3, 75, -16, -54.9, 69.59999999999999, 34.8, 32.1, -37.5, 22.8, 46.9, 10.7, 5.4, -1.3, -21.5, -13.3, 30.8, -10.7, 29.4, 0, -10.7, 14.7, -1.3, 0, 10.7, 10.7, -26.8, -14.7, -13.3, 0, 10.7, -14.7, -2.7, 10.7, -2.7, 10.7 }; Range j = new Range(inputs.Length); Vector[] inputsVec = Util.ArrayInit(inputs.Length, i => Vector.FromArray(inputs[i])); VariableArray <Vector> x = Variable.Observed(inputsVec, j).Named("x"); VariableArray <double> y = Variable.Observed(outputs, j).Named("y"); // Set up the GP prior, which will be filled in later Variable <SparseGP> prior = Variable.New <SparseGP>().Named("prior"); Variable <SparseGP> prior2 = Variable.New <SparseGP>().Named("prior2"); // The sparse GP variable - a distribution over functions Variable <IFunction> f = Variable <IFunction> .Random(prior).Named("f"); Variable <IFunction> r = Variable <IFunction> .Random(prior2).Named("r"); Variable <double> mean = Variable.FunctionEvaluate(f, x[j]).Named("mean"); Variable <double> logVariance = Variable.FunctionEvaluate(r, x[j]).Named("logVariance"); Variable <double> variance = Variable.Exp(logVariance); y[j] = Variable.GaussianFromMeanAndVariance(mean, variance); InferenceEngine engine = new InferenceEngine(); GaussianProcess gp = new GaussianProcess(new ConstantFunction(0), new SquaredExponential(0)); GaussianProcess gp2 = new GaussianProcess(new ConstantFunction(0), new SquaredExponential(0)); // Fill in the sparse GP prior //Vector[] basis = Util.ArrayInit(120, i => Vector.FromArray(0.5*i)); Vector[] basis = Util.ArrayInit(60, i => Vector.FromArray(1.0 * i)); prior.ObservedValue = new SparseGP(new SparseGPFixed(gp, basis)); prior2.ObservedValue = new SparseGP(new SparseGPFixed(gp2, basis)); // Infer the posterior Sparse GP SparseGP sgp = engine.Infer <SparseGP>(f); // Check that training set is classified correctly Console.WriteLine(); Console.WriteLine("Predictions on training set:"); for (int i = 0; i < outputs.Length; i++) { Gaussian post = sgp.Marginal(inputsVec[i]); //double postMean = post.GetMean(); Console.WriteLine("f({0}) = {1}", inputs[i], post); } //TODO: change path for cross platform using using (MatlabWriter writer = new MatlabWriter(@"..\..\HGPR.mat")) { int n = outputs.Length; double[] m = new double[n]; double[] s = new double[n]; for (int i = 0; i < n; i++) { Gaussian post = sgp.Marginal(inputsVec[i]); double mi, vi; post.GetMeanAndVariance(out mi, out vi); m[i] = mi; s[i] = System.Math.Sqrt(vi); } writer.Write("mean", m); writer.Write("std", s); } }
public void Sample(Options options, Matrix data) { if (options.numParams > 2) { throw new Exception("numParams > 2"); } int numStudents = data.Rows; int numQuestions = data.Cols; // initialize the sampler at the mean of the priors (not sampling from the priors) double abilityMean = abilityMeanPrior.GetMean(); double abilityPrec = abilityPrecPrior.GetMean(); double difficultyMean = difficultyMeanPrior.GetMean(); double difficultyPrec = difficultyPrecPrior.GetMean(); double discriminationMean = discriminationMeanPrior.GetMean(); double discriminationPrec = discriminationPrecPrior.GetMean(); double[] ability = new double[numStudents]; double[] difficulty = new double[numQuestions]; List <double>[] difficultySamples = new List <double> [numQuestions]; GaussianEstimator[] difficultyEstimator = new GaussianEstimator[numQuestions]; for (int question = 0; question < numQuestions; question++) { difficultyEstimator[question] = new GaussianEstimator(); difficultySamples[question] = new List <double>(); if (difficultyObserved != null) { difficulty[question] = difficultyObserved[question]; difficultyEstimator[question].Add(difficultyObserved[question]); difficultySamples[question].Add(difficultyObserved[question]); } } List <double>[] abilitySamples = new List <double> [numStudents]; GaussianEstimator[] abilityEstimator = new GaussianEstimator[ability.Length]; for (int student = 0; student < abilityEstimator.Length; student++) { abilityEstimator[student] = new GaussianEstimator(); abilitySamples[student] = new List <double>(); if (abilityObserved != null) { ability[student] = abilityObserved[student]; abilityEstimator[student].Add(abilityObserved[student]); abilitySamples[student].Add(abilityObserved[student]); } } double[] discrimination = new double[numQuestions]; List <double>[] discriminationSamples = new List <double> [numQuestions]; GammaEstimator[] discriminationEstimator = new GammaEstimator[numQuestions]; for (int question = 0; question < numQuestions; question++) { discriminationEstimator[question] = new GammaEstimator(); discriminationSamples[question] = new List <double>(); discrimination[question] = 1; if (discriminationObserved != null) { discrimination[question] = discriminationObserved[question]; discriminationEstimator[question].Add(discriminationObserved[question]); discriminationSamples[question].Add(discriminationObserved[question]); } } responseProbMean = new Matrix(numStudents, numQuestions); int niters = options.numberOfSamples; int burnin = options.burnIn; double logisticVariance = Math.PI * Math.PI / 3; double shape = 4.5; Gamma precPrior = Gamma.FromShapeAndRate(shape, (shape - 1) * logisticVariance); precPrior = Gamma.PointMass(1); double[,] prec = new double[numStudents, numQuestions]; double[,] x = new double[numStudents, numQuestions]; int numRejected = 0, numAttempts = 0; for (int iter = 0; iter < niters; iter++) { for (int student = 0; student < numStudents; student++) { for (int question = 0; question < numQuestions; question++) { // sample prec given ability, difficulty, x // N(x; ability-difficulty, 1/prec) = Gamma(prec; 1.5, (x-ability+difficulty)^2/2) Gamma precPost = precPrior; double xMean = (ability[student] - difficulty[question]) * discrimination[question]; double delta = x[student, question] - xMean; Gamma like = Gamma.FromShapeAndRate(1.5, 0.5 * delta * delta); precPost.SetToProduct(precPost, like); prec[student, question] = precPost.Sample(); // sample x given ability, difficulty, prec, data // using an independence chain MH bool y = (data[student, question] > 0); double sign = y ? 1.0 : -1.0; Gaussian xPrior = Gaussian.FromMeanAndPrecision(xMean, prec[student, question]); // we want to sample from xPrior*I(x>0) // instead we sample from xPost Gaussian xPost = xPrior * IsPositiveOp.XAverageConditional(y, xPrior); double oldx = x[student, question]; double newx = xPost.Sample(); numAttempts++; if (newx * sign < 0) { newx = oldx; // rejected numRejected++; } else { // importance weights double oldw = xPrior.GetLogProb(oldx) - xPost.GetLogProb(oldx); double neww = xPrior.GetLogProb(newx) - xPost.GetLogProb(newx); // acceptance ratio double paccept = Math.Exp(neww - oldw); if (paccept < 1 && Rand.Double() > paccept) { newx = oldx; // rejected numRejected++; } } x[student, question] = newx; if (iter >= burnin) { double responseProb = MMath.Logistic(xMean); responseProbMean[student, question] += responseProb; } } } if (abilityObserved == null) { // sample ability given difficulty, prec, x for (int student = 0; student < numStudents; student++) { Gaussian post = Gaussian.FromMeanAndPrecision(abilityMean, abilityPrec); for (int question = 0; question < numQuestions; question++) { // N(x; disc*(ability-difficulty), 1/prec) =propto N(x/disc; ability-difficulty, 1/disc^2/prec) = N(ability; x/disc+difficulty, 1/disc^2/prec) Gaussian abilityLike = Gaussian.FromMeanAndPrecision(x[student, question] / discrimination[question] + difficulty[question], prec[student, question] * discrimination[question] * discrimination[question]); post.SetToProduct(post, abilityLike); } ability[student] = post.Sample(); if (iter >= burnin) { abilityEstimator[student].Add(post); abilitySamples[student].Add(ability[student]); } } } // sample difficulty given ability, prec, x for (int question = 0; question < numQuestions; question++) { Gaussian post = Gaussian.FromMeanAndPrecision(difficultyMean, difficultyPrec); for (int student = 0; student < numStudents; student++) { // N(x; disc*(ability-difficulty), 1/prec) =propto N(x/disc; ability-difficulty, 1/disc^2/prec) = N(difficulty; ability-x/disc, 1/disc^2/prec) if (discrimination[question] > 0) { Gaussian like = Gaussian.FromMeanAndPrecision(ability[student] - x[student, question] / discrimination[question], prec[student, question] * discrimination[question] * discrimination[question]); post.SetToProduct(post, like); } } difficulty[question] = post.Sample(); if (iter >= burnin) { //if (difficulty[question] > 100) // Console.WriteLine("difficulty[{0}] = {1}", question, difficulty[question]); difficultyEstimator[question].Add(post); difficultySamples[question].Add(difficulty[question]); } } if (options.numParams > 1 && discriminationObserved == null) { // sample discrimination given ability, difficulty, prec, x for (int question = 0; question < numQuestions; question++) { // moment-matching on the prior Gaussian approxPrior = Gaussian.FromMeanAndVariance(Math.Exp(discriminationMean + 0.5 / discriminationPrec), Math.Exp(2 * discriminationMean + 1 / discriminationPrec) * (Math.Exp(1 / discriminationPrec) - 1)); Gaussian post = approxPrior; for (int student = 0; student < numStudents; student++) { // N(x; disc*delta, 1/prec) =propto N(x/delta; disc, 1/prec/delta^2) double delta = ability[student] - difficulty[question]; if (delta > 0) { Gaussian like = Gaussian.FromMeanAndPrecision(x[student, question] / delta, prec[student, question] * delta * delta); post.SetToProduct(post, like); } } TruncatedGaussian postTrunc = new TruncatedGaussian(post, 0, double.PositiveInfinity); double olddisc = discrimination[question]; double newdisc = postTrunc.Sample(); // importance weights Func <double, double> priorLogProb = delegate(double d) { double logd = Math.Log(d); return(Gaussian.GetLogProb(logd, discriminationMean, 1 / discriminationPrec) - logd); }; double oldw = priorLogProb(olddisc) - approxPrior.GetLogProb(olddisc); double neww = priorLogProb(newdisc) - approxPrior.GetLogProb(newdisc); // acceptance ratio double paccept = Math.Exp(neww - oldw); if (paccept < 1 && Rand.Double() > paccept) { // rejected } else { discrimination[question] = newdisc; } if (iter >= burnin) { discriminationEstimator[question].Add(discrimination[question]); discriminationSamples[question].Add(discrimination[question]); } } } // sample abilityMean given ability, abilityPrec Gaussian abilityMeanPost = abilityMeanPrior; for (int student = 0; student < numStudents; student++) { Gaussian like = GaussianOp.MeanAverageConditional(ability[student], abilityPrec); abilityMeanPost *= like; } abilityMean = abilityMeanPost.Sample(); // sample abilityPrec given ability, abilityMean Gamma abilityPrecPost = abilityPrecPrior; for (int student = 0; student < numStudents; student++) { Gamma like = GaussianOp.PrecisionAverageConditional(ability[student], abilityMean); abilityPrecPost *= like; } abilityPrec = abilityPrecPost.Sample(); // sample difficultyMean given difficulty, difficultyPrec Gaussian difficultyMeanPost = difficultyMeanPrior; for (int question = 0; question < numQuestions; question++) { Gaussian like = GaussianOp.MeanAverageConditional(difficulty[question], difficultyPrec); difficultyMeanPost *= like; } difficultyMean = difficultyMeanPost.Sample(); // sample difficultyPrec given difficulty, difficultyMean Gamma difficultyPrecPost = difficultyPrecPrior; for (int question = 0; question < numQuestions; question++) { Gamma like = GaussianOp.PrecisionAverageConditional(difficulty[question], difficultyMean); difficultyPrecPost *= like; } difficultyPrec = difficultyPrecPost.Sample(); // sample discriminationMean given discrimination, discriminationPrec Gaussian discriminationMeanPost = discriminationMeanPrior; for (int question = 0; question < numQuestions; question++) { Gaussian like = GaussianOp.MeanAverageConditional(Math.Log(discrimination[question]), discriminationPrec); discriminationMeanPost *= like; } discriminationMean = discriminationMeanPost.Sample(); // sample discriminationPrec given discrimination, discriminationMean Gamma discriminationPrecPost = discriminationPrecPrior; for (int question = 0; question < numQuestions; question++) { Gamma like = GaussianOp.PrecisionAverageConditional(Math.Log(discrimination[question]), discriminationMean); discriminationPrecPost *= like; } discriminationPrec = discriminationPrecPost.Sample(); //if (iter % 1 == 0) // Console.WriteLine("iter = {0}", iter); } //Console.WriteLine("abilityMean = {0}, abilityPrec = {1}", abilityMean, abilityPrec); //Console.WriteLine("difficultyMean = {0}, difficultyPrec = {1}", difficultyMean, difficultyPrec); int numSamplesUsed = niters - burnin; responseProbMean.Scale(1.0 / numSamplesUsed); //Console.WriteLine("acceptance rate = {0}", ((double)numAttempts - numRejected)/numAttempts); difficultyPost = Array.ConvertAll(difficultyEstimator, est => est.GetDistribution(Gaussian.Uniform())); abilityPost = Array.ConvertAll(abilityEstimator, est => est.GetDistribution(Gaussian.Uniform())); if (options.numParams > 1) { discriminationPost = Array.ConvertAll(discriminationEstimator, est => est.GetDistribution(new Gamma())); } abilityCred = GetCredibleIntervals(options.credibleIntervalProbability, abilitySamples); difficultyCred = GetCredibleIntervals(options.credibleIntervalProbability, difficultySamples); bool saveSamples = false; if (saveSamples) { using (MatlabWriter writer = new MatlabWriter(@"..\..\samples.mat")) { int q = 11; writer.Write("difficulty", difficultySamples[q]); writer.Write("discrimination", discriminationSamples[q]); } } }
public void RecordInferNETTime() { /**Records time by infer.net*/ /** * Only one W just like in Ali's paper. * In practice, we typically observe multiple sets of observations * where we want to do inference on the same model with the same * parameter. */ Rand.Restart(init_fixed_seed); Vector w = Vector.Zero(d); Rand.Normal(Vector.Zero(d), PositiveDefiniteMatrix.Identity(d), w); // Create the Logistic operator instance only one because we want to use the same // one after a new problem (new seed). // stopwatch for measuring inference time for each problem Stopwatch watch = new Stopwatch(); Type logisticOp = typeof(LogisticOp2); LogisticOp2.Watch = watch; List <long> allInferTimes = new List <long>(); var allPosteriors = new List <VectorGaussian>(); LogisticOp2.IsCollectLogisticMessages = false; LogisticOp2.IsCollectProjMsgs = false; LogisticOp2.IsCollectXMessages = false; for (int seed = seed_from; seed <= seed_to; seed++) { Rand.Restart(seed); double b = 0; // combine the bias term into W Vector[] X; bool[] Y; LogisticRegression.GenData(n, w, b, out X, out Y, seed); Console.Write("Y: "); StringUtils.PrintArray(Y); VectorGaussian wPost; // start the watch watch.Restart(); LogisticRegression.InferCoefficientsNoBias(X, Y, out wPost, epIter, logisticOp); // stop the watch long inferenceTime = watch.ElapsedMilliseconds; allInferTimes.Add(inferenceTime); allPosteriors.Add(wPost); //print Console.WriteLine("n: {0}", n); Console.WriteLine("d: {0}", d); int t = Y.Sum(o => o ? 1 : 0); Console.WriteLine("number of true: {0}", t); Console.WriteLine("True bias: {0}", b); // Vector meanW = wPost.GetMean(); Console.WriteLine("True w: {0}", w); Console.WriteLine("Inferred w: "); Console.WriteLine(wPost); } string fnameM = string.Format("rec_dnet_n{0}_logistic_iter{1}_sf{2}_st{3}.mat", n, epIter, seed_from, seed_to); string recordPathM = Config.PathToSavedFile(fnameM); MatlabWriter writer = new MatlabWriter(recordPathM); writer.Write("allInferTimes", MatrixUtils.ToDouble(allInferTimes)); Vector[] postMeans = allPosteriors.Select(vg => vg.GetMean()).ToArray(); Matrix[] postCovs = allPosteriors.Select(vg => vg.GetVariance()).ToArray(); writer.Write("postMeans", postMeans); writer.Write("postCovs", postCovs); writer.Write("dim", d); writer.Write("n", n); writer.Write("epIter", epIter); writer.Write("seed_from", seed_from); writer.Write("seed_to", seed_to); writer.Write("init_fixed_seed", init_fixed_seed); writer.Dispose(); }
static void Main(string[] args) { #region 保存txt文件格式 //decimal[] myValue = new decimal[] { 1.111111888m, 2.111111888m, 3.111111888m, 4.111111888m }; //double[] myValueDb = new double[myValue.Length]; //for (int i = 0; i < myValue.Length; i++) //{ // myValueDb[i] = (double)decimal.Round(myValue[i], 6); //} //string[] mydateTime = new string[] { DateTime.Now.ToString("F"), DateTime.Now.ToString("F"), DateTime.Now.ToString("F"), DateTime.Now.ToString("F") }; //string fileName = @"C:\Users\asus\Desktop\1.txt"; //using (FileStream fs = File.Open(fileName, FileMode.Create)) //{ // StreamWriter sw = new StreamWriter(fs); // sw.Write("序号|"); // sw.Write(" 时间 |"); // sw.WriteLine(" 值 "); // sw.WriteLine(); // for (int i = 0; i < myValue.Length; i++) // { // sw.Write(" " + (i + 1) + " |" + mydateTime[i].ToString() + " |"); // sw.WriteLine(" " + myValueDb[i].ToString()); // } // sw.Flush(); // sw.Close(); //} #endregion #region 保存csv文件格式 //string[] mydateTime = new string[] { DateTime.Now.ToString("F"), DateTime.Now.ToString("F"), DateTime.Now.ToString("F"), DateTime.Now.ToString("F") }; //decimal[] myValue = new decimal[] { 1.1111118m, 2.1111118m, 3.1111118m, 4.1111118m }; //double[] myValueDb = new double[myValue.Length]; //for (int i = 0; i < myValue.Length; i++) //{ // myValueDb[i] = (double)decimal.Round(myValue[i], 6); //} //string fileName = @"C:\Users\asus\Desktop\1.csv"; //StreamWriter sw = new StreamWriter(fileName, false, Encoding.UTF8); //sw.Write("序号" + "," + "时间" + "," + "值"); //sw.Write(Environment.NewLine); //for (int i = 0; i < myValue.Length; i++) //{ // sw.Write((i + 1) + "," + mydateTime[i] + "," + myValueDb[i]); // sw.Write(Environment.NewLine); //} //sw.Flush(); //sw.Close(); #endregion #region 保存mat格式 FileStream fs = File.Create(@"C:\Users\asus\Desktop\collection2.mat"); fs.Close(); double[] X = new double[] { 1.1111118f, 2.1111118f, 3.1111118f, 4.1111118f, 5.1111118f }; double[] Y = new double[] { 9.1111118f, 9.1111118f, 9.1111118f, 9.1111118f, 10.1111118f }; double[,] X_Y = new double[5, 2]; for (int i = 0; i < X.Length; i++) { X_Y[i, 0] = Math.Round(X[i], 7); X_Y[i, 1] = Math.Round(Y[i], 7); } var mb = Matrix <double> .Build; var myMatrix = mb.Dense(X.Length, 2, (i, j) => X_Y[i, j]); MatlabWriter.Write <double>(@"C:\Users\asus\Desktop\collection2.mat", myMatrix, "a"); Console.WriteLine(myMatrix.ToString("0.000000")); Console.ReadKey(); #endregion }
public void WriteNullMatrixThrowsArgumentNullException() { Assert.Throws <ArgumentNullException>(() => MatlabWriter.Write <double>("somefile2", null, "matrix")); }