So I have statsmodels and I see that smf.glm halts only when the variables aren't categorical (i.e. wrapped with a C()). If I modify the formula so that some of the variables are wrapped with a C() then they do not halt.
Why?
Related
This is a general question about if programming languages remember/store the output of function calls.
Suppose I need to calculate a quantity X which depends on some number of simpler calculations. Let's say
X=sin(t)+cos(t)+(cos(t)-sin(t))^2.
Naively, I could compute X as above, calling sin(t) twice, and cos(t) twice.
Or I could call sin(t) and cos(t) once:
a=sin(t)
b=cos(t)
and do
X=a+b+(b-a)^2
Intuitively, the second method should be twice as fast right? Is this the case in all programming languages?
I ask because, doing such a calculation in Julia, I noticed that computing the simpler quantities once vs calling them at each point they appear in the expression for X does not change the runtime.
It depends on how clever your compiler is, and on properties of the function.
First your compiler would need to figure out for example that you are calling sin(t) twice. That's not too difficult.
Second it needs to convince itself that t has the same value for each call. For example, if t was a static variable, and you didn't call sin(t) but some other function, that function call could modify t, so the second call sin(t) would have a different argument and sin(t) would have to be called twice.
Third it needs to convince itself that it doesn't matter whether sin(t) is called once or twice. (Such a function is called idempotent). For example, if you called a function that writes a message to a logfile, then the compiler would have to call it twice, or only one message is written to the logfile instead of two.
The problem that I am trying to solve is based on the following code:
https://www.mathworks.com/help/gads/examples/solving-a-mixed-integer-engineering-design-problem-using-the-genetic-algorithm.html
My function has a lot more variables but basically it is the same. I have a set of variables that needs to be optimized under given constraints. Some of the variables have to be discrete. However, they can only take the values 0 and 1, I don't have to specify them, as it is shown in the example. (I have tried both methods though)
First I create the upper and lower boundaries, which creates a variable of size 1x193, respectively.
[lb,ub] = GWO_LUBGA(n_var,n_comp,C,n_comp);
Afterwards I call up the constraints. As I have discrete values, I cannot use equality constraints. Therefore I am using the workaround that was proposed here:
http://www.mathworks.com/help/gads/mixed-integer-optimization.html
ObjCon = #(x) funconGA(x,C,ub,n_comp);
Same for the objective function:
ObjFcn = #(x) CostFcnGA(x,C);
Afterwards I pass it over to the genetic algorithm:
[Pos,Best,~,GWO_cg_curve] = ga(ObjFcn,n_var,[],[],[],[],lb,ub,ObjCon,C.T*6+2:C.T*8+1,opts);
with n_var = 193 and C.T=24
When I try to compile I receive the following error:
Error using ga (line 366)
Dimensions of matrices being concatenated are not consistent.
Line 366 contains the following code. Unfortunately gaminlp cannot be opened.
% Call appropriate single objective optimization solver
if ~isempty(intcon)
[x,fval,exitFlag,output,population,scores] = gaminlp(FitnessFcn,nvars, ...
Aineq,bineq,Aeq,beq,lb,ub,NonconFcn,intcon,options,output,Iterate);
Both anonymous functions work when random values are entered. What could be the reason for this error?
I want to process arithmetic expressions with variables in it. Variables should be left as it is, and the other parts should be calculated. For example,
?-result(7*x,R).
R=7*x.
?-result(x+(2*3),R).
R=x+6.
How should I do this?
I cannot find how to use any variable name i want in mathematica.
(I've searched where i could but i cannot think of the correct terms to search for)
For example, I wanted to name one of my variables {pi}' (with {pi} I mean the greek pi, just cannot type it here).
However, there are problems. First, {pi} is already predefined as a constant equal to 3.14..
Second, the prime (') symbol is predefined as derivative.
So, {pi}' evaluates to 0& by default.
Another example would be, if i want to name my variable with a "0" in the exponent. However, I do not want mathematica to understand it as exponent, but just as name.
It would be fine if I could just deactivate that ' is interpreted as derivative, or that superscript is interpreted as exponent. But i would prefer some function which lets me define any collection of symbols as variable.
Can someone help?
I want to assign values to variables (like c for speed of light or G for gravitational constant) but have formulas calculated symbolically until last step.
How is it possible to do this in shortest way?
Replace is very long and duplicating while HoldForm can require multiple RealeaseHold if nested.
Is there some other functions for this?
We can define N values for our constants. For example:
N[c] = 299792458;
This defines a numerical value for c. We can define a function that uses the constant:
f[v_] := Sqrt[1-v^2/c^2]
When we evaluate this function normally, it leaves c in symbolic form:
In[11]:= f[200000000]
Out[11]= Sqrt[1 - 40000000000000000/c^2]
But if we apply N, then c gets evaluated numerically:
In[12]:= f[200000000] // N
Out[12]= 0.744943
an example will help. But if I understood you, then you have
expr=9 c + 10 gravity
then you can write
expr /. {c -> 299792458, gravity -> 9.8}
to evaluate the symbolic expression with new values for the symbols involved.
The expression can remain symbolic all the time, and you can simply evaluates it for different values for the symbols in it.
I think this question has two parts.
(1) Whether we should force Mathematica to do all calculations symbolically. This is (almost always) wrong. Mathematica can do arbitrary precision numerics, so we should prefer to tell it the precision of our physical constants (when they exceed $MachinePrecision) and let it choose the most efficient way to solve the problem.
(2) How do we print intermediate steps in symbolic form. For this, use HoldForm[expr], and then
expr //. HoldForm[x_]:>ReleaseHold[HoldForm[x]]
should give you the evaluation results as you indicate.
Regarding a "ReleaseHoldAll" function, MapAll (short form //#) maps a function to all parts of an expression. Therefore, you can use:
ReleaseHold //# expr
where expr is your expression containing Hold, HoldForm, etc., at any level.
There are strange attributes to using the replacement operator in mathematica sometimes. This has to do with the context in which you apply it. The above answer will probably work well, but personally I always use Block[{variable=number}, code] command, which makes the variables act as global within the Block brackets, but once the evaluation proceeded outside, the variables remain undeclared.
use it like this:
Block[{c = 299792458, gravity = 9.0 }, answer = 9 c + 10 gravity ]
gives output:
2.69813*10^9
and also sets answer globally to the value of the output so you can use it after :
answer/2
results in:
1.34907*10^9