Use of DerivativeStructure in Apache Commons Math - java

I am having a hard time understanding how to use DerivativeStructure in Apache Commons Math.
I have a Logit function for which I would like to get the first order derivative. Then I would like to get the value of that derivative on multiple distinct values.
Logit logit = new Logit(0.1, 10.0);
DerivativeStructure ds = // How to instanctiate?
DerivativeStructure dsRes = logit.value(ds);
// How to use dsRes to get the value of the derivative function applied on
// several values?
In addition, if there is any document describing how to use that DerivativeStructure, I am highly interested!
Thanks for your help.

In the Apache Commons Math User Guide, the section on Numerical analysis Differentiation, there is a reasonable introduction on how to apply the DerivativeStructure.

Related

How could I new parameter to TFIDF in Solr?

I'm new in java, and my research about improvement TF IDF in Solr. My question is,
How to add new parameter (except Freq) in TF method in Solr ?
Should I do Overloading???
Thanks
#Override public float tf(float freq) {
return (float)Math.sqrt(freq);}
Yes.
Create your own custom similarity, which will allow you to use any parameter / calculation for each of the different parts of the current formula for the DefaultSimilarity.
If you want to go even deeper, look at Build your own custom lucene query and scorer.

Apache Commons Math Normal Cumulative Probability

Wikipedia has listed a variety of numerical methods for computing cumulative probability of a normal distribution. However, with Apache Commons Math you do not need know about any of them as the library simply does the job for you:
NormalDistribution normal = new NormalDistribution(mu, sigma);
normal.cumulativeProbability(x);
For some research project, I'm interested to know what method they use. Does anyone know what method Apache Commons Math uses to approximate the normal cumulative value? Is it from the methods listed in wikipedia or they have implemented something different?
The beauty of open source software is that you can always check the source code. The implementation of cumulativeProbability is rather simple, it just returns
0.5 * (1 + Erf.erf(dev / (standardDeviation * SQRT2)));
where Erf.erf computes the error function. It's defined here.
And no, it doesn' use any of the special methods in the mentioned Wikipedia article. It's just a straight-forward implementation of the formula
You can probably see the source code or the javadoc. See there http://commons.apache.org/proper/commons-math/source-repository.html
and
http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/distribution/NormalDistribution.html
Also, there is alot of information in the user's guide. The section about distribution seems interesting: http://commons.apache.org/proper/commons-math/userguide/distribution.html

Calculating t-inverse

I'm trying to calculate a the inverse of a 2 tailed Student Distribution using commons-math. I'm using Excel to compare values and validate if my results are correct.
So Using excel to calculate TINV with 5 degrees of freedom and 95.45% I use
=TINV(0.0455,5)
And get the Result: 2.64865
Using commons Math like so :
TDistribution t = new TDistribution(5);
double value = t.inverseCumulativeProbability(0.9545);
I get Result : 2.08913
I'm probably doing something wrong obviously. I'm not really that math savvy but I need to port an Excel sheet formula to Java for a project and got stuck on this.
What should I be using to get the result exactly like the TINV value? What am I missing.
MS documentation [1] says that TINV returns a two-tailed value. I'm pretty sure Commons Math is returning a one-tailed value. In order to get Commons Math to agree with Excel, cut the tail mass in half, i.e., call
t.inverseCumulativeProbability (1 - tail_mass/2);
[1] http://office.microsoft.com/en-us/excel-help/tinv-function-HP010335663.aspx

Convert Excel solver to java

I am trying to convert a Excel Solver solution to a java app
The excel solver is
Solver Parameters:
Set Objective: D24 to Max
By Changing Variable Cells: C4:C23
Subject to the Constraints:
C24 = B24
L24 <= N24
L25 >= N25
(non-negative)
GRG Nonlinear
I have been goggling for sometime and cannot find a java library to achieve this. Any ideas?
I have tried choco-solver http://www.emn.fr/z-info/choco-solver/
Solver solver = new Solver("my first problem");
// 2. Create variables through the variable factory
IntVar x = VariableFactory.bounded("X", 0, 5, solver);
IntVar y = VariableFactory.bounded("Y", 0, 5, solver);
// 3. Create and post constraints by using constraint factories
solver.post(IntConstraintFactory.arithm(x, "+", y, "<", 5));
// 4. Define the search strategy
solver.set(IntStrategyFactory.inputOrder_InDomainMin(new IntVar[]{x,y} ));
// 5. Launch the resolution process
if (solver.findSolution()) {
do {
prettyOut();
}
while (solver.nextSolution());
}
I am finding it difficult to relate this to the Excel solver functions, my math is not great
There is a Simplexsolver implementation in Apache Commons Math, but I can't say anything on performance or possible problem size. Finding non-propertery solutions for optimization problems, can be tricky because it is very difficult to efficently optimize large problem sizes and it is an ongoing field of research with only a hand full of good commerical/research solutions.
If you want to keep excelfiles as input you need to parse the data and convert it. For reading Excel files you can use Apache POI.
If you are looking for an API to read and write to microsoft documents, take a look at Apache POI:
http://poi.apache.org/
http://viralpatel.net/blogs/java-read-write-excel-file-apache-poi/

Looking for an expression evaluator

I'm looking for an evaluator for simple condition expressions.
Expressions should include variables (read only), strings, numbers and some basic operators.
E.g. expressions something like this:
${a} == "Peter" && ( ${b} == null || ${c} > 10 )
So far i implemented a rather "magical" parser that returns an AST that i can evaluate, but i can't believe that i'm the first one to solve that problem.
What existing code could i use instead?
Have you looked at MVEL? They provide a getting started guide and performance analysis.
Here's one of their simple examples:
// The compiled expression is serializable and can be cached for re-use.
CompiledExpression compiled = MVEL.compileExpression("x * y");
Map vars = new HashMap();
vars.put("x", new Integer(5));
vars.put("y", new Integer(10));
// Executes the compiled expression
Integer result = (Integer) MVEL.executeExpression(compiled, vars);
assert result.intValue() == 50;
Also (answering my own question) MVEL seems to provide some support for bytecode generation.
Other alternatives, culling from the above answers and my own:
Java Expression Parser (JEP) -- and note there is an old version available for free
Apache Commons JEXL
With regard to Rhino, here's a dude who did some arithmetic evaluation in that context (looks messy)
Sounds like JEXL might work well for you. Check out its syntax reference.
What about SPEL (Spring Expression Lang); http://static.springsource.org/spring/docs/3.0.x/reference/expressions.html
Why don't you use Rhino? It's a JavaScript engine already present inside the JDK.
It can evaluate whatever you like to write in JS.. take a look here
This simple recursive descent parser evaluates constants as named functions having no parameters.
A very simple and easy to use alternative with a lot of built in excel functions for string, date and number formatting.
The library also allows easy addition of custom functions. A lot of examples available on the git page. A simple example using variables
ExpressionsEvaluator evalExpr = ExpressionsFactory.create("LEFT(City, 3)");
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("City", "New York");
assertEquals("New", evalExpr.eval(variables));
Here is a little library I've worked on that supports expression evaluation (including variables, strings, boolean, etc...).
A little example :
String expression = "EXP(var)";
ExpressionEvaluator evaluator = new ExpressionEvaluator();
evaluator.putVariable(new Variable("var", VariableType.NUMBER, new BigDecimal(20)));
System.out.println("Value of exp(var) : " + evaluator.evaluate(expression).getValue());

Categories