Setting up test and live enviornment - how?

Question: I am a bit new to servers and stuff so had a question. I have my development team working on my website. They are in different countries and currently they put all the work live on the test site. But the test site is open to anyone who knows the URL. It is behind a directory but this effects my QA process because i cannot use the accurate URL structures to prevent the general public from seeing it. So what I want to do it:
Have my site live on the net but only for me and my team, so like an internal network. Also I will need to mirror this to my live site when i put it live. So i guess this is something like setting up a staging and live environment.

So how to do it and are both environments on the same physical server or do i need to buy two servers?
And if i setup a staging environment how will i access it and my team since we are all spread out so i assume we need to log into something to access it?
What about the URL - do i need a different URL for the test site or can i use the same live url for the test site?

I plan to get a dedicated server + CDN for my site. Platform is PHP codeignitor and MySQL.
Answer: Generally speaking your testing environment should be entirely separate from your production environment -- This allows you to test things like software updates before rolling them out to production, so if they break stuff you can determine what and how to fix it without the time pressures associated with your live environment being down.

My suggestion for an ideal scenario would be to build a development environment that is an exact copy of your production environment - same releases of all software, same database contents, etc.  This can be made publicly accessible, or locked away behind a firewall where all your developers & QA folks VPN in to do their work (which way you go depends heavily on your workflow and processes, but I personally like the VPN solution).
It's also often advantageous to build your development environment using something like VMWare, where you can snapshot the environment before making major changes.
You will also need a process in place for promoting your changes to production.  If you have the time to invest in a solution like Puppet or radmind now it will make future deployments much easier (VMWare comes in handy here too -- You can test your deployment process on virtual machines and debug it so when it runs in production you know how it will behave).
Comment: virtualisation can be a very handy tool in jobs like this. Try making a virtual farm of your production servers, and code against those.
Comment: The problem with VM I am seeing is HD space.  Unlike dedicated machines where space goes in TB all the hosting ads i am seeing have like 40 to 120 GB HD. That is too small. Need atleast 1 TB space to store all the images and stuff.
Comment: The usual solution for a development system is to buy your own box and install VMWare on it -- The Free ESXi stuff is usually sufficient.  Expect to spend between US $5K and $10K for hardware depending on how much RAM you need in addition to your (apparently massive) disk requirement thouth...
Answer: My basic approach has been to have two directories, same physical server.
I setup a separate VirtualHost in apache for dev.sitename.com and www.sitename.com. When I'm satisfied with how something looks on dev, I push the code to www.
Casually I usually do it with a dropbox shared folder and create a symlink to /var/www. This way it's easy for me to modify files and see the results.
If you're working with multiple users, it'd be much better to have a repository with version control for the dev environment. Setup a git server and symlink the repository to apache's http directory.
Comment: Is it beter to host my own repository in my server or use these 3rd party sites like git or assembla, etc? And which repository is better - svn or cvs?
Comment: I certainly wouldn't want to dive into a 'which is better' argument... I know that github is intended for public repositories (unless you have a paid account). --- my intuition could be wrong here, but I think it would be more difficult to get a development website linked to an external repository.


On the Hölder inequality

Question: If one ask to me to prove that the converse of Hölder inequality for $p=1,\infty$, then what statement should I prove? Do you guys agree that the term "the converse of Hölder inequality for $p=1,\infty$" does make sense?
Answer: I would guess it means something like this:
Let $p$ and $q$ be Hölder conjugates and let $f$ be measurable.  Then, if there is some constant $M$ such that
$$
\left\| fg\right\| _1\leq M\left\| g\right\| _q
$$
for all $g\in L^q$, then $f\in L^p$.
Comment: just to complement: ...and $\| f\| _p \leq M$

On the Hölder inequality


Finding a continuous function $f: \mathbb{R} \to \mathbb{R}$ such that $f(\mathbb{R})$ is neither open nor closed

Question: Find a bounded, continuous function $f: \mathbb{R} \to \mathbb{R}$ such that $f(\mathbb{R})$ is neither open nor closed?
Comment: I see no questions, just an order. Unless you contribute some of your ideas to the post, this is likely to be closed.
Comment: @rschwieb Not a question: "Derive the quadratic formula." $\mapsto$ Question: "Derive the quadratic formula?"
Comment: @tacos_tacos_tacos ERight :) What could would a question mark be if it didn't transform any string of letters into a question?
Answer: Take $f(x):=e^{-x^2}$: it's a continuous bounded function on the real line, and $f(\Bbb R)=(0,1]$, which is neither open nor closed.
Answer: Let $f:\mathbb{R}\rightarrow\mathbb{R}$ be defined as $f(x)=\frac{x^2}{x^2+1}$. You have that $f(\mathbb{R})=[0,1)$ which is neither open nor closed.
Answer: Take $f(x) = \arctan(x^2)$. Then, $f(\mathbb{R}) = [0, \pi/2)$.
Answer: Take $f(x)$ to be
$
f(x)=
\begin{cases}
\dfrac{1}{x^2}, \ x\not\in [-1,1],
\\
\\
\\
1 \ \ \  \ , x\in[-1,1]
\end{cases}
$.
Is $f$ continuous? Bounded? What is $f(\mathbb R)$?


Self promotion on the site

Question: Based on this answer, how should we deal with self-promotion on this site? The poster does clearly say his involvment, and it does answer the question, but in the answer apart from his company there is only one more link, and that also seems to have an embedded tracker ID (which is somewhat stated in the post as well).
Here is a link on the same issue on Meta.Travel.SE
Answer: I think the answers need to be taken on a case by case basis. In this case it smells like spam to me since the OP asked two questions and the answer doesn't answer either of them.
The first question is "how do I learn about fees". The answer touches on this a little talking about how fees can be wrapped up in exchange rates. It then provides a link to a website that doesn't cover the countries in questions. It then provides a list of companies with one linked that supposedly have the cheapest costs. No information about how that list was created or what the fees are. The little I know about money transfers is that things are seldom static and that the ordering of the list is likely to change with time. Overall I don't think the answer makes an attempt at answering this first question
The second question is "what rights do I have if the transfer company doesn't deliver". the answer makes no attempt at answering this question. Presumably having a reputable company to do you money transfer is important.
Answer: It's going to be interesting here, so here's a bit of guidance.
Promotional posts by new users are generally suspect
If a new user arrives to the site and begins immediately promoting a product or service, there's a very good chance that they've come here for the wrong reasons, and should probably be destroyed. Flag these as spam.
However, if they've got some good contributions to their credit as well, it might simply be prudent for moderators to contact them and let them know excessive self-promotion is not tolerated. If unsure and you think there might be a real user behind these posts, flag as other and explain, otherwise flag as spam.
If they've actually answered the question, then assume it's a live human being on the other end - mods should start a dialog before doing anything destructive.
Promotional posts by established users are harder to sort
If a significant number of a user's posts do nothing but promote their company, they probably need to have a conversation with the moderators. This is especially true if promotional posts only tangentially answer the question at hand. Flag this as other and let the mods know specifically what you saw, where to look, etc. Please don't go through the user's profile spam-flagging a lot of things, this will lead to an enormous, emotionally-draining explosion later.
Obvious spam is obvious
If the question is about transferring money between countries, for instance, and you see low quality mostly-link answers, flag as spam - enough flags on these automatically delete the post, and feed the spam system.
Pay attention while reviewing suggested edits
Most of the spam that Stack Exchange sees across the network comes in the form of anonymous suggested edits. Watch out for links being introduced (they sometimes use strange formatting to try and hide them), and links being changed. Reject edits that look like spam as spam - this feeds the spam system to keep them from doing it again.
Actually, pay attention everywhere
We're going to get questions where folks are going to be giving honest testimonials of something they've used. Just make certain you've read the question the answer is attempting to answer, and involve a moderator if you're ever uncertain.
After a few months, the most active core community here is going to get a pretty good sense of what's legit and what isn't - I do see the same sort of guerrilla marketing onslaughts happening here as we've seen on Travel in the past. Keep your eyes peeled, but make sure you act with confidence.
Answer: Generally I wouldn't be against self promotion, if and only if it does add value to the answer.
My feeling is, that in this particular case it not only does not add anything, but quite the opposite. First of all, the site in question is completely useless as far as intra-EU transfers go. Secondly it suggest that SEPA transfers are something like world transfers and options other than banks are better, which is completely untrue. It does depend on banks at both ends, which answer completely fails to take into account.
The only reason I didn't outright downvote that answer is the fact that it does explain fees which are applied.
Answer: The acid test should solely be whether or not the answer is a valid, constructive answer, in accordance with SE policy. imho, provided the poster disclaims any relationship, then that should provide readers with enough information to form their own opinion of the validity of the response.
Over on StackOverflow, it is quite common for the authors of software products to post answers related to those products, and they are generally of the highest quality.
Comment: There is difference between question "Howdo I do something in product X?" being answered by author of X, and question "How do I deal with product of category A" being answered "You should use product category B instead" by author of product belonging to the latter category.
Comment: Your use of the word "should" distorts the meta topic, and I have yet to see any SE answer use it. The more common case, that I see no problem with, is Q:"how do I xxx?" A:"you can use yyyyyy. Disclaimer I'm the author of yyyy"

Self promotion on the site


Polynomial Factoring

Question: $(2^x)+(2^{-x})=a$.  What is $(8^x)+(8^{-x})$?
I did:
$$\begin{align} (2^x)^3+(2^{-x})^3
 &= [(2^x)+(2^{-x})] [(2^x)^2-(2^x)(2^{-x})+(2^{-x})^2]
\\&= a [(2^x)^2+2(2^x)(2^{-x})+(2^{-x})^2-3(2^x)(2^{-x})]
\\&= a {[(2^x)+(2^{-x})]^2-3(2^x)(2^{-x})}
\\&= a [a^2-3(2^x)(2^{-x})]
\\&= a^3-3a(2^x)(2^{-x}) \end{align}$$
The answer is $(a^3-3a)$. But I don't know how to solve now. Please, could you help me?
Comment: Post edit, it looks done.  $2^x\times 2^{-x}=1$ so...
Answer: You're solution looks good. One typo: on line 3, it should read $...-3a(2^x)(2^{-x})$ instead of $...-3(2^x)(2^{-x})$. You're so close! Note that $(2^x)(2^{-x})=1$ for every $x\in\Bbb R$.
Comment: Thanks Dave!!! Now that I noticed that this expression is equals to 1.
Answer: The posted proof looks good. Alternatively, using the identity $(u+v)^3=u^3+v^3+3uv(u+v)$:
$$a^3=(2^x+2^{-x})^3=\left(2^x\right)^3+\left(2^{-x}\right)^3+3\cdot 2^x\cdot 2^{-x}(2^x + 2^{-x})=8^x+8^{-x}+ 3 \cdot 1 \cdot a$$

Polynomial Factoring


Change Default Hover Data for hvplot.hist

Question: I have the following dataframe called df that contains 2 columns:
In [4]: df.head(20)
Out[4]:
     age age_band
0    NaN      NaN
1   61.0    55-64
2    NaN      NaN
3   55.0    55-64
4    NaN      NaN
5   67.0      65+
6    NaN      NaN
7   20.0    18-24
8   53.0    45-54
9    NaN      NaN
10   NaN      NaN
11  23.0    18-24
12  60.0    55-64
13   NaN      NaN
14  54.0    45-54
15   NaN      NaN
16  67.0      65+
17   NaN      NaN
18  50.0    45-54
19  70.0      65+

In [5]: df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 107632 entries, 0 to 107631
Data columns (total 2 columns):
age         73289 non-null float64
age_band    73289 non-null object
dtypes: float64(1), object(1)
memory usage: 1.6+ MB

In [7]: df["age_band"].value_counts()
Out[7]:
45-54    22461
55-64    17048
35-44    14582
65+      12990
25-34     4078
18-24     2130
Name: age_band, dtype: int64

In [8]: df["age"].min()
Out[8]: 19.0

In [9]: df["age"].max()
Out[9]: 74.0

