How to calculate inflection point in C# by a given function - c#

My function always looks like this: y = beta1 / (1 + exp(beta2 + beta3 * x)). With the data it can get, it always looks like a mirror of that (it starts with high values, then decreases)
I have the values beta1, beta2 and beta3. Now I need to calculate the point of inflection. I guess I could do it with an algorithm, but are there any functions provided by a library I could use? (Currently I cannot use Accord.NET because I still have C# 3.5 and it does only support 4.+)
update I'd be fine with a method to take the derivative ;)
Best regards

What you need is second derivation, and if the function only differs in constants, you can pre-compute it somewhere, and then only fill in constants.
Here you are: wolfram
There is no standard library function that derivates for you.

No need to write an algorithm: the second derivative is null for x = - beta2 / beta3. There is a close solution to this problem.

Related

What type in c# is most like an expression in R and why?

As a C# developer I curious about the formula parameter in R's lm function
As in the following code
wine<-read.csv("wine_train.csv")
fit<-lm(formula=quality ~ ., data=wine)
In the help shown by
?lm
It says
formula
an object of class "formula" (or one that can be coerced to
that class): a symbolic description of the model to be fitted.
In the help shown by
? formula
It says
Model Formulae
The generic function formula and its
specific methods provide a way of extracting formulae which have been
included in other objects.
So I am confused as to whether formula is a class or a function or an "expression" whatever that is.
In C# we have delegates, lambdas and actions that all seem useful to treat code snippets as memory variables.
R is strongly but dynamically typed where as c# is mostly statically typed
Is there a useful parallel in C# for the formula in R?
[Update]
I get that the left and right side of the tilde act as parameters to be used in lm
Technically, a formula object is an unevaluated function call, along with an associated environment:
f <- as.formula(y ~ x1 + x2)
> str(f)
Class 'formula' language y ~ x1 + x2
..- attr(*, ".Environment")=<environment: R_GlobalEnv>
> is.call(f)
[1] TRUE
Thinking of the y ~ x1 + x2 part as an unevaluated function call is a bit misleading, because we wouldn't typically really use it like we would other functions. But you can see it really is stored that way:
> f[[1]]
`~`
> f[[2]]
y
> f[[3]]
x1 + x2
i.e. it's a quoted string and two arguments, matching the description of a call from ?call. Of course, we wouldn't really think of ~ as a function in R, but it is one if you check help("~") you'll see it's a primitive. Using an unevaluated call in this way is one of R's more unique features, in that it is leveraging this ability to compute on the language itself to do something nominally unrelated: create a new "symbolic" object to store the conceptual definition of a model.
You can see how the formula object is actually used in this sense in model.frame.default, although that code is....fairly dense itself. The basic idea is that R is piggybacking on this ability to create functions with associated environments to allow the formula to be "evaluated" in the context of that environment, like a function would. But instead of "calling a function" it's more a vehicle for transporting model information that is then used to build the matrices you need for the model fitting process.

Using Math.Net Numerics, how to find a curve fitting for "Power"

I'm trying to reproduce same curve fitting (called "trending") in Excel but in C#: Exponential, Linear, Logarithmic, Polynomial and Power.
I found linear and polynomial as :
Tuple<double, double> line = Fit.Line(xdata, ydata);
double[] poly2 = Fit.Polynomial(xdata, ydata, 2);
I also found Exponential fit.
But I wonder how to do curve fitting for Power. Anybody has an idea?
I should be able to get both constants like shown into the Excel screen shot formula:
power
multiplier (before x)
Before anybody would be the fifth who vote to close this question...
I asked the question directly to the forum of mathdotnet (that I recently discovered). Christoph Ruegg, the main developper of the lib, answered me something excellent that I want to share in order to help other with the same problem:
Assuming with power you’re referring to a target function along the
lines of y : x -> a*x^b, then this is a simpler version of what I’ve
described in Linearizing non-linear models by transformation.
This seems to be used often enough so I’ve started to add a new
Fit.Power and Fit.Exponential locally for this case - not pushed yet
since it first needs more testing, but I expect it to be part of v4.1.
Alternatively, by now we also support non-linear optimization which
could also be used for use cases like this (FindMinimum module).
Link to my question: mathdonet - Curve fitting: Power

Are there any function in C# that returns p-value associated with a chi-square goodness of fit test?

i'm new here and my English isn't very good, so i'll try to explain as well as possible.
I'm doing a web application in ASP.NET and C# about steganalysis.
I was looking for internet a function that calculates the observed significance level, or p-value in a chi-square test
for my algorithm and I found it in Java:
This is the result of mi search:
chi[block]= chiSquareTest(expectedValues, pod);
chiSquareTest(double[] expected, long[] observed)
Returns the observed significance level, or p-value,
associated with a Chi-square goodness of fit test comparing
the observed frequency counts to those in the expected array.
My question is, Are there any equivalent function in C# that returns the same parameter?
Thank you in advance,
Ana.
The MathNet nugetpackage contains the ChiSquared distribution - you can get the cumulative or inverse cumulative.
ChiSquared c = new ChiSquared(degreesOfFreedom);
return c.CumulativeDistribution(testValue);
I doubt there is an inbuilt function.
You should try looking for a library that contains the function or implement it yourself.
A quick search returned me this
http://www.alglib.net/specialfunctions/distributions/chisquare.php
and
http://www.codeproject.com/Articles/432194/How-to-Calculate-the-Chi-Squared-P-Value
I know this is an old question but I figured I'd post anyway.
The Accord.NET framework has a library and NuGet package Accord.Statistics which has a ChiSquareTest class that can be used in a similar manor as mentioned in your question:
ChiSquareTest chiSquareTest = new ChiSquareTest(observedArray, expectedArray, DOF);
chiSquareTest.PValue; // gets p-value
chiSquareTest.Significant; // true if statistically significant
The only thing is that you'll have to calculate your DOF - degrees of freedom.

How to handle float values that are greater than the maximum value of double

I have values greater than 1.97626258336499E-323
I cant use BigInteger also as it handler only integer values
Any help is appreciated
Here is the code that failed also failed with some solution given by some users:
BigValue / (Math.Pow((1 + ret), j));
WHere BigValue is something like 15000.25
ret is -0.99197104212554987
And j will go to around 500-600.
I am not gettting how to use Rational Class for this too
BigRational from the base class library team from Microsoft. It uses big integers to store it as a fraction, but supports all kinds of operators.
When it comes to printing it as a decimal, I think you need to write your own implementation for that. I have one written somewhere for this class, but I'd have to find it.
Here is something that may be useful. I used it a while back with no problem. It is a .Net BigDecimal class, you can download it from codeplex(or just look at the source):
http://bigdecimal.codeplex.com/releases/view/44790
It is written in VB.Net (.Net 4.0), but that shouldn't matter.
An example of its use in C#: http://www.dreamincode.net/forums/blog/217/entry-2522-the-madman-scribblings/
You will have to switch languages to one that has a BigFloat type (e.g. Haskel and Python have native packages) or else find a third party big float library with a C# binding. There was some discussion of such a binding for GNU MP, but it faded away. Maybe you'll write one!
See also this discussion where MS BigRational is discussed. However this is different from BigFloat.
One solution might be to do your problems in log space instead.
your example would become:
exp(log(Number) - log(1-0.9999999) * 400)
Learn how to use logs to work with numbers like these. Yes, you CAN use a big float package, but that is overkill almost always. You can usually get what you need using logs.

Generating and then using a taylor polynomial in C#

I've written a simple graphing implementation in C#, and I can graph things by comparing each pixel to the position on the graph it represents and plugging that position into the function I have to see if it is on the curve. That's all well and good.
The problem I'm having is USING a generated taylor polynomial. For example, I am able to create the nth taylor polynomial of a transcendent function f centered at c by doing
summation of this from 0 to to n with the counter variable being k = ((kth derivative of f(c)) * (x-c)^k)/k!
I am not sure how to do math markup on stackoverflow nor am I too competent with doing that on the web, but I hope that is understandable. The left side could be written as sigma _k=0 ^n or something like that with _ representing the section under sigma and the ^ representing the part above...
So I end up generating a Taylor polynomial of the 6th degree for cos(x) centered at 0(maclaurin, I know) that looks something like
"1 - x^2/2! + x^4/4! - x^6/6!"
This can be done through simple string manipulation in C#. I can just loop through and add the next term to the string.
I really can't comprehend how I would actually be able to use the string as a function to compare to graph positions to see if that graph position is actually on this graph to therefore graph it. So essentially: How would I use a string as an actual mathematical function in C#, or is there a better way of doing this.
Really sorry if it's confusing...really trying my best to explain it in a way that people can help.
You need a parser of string -> function. See MathParser for an example, that probably does everything you mentioned you need.
From a general perspective, anytime you want to convert a string into something that does work, you have to implement a parser, which will interpret the string and perform the actions dictated by it. For mathematical formulas, an expression tree may be of use to maintain order of operations and grouping. There are probably some math expression libraries available that will do this, or you can roll your own. This is not a trivial task, but it's certainly possible.
Once you have the expression tree, to figure out if a value f(x) for a given x is graphable, just evaluate it. For an f(x) graph, you can test x first to see if it falls in the visible domain of the graphing area. If it does, evaluate f(x) and if the point (x,f(x)) is graphable then draw the point.

Categories

Resources