Math.NET Numericshttp://mathnetnumerics.codeplex.com/project/feeds/rssMath.NET Numerics is an open source numerical library for the .NET Framework and Mono.New Post: add several vectorshttp://mathnetnumerics.codeplex.com/discussions/567261<div style="line-height: normal;">Assuming all of the "vectors" are the same length (if they aren't, then the result will be the same length as the shortest):<br />
<pre><code>List<double[]> lstVectors = ...;
double[] vectorSum = lstVectors.Aggregate((a,b) => a.Zip(b, (aa, bb) => aa + bb).ToArray());</code></pre>
Not the most efficient, but it ought to do the job.<br />
<br />
(For some reason the "plus" in the expression above is getting converted to the XML entity notation? But not in the editor Preview...)<br />
</div>MattHeffronFri, 19 Sep 2014 18:22:52 GMTNew Post: add several vectors 20140919062252PNew Post: Implementing Power, exponential and logarithmic function as best fit in C#http://mathnetnumerics.codeplex.com/discussions/567581<div style="line-height: normal;">Greetings,
<br />
Can somebody help me implementing following formula’s in C# for best fit?
<br />
<br />
Logarithmic Trendline
<br />
The Logarithmic trendline fits a line to ln(x), y–that is, y = a*ln(x) + b.
<br />
<br />
Exponential Trendline
<br />
The Exponential trendline fits the function y = a * e ^(b * x). This can also be computed using LinearRegression by fitting a line to x, ln(y). Taking the log of both sides of the function gives:
<br />
ln(y) = ln(a * e ^(b * x)) = ln(a) + bx
<br />
<br />
Power Trendline
<br />
The Power trendline fits the function y = a * x^b. This can be computed using LinearRegression by fitting a line to ln(x), ln(y). Taking the log of both sides of the equation gives:
<br />
ln(y) = ln(a * x^b) = ln(a) + b * ln(x)
<br />
<br />
All 3 functions can be implemented using lineair trendline.
<br />
For all 3 functions I need the: a and b factor and RSquared.
<br />
These 3 functions are common in Excell as trendline for best fit. Iám looking for a replacement for these Excel functions.
<br />
<br />
I already implemented:
<br />
Tuple<double, double> p = MathNet.Numerics.Fit.Line(xSloc, xPI)
<br />
For the normal lineair line: y = a + bx
<br />
<br />
But I don’t know how to implement the other 3 formules in MathNet.Numerics for best it.
<br />
<br />
The programming language is C#.
<br />
<br />
Thanks for a reply
<br />
<br />
Greetings
<br />
Peter Bellen
<br />
<a href="mailto:peter.bellen@12move.nl" rel="nofollow">peter.bellen@12move.nl</a><br />
</div>pbellenTue, 16 Sep 2014 15:28:27 GMTNew Post: Implementing Power, exponential and logarithmic function as best fit in C# 20140916032827PNew Post: (CubicSpline) Could I get value X from value Y?http://mathnetnumerics.codeplex.com/discussions/567275<div style="line-height: normal;">Maybe combine it with a root finder?<br />
</div>candychiuTue, 16 Sep 2014 14:09:03 GMTNew Post: (CubicSpline) Could I get value X from value Y? 20140916020903PNew Post: How do I obtain efficient addition of weighted Complex DenseMatrices? http://mathnetnumerics.codeplex.com/discussions/567450<div style="line-height: normal;">I'm trying to perform the following operation efficiently with Complex dense matrices
<br />
<br />
resultsSumMatrix = weightA x aMatrix + weightB x bMatrix
<br />
<br />
My matrices are often 2000 x 2000. I currently perform the above operation as follows, which seems slow:<br />
<pre><code>Complex[] aValues = aMatrix.Values;
Complex[] bValues = bMatrix.Values;
Complex[] resultValues = resultSumMatrix.Values;
int nValue = aValues.Length;
for (int i = 0; i < nValue; i++)
resultValues[i] = weightA*aValues[i] + weightB*bValues[i];</code></pre>
Are there any suggestions for getting faster performance? I find that writing native C++ for the above operation is tricky because CLR uses System.Numerics.Complex for complex numbers. <br />
</div>KevinMcTaggartMon, 15 Sep 2014 12:07:58 GMTNew Post: How do I obtain efficient addition of weighted Complex DenseMatrices? 20140915120758PNew Post: (CubicSpline) Could I get value X from value Y?http://mathnetnumerics.codeplex.com/discussions/567275<div style="line-height: normal;">I made a simple curve like this.<br />
<pre><code>double[] px = new double[] { 0.0, 5.0, 10.0 };
double[] py = new double[] { 0.0, 3.0, 10.0 };
CubicSpline spline1 = CubicSpline.InterpolateNatural(px, py);</code></pre>
axis-x is time, axis-y is value. and get value y coding <br />
<pre><code>double value = spline1.Interpolate(x);</code></pre>
But sometime i need to know how long the value will become specify value. And I create this<br />
<pre><code>CubicSpline spline2 = CubicSpline.InterpolateNatural(py, px);</code></pre>
Testing with these code<br />
<pre><code>Console.WriteLine(spline2.Interpolate(spline1.Interpolate(0))); // output: 0
Console.WriteLine(spline2.Interpolate(spline1.Interpolate(5))); // output: 5
Console.WriteLine(spline2.Interpolate(spline1.Interpolate(10))); // output: 10
Console.WriteLine(spline2.Interpolate(spline1.Interpolate(2.0))); // output: 1.553190912
Console.WriteLine(spline2.Interpolate(spline1.Interpolate(3.0))); // output: 2.51721969371429</code></pre>
Its correct at Key point, others are incorrect.
<br />
<br />
Is there any function i could use?<br />
</div>nomauciFri, 12 Sep 2014 16:09:24 GMTNew Post: (CubicSpline) Could I get value X from value Y? 20140912040924PNew Post: Build DenseMatrix from DenseVectorhttp://mathnetnumerics.codeplex.com/discussions/567263<div style="line-height: normal;">Dear community,
<br />
<br />
I want to build a DenseMatrix from several DenseVectors. I've been succeed but I've done via:
<br />
<br />
//delete next extra row
<br />
resultDenseMatrix = (DenseMatrix)result.SubMatrix(...);
<br />
//insert new vector
<br />
resultDenseMatrix = (DenseMatrix)result.InsertRow(...);
<br />
<br />
That seems pretty weird to me. Is there an easy way?
<br />
<br />
Thanks in advance, <br />
</div>agentDunhamFri, 12 Sep 2014 13:29:52 GMTNew Post: Build DenseMatrix from DenseVector 20140912012952PNew Post: add several vectorshttp://mathnetnumerics.codeplex.com/discussions/567261<div style="line-height: normal;">Dear community,
<br />
<br />
I'd like to know how to add in a smart way several vectors, that is,
<br />
<br />
I have a List<double[]> lstVectors, and I want to all vectors in the array and have in return another double[]. Is there anything already implemented?
<br />
<br />
Thanks in advance,<br />
</div>agentDunhamFri, 12 Sep 2014 13:23:20 GMTNew Post: add several vectors 20140912012320PNew Post: Please help me to find the correct classes and methods for this simple goodness of fit testshttps://mathnetnumerics.codeplex.com/discussions/567170<div style="line-height: normal;">Warning serious NOOB alert
<br />
<br />
Say, I have a dice that is 19 times as likely to produce a six as a one, because it has been tampered with.
<br />
When I throw this dice 60 times the expected versus observed frequencies of the six possible outcomes are:
<br />
<br />
1: 10, 1<br/>
<br />
2: 10, 10<br/>
<br />
3: 10, 10<br/>
<br />
4: 10, 10<br/>
<br />
5: 10, 10<br/>
<br />
6: 10: 19
<br />
<br />
I want to feed this expected-observed pairs to an algorithm to determine how likely it is that the dice indeed has been tampered with.
<br />
<br />
When I enter the value pairs on <a href="http://www.quantpsy.org/chisq/chisq.htm" rel="nofollow">this website</a> it calculates a Chi square value of 16.2 and a P value of 0.00629567, indicating that it is very unlikely that the observed results are in accordance with the expected distrbution of the values one to six.
<br />
<br />
I want to calculate the P value using math.net numerics, but although I can find a ChiSquared class I cannot find how to feed the expected-observed value pairs to it in order te get the P value.
<br />
<br />
How can it be done?
<br />
<br />
Thanks for you patience beforehand :-)<br />
</div>DabblerNLThu, 11 Sep 2014 19:58:39 GMTNew Post: Please help me to find the correct classes and methods for this simple goodness of fit tests 20140911075839PNew Post: Quadratic equation system solverhttp://mathnetnumerics.codeplex.com/discussions/566794<div style="line-height: normal;">Is it possible to use math.net framework to solve nonlinear equation system (4 quadratic equations, 4 variables)?<br />
<br />
Can you please give me a sample or give me a link to an existent one? I've searched the forum but I've not found any related question.<br />
<br />
Thank you in advance.<br />
</div>SimoColMon, 08 Sep 2014 14:42:58 GMTNew Post: Quadratic equation system solver 20140908024258PNew Post: Multiple Linear regression in C#http://mathnetnumerics.codeplex.com/discussions/564962<div style="line-height: normal;">I am having issues using Math.NET for Multiple Linear regression, I have 36 Independent variables and 1 dependent variable, so I am not sure how to set up the matrix to run the Linear regression. I would also like to point out that I am new to Math.Net but have been using C# for some time now. I do however use SPSS and SYSTAT daily to create regression by hand. I am looking for a way to automate this process.<br />
<br />
Thanks!<br />
<br />
A matrix that I am trying to create would be:<br />
<pre><code> var xdata = new double[,] { { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
var ydata = new double[] { 5, 4, 7, 6, 6, 7
Vector<double> p = MathNet.Numerics.LinearRegression.MultipleRegression.NormalEquations(xdata, ydata);
</code></pre>
The 0,1 represent the 36 independent variables and the yData represents the dependent variables. Is this correct I cannot get it to run correctly. <br />
<br />
The line: Vector<double> p = MathNet.Numerics.LinearRegression.MultipleRegression.NormalEquations(xdata, ydata); <br />
does not seem to like the data.<br />
</div>tmorfordTue, 02 Sep 2014 14:22:48 GMTNew Post: Multiple Linear regression in C# 20140902022248PNew Post: ARMA - ARCH - GARCH Model Fittinghttp://mathnetnumerics.codeplex.com/discussions/445314<div style="line-height: normal;">Hey,
<br />
<br />
I am really wondering if you ever found a solutions for this, I am also looking to implement the GARCH model for a small project of mine and just out of interest!
<br />
<br />
Greets<br />
</div>ovanwijkWed, 27 Aug 2014 13:14:01 GMTNew Post: ARMA - ARCH - GARCH Model Fitting 20140827011401PNew Post: StyleCophttp://mathnetnumerics.codeplex.com/discussions/561901<div style="line-height: normal;">Hi, Christoph,
<br />
<br />
I'd like to ask which tool(s) do you use to bring the code to StyleCop compliant? I am working on a project which needs some standardization.<br />
<br />
Thanks for your help. Sorry for to post a non-related question.<br />
<br />
Candy<br />
</div>candychiuFri, 22 Aug 2014 20:29:02 GMTNew Post: StyleCop 20140822082902PNew Post: Is it possible to build a Slice/SubVector with shared storage?http://mathnetnumerics.codeplex.com/discussions/559981<div style="line-height: normal;">No, this is not currently possible, except with a user wrapper. How do you intend to use the resulting sub-vector? Does it need to behave like a normal vector?
<br />
<br />
Thanks,
<br />
Christoph<br />
</div>cdrnetTue, 12 Aug 2014 15:33:54 GMTNew Post: Is it possible to build a Slice/SubVector with shared storage? 20140812033354PNew Post: Using IIR for determining frequency rangehttp://mathnetnumerics.codeplex.com/discussions/560788<div style="line-height: normal;">For filtering you'd need the Math.NET Filtering library in addition to Math.NET Numerics. Note that Filtering is only available as preview release yet. Indeed, there seems to be an issue with IIR filter design, but it works with FIR filters.
<br />
<br />
Essentially you could apply several band-pass filters and then choose the frequency band where the resulting signal has the highest energy.
<br />
<br />
In LINQPad:<br />
<pre><code>void Main()
{
var sampleRate = 500; // 500 Hz sampling rate
var signal = Generate.Sinusoidal(1000,sampleRate,90,1.0); // 90 Hz sinus
BandEnergy(signal, sampleRate, 0, 25).Dump("0..25");
BandEnergy(signal, sampleRate, 25, 85).Dump("25..85");
BandEnergy(signal, sampleRate, 85, 180).Dump("85..180");
BandEnergy(signal, sampleRate, 180, 250).Dump("180..250");
}
double BandEnergy(double[] signal, double sampleRate, double lowCutoff, double highCutoff)
{
var filter = OnlineFilter.CreateBandpass(ImpulseResponse.Finite, sampleRate, lowCutoff, highCutoff);
var filtered = filter.ProcessSamples(signal);
return Vector<double>.Build.Dense(filtered).L2Norm();
}</code></pre>
returns:<br />
<pre><code>0..25
0.329663254046423
25..85
1.22288405899457
85..180
22.2095816072106
180..250
0.185562999270445 </code></pre>
Hence the dominant frequency band is 85..180 Hz, as expected for a 90 Hz sinus signal.
<br />
<br />
Thanks,
<br />
Christoph<br />
</div>cdrnetTue, 12 Aug 2014 09:27:39 GMTNew Post: Using IIR for determining frequency range 20140812092739ANew Post: Using IIR for determining frequency rangehttp://mathnetnumerics.codeplex.com/discussions/560788<div style="line-height: normal;">Hi,
<br />
I'm a total newbie in this field.
<br />
I was asked to analyze a fraction of a wav file (given timestamps) in order to find the dominant frequency (e.g. 85-180hz).
<br />
I was recommended to use IIR filtering.
<br />
can anyone lead me to to some docs/samples please?
<br />
Thanks,
<br />
udibs<br />
</div>udibsSun, 10 Aug 2014 19:46:23 GMTNew Post: Using IIR for determining frequency range 20140810074623PNew Post: Performance - arrays vs libraryhttp://mathnetnumerics.codeplex.com/discussions/560522<div style="line-height: normal;">Thank you very much for your analysis! Was really helpful for my understanding of F# and Math.Net.
<br />
<br />
I can confirm that A) speeds up the calculation (by about 15% on my laptop), and if a Fold2 method for vectors in the next release will almost close the gap between Math.Net and a pure array variant, then I can't wait to try it out :-)
<br />
<br />
You are right on E).<br />
</div>RadiborSat, 09 Aug 2014 21:57:53 GMTNew Post: Performance - arrays vs library 20140809095753PNew Post: Performance - arrays vs libraryhttp://mathnetnumerics.codeplex.com/discussions/560522<div style="line-height: normal;">In general Math.NET cannot really be much faster than 1D-arrays in such algorithms, but it may be faster than 2D-arrays as used here (which are less optimized in .Net). I ran a few benchmarks (without profiling), some of my findings:<br />
<br />
A) Both variants can be made significantly faster (the same way) by reusing the extracted row vectors <code>mat.[i,*]</code> in the other loop and <code>mat.[j,*]</code> in the inner loop.<br />
<br />
B) I've added locally (not in mainline yet) a Fold2 method to the vectors, bringing us very close to the array version (~4% slower on my machine). However, the way our F# extensions convert the F# functions to .Net Func delegates seems not to optimize away properly, leaving us with some overhead (~8% slower to use Vector.fold2 instead of Vector's new Fold2 method directly). Hopefully this can be improved somehow, by converting or rewriting instead of wrapping the lambda.<br />
<br />
C) Very small improvement by writing <code>let av = mati.Map2((fun x y -> (x + y) / 2.0), matj)</code> instead of <code>let av = (mati + matj) / 2.0</code>. Using the method instead of the F# extension for the same reason as in B).<br />
<br />
D) Very small improvement by transposing the matrix in the beginning and extracting column vectors instead of row vectors (dense matrices are column-major).<br />
<br />
E) The definition of kld is a bit confusing in js_distance2 as a is actually the state, b is the first vector and it returns a function from the second vector to the scalar result. I assume it should actually be <code>let kld a b = Array.fold2 (fun acc p q -> if p = 0.0 || q = 0.0 then acc else acc + log(p / q) * p) 0.0 a b</code>, and the first argument <code>0.0</code> dropped when it is called in the second last name?<br />
</div>cdrnetFri, 08 Aug 2014 15:58:58 GMTNew Post: Performance - arrays vs library 20140808035858PNew Post: Performance - arrays vs libraryhttp://mathnetnumerics.codeplex.com/discussions/560522<div style="line-height: normal;">To learn F# and Math.Net I've written 2 versions of the Jensen Shannon distance: <br />
<pre><code>let m1 = DenseMatrix.init 2000 20 (fun i j -> float (i+j)) //values not normalized
let js_distance1 (mat : Matrix<float>) =
let kld a b = Vector.map2 (fun p q -> if p = 0.0 || q = 0.0 then 0.0 else log(p / q) * p) a b |> Vector.sum
let rowsA = mat.RowCount
let res = DenseMatrix.create rowsA rowsA 0.0
for i = 0 to rowsA - 2 do
for j = i + 1 to rowsA - 1 do
let av = (mat.[i,*] + mat.[j,*]) / 2.0
res.[j, i] <- sqrt(0.5 * (kld mat.[i,*] av + kld mat.[j,*] av))
res</code></pre>
<pre><code>let m2 = Array2D.init 2000 20 (fun i j -> float (i+j)) //values not normalized
let js_distance2 mat =
let kld a b = Array.fold2 (fun acc p q -> if p = 0.0 || q = 0.0 then acc else acc + log(p / q) * p) a b
let rowsA = Array2D.length1 mat
let res = Array2D.create rowsA rowsA 0.0
for i = 0 to rowsA - 2 do
for j = i + 1 to rowsA - 1 do
let av = Array.map2 (fun x y -> (x + y) / 2.0) mat.[i,*] mat.[j,*]
res.[j, i] <- sqrt(0.5 * (kld 0.0 mat.[i,*] av + kld 0.0 mat.[j,*] av))
res</code></pre>
The array version is approx. 1.8 times faster on my laptop.
<br />
<br />
Is this kind of performance penalty to be expected when working with a numerical library like Math.NET?
<br />
<br />
Am I missing any obvious speed-up options? I read in some discussions that the access to matrix or vector values can be a bit slow, but the alternate methods I tried like mat.Rows(i) didn't make any difference.
<br />
<br />
Thanks!<br />
</div>RadiborThu, 07 Aug 2014 22:36:18 GMTNew Post: Performance - arrays vs library 20140807103618PNew Post: How to create a random matrix in F#?http://mathnetnumerics.codeplex.com/discussions/560401<div style="line-height: normal;">Good point, I just added a few examples:<br />
<pre><code>// random matrix with standard distribution:
let m6 = DenseMatrix.randomStandard<float> 3 4
// random matrix with a uniform and one with a Gamma distribution:
let m7a = DenseMatrix.random<float> 3 4 (ContinuousUniform(-2.0, 4.0))
let m7b = DenseMatrix.random<float> 3 4 (Gamma(1.0, 2.0))</code></pre>
I.e. in your examples was very close - ContinuousUniform is a normal constructor and thus requires tuple syntax:<br />
<pre><code>let m : Matrix<float> = DenseMatrix.random 3 4 (ContinuousUniform(0.0, 1.0)) </code></pre>
</div>cdrnetThu, 07 Aug 2014 09:52:23 GMTNew Post: How to create a random matrix in F#? 20140807095223ANew Post: How to create a random matrix in F#?http://mathnetnumerics.codeplex.com/discussions/560401<div style="line-height: normal;">Would it be possible to add an example of how to create a random matrix in F# say for the uniform distribution?
<br />
<br />
<a href="http://numerics.mathdotnet.com/docs/Matrix.html" rel="nofollow">http://numerics.mathdotnet.com/docs/Matrix.html</a>
<br />
<br />
The doc explains how to do it via Array2d conversion or linear sequences, but I guess it's directly possible as well. Something like
<br />
<br />
let m : Matrix<float> = DenseMatrix.random 3 4 (ContinuousUniform 0 1)
<br />
<br />
Thanks!<br />
</div>RadiborThu, 07 Aug 2014 08:14:40 GMTNew Post: How to create a random matrix in F#? 20140807081440A