AIM:
I want to plot a histogram for df["age"] using hvplot. In this plot, I would like to place the ages into bins that correspond with my df["age_band"] column values. The following plot does this:
In [10]: df.hvplot.hist("age",bins=[18,25,35,45,55,65,74],xticks=[18,25,35,45,55,65,74],hover_cols
    ...: =["age_band"],line_width=4,line_color="w")

When you hover over each bin, the count for each age_band correctly displays as Count, however, rather than each age band value, it seems to display the mean or median age for each bin.
Upon further investigation, it appears that setting hover_cols="age_band" actually had no effect on the plot (you get an identical plot if it is omitted.)
I then tried to use HoverTool:
In [11]: from bokeh.models import HoverTool
    ...:
    ...: hover = HoverTool(tooltips=df["age_band"].dropna())
    ...:
    ...: df.hvplot.hist("age",bins=[18,25,35,45,55,65,74],xticks=[18,25,35,45,55,65,74],line_width
    ...: =4,line_color="w").opts(tools=[hover])

However I got the following error:
ValueError: expected an element of either String or List(Tuple(String, String)), got 1         55-64

So then I tried:
In [12]: from bokeh.models import HoverTool
    ...:
    ...: hover = HoverTool(tooltips="age_band")
    ...:
    ...: df.hvplot.hist("age",bins=[18,25,35,45,55,65,74],xticks=[18,25,35,45,55,65,74],line_wi
    ...: dth=4,line_color="w").opts(tools=[hover])

Which resulted in:

So then I also tried:
In [13]: hover = HoverTool(tooltips=[("18-24","2130"),("25-34","4078"),("35-44","14582"),("45-54",
    ...: "22461"),("55-64","17048"),("65+","12990")])
    ...:
    ...: df.hvplot.hist("age",bins=[18,25,35,45,55,65,74],xticks=[18,25,35,45,55,65,74],line_width
    ...: =4,line_color="w").opts(tools=[hover])

Which resulted in the following:

Is there a way to produce a histogram of df["age"], using hvplot.hist, where when you hover over a bin then you are presented with the corresponding age_band & Count of age_band?
Thanks
Answer: Setting by=['age_band'] should work and should show you that column when you hover:
df.hvplot.hist(
    y='age',
    by=['age_band'],
    legend=False,
    color='lightblue',
    bins=[18,25,35,45,55,65,74],
    xticks=[18,25,35,45,55,65,74],
)

Although in the case you describe you could also choose to create a barplot on the value_counts:
age_band_counts = df['age_band'].value_counts().sort_index()

age_band_counts.hvplot.bar(bar_width=1.0)


Primefaces picklist source and target not updated

Question: When I select one group from dataTable, I want to display corresponding users in pickList. When I move some users within pickList, I want these new values to be saved in bean's object even before submitting the values, or before selection in dataTable is changed.
When I move a user from source pickList to target pickList (or vice versa), and then change selection in dataTable, the pickList source and target values stay the same as the were when they got populated.
User.java
package com.test.model;

public class User {
    private String userName;

    public User(String userName) {
        this.userName = userName;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}

Group.java
package com.test.model;

import java.util.List;

public class Group {
    private String groupName;
    private List<User> users;

    public Group(String groupName, List<User> users) {
        this.groupName = groupName;
        this.users = users;
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }
}

UserBean.java
package com.test.beans;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.test.model.Group;
import com.test.model.User;
import org.primefaces.model.DualListModel;

@ManagedBean
@ViewScoped
public class UserBean {
    private List<Group> groups = new ArrayList<>();
    private List<User> allUsers = new ArrayList<>();
    private Group selectedGroup;
    private DualListModel<User> users = new DualListModel<>();

    public List<Group> getGroups() {
        return groups;
    }

    public Group getSelectedGroup() {
        return selectedGroup;
    }

    public void setSelectedGroup(Group selectedGroup) {
        this.selectedGroup = selectedGroup;
    }

    public DualListModel<User> getUsers() {
        return users;
    }

    public void setUsers(DualListModel<User> users) {
        this.users = users;
    }

    @PostConstruct
    private void init()  {
        List<User> usersGroup1 = new ArrayList<>();
        usersGroup1.add(new User("User1"));
        usersGroup1.add(new User("User2"));

        List<User> usersGroup2 = new ArrayList<>();
        usersGroup2.add(new User("User3"));
        usersGroup2.add(new User("User4"));

        allUsers.addAll(usersGroup1);
        allUsers.addAll(usersGroup2);

        groups.add(new Group("Group1", usersGroup1));
        groups.add(new Group("Group2", usersGroup2));
    }

    public void onRowSelect() {
        // here I want to save the picklist source, but it is already reset
        List<User> storeUsers = users.getSource();
        fillPickList();
    }

    private void fillPickList() {
        List<User> assignedUsers = new ArrayList<>();
        List<User> availableUsers = new ArrayList<>();

        assignedUsers.addAll(selectedGroup.getUsers());
        availableUsers.addAll(allUsers);

        for (User user : assignedUsers) {
            if (availableUsers.contains(user))
                availableUsers.remove(user);
        }

        users.setSource(assignedUsers);
        users.setTarget(availableUsers);
    }
}

index.xhtml
<!DOCTYPE html>

<h:head>
    <link rel="shortcut icon" type="image/x-icon" href="#{resource['images/favicon.ico']}"/>
</h:head>

<h:body>
    <h:form>

                <h:outputText value="#{group.groupName}"/>

    </h:form>
</h:body>

Comment: Please run your application in development mode and see if you have any errors. And **always** create a [mcve] to help us determine where the problem **might** be. Now there are to many possible causes that are invisible. And 'guessing' or asking more details in multiple questions is too time consuming
Comment: I don't understand the question. "I want these new values to be saved in bean's object even before submitting the values" - what? You don't want to send values to the server, but you want them to just appear on the server?
Comment: I want to be able to modify users from multiple groups and then save this changes all together
Answer: I solved it by creating a Map<Group, Set<User>> and then working with this object and updating it on each item transfer via public void onTransfer(TransferEvent event) method.
But I still don't understand what are the getSource() and getTarget() methods for, when they are not updated dynamically.
Comment: You don't send the picklist data to the server when you change things. Use ajax for that too. Or **don't** process the picklist in thhttp://stackoverflow.com/questions/25339056/understanding-primefaces-process-update-and-jsf-fajax-execute-render-attributes. Might make a difference
Comment: http://stackoverflow.com/questions/25339056/understanding-primefaces-process-update-and-jsf-fajax-execute-render-attributes
Answer: I solved it like this.
My xhtml:

                            <f:facet name="sourceCaption">Remove</f:facet>
                            <f:facet name="targetCaption">Add</f:facet>

My ManagedBean:
public void onAlterarPickList(TransferEvent event) {
        for(Object item : event.getItems()) {

            if(event.isRemove()) {
                objectList.remove(item);
                log.info("Removed:"+item.toString());
            }
            else if(event.isAdd()) {
                objectList.add(item.toString());
                log.info("Added:"+item.toString());
            }
        }
    }

Answer: i simply surrounded my <pickList> element with a <form> and it worked. If doesn't work for you try to watch this youtube video, for this guy worked well. Here's my xhtml code:
<h:form>

</h:form>


Where is the specification for conversion in JavaScript's left shift (<<) operator

Question: I would like to determine where my lack of knowledge is with respect to JavaScript's number handling. The gap shows up in the way that JS handles the shift left operator.
It is my understanding that in in JavaScript "all numbers are floating point". But that doesn't fit with bit shifting operators, and indeed, the demonstrable behavior also tells me there's more to this than I'm aware of.
I checked the specification here: bitshift operators and all it says (at this point) is

The Left Shift Operator ( << ) Performs a bitwise left shift operation on the left operand by the amount specified by the right operand.

But if all numbers are in fact represented as a floating point format, then this, taken literally would mean we'd be shifting the bit representations of mantissa and exponent, and would get total nonsense values as the exponent bits moved into the mantissa bits. So, my next thought was that perhaps it just takes the mantissa part, shifts that, and puts it back. But that doesn't appear to be the case either, since 2.5 << 1 is 4, not 5.
I also found that any number with a large exponent value seems to result in zeroes.
So, my guess, from this brief examination, is that the system actually takes the number, performs a conversion that would be described as "chop out a 32 bit two's complement integer value" and then does a shift on that, before converting the resulting int back to a floating representation. (Unless I was lied to about the "all numbers are floats" in the first place!)
Can anyone tell me:
a) was I lied to about the "all numbers are floats" thing?
b) if I'm right in my guess about the "convert to int, shift, then convert back" behavior, where is that documented. I'm have to believe it's in the spec, but it's a big one, and while I've done some searching, I've not read it all (nor do I particularly want to if I can get a hint!)
Comment: a) no, b) looking into it
Answer: It can be found in the abstract operation Number::leftShift:

Let lnum be ! ToInt32(x).
Let rnum be ! ToUint32(y).

Whereas ToInt32 basically performs:

Let int32bit be int modulo 2³²
Comment: Oh, another entire segment of the docs, many thanks, that's very helpful.
Comment: @TobyEggitt See http://ecma-international.org/ecma-262/11.0/#sec-left-shift-operator and http://ecma-international.org/ecma-262/11.0/#sec-numeric-types


Fitted values of ARMA model

Question: I'm trying to understand how fitted values are calculated for ARMA(p,q) models. I've already found a question on here concerning fitted values of ARMA processes but haven't been able to make sense of it.
If I have a ARMA(1,1) model, i. e.
$$X_t = \alpha_1X_{t-1}+\epsilon_t - \beta_1 \epsilon_{t-1}$$
and am given a (stationary) time series I can estimate the parameters. How would I calculate the fitted values using those estimates. For an AR(1) model the fitted values are given by
$$\hat{X_t} = \hat{\alpha_1}X_{t-1} . $$
Since the innovations in an ARMA model are unobservable how would I use the estimate of the MA parameter? Would I just ignore the MA part and calculate the fitted values of the AR part?
Answer: To answer your questions, you basically need to know how the residuals i.e.  $e_t$ are calculated in an arma model. Because then $\hat{X_{t}}=X_{t}-e_{t}$. Let's first generate a fake data ($X_t$) from arima(.5,.6) and fit the arma model (without mean):
library(forecast)
n=1000
ts_AR <- arima.sim(n = n, list(ar = 0.5,ma=0.6))
f=arima(ts_AR,order=c(1,0,1),include.mean=FALSE)
summary(f)
    Series: ts_AR
    ARIMA(1,0,1) with zero mean

    Coefficients:
             ar1     ma1
          0.4879  0.5595
    s.e.  0.0335  0.0317

    sigma^2 estimated as 1.014:  log likelihood=-1426.7
    AIC=2859.4   AICc=2859.42   BIC=2874.12

    Training set error measures:
                         ME    RMSE       MAE      MPE     MAPE      MASE
    Training set 0.02102758 1.00722 0.8057205 40.05802 160.1078 0.6313145

Now I create the residuals as follows: $e_1=0$ (since there is no residual at 1) and for $t=2,...,n$ we have: $e_t=X_t-Ar*X_{t-1}-Ma*e_{t-1}$, where $Ar$ and $Ma$ are the estimated auto-regressive and moving average part in above fitted model. Here is the code:
e = rep(1,n)
e[1] = 0 ##since there is no residual at 1, e1 = 0
for (t in (2 : n)){
  e[t] = ts_AR[t]-coef(f)[1]*ts_AR[t-1]-coef(f)[2]*e[t-1]
}

Once you find the residuals $e_{t}$, the fitted values are just $\hat{X_{t}}=X_{t}-e_{t}$. So in the following, I compared the first 10  fitted values obtained from R and the ones I can calculate from $e_{t}$ I created above (i.e. manually).
cbind(fitted.from.package=fitted(f)[1:10],fitted.calculated.manually=ts_AR[1:10]-e[1:10])
      fitted.from.package fitted.calculated.manually
 [1,]          -0.4193068                 -1.1653515
 [2,]          -0.8395447                 -0.5685977
 [3,]          -0.4386956                 -0.6051324
 [4,]           0.3594109                  0.4403898
 [5,]           2.9358336                  2.9013738
 [6,]           1.3489537                  1.3682191
 [7,]           0.5329436                  0.5219576
 [8,]           1.0221220                  1.0283511
 [9,]           0.6083310                  0.6048668
[10,]          -0.5371484                 -0.5352324

As you see there are close but not exactly the same. The reason is that when I created the residuals I set $e_{1}=0$. There are other choices though. For example based on the  help file to arima,  the residuals and their variance found by a Kalman filter and therefore their calculation of $e_t$ will be slightly different from me. But as time goes on they are converging.
Now for the Ar(1) model. I fitted the model (without mean) and directly show you how to calculate the fitted values using the coefficients. This time I didn't calculate the residuals. Note that I reported the first 10 fitted values removing the first one (as again it would be different depending on how you define it). As you can see, they are completely the same.
f=arima(ts_AR,order=c(1,0,0),include.mean=FALSE)
cbind(fitted.from.package=fitted(f)[2:10],fitted.calculated.manually=coef(f)*ts_AR[1:9])
      fitted.from.package fitted.calculated.manually
 [1,]          -0.8356307                 -0.8356307
 [2,]          -0.6320580                 -0.6320580
 [3,]           0.0696877                  0.0696877
 [4,]           2.1549019                  2.1549019
 [5,]           2.0480074                  2.0480074
 [6,]           0.8814094                  0.8814094
 [7,]           0.9039184                  0.9039184
 [8,]           0.8079823                  0.8079823
 [9,]          -0.1347165                 -0.1347165

Comment: In the help file to `arima` they say: "(...) the innovations and their variance found by a Kalman filter." So the function apparently somehow uses the Kalman filter for the initial values.

Fitted values of ARMA model


returning CSS value for a jQuery UI element

Question: I'm not sure if this is possible via jQuery or other approach, but i've incorporated the theme switcher widget into my web app. I have a set of Selectable UI elements that I'd like to color the same as the current theme's ui-state-highlight color upon page load/theme switch. I'm attempting to store the value of the background in a var, so I can use .css to set the selectable color to match.
input = $('.ui-state-highlight').css('background')
returns undefined, but if I try with
input = $('.ui-state-default').css('color')

I can return a value.
What did I miss?
Answer: Your first example you have no period before 'ui-state-highlight' so you are trying to select elements with a TYPE of ui-state-highlight (as opposed to a type of DIV, INPUT, SELECT, etc)
 $('ui-state-highlight')

Your second example you actually select elements with a class of 'ui-state-default'
 $('.ui-state-default')

Edit: Based on your corrections I am guessing that the problem here is that you may be trying to get the background color for any elements with the ui-state-highlight class but there aren't any actually displayed on your page.
Try the following code, based on answer to Get a CSS value from external style sheet with Javascript/jQuery
var $p = $("

").hide().appendTo("body");
input = $p.css("background");
$p.remove();

You create a new

 temporarily and then grab the background color off it before removing it.
Comment: my mistake, that was a typing fail. I do have the period before my selector, my return is undefined.
Comment: I've updated my answer so let me know if it helps at all now or I will delete it to make sure others correctly see your question as 'unanswered'


TypeError: unsupported operand type(s) for &: 'datetime.datetime' and 'bool'

Question: I was trying to replicate some SQL code:
proc sql;
        update __crsp_sf1
        set vol =
            case
                when date < '01FEB2001'd then vol / 2
                when date <= '31DEC2001'd then vol / 1.8
                when date < '31DEC2003'd then vol / 1.6
                else vol
            end
        where exchcd = 3;
    quit;

and I used
crsp_sf1.loc[(crsp_sf1['exchcd'] == 3)&(crsp_sf1['date']<datetime.datetime(2001, 2, 1)),'vol']/=2
crsp_sf1.loc[(crsp_sf1['exchcd'] == 3)&(crsp_sf1['date']>datetime.datetime(2001, 2, 1)&(crsp_sf1['date']<=datetime.datetime(2001, 12, 31))),'vol']/=1.8
crsp_sf1.loc[(crsp_sf1['exchcd'] == 3)&(crsp_sf1['date']>datetime.datetime(2001, 12, 31)&(crsp_sf1['date']<datetime.datetime(2003, 12, 31))),'vol']/=1.6

But I got a warning saying "TypeError: unsupported operand type(s) for &: 'datetime.datetime' and 'bool'". At the same time, I checked my dataframe to find some values of 'vol' column were updated with 1/2 of the previous value. But I did not know if every row was updated or the meaning of this warning.
How should I did this SQL code in python correctly?
Comment: In the statements with three clauses, you have a parenthesis in the wrong place.  There needs to be TWO parens before the `&` sign, and then only TWO parens before the `,`.  So, it's a typo.  When I fix that, your code works for me.
Comment: Will you please add output of running `print(crsp_sf1.head().to_dict())` to the question, so I can run the code myself?
Answer: I'm violating policy here by posting an answer to what is a typo, but because the lines are complicated, it might not be clear where the problem is.  Here is your code, spaced out vertically to be easier to read (and, hence, easier to spot problems).  This works.
import pandas as pd
import datetime

data = [
    [ 3, '2001-01-15', 100 ],
    [ 3, '2001-04-15', 100 ],
    [ 3, '2001-09-15', 100 ],
    [ 3, '2002-01-15', 100 ],
    [ 3, '2002-04-15', 100 ],
    [ 3, '2002-09-15', 100 ]
]

crsp_sf1 = pd.DataFrame(data, columns=['exchcd','date','vol'])
crsp_sf1['date'] = pd.to_datetime(crsp_sf1['date'])
print(crsp_sf1)

crsp_sf1.loc[
    (crsp_sf1['exchcd'] == 3)
    &
    (crsp_sf1['date']<datetime.datetime(2001, 2, 1))
    ,
    'vol'] /= 2
print(crsp_sf1)

crsp_sf1.loc[
    (crsp_sf1['exchcd'] == 3)
    &
    (crsp_sf1['date']>datetime.datetime(2001, 2, 1))
    &
    (crsp_sf1['date']<=datetime.datetime(2001, 12, 31))
    ,
    'vol'] /= 1.8
print(crsp_sf1)

crsp_sf1.loc[
    (crsp_sf1['exchcd'] == 3)
    &
    (crsp_sf1['date']>datetime.datetime(2001, 12, 31))
    &
    (crsp_sf1['date']<datetime.datetime(2003, 12, 31))
    ,
    'vol'] /= 1.6
print(crsp_sf1)

Answer: It's simple a parentheses mix-up, as @TimRoberts pointed out:
crsp_sf1.loc[(crsp_sf1['exchcd'] == 3)&(crsp_sf1['date']<datetime.datetime(2001, 2, 1)),'vol']/=2

crsp_sf1.loc[(crsp_sf1['exchcd'] == 3)&(crsp_sf1['date']>datetime.datetime(2001, 2, 1))&(crsp_sf1['date']<=datetime.datetime(2001, 12, 31))),'vol']/=1.8
                                                                                ^ add this                                           ^ remove this
crsp_sf1.loc[(crsp_sf1['exchcd'] == 3)&(crsp_sf1['date']>datetime.datetime(2001, 12, 31))&(crsp_sf1['date']<datetime.datetime(2003, 12, 31))),'vol']/=1.6
                                                                                ^ add this                                          ^ remove this


Export Excel chart to SVG creates an empty file

Question: I am trying to export an Excel chart in SVG format using VBA.
    Set objChrt = ActiveChart.Parent
    objChrt.Activate
    Set curChart = objChrt.Chart

    curChart.Export fileName:=fileName, FilterName:="SVG"

If I replace "SVG" by "PNG", the export works exactly as intended and produces a valid PNG file. However, "SVG" results in an empty file. (Manually, there is an option to save as SVG inside Excel 365, so the export filter exists).
According to the documentation, Filtername is "The language-independent name of the graphic filter as it appears in the registry.", but I couldn't find anything like that in the registry, and either way, it's hard to imagine the SVG filtername being named anything other than "SVG".
Is there a way to export a Chart in SVG format using VBA?

Note: There is another question about Chart.export producing an empty file, and the fix was to use ChartObject.Activate before the export. This question is different because the code works correctly with "PNG" but fails with "SVG" (so it's not an issue related to activation or visibility). Also the recommended fix does not work.
Comment: After some testing/research, I doubt this is currently doable directly with `Chart.Export`.
Comment: Does the macro recorder help - what code does it produce if you export manually?
Comment: @FoxfireAndBurnsAndBurns, the problem is that this won't create a real .svg in terms of a vector graphic... .svgs can contain embedded .png files, which is basically happening in the case you describe I suppose.
Comment: @BigBen That was worth a try, but sadly, the only thing recorded is "ActiveSheet.ChartObjects("Graphique 11").Activate"
Comment: Exporting to png and renaming the file to SVG?? it makes an image that can be opened in mspaint at least...
Answer: 2023 Update
This issue seems to be fixed in Excel Version 2302 Build 16.0.16130.20186) 64-bit, it was fixed in one of the versions released since 2021. Unfortunately, I can not find a mention of this fix in the release notes / archive.
This now works as specified in the documentation:
With ThisWorkbook.Worksheets("Sheet1")
    .ChartObjects("Chart 1").Chart.Export FileName:="path\name.svg", _
                                          FilterName:="SVG"
End With

For future reference, I will leave the original workaround for the bug below. However, even if you are working with one of the bugged versions of Excel, I recommend using this more elegant workaround by Jeremy Lakeman instead.

Old Workaround
Exporting to .SVG Without Any External Applications Using Only Excel and VBA
This is a hacky mess, but it works. At least for now...
First I will explain how it works, what problems exist that had to be overcome, and how they were solved. If you are not interested in the technicalities, you can skip to the section Simple Usage Guide.
What's the idea?
The code attempts to just use the manual export method. There are several problems with this, the first being yet another bug in the Chart.Export method. Chart.Export Interactive:=True is supposed to open the desired dialog box, but this just doesn't work. By leveraging some shortcuts, the export window can be opened very reliably using SendKeys "+{F10}" followed by SendKeys "g". The first hurdle is taken, but the trouble has only just begun!
It turns out, that opening a modal Dialog stops all code execution in the entire Application. Even if code in another application instance is called before we open the dialog, how can we keep it running there and also return to finish opening the dialog? It sounds impossible because VBA is strictly single-threaded...
It turns out, the single threading is not quite so strict. The solution is called Application.OnTime, which starts a procedure at a predetermined time in the future. That procedure has to run in a different instance of Excel.Application because Application.OnTime will only start a procedure if the application is in certain modes (Ready, Copy, Cut, or Find), and having VBA code running or having a modal dialog open are not among those. Therefore, before the dialog is opened, a background instance of the Excel app needs to be created, VBA code inserted into it and scheduled to start running in the background instance once the dialog is open. Note: Because the code is inserted automatically into the background instance, Trust access to the VBA project object model needs to be enabled.
How can we now work with the Windows dialog box using only VBA code? I managed to get all the window and control handles of the dialog via EnumChildWindows and used the information to insert text into the "FileName" ComboBox. Since this input box also accepts the path, the only problems left were selecting ".svg" in the FileFormat ComboBox and clicking the "Save" Button. Unfortunately I didn't manage to avoid using SendKeys here.
Changing the selection in the Combobox is relatively easy using Windows API functions but the problem is to actually get it to register the change. It appears to change in the dialog but when clicking "Save" it still saves as .png. I spent hours in Spy++ monitoring the messages that are sent during a manual change but I wasn't able to reproduce them with VBA. Because of this, it had to be SendKeys again for changing the file format and pressing 'Save'.
SendKeys is used very carefully in this solution, including various safety checks, and pulling the target window to the front before every usage, but it is never 100% safe if the pc is being interacted with while the macro is running.
Because the method requires a background instance of an app similar to here and here, I implemented a class for a ShapeExporter object. Creating the object opens the background app, destroying the object closes it.
Simple Usage Guide
The following procedure will export all ChartObjects in the specified worksheet to the folder the workbook is saved in.
Sub ExportEmbeddedChartToSVG()
    Dim MyWorksheet As Worksheet
    Set MyWorksheet = Application.Worksheets("MyWorksheet")

    'Creating the ShapeExporter object
    Dim oShapeExporter As cShapeExporter
    Set oShapeExporter = New cShapeExporter

    'Export as many shapes as you want here, before destroying oShapeExporter
    Dim oChart As ChartObject
    For Each oChart In MyWorksheet.ChartObjects
        'the .ExportShapeAsSVG method of the object takes three arguments:
        '1. The Chart or Shape to be exported
        '2. The target filename
        '3. The target path
        oShapeExporter.ExportShapeAsSVG oChart, oChart.Name, ThisWorkbook.Path
    Next oChart

    'When the object goes out of scope, its terminate procedure is automatically called
    'and the background app is closed
    Set oShapeExporter = Nothing
End Sub

For the code to work, you must first:

Trust access to the VBA project object model (for reason see detailed description of the macro)
Create a class module, rename it to "cShapeExporter", and paste the following code into it:

'Class for automatic exporting in SVG-Format
'Initial author: Guido Witt-Dörring, 09.12.2020
'https://stackoverflow.com/a/65212838/12287457

'Note:
'When objects created from this class are not properly destroyed, an invisible
'background instance of Excel will keep running on your computer. In this
'case, you can just close it via the Task Manager.
'For example, this will happen when your code hits an 'End' statement, which
'immediately stops all code execution, or when an unhandled error forces
'you to stop code execution manually while an instance of this class exists.

Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    Private Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal hWnd As LongPtr) As Boolean
    Private Declare PtrSafe Function IsWindowVisible Lib "user32" (ByVal hWnd As LongPtr) As Boolean
    Private Declare PtrSafe Function IsIconic Lib "user32" (ByVal hWnd As LongPtr) As Boolean
    Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
#Else
    Private Declare Sub Sleep Lib "kernel32" (ByVal lngMilliSeconds As Long)
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Boolean
    Private Declare Function IsWindowVisible Lib "User32" (ByVal hWnd As Long) As Boolean
    Private Declare Function IsIconic Lib "User32" Alias "IsIconic" (ByVal hWnd As long) As boolean
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
#End If

Private NewXlAppInstance As Excel.Application
Private xlWbInOtherInstance As Workbook

Private Sub Class_Initialize()
    Set NewXlAppInstance = New Excel.Application
    Set xlWbInOtherInstance = NewXlAppInstance.Workbooks.Add

    NewXlAppInstance.Visible = False

    On Error Resume Next
    xlWbInOtherInstance.VBProject.References.AddFromFile "scrrun.dll"
    xlWbInOtherInstance.VBProject.References.AddFromFile "FM20.dll"
    On Error GoTo 0

    Dim VbaModuleForOtherInstance As VBComponent
    Set VbaModuleForOtherInstance = xlWbInOtherInstance.VBProject.VBComponents.Add(vbext_ct_StdModule)

    VbaModuleForOtherInstance.CodeModule.AddFromString CreateCodeForOtherXlInstance
End Sub

Private Sub Class_Terminate()
    NewXlAppInstance.DisplayAlerts = False
    NewXlAppInstance.Quit
    Set xlWbInOtherInstance = Nothing
    Set NewXlAppInstance = Nothing
End Sub

Public Sub ExportShapeAsSVG(xlShp As Object, FileName As String, FilePath As String)
    'Check if path exists:
    If Not ExistsPath(FilePath) Then
        If vbYes = MsgBox("Warning, you are trying to export a file to a path that doesn't exist! Continue exporting to default path? " & vbNewLine & "Klick no to resume macro without exporting or cancel to debug.", vbYesNoCancel, "Warning") Then
            FilePath = ""
        ElseIf vbNo Then
            Exit Sub
        ElseIf vbCancel Then
            Error 76
        End If
    End If
    If TypeName(xlShp) = "ChartObject" Or TypeName(xlShp) = "Shape" Or TypeName(xlShp) = "Chart" Or TypeName(xlShp) = "ChartArea" Then
        'fine
    Else
        MsgBox "Exporting Objects of type " & TypeName(xlShp) & " not supported, sorry."
        Exit Sub
    End If

    If TypeName(xlShp) = "ChartArea" Then Set xlShp = xlShp.Parent

retry:
    SetForegroundWindow FindWindow("XLMAIN", ThisWorkbook.Name & " - Excel")

    If Not Application.Visible Then 'Interestingly, API function "IsWindowVisible(Application.hWnd)" doesn't work here! (maybe because of multi monitor setup?)
        MsgBox "The workbook must be visible for the svg-export to proceed! It must be at least in window mode!"
        Application.WindowState = xlNormal
        Application.Visible = True
        Sleep 100
        GoTo retry
    End If

    If IsIconic(Application.hWnd) Then 'Interestingly "Application.WindowState = xlMinimized" doesn't work here!"
        MsgBox "The workbook can't be minimized for the svg-export to proceed! It must be at least in window mode!"
        Application.WindowState = xlNormal
        Sleep 100
        GoTo retry
    End If

    'check if background instance still exists and start support proc
    On Error GoTo errHand
    NewXlAppInstance.Run "ScheduleSvgExportHelperProcess", Application.hWnd, ThisWorkbook.Name, FileName, FilePath
    On Error GoTo 0

    Sleep 100

    xlShp.Activate

    SetForegroundWindow FindWindow("XLMAIN", ThisWorkbook.Name & " - Excel")
    SendKeys "+{F10}"
    DoEvents
    SendKeys "g"
    DoEvents
    Exit Sub
errHand:
    MsgBox "Error in ShapeExporter Object. No more shapes can be exported."
    Err.Raise Err.Number
End Sub

Public Function ExistsPath(ByVal FilePath As String) As Boolean
    Dim oFso As Object
    Dim oFolder As Object

    Set oFso = CreateObject("Scripting.FileSystemObject")
    'Setting the Folder of the Filepath
    On Error GoTo PathNotFound
    Set oFolder = oFso.GetFolder(Left(Replace(FilePath & "\", "\\", "\"), Len(Replace(FilePath & "\", "\\", "\")) - 1))
    On Error GoTo 0

    ExistsPath = True
    Exit Function

PathNotFound:
    ExistsPath = False
End Function

Private Function CreateCodeForOtherXlInstance() As String
    Dim s As String
    s = s & "Option Explicit" & vbCrLf
    s = s & "" & vbCrLf
    s = s & "#If VBA7 Then" & vbCrLf
    s = s & "    Public Declare PtrSafe Sub Sleep Lib ""kernel32"" (ByVal dwMilliseconds As Long)" & vbCrLf
    s = s & "    Private Declare PtrSafe Function GetForegroundWindow Lib ""user32"" () As LongPtr" & vbCrLf
    s = s & "    Private Declare PtrSafe Function GetWindowText Lib ""user32"" Alias ""GetWindowTextA"" (ByVal hWnd As LongPtr, ByVal lpString As String, ByVal cch As Long) As Long" & vbCrLf
    s = s & "    Private Declare PtrSafe Function FindWindow Lib ""user32"" Alias ""FindWindowA"" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr" & vbCrLf
    s = s & "    Private Declare PtrSafe Function SetForegroundWindow Lib ""user32"" (ByVal hWnd As LongPtr) As Boolean" & vbCrLf
    s = s & "    Private Declare PtrSafe Function SendMessage Lib ""user32"" Alias ""SendMessageA"" (ByVal hWnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr" & vbCrLf
    s = s & "    Private Declare PtrSafe Function GetClassName Lib ""user32"" Alias ""GetClassNameA"" (ByVal hWnd As LongPtr, ByVal lpStr As String, ByVal nMaxCount As Long) As Long" & vbCrLf
    s = s & "    Private Declare PtrSafe Function EnumChildWindows Lib ""user32"" (ByVal hWndParent As LongPtr, ByVal lpEnumFunc As LongPtr, ByVal lParam As LongPtr) As Boolean" & vbCrLf
    s = s & "    Private Declare PtrSafe Function GetWindowTextLength Lib ""user32"" Alias ""GetWindowTextLengthA"" (ByVal hWnd As LongPtr) As Long" & vbCrLf
    s = s & "    Private Declare PtrSafe Function GetWindowLongPtr Lib ""user32"" Alias ""GetWindowLongPtrA"" (ByVal hWnd As LongPtr, ByVal nindex As Long) As LongPtr" & vbCrLf
    s = s & "#Else" & vbCrLf
    s = s & "    Public Declare Sub Sleep Lib ""kernel32"" (ByVal lngMilliSeconds As Long)" & vbCrLf
    s = s & "    Private Declare Function GetForegroundWindow Lib ""user32"" () As Long" & vbCrLf
    s = s & "    Private Declare Function GetWindowText Lib ""user32"" Alias ""GetWindowTextA"" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long" & vbCrLf
    s = s & "    Private Declare Function FindWindow Lib ""user32"" Alias ""FindWindowA"" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long" & vbCrLf
    s = s & "    Private Declare Function SetForegroundWindow Lib ""user32"" (ByVal hwnd As Long) As Boolean" & vbCrLf
    s = s & "    Private Declare Function SendMessage Lib ""user32"" Alias ""SendMessageA"" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long" & vbCrLf
    s = s & "    Private Declare Function GetClassName Lib ""user32"" Alias ""GetClassNameA"" (ByVal hwnd As Long, ByVal lpStr As String, ByVal nMaxCount As Long) As Long" & vbCrLf
    s = s & "    Private Declare Function EnumChildWindows Lib ""User32"" (ByVal hwndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As boolean" & vbCrLf
    s = s & "    Private Declare Function GetWindowTextLength Lib ""User32"" Alias ""GetWindowTextLengthA"" (ByVal hwnd As Long) As Long" & vbCrLf
    s = s & "    Private Declare Function GetWindowLongPtr Lib ""User32"" Alias ""GetWindowLongPtrA"" (ByVal hwnd As Long, ByVal nindex As Long) As Long" & vbCrLf
    s = s & "#End If" & vbCrLf
    s = s & "" & vbCrLf
    s = s & "Private Const GWL_ID = -12" & vbCrLf
    s = s & "" & vbCrLf
    s = s & "Private Const WM_SETTEXT = &HC" & vbCrLf
    s = s & "" & vbCrLf
    s = s & "'Const for this Application:" & vbCrLf
    s = s & "Private Const dc_Hwnd = 1" & vbCrLf
    s = s & "Private Const dc_ClassName = 2" & vbCrLf
    s = s & "Private Const dc_CtlID = 3" & vbCrLf
    s = s & "Private Const dc_CtlText = 4" & vbCrLf
    s = s & "" & vbCrLf
    s = s & "Private Const Window_Search_Timeout As Single = 5#" & vbCrLf
    s = s & "Public ChildWindowsPropDict As Object" & vbCrLf
    s = s & "" & vbCrLf
    s = s & "#If VBA7 Then" & vbCrLf
    s = s & "    Private Function GetCtlText(ByVal hctl As LongPtr) As String" & vbCrLf
    s = s & "#Else" & vbCrLf
    s = s & "    Private Function GetCtlText(ByVal hctl As Long) As String" & vbCrLf
    s = s & "#End If" & vbCrLf
    s = s & "    Dim ControlText As String" & vbCrLf
    s = s & " On Error GoTo WindowTextTooLarge" & vbCrLf
    s = s & "    ControlText = Space(GetWindowTextLength(hctl) + 1)" & vbCrLf
    s = s & "    GetWindowText hctl, ControlText, Len(ControlText)" & vbCrLf
    s = s & "    GetCtlText = ControlText 'Controls Text" & vbCrLf
    s = s & "    Exit Function" & vbCrLf
    s = s & "    " & vbCrLf
    s = s & "WindowTextTooLarge:" & vbCrLf
    s = s & "    ControlText = Space(256)" & vbCrLf
    s = s & "    On Error GoTo -1" & vbCrLf
    s = s & "    GetWindowText hctl, ControlText, Len(ControlText)" & vbCrLf
    s = s & "    GetCtlText = ControlText  'Controls Text" & vbCrLf
    s = s & "End Function" & vbCrLf
    s = s & "" & vbCrLf
    s = s & "#If VBA7 Then" & vbCrLf
    s = s & "    Private Function EnumChildProc(ByVal hWnd As LongPtr, ByVal lParam As LongPtr) As Long" & vbCrLf
    s = s & "#Else" & vbCrLf
    s = s & "    Private Function EnumChildProc(ByVal hWnd As Long, ByVal lParam As Long) As Long" & vbCrLf
    s = s & "#End If" & vbCrLf
    s = s & "    Dim ClassName As String" & vbCrLf
    s = s & "    Dim subCtlProp(1 To 4) As Variant" & vbCrLf
    s = s & "    " & vbCrLf
    s = s & "    subCtlProp(dc_Hwnd) = hWnd 'Controls Handle" & vbCrLf
    s = s & "    " & vbCrLf
    s = s & "    ClassName = Space(256)" & vbCrLf
    s = s & "    GetClassName hWnd, ClassName, Len(ClassName)" & vbCrLf
    s = s & "    subCtlProp(dc_ClassName) = Trim(CStr(ClassName)) 'Controls ClassName" & vbCrLf
    s = s & "    " & vbCrLf
    s = s & "    subCtlProp(dc_CtlID) = GetWindowLongPtr(hWnd, GWL_ID) 'Controls ID" & vbCrLf
    s = s & "    " & vbCrLf
    s = s & "    subCtlProp(dc_CtlText) = GetCtlText(hWnd)   'Controls Text 'Doesn't always work for some reason..." & vbCrLf
    s = s & "                                                '(sometimes returns """" when Spy++ finds a string)" & vbCrLf
    s = s & "    ChildWindowsPropDict.Add key:=CStr(hWnd), Item:=subCtlProp" & vbCrLf
    s = s & "    " & vbCrLf
    s = s & "    'continue to enumerate (0 would stop it)" & vbCrLf
    s = s & "    EnumChildProc = 1" & vbCrLf
    s = s & "End Function" & vbCrLf
    s = s & "" & vbCrLf
    s = s & "#If VBA7 Then" & vbCrLf
    s = s & "    Private Sub WriteChildWindowsPropDict(hWnd As LongPtr)" & vbCrLf
    s = s & "#Else" & vbCrLf
    s = s & "    Private Sub WriteChildWindowsPropDict(hWnd As Long)" & vbCrLf
    s = s & "#End If" & vbCrLf
    s = s & "    On Error Resume Next" & vbCrLf
    s = s & "    Set ChildWindowsPropDict = Nothing" & vbCrLf
    s = s & "    On Error GoTo 0" & vbCrLf
    s = s & "    Set ChildWindowsPropDict = CreateObject(""Scripting.Dictionary"")" & vbCrLf
    s = s & "    EnumChildWindows hWnd, AddressOf EnumChildProc, ByVal 0&" & vbCrLf
    s = s & "End Sub" & vbCrLf
    s = s & "" & vbCrLf
    s = s & "Private Function ExistsFileInPath(ByVal FileName As String, ByVal FilePath As String, Optional warn As Boolean = False) As Boolean" & vbCrLf
    s = s & "    Dim oFso As Object" & vbCrLf
    s = s & "    Dim oFile As Object" & vbCrLf
    s = s & "    Dim oFolder As Object" & vbCrLf
    s = s & "    " & vbCrLf
    s = s & "    Set oFso = CreateObject(""Scripting.FileSystemObject"")" & vbCrLf
    s = s & "    'Setting the Folder of the Filepath" & vbCrLf
    s = s & "    On Error GoTo PathNotFound" & vbCrLf
    s = s & "    Set oFolder = oFso.GetFolder(Left(Replace(FilePath & ""\"", ""\\"", ""\""), Len(Replace(FilePath & ""\"", ""\\"", ""\"")) - 1))" & vbCrLf
    s = s & "    On Error GoTo 0" & vbCrLf
    s = s & "    " & vbCrLf
    s = s & "    'Writing all Filenames of the Files in the Folder to flStr" & vbCrLf
    s = s & "    For Each oFile In oFolder.Files" & vbCrLf
    s = s & "        If oFile.Name = FileName Then" & vbCrLf
    s = s & "            ExistsFileInPath = True" & vbCrLf
    s = s & "            Exit Function" & vbCrLf
    s = s & "        End If" & vbCrLf
    s = s & "    Next oFile" & vbCrLf
    s = s & "    " & vbCrLf
    s = s & "    ExistsFileInPath = False" & vbCrLf
    s = s & "    Exit Function" & vbCrLf
    s = s & "    " & vbCrLf
    s = s & "PathNotFound:" & vbCrLf
    s = s & "    If warn Then MsgBox ""The path "" & Chr(10) & FilePath & Chr(10) & "" was not found by the function ExistsFileInPath."" & Chr(10) & ""Returning FALSE""" & vbCrLf
    s = s & "    ExistsFileInPath = False" & vbCrLf
    s = s & "End Function" & vbCrLf
    s = s & "" & vbCrLf
    s = s & "#If VBA7 Then" & vbCrLf
    s = s & "    Public Sub ScheduleSvgExportHelperProcess(ByVal Wb1hwnd As LongPtr, ByVal Wb1Name As String, ByVal SvgFileName As String, ByVal SvgFilePath As String)" & vbCrLf
    s = s & "#Else" & vbCrLf
    s = s & "    Public Sub ScheduleSvgExportHelperProcess(ByVal Wb1hwnd As Long, ByVal Wb1Name As String, ByVal SvgFileName As String, ByVal SvgFilePath As String)" & vbCrLf
    s = s & "#End If" & vbCrLf
    s = s & "    If Not Wb1hwnd = FindWindow(""XLMAIN"", Wb1Name & "" - Excel"") Then" & vbCrLf
    s = s & "        MsgBox ""Error finding Wb1hwnd - something unforseen happened!""" & vbCrLf
    s = s & "        GoTo badExit" & vbCrLf
    s = s & "    End If" & vbCrLf
    s = s & "    " & vbCrLf
    s = s & "    Application.OnTime Now + TimeValue(""00:00:02""), ""'SvgExportHelperProcess """""" & CStr(Wb1hwnd) & """""", """""" & Wb1Name & """""", """""" & SvgFileName _" & vbCrLf
    s = s & "                        & """""", """""" & SvgFilePath & """"""'"", Now + TimeValue(""00:00:015"")" & vbCrLf
    s = s & "    Exit Sub" & vbCrLf
    s = s & "badExit:" & vbCrLf
    s = s & "    MsgBox ""Shutting down background instance of excel.""" & vbCrLf
    s = s & "    Application.DisplayAlerts = False" & vbCrLf
    s = s & "    Application.Quit" & vbCrLf
    s = s & "End Sub" & vbCrLf
    s = s & "" & vbCrLf
    s = s & "Public Sub SvgExportHelperProcess(ByVal Wb1hwndStr As String, ByVal Wb1Name As String, ByVal SvgFileName As String, ByVal SvgFilePath As String)" & vbCrLf
    s = s & "    #If VBA7 And Win64 Then" & vbCrLf
    s = s & "        Dim Wb1hwnd As LongPtr" & vbCrLf
    s = s & "        Wb1hwnd = CLngPtr(Wb1hwndStr)" & vbCrLf
    s = s & "        Dim dlgHwnd As LongPtr" & vbCrLf
    s = s & "        Dim tempHctrl As LongPtr" & vbCrLf
    s = s & "    #Else" & vbCrLf
    s = s & "        Dim Wb1hwnd As LongPtr" & vbCrLf
    s = s & "        Wb1hwnd = CLng(Wb1hwndStr)" & vbCrLf
    s = s & "        Dim dlgHwnd As Long" & vbCrLf
    s = s & "        Dim tempHctrl As Long" & vbCrLf
    s = s & "    #End If" & vbCrLf
    s = s & "    Dim i As Long" & vbCrLf
    s = s & "    Dim stopTime As Single" & vbCrLf
    s = s & "    " & vbCrLf
    s = s & "    'Find dialog window handle" & vbCrLf
    s = s & "    stopTime = Timer() + Window_Search_Timeout" & vbCrLf
    s = s & "    Do" & vbCrLf
    s = s & "        dlgHwnd = 0" & vbCrLf
    s = s & "        Sleep 15" & vbCrLf
    s = s & "        DoEvents" & vbCrLf
    s = s & "        SetForegroundWindow Wb1hwnd  'FindWindow(""XLMAIN"", Wb1Name & "" - Excel"")" & vbCrLf
    s = s & "        Sleep 150" & vbCrLf
    s = s & "        dlgHwnd = FindWindow(""#32770"", vbNullString)" & vbCrLf
    s = s & "    Loop Until Timer() > stopTime Or dlgHwnd <> 0" & vbCrLf
    s = s & "    " & vbCrLf
    s = s & "    If dlgHwnd = 0 Then" & vbCrLf
    s = s & "        MsgBox ""Couldn't find dialog window handle!""" & vbCrLf
    s = s & "        GoTo errHand" & vbCrLf
    s = s & "    End If" & vbCrLf
    s = s & "    " & vbCrLf
    s = s & "    'Enumerate the child windows of the dialog and write their properties to a dictionary" & vbCrLf
    s = s & "    WriteChildWindowsPropDict dlgHwnd" & vbCrLf
    s = s & "" & vbCrLf
    s = s & "    'the first window of class ""Edit"" inside ChildWindowsPropDict will be the filename box" & vbCrLf
    s = s & "    Dim v As Variant" & vbCrLf
    s = s & "    For Each v In ChildWindowsPropDict.items" & vbCrLf
    s = s & "        If Left(CStr(v(dc_ClassName)), Len(CStr(v(dc_ClassName))) - 1) = ""Edit"" Then" & vbCrLf
    s = s & "            tempHctrl = v(dc_Hwnd)" & vbCrLf
    s = s & "            'send message" & vbCrLf
    s = s & "            SendMessage tempHctrl, WM_SETTEXT, 0&, ByVal SvgFilePath & ""\"" & SvgFileName" & vbCrLf
    s = s & "            'we don't need this hwnd anymore" & vbCrLf
    s = s & "            ChildWindowsPropDict.Remove CStr(v(dc_Hwnd))" & vbCrLf
    s = s & "            Exit For" & vbCrLf
    s = s & "        End If" & vbCrLf
    s = s & "    Next v" & vbCrLf
    s = s & "" & vbCrLf
    s = s & "retry:" & vbCrLf
    s = s & "    SetForegroundWindow dlgHwnd" & vbCrLf
    s = s & "    " & vbCrLf
    s = s & "    SendKeys ""{TAB}""" & vbCrLf
    s = s & "    Sleep 250" & vbCrLf
    s = s & "    SetForegroundWindow dlgHwnd" & vbCrLf
    s = s & "    For i = 1 To 10" & vbCrLf
    s = s & "        SendKeys ""{DOWN}""" & vbCrLf
    s = s & "        Sleep 100" & vbCrLf
    s = s & "        SetForegroundWindow dlgHwnd" & vbCrLf
    s = s & "    Next i" & vbCrLf
    s = s & "    " & vbCrLf
    s = s & "    SendKeys ""~""" & vbCrLf
    s = s & "    Sleep 100" & vbCrLf
    s = s & "    SetForegroundWindow dlgHwnd" & vbCrLf
    s = s & "    SendKeys ""~""" & vbCrLf
    s = s & "    Sleep 50" & vbCrLf
    s = s & "    " & vbCrLf
    s = s & "    'give the keystrokes time to process" & vbCrLf
    s = s & "    Sleep 300" & vbCrLf
    s = s & "" & vbCrLf
    s = s & "    'Wait until the file appears in the specified path:" & vbCrLf
    s = s & "    Dim cleanFileName As String" & vbCrLf
    s = s & "    If InStr(1, Right(SvgFileName, 4), "".svg"", vbTextCompare) = 0 Then" & vbCrLf
    s = s & "        cleanFileName = SvgFileName & "".svg""" & vbCrLf
    s = s & "    Else" & vbCrLf
    s = s & "        cleanFileName = SvgFileName" & vbCrLf
    s = s & "    End If" & vbCrLf
    s = s & "    " & vbCrLf
    s = s & "    Dim retryTime As Single" & vbCrLf
    s = s & "    retryTime = Timer + 5" & vbCrLf
    s = s & "    stopTime = Timer + 60  '1 minute timeout." & vbCrLf
    s = s & "                            'relatively long in case a file already exists dialog appears..." & vbCrLf
    s = s & "    Do Until ExistsFileInPath(SvgFileName, SvgFilePath, False)" & vbCrLf
    s = s & "        Sleep 700" & vbCrLf
    s = s & "        DoEvents" & vbCrLf
    s = s & "        If Timer > retryTime Then" & vbCrLf
    s = s & "            'check if graphic export dialog is top window" & vbCrLf
    s = s & "            If dlgHwnd = GetForegroundWindow Then GoTo retry" & vbCrLf
    s = s & "        End If" & vbCrLf
    s = s & "        If Timer > stopTime Then GoTo timeoutHand" & vbCrLf
    s = s & "    Loop" & vbCrLf
    s = s & "    " & vbCrLf
    s = s & "    Exit Sub" & vbCrLf
    s = s & "errHand:" & vbCrLf
    s = s & "    MsgBox ""Error in the helper process""" & vbCrLf
    s = s & "    GoTo badExit" & vbCrLf
    s = s & "    " & vbCrLf
    s = s & "timeoutHand:" & vbCrLf
    s = s & "    MsgBox ""Timeout. It seems like something went wrong creating the file. File "" & cleanFileName & "" didn't appear in folder "" & SvgFilePath & "".""" & vbCrLf
    s = s & "    GoTo badExit" & vbCrLf
    s = s & "    " & vbCrLf
    s = s & "badExit:" & vbCrLf
    s = s & "    MsgBox ""Shutting down background instance of excel.""" & vbCrLf
    s = s & "    Application.DisplayAlerts = False" & vbCrLf
    s = s & "    Application.Quit" & vbCrLf
    s = s & "End Sub" & vbCrLf
    s = s & "" & vbCrLf
    CreateCodeForOtherXlInstance = s
End Function

Comment: Wow, that's impressive. I had no idea you could do that in VBA.
Comment: When you copy a chart, excel adds an "image/svg+xml" type to the clipboard. I haven't tried yet, but exporting that format feels a lot less hacky.
Comment: I've tried it now, see answer (currently below...)
Answer: If you don't need .svg in particular then .emf is another vector format. It does not work directly from Excel but it does work using a 'helper' PowerPoint app:
Sub ExportChartToEMF(ByVal ch As Chart, ByVal filePath As String)
    Const methodName As String = "ExportChartToEMF"
    Const ppShapeFormatEMF As Long = 5
    '
    If ch Is Nothing Then Err.Raise 91, methodName, "Chart not set"
    '
    Dim pp As Object
    Dim slide As Object
    Dim errNumber As Long
    '
    Set pp = CreateObject("PowerPoint.Application")
    With pp.Presentations.Add(msoFalse) 'False so it's not Visible
        Set slide = .Slides.AddSlide(.Slides.Count + 1, .Designs(1).SlideMaster.CustomLayouts(1))
    End With
    '
    ch.Parent.Copy
    On Error Resume Next
    slide.Shapes.Paste.Export filePath, ppShapeFormatEMF
    errNumber = Err.Number
    On Error GoTo 0
    '
    pp.Quit
    If Err.Number <> 0 Then Err.Raise Err.Number, methodName, "Error while exporting to file"
End Sub

You would use it like:
ExportChartToEMF ActiveChart, "[FolderPath]\[FileName].emf"

If you really need .svg then unfortunately the functionality is not exposed to VBA although it works manually in Excel and PowerPoint via the Save as Picture dialog (right-click on chart shape).
In short, you cannot fully automate the export of chart to .svg file unless you go through an intermediate format (like .emf or .pdf) or manually saving to .svg via the Save as Picture dialog.
Answer: When you copy a chart to the clipboard, Excel adds lots of different clipboard formats. Since version 2011 (Application.Build >= 13426), this now includes "image/svg+xml".
So all we have to do is find that format on the clipboard and save it to a file. Which turns out to be fairly annoying.
Private Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As LongPtr
Private Declare PtrSafe Function EnumClipboardFormats Lib "user32" (ByVal wFormat As Long) As Long
Private Declare PtrSafe Function GetClipboardFormatName Lib "user32" _
    Alias "GetClipboardFormatNameW" _
    (ByVal wFormat As Long, _
    ByVal lpString As LongPtr, _
    ByVal nMaxCount As Integer) As Integer
Private Declare PtrSafe Function CloseClipboard Lib "user32" () As Long

Private Declare PtrSafe Function GlobalUnlock Lib "Kernel32" (ByVal hMem As LongPtr) As LongPtr
Private Declare PtrSafe Function GlobalLock Lib "Kernel32" (ByVal hMem As LongPtr) As LongPtr
Private Declare PtrSafe Function GlobalSize Lib "Kernel32" (ByVal hMem As LongPtr) As LongPtr

Private Declare PtrSafe Function CreateFile Lib "Kernel32" _
    Alias "CreateFileA" (ByVal lpFileName As String, _
    ByVal dwDesiredAccess As Long, _
    ByVal dwShareMode As Long, _
    ByVal lpSecurityAttributes As LongPtr, _
    ByVal dwCreationDisposition As Long, _
    ByVal dwFlagsAndAttributes As Long, _
    ByVal hTemplateFile As LongPtr) As LongPtr

Private Declare PtrSafe Function WriteFile Lib "Kernel32" _
    (ByVal hFile As LongPtr, _
    ByVal lpBuffer As LongPtr, _
    ByVal nNumberOfBytesToWrite As Long, _
    ByRef lpNumberOfBytesWritten As Long, _
    ByVal lpOverlapped As LongPtr) As Long

Private Declare PtrSafe Function CloseHandle Lib "Kernel32" (ByVal hObject As LongPtr) As Long

Sub SaveClipboard(formatName As String, filename As String)
    Dim fmtName As String
    Dim fmt As Long
    Dim length As Long
    Dim wrote As Long
    Dim data As LongPtr
    Dim fileHandle As LongPtr
    Dim content As LongPtr
    Dim ret As Long

    If OpenClipboard(ActiveWindow.hwnd) = 0 Then
        Exit Sub
    End If

    fmt = 0
    Do
        fmt = EnumClipboardFormats(fmt)
        If fmt = 0 Then Exit Do

        fmtName = String$(255, vbNullChar)
        length = GetClipboardFormatName(fmt, StrPtr(fmtName), 255)
        If length <> 0 And Left(fmtName, length) = formatName Then
            data = GetClipboardData(fmt)

            length = CLng(GlobalSize(data))
            content = GlobalLock(data)

            ' use win32 api file handling to avoid copying buffers
            fileHandle = CreateFile(filename, &H120089 Or &H120116, 0, 0, 2, 0, 0)
            ret = WriteFile(fileHandle, content, length, wrote, 0)
            CloseHandle fileHandle

            GlobalUnlock data
            Exit Do
        End If
    Loop

    CloseClipboard

    If fmt = 0 Then
        MsgBox "Did not find clipboard format " & formatName
        Exit Sub
    End If

End Sub

Then just copy the chart and save the svg;
shape.Copy
SaveClipboard "image/svg+xml", "C:\temp\output.svg"

Comment: This is a great solution! Upon running it in the 64bit version of Excel I got a `Type mismatch` compilation error because `GlobalSize(data)` returns a `LngPtr` and `length` is declared as `long`. Changing the line to `length = CLng(GlobalSize(data))` fixed the issue and it worked perfectly. This is indeed less hacky than my idea...


add flag to product display view

Question: I have a product display using facets that I have tried in vain in views to add a flag/unflag link to with views I already built a separate comparison view with the relationship to flag and the ability to unflag but I want to be able to flag products from the facets display (if it's even possible) rather than a second display that looks the same as the facets display except that it uses content linked to the product entity and wont show the facets
Does anyone know if it's even possible to add a flag to a view of type "Product Display"?(Correction: "Product Display" was the name of my search api index being exposed to views)
For now I have this working by exposing the flag link on the product displays themselves which is a pain in the neck to flip back and forth and some users probably aren't going to be able to be techie enough to figure it out
Answer: I am having the same problem. It seems like there is no way to add 'flag' relationship in view used by facets..
The way I have done this is:

I installed the module 'Views PHP'
(https://drupal.org/project/views_php)
Added a 'global PHP'
field in my faceted view.
called the function to create flag
link and printed the link in the global php field: print flag_create_link('bookmarks', $data->entity)

Dont know why, my php view cudn't render $row->nid, thus I used $data->entity for node id.
Remember to enable php filter once views php is installed.
Hope It helps.

add flag to product display view


Fitting model to observed data by obtimising parameters using optim() minimising residual sum of squares

Question: I am trying to fit this very simple 4 species linear Lotka-Volterra competition model to observed data but for some reason when I try the optim() function something with regards to deSolve seems to fail.
# Data
data <- data.frame(Cod = c(0.1966126, 0.1989563, 0.2567677, 0.3158896, 0.4225435, 0.7219856,
                           1.0570824, 0.7266830, 0.6286763, 0.6389475),
                   Herring = c(1.988372, 2.788014, 3.397138, 2.557245, 2.627013, 3.045617,
                               3.161002, 3.531306, 3.432021, 3.617174),
                   Sprat = c(2.030273, 3.480469, 3.009277, 1.895996, 2.457520, 1.991211, 2.350098,
                             2.118164, 1.693359, 1.869141),
                   Flounder = c(0.4758220, 0.4425532, 0.4185687, 0.4967118, 0.7102515, 0.5733075,
                                0.7404255, 0.5996132, 0.6235977, 0.7187621))
# Model formulation
LLV <- function(time, state, parameters) {
  with(as.list(c(state, parameters)), {
    db1.dt = b1*(r1+a11*b1+a12*b2+a13*b3+a14*b4)
    db2.dt = b2*(r2+a22*b2+a21*b1+a23*b3+a24*b4)
    db3.dt = b3*(r3+a33*b3+a31*b1+a32*b2+a34*b4)
    db4.dt = b4*(r4+a44*b4+a41*b1+a42*b2+a43*b3)
    list(c(db1.dt, db2.dt, db3.dt, db4.dt))
  })
}
# Model input and simulation
# Model input
params <- c(r1 = -0.342085, r2 = 0.6855681, r3 = 2.757769, r4 = 0.9744113,
            a11 = -1.05973762, a12 = 0.09577309, a13 = -0.01915480, a14 = 1.36098939,
            a21 = 0.17533326, a22 = -0.32247342, a23 = 0.03111628, a24 = 0.30212711,
            a31 = 0.5303516, a32 = -0.4869761, a33 = -0.3194882, a34 = -1.5089027,
            a41 = 0.004418133, a42 = 0.163716414, a43 = -0.237873378, a44 = -1.519158802)
ini <- c(b1 = data[1,1], b2 = data[1,2], b3 = data[1,3], b4 = data[1,4])
tmax <- 10
t <- seq(1,tmax,0.1)
# Results and first parameter guess is more or less okay
results <- deSolve::ode(y = ini, times = t, func = LLV, parms = params)
matplot(data, pch = 1)
matplot(x = results[,1], y = results[,-1], type = "l", add = TRUE)

Here I proceed and write a function that minimises the residual sum of squares that when included in optim() with the above initial parameter guess should produce what I am looking for.
min.RSS <- function(data, params) {
  output <- deSolve::ode(y = ini, times = t, func = LLV, parms = params)
  predictions <- exp(output[,-1])
  observations <- data
  return(sum((predictions-observations)^2))
}
result <- optim(par = params, fn = min.RSS, data = data)
fit <- deSolve::ode(y = ini, times = t, func = LLV, parms = result$par)
matplot(x = fit[,1], y = fit[,-1], type = "l", lwd = 3, add = TRUE)

Any idea on how to solve this problem will be very much appreciated.
Comment: This is a *hard* problem. I agree that you might think that nonlinear optimization should e easy because you have a good starting condition, but you are trying to fit a 20-parameter nonlinear model to 40 data points. I will take a look and see if I can suggest any solutions, but in general this is going to be **very difficult**. Something like MARSS (multivariate autoregressive state space) models might work ... ?
Comment: Hi Ben, thank you ever so much for the response. Yes, perhaps I'm not allowing optim() to efficiently search with 40 data points. I could try to extend the time series but it is usually what fisheries scientists often have to sort of optimise the fit of more complex models like EwE. I could simulate some data if you think that will sort out the problem to fit the in between years adding some a priori uncertainty to the sampling. I know the code for the model could be written as db.dt=(r+Ab)b but I would like to then add some higher order interespecific interactions.
Comment: The HOI would be coded with the form db1.dt=b1(r1+a11b1+a12b2^2...) I will then reapply the optimiser to then have a somewhat "realistic" (as far as the LV model goes) model that I could use to apply an algorithm that finds Nash Equilibrium harvesting rates. I have used more complex models with this algorithm but the modified LV seems perfect for a tutorial describing the whole process. But the step I am struggling with is this fitting as LV parameters in normal tutorials are quite cryptic for students usually and perhaps having a real system will ease the process of understanding.
Comment: I will have a look at MARSS and thank you again for taking the time to assist/suggest solutions. I really enjoy reading your work :)!
Comment: see my edits: I don't want to rain on your parade, but this model is sufficiently complex that using it to make predictions about what will happen in a fishery is extremely ill-advised - let alone making the more model complex by adding HOIs ...
Answer: You got a better fit, but you should be very careful with this problem. I went a little crazy and used the (in-development) fitode package to tackle this problem. I fitted the model and got a much better fit, also tried fitting with 100 randomly varying starting points around my best fit.  Your residual sum of squares was 1.19; fitode got to 0.29 on the first try, and the best of 100 fits was RSS=0.16. However: these fits are highly unstable. This plot shows the fits to the data and predictions 5 time steps in the future for (1) your fit (dashed lines); (2) fitode initial fit (dotted line); (3) the 100 other fitode fits (the ones within 0.05 RSS of the best fit are solid, the ones worse than that are drawn very lightly).
You can see that the out-of-sample predictions are mostly crazy. Your fit is actually more stable than some of the better fits - it gets to time step 13 before the entire community crashes - but the bottom line is that a good fit to the data in this case in no way guarantees a sensible answer. It looks like a single one of the 100 fits reaches the end of the prediction time series without collapsing (which seems like a reasonably sensible "common sense" prediction based on the observed time series).
In order to fit these data reliably, you either need a model with many fewer parameters, or external information supplied in the form of priors, or regularization - some way to make penalize fits that imply 'wiggly' deterministic trajectories, or interaction parameters/growth rates that are unreasonable.

## remotes::install_github("parksw3/fitode")
library(fitode)

## data with tags for fitode
data2 <- setNames(data,paste0(names(data),"_obs"))
data2 <- data.frame(times=seq(nrow(data2)),data2)

## Model formulation (for fitode)
LV_model <- odemodel(
    name="4-species LV",
    model=list(
        Cod ~ Cod*(r1+a11*Cod+a12*Herring+a13*Sprat+a14*Flounder),
        Herring ~ Herring*(r2+a22*Herring+a21*Cod+a23*Sprat+a24*Flounder),
        Sprat ~ Sprat*(r3+a33*Sprat+a31*Cod+a32*Herring+a34*Flounder),
        Flounder ~ Flounder*(r4+a44*Flounder+a41*Cod+a42*Herring+a43*Sprat)
    ),
    observation=list(
        Cod_obs ~ ols(mean=Cod),
        Herring_obs ~ ols(mean=Herring),
        Sprat_obs ~ ols(mean=Sprat),
        Flounder_obs ~ ols(mean=Flounder)
    ),
    initial=list(
        Cod ~ data2$Cod_obs[1],
        Herring ~ data2$Herring_obs[1],
        Sprat ~ data2$Sprat_obs[1],
        Flounder ~ data2$Flounder_obs[1]
    ),
    link=setNames(rep("identity",length(pars)),pars),
    par= pars
)

## plot results
plotres <- function(p,ODEint="rk",lty=1,
                    dt=0.1,
                    tvec=seq(1,10,by=dt),...) {
    par(las=1, bty="l")
    res <- deSolve::ode(ini, tvec, LLV, p, method=ODEint)
    matplot(res[,1],res[,-1],type="l",lty=lty,...)
    return(invisible(res[,-1]))
}

f1 <- fitode(
    LV_model,
    data=data2,
    start=params,
    control=list(maxit=1e5,trace=1000)
)

## fitode with multistart

ranfit <- function(n,fit,range=0.5) {
    ##
    rpars <- params*runif(length(params),1-range,1+range)
    newfit <- try(update(fit, start=rpars))
    return(newfit)
}

cl <- makeCluster(10)
clusterSetRNGStream(cl = cl, 101)
clusterExport(cl, c("params","LV_model","data2"))
clusterEvalQ(cl,invisible(library(fitode)))
system.time(
    multifit <- parLapply(cl, 1:100, ranfit, fit=f1, tvec=tvec)
)
saveRDS(multifit,file="SO65440448_multifit.rds")

ivec <- seq_along(multifit)
ivec <- ivec[sapply(multifit,function(x) !inherits(x,"try-error"))]
coef <- pred <- vector("list", length=length(ivec))
ll <- conv <- rep(NA,length(ivec))
for (i in seq_along(ivec)) {
    nf <- multifit[[ivec[i]]]
    coef[[i]] <- coef(nf)
    pp <- predict(nf, times=1:10)
    pred[[i]] <- cbind(times=pp[[1]][,1],
                do.call(cbind,lapply(pp,"[",-1)))
    ll[i] <- logLik(nf)
    conv[i] <- nf@mle2@details$convergence
}

par(las=1,bty="l")
matplot(pred[[1]][,1],pred[[1]][,-1],
        type="n",lty=1,ylim=c(0,6),
        xlab="time",ylab="density")
lthresh <- 0.05
for (i in 1:length(pred)) {
    good <- ll[i]>(max(ll)-lthresh)
    alpha <- if (good) 0.8 else 0.1
    lwd <- if (good) 2 else 1
    matlines(pred[[i]][,1],pred[[i]][,-1],lty=1,
             col=adjustcolor(palette()[1:4],alpha.f=alpha),
             lwd=lwd)
}
matpoints(data2[,1],data2[,-1],pch=16,cex=3)
plotres(optimres$par,add=TRUE, lwd=3,lty=2,dt=1)
plotres(coef(f1),add=TRUE, lwd=3,lty=3,dt=1)

Comment: Dear Ben, thanks for the thorough answer. I will play around with the `fitode` package reducing the dimensionality of the problem to 2 species with a time series of 46 years. I know that this very simple LV competition model is not great to make deterministic predictions. I am using it to introduce a novel algorithm because is a very simple (phenomenological) model describing spp. interactions (reason why I did not spend to much time making a fancy fitting optimiser). Now that I know that such tools exist in `R` already I will used them and get back to you via this post soon.
Comment: 46 year with 2 species might work OK, especially if you start a simulation reasonably far from equilibrium (so that both the growth parameters (r_i) and the competition parameters (alpha_ij) are identifiable)
Answer: To those interested I have managed to get a solution that involves changing the ode integration method. Here is the working optimiser:
# Optimising parameter fit
LVmse = function(parms) {
  out = as.matrix(deSolve::ode(ini, 1:10, LLV, parms, method="rk")[,-1])
  RSS = sum((spp-out)^2, na.rm = TRUE) # Minimising residual sum of squares
  return(RSS)
}
optimres <- optim(par = params, fn = LVmse)

Comment: When I run this it stops at max iterations=501 with convergence code 1 ('not converged') ...


Angle between vectors $a$ and $b$

Question: Given $a = (4,5,-3)$ and $b = -i + 2 j + k$.
I am asked to measure the angle between $a - b$ and $-j - k$.
I know how to get the $a-b$ angle but I don't understand what to do for the $-j-k$ angle.
Am I supposed to take just  $a= (5,-3)$ and $b=(2,1)$ and use those?
Comment: had to roll this question back, not sure why the OP decided to vandalize own question
Comment: Please show us how you found the $a-b$ angle.
Comment: Its not the $a-b$ angle or $-j-k$ angle. We are asked to find the angle between the two vectors $a-b$ and $-j-k$
Comment: @jimjim bumping the question
Answer: The first vector is $\vec x = \vec a - \vec b = (5,3,-4)$.
The second vector $\vec y =  -\textbf{j} - \textbf{k} = 0\textbf{i} -1\textbf{j} - 1\textbf{k}=(0,-1,-1)$
Use the dot product. $\vec x \cdot \vec y = |\vec x||\vec y| \cos \theta$, where $\theta$ is the angle you need to find.
This gives you $(5)(0)+ (3)(-1) +(-4)(-1) = \sqrt{5^2 + 3^2 + (-4)^2} \sqrt{0^2 + (-1)^2 + (-1)^2} \cos \theta$ which yields $\theta = \arccos \frac 1{10} \approx 84.26^{\circ}$

Angle between vectors $a$ and $b$


jsPDF page split breaks the content after it's page size exceeds

Question: I have this code. Can anyone tell me how I can split the page without affecting the content?
<code>$('#export').click(function(e) {
  e.preventDefault();
  var options = {
    pagesplit: true,
    margin: {
      top: 10,
      right: 10,
      bottom: 10,
      left: 10,
      useFor: 'page'
    }
  };
  var pdf = new jsPDF('p', 'mm', 'a4');
  pdf.internal.scaleFactor = 5;
  pdf.page = 1;
  //  var width = "1000px"
  //   var height = "1000px"
  pdf.addHTML($("#pdfdownload"), 0, 0, options, function() {
    // function footer() {
    var pageCount = pdf.internal.getNumberOfPages();
    for (i = 0; i < pageCount; i++) {
      pdf.setPage(i);
      pdf.text(10, 10, pdf.internal.getCurrentPageInfo().pageNumber + "/" + pageCount);
    };
    pdf.save('Audit.pdf');
  });
});
</code>
Answer: <code>let HTML_Width = $("#target").width();
            let HTML_Height = $("#target").height();
            let top_left_margin = 1;
            let PDF_Width = HTML_Width + (top_left_margin * 2);
            let PDF_Height = (PDF_Width * 1.5) + (top_left_margin * 2);
            let canvas_image_width = HTML_Width;
            let canvas_image_height = HTML_Height;
            let totalPDFPages = Math.ceil(HTML_Height / PDF_Height) - 1;
            let user = this.auth_user;
            html2canvas($("#target")[0], {allowTaint: true}).then(function (canvas) {
                canvas.getContext('2d');
                let imgData = canvas.toDataURL("image/jpeg", 1.0);
                let pdf = new jsPDF('p', 'pt', [PDF_Width, PDF_Height]);

                pdf.addImage(imgData, 'JPG', top_left_margin, top_left_margin, canvas_image_width, canvas_image_height);

                let counter = 0;

                for (let i = 1; i <= totalPDFPages; i++) {
                    counter++;
                    pdf.addPage(PDF_Width, PDF_Height);
                    pdf.addImage(imgData, 'JPG', top_left_margin, -(PDF_Height * i) + (top_left_margin * 4), canvas_image_width, canvas_image_height);
                }
                pdf.save(user + ".pdf");
            });
</code>
It's works for me


Batch operation doesn't get called

Question: I am trying to write a batch. And I have a problem that none of the batch operations callback functions are getting called.
I wrote drupal_set_message at the start of each callback and none of the messages are displayed.
I can get the batch page, but the callback ops aren't called at all.
Here's the code I used:
function mymdoule_node_batch()
{
    $batch = array(
        'title'         => t("Migrating"),
        'operations'    => array(
            array('mycallback_001'),
            array('mycallback_002'),
        ),
        'finished'      => 'mycallback_finished',
        'init_message'  => t('Initializing...'),
        'error_message' => t('An error occurred.'),
    );

    batch_set($batch);
    drupal_set_message("step: 0"); // this is the only message the system returns
}

function mycallback_001(&$context)
{
    drupal_set_message("step: 1");
    $nids = db_select("node", "n")
            ->fields("n", array("nid"))
            ->orderBy("nid", "ASC")
            ->execute()
            ->fetchCol();
}

function mycallback_002(&$context)
{
    drupal_set_message("step: 2");
    $nids = db_select("node", "n")
            ->fields("n", array("nid"))
            ->orderBy("nid", "ASC")
            ->execute()
            ->fetchCol();
}

P.s. The batch is getting called in form submission.
What's wrong in that?
Comment: add one redirection at the end of mymdoule_node_batch function then check. batch_process(URL_TO_REDIRECT);
Comment: What do you mean by `add one redirection at the end of mymdoule_node_batch`? Can you provide a code sample?
Comment: When I add `batch_process();` before `drupal_set_message("step: 0");` the message is not displayed as well.
Comment: where is your mycallback_finished function. can you add that one to the question
Answer: It worked after I added file to the $batch array.
function mymdoule_node_batch()
{
    $batch = array(
        'title'         => t("Migrating"),
        'operations'    => array(
            array('mycallback_001'),
            array('mycallback_002'),
        ),
        'finished'      => 'mycallback_finished',
        'init_message'  => t('Initializing...'),
        'error_message' => t('An error occurred.'),
        'file'          => 'mypatch.ops.inc',
    );

    batch_set($batch);
    drupal_set_message("step: 0");
}

Answer: This Problem happens when we put our file in different folder like as includes/form_test.inc.In this scenario batch function can't call.So to handle this we put 'file' => 'path_to_file_containing_myfunctions' in our submit function.
    $batch = array(
      'title' => t('Custom File uploader'),
      'operations' => array(
        array('test_feeds_import_form_batch', array($uri)),
      ),
      'progress_message' => t('Uploading Multiple Products...'),
      'error_message' => t('Error!'),
      'finished' => 'test_feeds_import_form_batch_finish',
      'file' => drupal_get_path('module', 'test') . '/includes/test_form.inc',
    );

    batch_set($batch);
    batch_process('admin/import-data'); //Work like as drupal goto

Batch operation doesn't get called


Show that quotient ring of a $\Bbb C$-algebra by a maximal ideal is isomorphic to $\mathbb{C}$.

Question:
Let $R = \mathbb{C}[x_1,...,x_n]/I$ be a quotient of a polynomial ring over $\mathbb{C}$, and let $M$ be a maximal ideal of $R$. How do I show that quotient ring $R/M$ is isomorphic to $\mathbb{C}$?

So I use the fact that $M$ is a maximal ideal of $R$ if and only if $R/M$ is a field. Obviously $\mathbb{C}$ is a field.
How would I use this theorem? Hilbert's Nullstellensatz: the maximal ideals of the polynomial ring $\mathbb{C}[x_1,...x_n]$ are in bijective correspondence with points of complex n-dimensional plane. A point a in $\mathbb{C^n}$ corresponds to the kernel of a substitution map which sends f(x) in $\mathbb{C}[x_1,...x_n]$ to f(a). the kernel of this map is the ideal generated by linear polynomials with roots consisting of the components of a
Comment: Claim: $R/M$ is a finite field extension of $\mathbb C$. If you can prove this, you're done.
Comment: If $k$ is a field, we say that $F/k$ is a finite field extension if $k\subset F$, $F$ is a field, and $F$ is finitely generated as a $k$-algebra.
Comment: i know what a field is
Comment: what is a finite field extension?
Comment: Have you done any field theory?
Answer: This follows from Zariski's lemma, and from the fact that $\mathbf C$ is algebraically closed.
Edit: Since your edit, I see that the intended solution was most likely the one provided by Sammy, below. Nevertheless, I think that this proof is the "correct" proof, because it is "independent of coordinates".
Answer: By the Lattice Isomorphism Theorem, a maximal ideal $M$ in the ring $R$ corresponds to a maximal ideal $M'$ in $\mathbb{C}[x_1, \ldots, x_n]$ that contains $I$.  Specifically,
$$
M = M'/I.
$$
In particular, $R / M \cong \mathbb{C}[x_1, \ldots, x_n] / M' \cong \mathbb{C}$.
Comment: Dear Sammy: This works, if the OP knows the result for $R=\mathbf C[x_1, \dots, x_n]$.
Comment: That's true, @Marie.


How to add CSS to a dynamic table created with javascript

Question: Basically I'm creating a dynamic table on javascript and displaying it in the html page. I also have a css file that works on static elements, but not on the table i'm creating dynamically. what can I do to style my dynamically created table?
I've tried this: table.classList.add('tablestyle'); but it doesn't work.
As you can imagne, I'm absolutly new to this kind of things.
I have 3 files:
reports.component.ts  (that contains html. references to sccs & js are in the file and they work correctly)

STORICO DEI DATI

reports.component.js

function addTable() {

    let colonne = Math.floor(Math.random() * 5)+2;
    let righe = Math.floor(Math.random() * 5)+2;

    var today = new Date();
    var dd = String(today.getDate()).padStart(2, '0');
    var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
    var yyyy = today.getFullYear();

    today = mm + '/' + dd + '/' + yyyy;

    let providers = ["p1", "p2", "p3", "np4", "p5"];
    let testcases = ["tc1", "tc2", "tc3", "tc4", "tc5"];

    var myTableDiv = document.getElementById("myDynamicTable");

    var table = document.createElement('table');
    table.classList.add('tablestyle');

    var tableBody = document.createElement('TBODY');
    table.appendChild(tableBody);

    for (var i=0; i<righe; i++){
       var tr =  document.createElement('tr');
       tr.style.backgroundColor = 'red';
       tableBody.appendChild(tr);

       for (var j=0; j<colonne; j++){
           var td = document.createElement('td');
           td.width='75';

           if(i==0){
                if(j==0){ //prima casella
                    addCell(td, tr, today);
                }
                else { //prima riga
                    addCell(td, tr, providers[j-1]);
                }
           }
           else {
               if(j==0){ //prima colonna
                    addCell(td, tr, testcases[i-1]);
               }
               else {
                    addCell(td, tr, Math.floor(Math.random() * 50));
               }

           }

       }
    }
    myTableDiv.appendChild(table);

}

function addCell(td, tr, valoreCella){
    td.appendChild(document.createTextNode(valoreCella));
    tr.appendChild(td);
}

report.component.scss  (longer, but this is the part i want to work)
.tablestyle{
  font-weight: bold;
}

Comment: Please specific *it doesn't work*. The command `table.classList.add('tablestyle')` itself is correct and should work, unless old browser.
Comment: You `body` tag is at the wrong spot.
Comment: I have specified it.. anyway i'm not using an old browser. btw, which "body" tag??
Comment: No, you did not. *Does the class not get added? Does it throw an error? Does your style not get applied? Did you check the console for errors? Did you check the devtool-inspectior for the class attribute on the actual table?* The [`body`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/body) tag which holds your `onload`.
Answer: Made you a snippet of your code to show you that it works. Mayhap you have contradictions in your actual stylesheet.
Be aware to place the body tag at the correct hierarchy.

function addTable() {

    let colonne = Math.floor(Math.random() * 5)+2;
    let righe = Math.floor(Math.random() * 5)+2;

    var today = new Date();
    var dd = String(today.getDate()).padStart(2, '0');
    var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
    var yyyy = today.getFullYear();

    today = mm + '/' + dd + '/' + yyyy;

    let providers = ["p1", "p2", "p3", "np4", "p5"];
    let testcases = ["tc1", "tc2", "tc3", "tc4", "tc5"];

    var myTableDiv = document.getElementById("myDynamicTable");

    var table = document.createElement('table');
    table.classList.add('tablestyle');

    var tableBody = document.createElement('TBODY');
    table.appendChild(tableBody);

    for (var i=0; i<righe; i++){
       var tr =  document.createElement('tr');
       tr.style.backgroundColor = 'red';
       tableBody.appendChild(tr);

       for (var j=0; j<colonne; j++){
           var td = document.createElement('td');
           td.width='75';

           if(i==0){
                if(j==0){ //prima casella
                    addCell(td, tr, today);
                }
                else { //prima riga
                    addCell(td, tr, providers[j-1]);
                }
           }
           else {
               if(j==0){ //prima colonna
                    addCell(td, tr, testcases[i-1]);
               }
               else {
                    addCell(td, tr, Math.floor(Math.random() * 50));
               }

           }

       }
    }
    myTableDiv.appendChild(table);

}

function addCell(td, tr, valoreCella){
    td.appendChild(document.createTextNode(valoreCella));
    tr.appendChild(td);
}
.tablestyle{
  font-weight: bold;
  color: green
}

STORICO DEI DATI

Answer: Add
table.className = 'tablestyle'

or
table.setAttribute("class", "tablestyle");

Comment: While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.


Requisição POST em Swift não funciona

Question: Bom dia, procurei por diversos sites uma maneira de fazer uma requisição POST para um servidor, encontrei diversas formas e testei todas elas, mas nenhuma funcionou.
Eu consigo receber os dados da página e até mesmo enviar variáveis GET na URL, mas as variáveis POST simplesmente não são enviadas.
func teste(){

    var request = NSMutableURLRequest(URL: NSURL(string: "http://minhaurl/efetuarLogin.php?variavel=teste&get=valor")!)

    var session = NSURLSession.sharedSession()

    request.HTTPMethod = "POST"

    var params = ["CPF":"00000000000", "senha":"12345678"] as Dictionary

    var err: NSError?

    request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: nil, error: &err)
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")

    var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in

        var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
        var err: NSError?

        var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error: &err) as NSDictionary

        if((err) != nil) {

            println(err!.localizedDescription)

        } else {

            println("JSON: \(json)")

        }

    })

    task.resume()

}

Na página PHP eu tento acessar as variáveis através da $_POST, $_GET e até mesmo $_REQUEST, apenas as variáveis GET são recebidas no servidor.
Alguém sabe me dizer o que pode estar acontecendo de erro?
Desde já, obrigado.
Answer: Como o que está sendo enviando é um JSON, com cabeçalho e tudo mais, não espere receber suas variáveis do lado do servidor (CPF e senha) como $_POST['senha'] por exemplo, já que não foi um formulário que foi enviado.
Você precisa obter o "corpo" da requisição, que é um JSON, decodificar e então sim você terá as variáveis que necessita. No lado do servidor, com PHP tente algo assim:
$inputJSON = file_get_contents('php://input');
$input = json_decode($inputJSON, TRUE);

// $input['CPF'], $input['senha']

No iOS está tudo correto. Veja se isso resolve a sua questão.
Comment: O file_get_contents deve receber "php://input" mesmo ou devo colocar outra coisa? Dessa forma o retorno foi "" :(
Comment: Sim. Antes foi possível você verificar o valor da variável `$inputJSON`?
Comment: Retornando o inputJSON obtive uma string vazia ""
Comment: Você tem acesso ao servidor para verificar se houve algum erro? Um ponto para lembrar, é que a função `file_get_contents` precisa da diretiva `allow_url_fopen` do PHP ativa.
Comment: Eu pensei logo de início que era algum bloqueio do servidor, mas o administrador garantiu que não havia bloqueio algum para esse endereço. As diretivas encontram-se da seguinte forma:
allow_url_fopen: On; allow_url_include: Off; always_populate_raw_post_data: Off;
Comment: Bom, então nesse caso você precisa ver se o JSON está realmente indo na requisição. Notei que no seu código você não verificou se houve erro quando você usou o método `dataWithJSONObject`. Veja se a variável **err** possui erro. Eu costumo utilizar nas opções desse método o `.PrettyPrinted`. Isso pode causar um *malformed json*.
Comment: err e error (do método dataWithJSONObject) estão nulos (nil). Acha seguro passá-los via GET? A forma como estou fazendo, é segura? Via GET é bem mais fácil, se for seguro compensa muito mais fazer via GET, estou a muito tempo enroscado nisso.
Comment: Vamos [continuar esta discussão no bate-papo](http://chat.stackexchange.com/rooms/21898/discussion-between-paulo-rodrigues-and-mukotoshi).
Answer: Na implementação que você postou não contem erros, é apenas um request bem simples.
Tente retirar os parâmetros da sua URL no request, pois pode estar sendo processado pelo servidor e ignorando o seu httpBody.
Comment: O código real não possui esses parâmetros, eu os coloquei aqui na pergunta para indicar que o envio variáveis GET funciona.

Requisição POST em Swift não funciona