java reflection field bitwise operation

Question: I do have good understanding of bitwise operations. My question is case-specific. In the following code taken from oracle tutorial, is it possible to change the expression:
if ((foundMods & searchMods) == searchMods)

To:
if (foundMods == searchMods)

? ... because we are not extracting any flag, we are only testing for equality. Right? ... or am I missing something?
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import static java.lang.System.out;

enum Spy { BLACK , WHITE }

public class FieldModifierSpy {
volatile int share;
int instance;
class Inner {}

public static void main(String... args) {
try {
    Class<?> c = Class.forName(args[0]);
    int searchMods = 0x0;
    for (int i = 1; i < args.length; i++) {
    searchMods |= modifierFromString(args[i]);
    }

    Field[] flds = c.getDeclaredFields();
    out.format("Fields in Class '%s' containing modifiers:  %s%n",
           c.getName(),
           Modifier.toString(searchMods));
    boolean found = false;
    for (Field f : flds) {
    int foundMods = f.getModifiers();
    // Require all of the requested modifiers to be present
    if ((foundMods & searchMods) == searchMods) {
        out.format("%-8s [ synthetic=%-5b enum_constant=%-5b ]%n",
               f.getName(), f.isSynthetic(),
               f.isEnumConstant());
        found = true;
    }
    }

    if (!found) {
    out.format("No matching fields%n");
    }

    // production code should handle this exception more gracefully
} catch (ClassNotFoundException x) {
    x.printStackTrace();
}
}

private static int modifierFromString(String s) {
int m = 0x0;
if ("public".equals(s))           m |= Modifier.PUBLIC;
else if ("protected".equals(s))   m |= Modifier.PROTECTED;
else if ("private".equals(s))     m |= Modifier.PRIVATE;
else if ("static".equals(s))      m |= Modifier.STATIC;
else if ("final".equals(s))       m |= Modifier.FINAL;
else if ("transient".equals(s))   m |= Modifier.TRANSIENT;
else if ("volatile".equals(s))    m |= Modifier.VOLATILE;
return m;
}
}

Comment: It is possible, but will change the behavior...
Answer: This:
if ((foundMods & searchMods) == searchMods)

says "if foundMods includes all the bits in searchMods".
This:
if (foundMods == searchMods)

would be "if foundMods has all the same bits as searchMods (and no others)".
If you know that foundMods won't have any other bits, then they're equivalent. Otherwise they are definitely different.
Comment: Thanks. Good quick concise answer. Keep up helping beginners.
Answer: Short answer No
More answer
Your assumption,
that if ((foundMods & searchMods) == searchMods) is equivalent to
if (foundMods == searchMods) is not valid.
The first if tests to see if all one value bits in the searchMods value are also one value bits in the foundMods value.
Thus,
if searchMods == 0000111 and foundMods == 0011111 then
if ((foundMods & searchMods) == searchMods) is true, but if (foundMods == searchMods) is false.
In the case of field modifiers,
searchMods may be a subset of modifiers that are present for the current (foundMods) field.
Answer: Let foundMods be Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL
Now let searchMods be Modifier.PUBLIC.
foundMods & searchMods) == searchMods will evaluate to true, whereas
foundMods == searchMods will be false.
So, yes, you're missing something and no, you cannot replace the first by the latter expression.
Answer:
we are not extracting any flag, we are only testing for equality. Right?

No. searchMods is a collection of all the "lit" flags you want to search for. Using the & operator like that will check if foundMods has all the "lit" flags that searchMods has - but it will ignore additional flags that foundMods may have, while the == operator will not.
Consider the following example:
int searchMods = Modifier.PUBLIC | Modifier.STATIC;
int foundMods = Modifier.PUBLIC | Modifier.STATIC | Modifier.SYNCHRONIZED;

System.out.println((foundMods & searchMods) == searchMods);
System.out.println(foundMods == searchMods);

The first print statement will print true, while the second will print false.

java reflection field bitwise operation


Does Intel TBB library have feature to choose number of cores?

Question: I am using Intel TBB to speed up a problem in graphics domain.
I want to analyze the scalability of my  method.
To find the scalability I want run the same algorithm
using 1, 2, 3 and 4 CPU cores.
Is there a way to tell TBB to use 2 & 3 cores
while running the tests on a 4 core CPU.
Comment: Possible duplicate: http://stackoverflow.com/questions/3786408/number-of-threads-used-by-intel-tbb
Comment: Possible duplicate of [Number of threads used by Intel TBB](https://stackoverflow.com/questions/3786408/number-of-threads-used-by-intel-tbb)
Answer: Yes, there is a way to do that:
The task_scheduler_init class lets you do that. You have to pass the constructor of the class the number of threads you want to use. You also have to make sure that all your TBB code runs after the instance is constructed and before it is destroyed.
Usually, it is sufficient to declare it as a variable in the main function, like this:
<code>int main()
{
    tbb::task_scheduler_init init(3);//three threads
    do_the_work();
}
</code>


How many the binary sequences of length n such there are no two zeros next to each other?

Question: I come up with:
$$\sum_{i=1}^{\left \lfloor{\frac{n+1}{2}}\right \rfloor}{{n-i+1}\choose{i}}$$
Which I think is true but is there a way to write it not as a sum?
Comment: Try the Fibonacci numbers ...
Comment: I mean to  the number of components of the sum doesn't  depend on $n$
Comment: You can use inclusion-exclusion
Comment: @Mickey I see but what should I include/exclude?
Comment: Hint: if your number is $f(n)$, show that $f(n) = f(n-1) + f(n-2)$ by conditioning on the first member of the sequence.
Comment: $r_1=n\cdot(1),m\cdot(0)$|||||
$r_2=n+m\cdot(1),n\cdot(0)$|||||
$r_3=2n+m\cdot(1),n+m\cdot(0)$
Comment: @RobertIsrael Ok but How to even come up with the idea of using the Fibonacci sequence?
Comment: Conditioning on the first term is often a good strategy.  Once you get the recursion, you recognize it as the Fibonacci recursion and then you're done.
Answer: For $n=1,2,3,...$ the answer is $2$, $3$, $5$, $8$, etc. Maybe a well-known sequence?
Comment: OEIS says that there are 825 well-known sequences with 2, 3, 5, 8 $\ddot\smile$.
Comment: This is more of a comment than an answer.


Use of PED's by players who are not currently under contract or are recovering in the off-season

Question: Does Major League Baseball restrict former MLB players who are not under contract from using legally perscribed PED's prior to coming back to play for a team? Are there restrictions on MLB players from using legally prescribed PED's in the off-season for recovery purposes?
Comment: @edmastermind29 Your interpretation is correct.
Comment: What do you mean by "legally prescribed?" I interpreted that as "a Prohibited Substance...through a valid, medically
appropriate prescription provided by a duly licensed physician."
Answer: This is all covered in Major League Baseball's Joint Drug Prevention and Treatment Program.

Does Major League Baseball restrict former MLB players who are not under
  contract from using legally perscribed PED's prior to coming back to
  play for a team?

If the former MLB player voluntarily and officially retires or if the former MLB player becomes under contract in the Minor League or another professional baseball league, then no.
The program covers:

all Players on the Major League Clubs' 40-man rosters
any Player who becomes a free agent under Article XIX or Article XX of the Basic Agreement; and
any Player who is released from a Major League roster unless the Player voluntarily retires or signs a Minor League contract or a
  contract with a club in an unaffiliated professional baseball league
  ("Players").

Are there restrictions on MLB players from using legally prescribed
  PED's in the off-season for recovery purposes?

Yes, there are restrictions. However, a player may be authorized to use a prohibited substance.
A player must receive a "Theraputic Use Exemption"  in order to use "a Prohibited Substance through a valid, medically appropriate prescription."
This is covered in the program's "Testing" section under "Therapeutic Use Exemption" (Section 3, Article I). Below states how a player would be eligible.

A Player authorized to ingest a Prohibited Substance through a valid,
  medically appropriate prescription provided by a duly licensed
  physician shall receive a Therapeutic Use Exemption ("TUE"). To be
  "medically appropriate," the Player must have a documented medical
  need under the standards accepted in the United States or Canada for
  the prescription in the prescribed dosage.


Replace punctuation in string

Question: I want to replace the punctuation in a string by adding '\\' before the punctuation. The reason is I will be using regex on the string afterwards and it fails if there is a question mark without '\\' in front of it.
So basically, I would like to do something like this:
gsub("\\?","\\\\?", x)
Which converts a string "How are you?" to "How are you\\?" But I would like to do this for all punctuation. Is this possible?
Comment: Depending on your use case, you may be able to skip this step and just use the `fixed = TRUE` argument in your subsequent regex.
Comment: I don't have experience with r, but `gsub("\\([.?])","\\\\$1",x)` or `gsub("\\([.?])","\\\\\\1",x)` should work.
Comment: There is no metacharacter escape function in _r_ ?
Comment: I think probably you have it backwards. The target string never has to be escaped. Only the regex string string with literal metachar's (ie. `?*+`,  etc) need to be escaped.
Comment: What are you trying to do? I don't think answering this question will help you get your real task done properly
Comment: [More good reading here](http://stackoverflow.com/a/27721009/903061). In addition to `fixed`, another easy solution would be to use `\\Q` and `\\E` at the start and end of your strings.
Answer: You can use gsub with the [[:punct:]] regular expression alias as follows:
> x <- "Hi! How are you today?"
> gsub('([[:punct:]])', '\\\\\\1', x)
[1] "Hi\\! How are you today\\?"

Note the replacement starts with '\\\\' to produce the double backslash you requested while the '\\1' portion preserves the punctuation mark.

Replace punctuation in string


Vue - Retrieve events and v-model from component

Question: I'm fairly new to Vue and I wanted to have a reusable component for v-select (using vuetify). Now the problem is I don't know how to pass the v-model and @events to the component. Here is my code below.
Template.vue
      <base-select
           v-model="rows.corporate_objective_id"
           :items="corporate_objectives"
           :item-text="corporate_objective"
           :item-value="id"
           placeholder="Please select a Corporate Objective"
           @change='corporateObjectiveDescription(); departmentObjectives()'
         />

v-select component
    <template>
      <v-select
        :value="value"
        @input="$emit('input', $event.target.value)"
        :items="items"
        :item-text="item-text"
        :item-value="item-value"
        dense
        clearable
      />
    </template>

Comment: check this resource, it is really good to understand v-model and how to implement it in custom components: https://alligator.io/vuejs/add-v-model-support/
Answer: You can pass all the attributes and listeners to the child component writing this:
<template>
  <v-select
    v-bind="$attrs"
    v-on="$listeners"
    dense
    clearable
  />
</template>

You can look at this video from 21:50. It explains very well how to make "transparent wrappers"
Comment: This makes things really easier. Thank you very much!


Running a service

Question: I have a service that exports a file to a location on the server, however I need this service to be able to copy the file to another domain as well as the current one. I have attempted to run the service using admin rights but that will not work as the system admin is on domain1/admin and this does not have access to domain2 same as domain2/admin does not have access to domain1. Is it possible to skip the log on part of a service?
Answer: No - the way I see it, there are only a few ways around this:

Enable a trust between your two domains, so that DOMAIN1\User has rights to copy a file to DOMAIN2's server
Write the service so that it can internally authenticate with DOMAIN2's file share using DOMAIN2\User
The only other way I could think of would be to map a network drive in the context DOMAIN1\User, to the DOMAIN2 file sharer using DOMAIN2\User credentials. Then, run the service in the context of DOMAIN1\User. To be honest, I don't know if this will work
If possible, use a scheduled task instead of a service. You could definitely do (3) using  a batch file, then
NOT RECOMMENDED - Allow anonymous access to the file share. Please don't do this, it's just here for completeness
Answer: No, your service has to run under SOME security context.  You'll need to run it as a user that has access to resources on both domains, making sure that the proper trusts exist between the domains.
Answer: You're going to need to use an account that has permission in both domains. Assuming you have a trust of some sort between the two, you're going to want to grant the service account running your service the appropriate rights on the servers in each domain.
Comment: Re-write your service to authenticate using alternate credentials for each domain and rely on your app for the authentication and not the account running the service.
Comment: Is there any way to do this without a trust?

Running a service


Access denied hook_menu Drupal 7

Question: I am trying my first module on Drupal 7 on the behalf of experience on Drupal 6.
<code>function custom_work_menu(){
  $items = array();

  $items['mypage']=array(
    'title'=>t('Title'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('custom_work_mypage_view'),
    'access arguments' => array('access content'),
  );

  return $items;
}
</code>
Upon accessing my page, I get access denied. What am I missing here?
Answer: Revised:
Your code looks right
3 Things could be the problem

'mypage' could be a banned link.
you don't have access to 'custom_work_mypage_view' form.
Clear your cache.   Without the 'access argument' line you will get access denied.  If you added it then save your file, you will still get access denied until you clear your cache.

My gut feeling is #3.
Comment: access callback can not take place here, access callback is defined in string unlike access arguments as array.

Access denied hook_menu Drupal 7


Comparing characteristics of individuals

Question: The data table for my company's salesmen/women commission for FY23 follows.

Below, I compare the commission of each age group to know which age group is performing well (which I don't think matters), and please notice the groupings are arbitrary and hence meaningless. I compare commission for each age group not intending to see any causal relationship or correlation but rather for a descriptive view, i.e. to gain a deeper understanding of the data. In short, I'm summarizing the data.

Problem: Before I take you toward the problem, I want to clarify a term: Comparison. To compare means to note differences and similarities in characteristics between two or more things. I always thought I can only compare individuals (which are names in this data set) on a certain characteristic: maybe I compare the age of each individual, the commission of each individual, the gender of each individual, the salary of each individual, shirt color of each individual, and what not "of" individuals. Now, it starts to dawn on me that I can compare even the characteristics of individuals to other characteristics: in this case, age group and commission are being compared. My question is, which is unintuitive to me since to compare is to describe the differences in characteristics of two or more things, how in the world is commission a characteristic of age groups? Am I thinking wrong?
P.S. Even though the answers provided by the community were far more intuitive, I still didn't follow them. It is because I didn't make myself clear, which was my fault. To right the wrong, I edit the whole question (including the title).
Comment: Dependent on what? This is just data, if you are going to analyze it you have to define a model. What is your model? What are you trying to answer?
Comment: Why do you need to characterize any variable as "dependent" or "independent"?  A more fundamental and important question is what do you mean by "age group"?
Comment: Please give your data not only as an image, but as text. Users can then easily copy&paste it into their choosen software, which is not possible now!
Comment: If you can, you may also want to consider the expected commission's association with gender as well as with age.
Comment: Put age on the x-axis and commision on the y-axis. Show each person as a dot. Make dots in two colours, one for each gender. Interpret the result. -- In this case, you might earn more or less with age but you cannot change your age by commision! Hence commision must be the dependent variable.
Answer: "What makes commission dependent on age?"
The answer is not in statistics, but in substantive knowledge of what the two words mean and how they might relate.  Age dependent on commission makes no sense. But if you run the regression that way, you won't get errors.
This is symptomatic of a larger problem: People asking statistics to solve problems that are substantive. When I was a consultant (now I'm retired) it was often hard to get my client to realize that a lot of the work had to be done by them.
But also note @whuber 's comment on Nuclear Hypothesis's answer. You not only have to think about what makes sense in terms of "dependence", but whether "dependent" is even the right term. (Statistics often uses ordinary English words in unusual and confusing ways.)
Answer: At a very broad level, independent and dependent variables can often (but not always) be thought of as causes and effects. Independent variables are termed such because they do not depend on other variables, while dependent variables do. In other words, you can pick a value of the independent variables and see what the downstream effects are in the dependent variables, but you wouldn't generally set the dependent variable directly.
Note that these are generally rules of thumb, it's not always obvious which are dependent/independent variables or if those terms even truly apply - it more comes down to how you think about and use the variables in your analysis. Which one you call "dependent" should more be a function of how you plan to analyze the data, rather than your choice of label informing your analysis plan.
Here, you'd probably want to set age as the independent variable and commission as the dependent one, since that is the likely direction of cause-effect. It's quite possible that someone's commission is dependent on their age (if older more experienced people make more money, for example), but it would be hard to imagine a case where someone's age depended on their commission (implying that people might get older/younger if they make more/less). The choice of dependent/independent variables may also depend somewhat on what is readily observable and what you're trying to infer - you may build a model of causes as the dependent variables using observable effects as the independent variable.
Comment: This cause-effect characterization, although it often holds, is specious.  Consider, for instance, the problem of estimating historical climate conditions from data like tree rings or fossils.  In this problem the *climate conditions* are the target and therefore are the "dependent" variable, but then your characterization would have us suppose that tree growth caused the climate to vary!
Comment: It's worth commenting that this textbook definition does not invoke any sense of cause and effect, which therefore are irrelevant.  Where such considerations become useful, as suggested by the present answer, is that *often* (but not always!) causal variables are regressors (independent), so that distinction can serve as a heuristic guide in model building.  But it's not dispositive.  Indeed, in many observational studies the cause/effect distinction is not present: one is interested in estimating how variables are associated or how the value of one might be predicted from values of others.
Comment: Yes: it's the one that appears in any good textbook that covers regression.  The definition is made in terms of the model one applies to the data, which clearly distinguishes the roles.  That begs the question of how to decide, in any application, what an appropriate model might be: and sometimes it's okay to model the data *both ways,* by switching the roles of the variables!  Tukey describes this approach in his book *EDA* on exploratory data analysis, for instance.  Thus, a full and correct answer to your question would have to discuss what it means to create and apply a statistical model.
Comment: @whuber Can you provide a non-specious, yet plausible definition for Independent and Dependent variables that holds every time?


How to find the principal part of the Laurent expansion of $\frac{1}{(\operatorname{cos}(z)-1)^2}$

Question: I'm confused how to find the principal part of the Laurent expansion of this function?
$\frac{1}{(\operatorname{cos}(z)-1)^2}$ in $0<|z|<\pi$
If you factor out negative 1 then can you expand it using binomial theorem? By doing this you find no negative powers? I would very much appreciate being pointed in the correct direction!
Comment: @LHS You don't want to do that, because the binomial theorem works for small x, and cos is not small near 0.
Comment: Could you explain a bit more about your idea of "factoring out negative 1"? It's not clear to me what that means.
Comment: $(1-\operatorname{cos}(z))^{-2}$ is the same as the function stated above, can we not use binomial theorem to expand this? as it is in the form $(1+x)^{-n}$
Comment: Perhaps, but is looks like this will lead you into a little maze of twisty Chebyshev polynomials. What I would do is notice that $\cos(z)-1=-2\sin^2(z/2)$ to get rid of the subtraction.
Answer: Laurent series expansion at what point? The function is holomorphic everywhere in $\mathbb{C}\backslash\{2k\pi, k\in\mathbb{Z}\}$. This means that for any $z_0\neq 2 k \pi$, the Laurent series expansion at $z_0$ will have a zero principal part.
As for the Laurent series expansion at any of the singularities, consider for example the expansion at $z_0=0$. Since for $|z|\ll 1$, $\cos z-1 = -\frac{1}{2}z^2 + \frac{1}{24}z^4+O(z^6)$ we have
$$
\frac{1}{(\cos z-1)^2} = \frac{4}{z^4}\left(1-\frac{1}{12} z^2 + O(z^4)\right)^{-2}
= \frac{4}{z^4} + \frac{2}{3 z^2} + O(1)
$$
So the principal part of the Laurent series expansion at $z=0$ is $\frac{4}{z^4} + \frac{2}{3 z^2}$.
Comment: How about in the region I specified?


Exe file from windows console

Question: I have a .net application (winform) that I need to always run on the server but users are logging into the the remote session and then logging off thus closing the application.
Is there a way that I can launch it on the console remote session or make it a service?
I tried using the sc.exe command tool but the service won't run, its probably because it's not a service application.
How can I run this application?
Thanks
Answer: Microsoft KB entry 137890 should help. At least it worked for us.

Exe file from windows console


Solving a PDE with Fourier transform

Question: [Homework problem:] In my assignment, I was given a problem where I had to solve a PDE using Fourier transform. It goes like this:

Solve the PDE $$u_t=u_{xx}$$ subject to the initial conditions: $u(0, t)=0$ and $u(x, 0)=f(x)$.

My attempt at a solution:
Using Fourier transform on both sides of the original equation, I get:
$$\frac{\partial{\tilde{u}}}{\partial{t}}=-k^2\tilde{u}(k,t)$$ [Here I have used the property of Fourier transform of derivatives which states that $\cal{F}(f^\prime(x))=-ik\cal{F}(f(x))$.]
Let $\tilde{u}(k, t)=U(t)$ since here we are differentiating with respect to $t$ only, so $\tilde{u}$ is effectively a function of $t$ only.
Then we get an ODE in $t$ that can be easily solved: $$\frac
{dU}{dt}=-k^2U$$ whose solution is of the form $U=Ce^{-k^2t}$, where $C$ is an integration constant.
Now, we see that $C=\tilde{u}(k, 0)$ actually, and we are given that $u(x, 0)=f(x)$, so $C$ must be $\tilde{f}(k)$, or $$\tilde{u}(k, t)=\tilde{f}(k)e^{-k^2t}.$$
Then:
$$u(x, t)=\frac{1}{\sqrt{2\pi}}\int_{-\infty}^{\infty}\tilde{u}(k, t)e^{ikx}dk$$
$$=\frac{1}{\sqrt{2\pi}}\int_{-\infty}^{\infty}\tilde{f}(k)e^{ikx}e^{-k^2t}dk$$
Now, $$\tilde{f}(k)=\frac{1}{\sqrt{2\pi}}\int_{-\infty}^{\infty}f(x)e^{-ikx}dx$$
$$\Rightarrow u(x, t)=
\frac{1}{2\pi}\int_{-\infty}^{\infty}\int_{-\infty}^{\infty}f(x)e^{-ikx}e^{ikx}
e^{-k^2t}dkdx$$
$$=\frac{1}{2\pi}\int_{-\infty}^{\infty}f(x)dx\int_{-\infty}^{\infty}e^{-k^2t}
dk$$
But I am stuck here since this only gives me the integral form, I can't evaluate the exact solution from here. Can I use the condition $u(0, t)=0$ somehow to get the solution? Or do I need the exact functional form of $f(x)$ to find the exact solution?
Comment: Be careful with the variables! You have two $x$:s that you have treated as one. One is a variable of integration (when calculating $\tilde{f}$) and one is a free variable (the one in $u(x,t)$).
Comment: There is no exact solution. It depends on $f(x)$
Answer: Without knowing $f$ the closest we can come is writing the solution as an integral or a convolution:
$$
u(x,t)
= \frac{1}{\sqrt{2\pi}}\int_{-\infty}^{\infty}\tilde{f}(k)e^{ikx}e^{-k^2t}dk
= \frac{1}{\sqrt{2\pi}}\int_{-\infty}^{\infty}\left(\frac{1}{\sqrt{2\pi}}\int_{-\infty}^{\infty}f(x')e^{-ikx'}dx'\right)e^{ikx}e^{-k^2t}dk \\
= \frac{1}{2\pi} \int_{-\infty}^{\infty} f(x') \left(\int_{-\infty}^{\infty} e^{-ik(x-x')} e^{-k^2t} dk \right) dx'
= \frac{1}{2\pi} \int_{-\infty}^{\infty} f(x') \, \sqrt{\frac{\pi}{t}} e^{-\frac{(x-x')^2}{4t}} dx' \\
= \frac{1}{2\sqrt{\pi t}} \int_{-\infty}^{\infty} f(x') \, e^{-\frac{(x-x')^2}{4t}} dx'
= \left(f*\frac{1}{2\sqrt{\pi t}} e^{-\frac{\bullet^2}{4t}}\right)(x)
$$

Solving a PDE with Fourier transform


C# - File Assocation Access to the registry key 'HKEY_CLASSES_ROOT\' is denied

Question: I currently have a program that sets file association using the registry (Note, it's a .NET 2.0 so I have to go through the registry method).  The problem is, since it's trying to write to HKCR, the possibility exists of it running into an administrative problem (on other machines).  Is there a more universal way to set file association (for .NET 2.0) such as  using HKCU or some other method that doesn't require administrative privileges.
string keyName = "Class";
string keyValue = "Class File";
string apppath = Application.ExecutablePath.ToLower() + " \"%1\"";
RegistryKey key;
key = Registry.ClassesRoot.CreateSubKey(keyName);
key.SetValue("", keyValue);

RegistryKey iconkey;
key = Registry.ClassesRoot.CreateSubKey(keyName);
iconkey = key.CreateSubKey("DefaultIcon");
iconkey.SetValue("", Environment.CurrentDirectory + "\\resources\\musicfile.ico");

key = key.CreateSubKey("shell");
key = key.CreateSubKey("open");
key = key.CreateSubKey("command");
key.SetValue("", apppath);

Answer: HKEY_CLASSES_ROOT is an alias, a merging, of two other locations:

HKEY_CURRENT_USER\Software\Classes
HKEY_LOCAL_MACHINE\Software\Classes

You have to decide why you're registering the file. Is the file registration supposed to be for all users? If so, you want to register the association with HKEY_CLASSES_ROOT (which will implicitly write it to HKEY_LOCAL_MACHINE.
Or is the registration meant for only the current user? If so, register your file in HKEY_CURRENT_USER.
Keep in mind that just because one user installs your program doesn't mean that all users want to have it; so consider what the program is for.
Either way, the proper time to register your file association was during installation with the MSI installer. MSI installers know how to ask for elevation during install - if it's required. Not all software has to go in Program Files, some can go in %APPDATA% (i.e. Chrome) where no admin privileges are required to install - and it's only installed for the one user.
Comment: @David  Yeah...but it's sorta cheap. You answered it first, i just fished for the votes :)
Comment: Thank you for all the information.  It seems uses HKCU would be more appropriate.  For the installer, I currently use Visual Studios supplied installer.  Plus, in my application, there is an option to disable the file association, so it goes beyond the installer.
Answer: You can use user-specific file associations in situations where you do not have access to HKLM.
Alternatively you can manifest your app to require administrator rights.
More normally file associations are set during application install where admin rights are normally available.
Comment: As I responded to Ian's answer, the registry settings go beyond just installation.  Also, I'd hate to cripple the program to only be accessible to administrators. +1 though for the useful link.
Comment: @Nate Fine, I just wrote what I knew and now of course you can take from that whatever proves useful


How to modify a substantial amount of data in an existing xml doc in erlang

Question: let's asume you've an existing XML document of about 1 megabyte size. So it's quite small in general but still not trivially small. Now I'd like to work on this document most of the time inserting data like small lists or add text on several nodes inside this document. Let's say about 25 modifications. What's the best way of doing this in erlang.
Thanks.
-dennis
Answer:
Read in the whole document using xmerl (comes with Erlang/OTP)
Split it up in parts: the ones you don't need to change and the ones you'll modify
Let functions manipulate the dynamic parts
Put the whole document together again by concatenating the static and dynamic parts
Export it to text again using xmerl
Comment: Thanks. I'm affraid that this is not so easy if your xml has several levels of nesting. -dennis
Comment: Read up on using the `xmerl` library, and post any follow up questions here on Stack Overflow.
Comment: 1 Meg is small enough to parse all and modify in memory. If the XML structure is complicated its easier to manipulate it that way.  So xmerl is the way to go
Answer: xmerl can be disturbing. I have used erlsom library. It is more flexible and converts XML, SOAP Documents, XSL structures direct into erlang records. Download it and follow its documentation.
Xmerl will be stressing. Its documentation is vague. I advise you use erlsom.erlsom also ships in with the libraries for creating SOAP clients and servers. its so direct when parsing XML  files or streams into Erlang types


Send email to Commerce customer after order status is changed to Completed

Question: I am attempting to develop a module that will send an email to a customer in my Commerce 2 store when that customer's order's status is updated to "Completed". I am basing my module off of the example shown in the handbook (https://docs.drupalcommerce.org/commerce2/developer-guide/orders/workflows/react-to-workflow-transitions)
Here's what I have so far:
File: /modules/custom/tracking_number_email/tracking_number_email.info.yml
name: Commerce Tracking Number Email
type: module
description: Uses an event subscriber to send an email with tracking number to customer after order status has been updated.
package: Commerce
core: 8.x

File: /modules/custom/tracking_number_email/tracking_number_email.services.yml
services:
  tracking_number_email.order_fulfillment_subscriber:
    class: Drupal\tracking_number_email\EventSubscriber\OrderFulfillmentSubscriber
    arguments: ['@language_manager', '@plugin.manager.mail']
    tags:
      - { name: event_subscriber }

File: /modules/custom/tracking_number_email/src/EventSubscriber/TrackingNumberEmail.php
<?php

    namespace Drupal\tracking_number_email\EventSubscriber;

    use Drupal\state_machine\Event\WorkflowTransitionEvent;
    use Drupal\Core\Language\LanguageManagerInterface;
    use Drupal\Core\Mail\MailManagerInterface;
    use Drupal\Core\StringTranslation\StringTranslationTrait;
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;

    /**
     * Sends an email when the order transitions to Fulfillment.
     */
    class OrderFulfillmentSubscriber implements EventSubscriberInterface {

      use StringTranslationTrait;

      /**
       * The language manager.
       *
       * @var \Drupal\Core\Language\LanguageManagerInterface
       */
      protected $languageManager;

      /**
       * The mail manager.
       *
       * @var \Drupal\Core\Mail\MailManagerInterface
       */
      protected $mailManager;

      /**
       * Constructs a new OrderFulfillmentSubscriber object.
       *
       * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
       *   The language manager.
       * @param \Drupal\Core\Mail\MailManagerInterface $mail_manager
       *   The mail manager.
       */
      public function __construct(
        LanguageManagerInterface $language_manager,
        MailManagerInterface $mail_manager
      ) {
        $this->languageManager = $language_manager;
        $this->mailManager = $mail_manager;
      }

      /**
       * {@inheritdoc}
       */
      public static function getSubscribedEvents() {
        $events = [
          'commerce_order.fulfill.post_transition' => ['sendEmail', -100],
        ];
        return $events;
      }

      /**
       * Sends the email.
       *
       * @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event-
       *   The transition event.
       */
      public function sendEmail(WorkflowTransitionEvent $event) {
        // Create the email.
        $order = $event->getEntity();
        $to = $order->getEmail();
        $params = [
          'from' => $order->getStore()->getEmail(),
          'subject' => $this->t(
            'Regarding your order [#@number]',
            ['@number' => $order->getOrderNumber()]
          ),
          'body' => ['#markup' => $this->t(
            'Your order with #@number that you have placed with us has been processed and is awaiting fulfillment.',
            ['@number' => $order->getOrderNumber()]
          )],
        ];

        // Set the language that will be used in translations.
        if ($customer = $order->getCustomer()) {
          $langcode = $customer->getPreferredLangcode();
        }
        else {
          $langcode = $this->languageManager->getDefaultLanguage()->getId();
        }

        // Send the email.
        $this->mailManager->mail('commerce', 'receipt', $to, $langcode, $params);
      }

    }

I am receiving the following error message when this custom module is installed:

ReflectionException: Class
Drupal\tracking_number_email\EventSubscriber\OrderFulfillmentSubscriber
does not exist in ReflectionClass->__construct() (line 30 of
/home1/mysite/public_html/core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterEventSubscribersPass.php).

If I remove the tracking_number_email.services.yml the error goes away, but the module doesn't work (to be clear, it doesn't work whether or not that services.yml file is in place). I assume I have something erroneous in that services.yml file, but I am stuck trying to figure out what is wrong.
Answer: I didn't read through your code attempts, but have you evaluated Commerce Email yet? The module was basically written to serve this purpose.
Comment: I ended up getting it to work after changing the filename to OrderFulfillmentSubscriber.php, to match with the class OrderFulfillmentSubscriber implements EventSubscriberInterface {  portion of the code. Thanks!
Comment: Thanks for pointing out that module, I did not know that it existed. I installed it, but it appears that it only reacts to 3 events: Checkout Completion Register, Order Paid, and Order Placed. It does not appear that it has the capability to react to "Order Status Change" or better yet, "Order Fulfilled".
Comment: fwiw, creating a new plugin for Commerce Email to send an email on a specific state transition should be pretty easy. All you really need to do is copy the OrderPlaced plugin and change the event_name to the proper transition name: https://git.drupalcode.org/project/commerce_email/-/blob/8.x-1.x/src/Plugin/Commerce/EmailEvent/OrderPlaced.php (If that's more than you bargained for, open a feature request in the module and we'll see what we can do. : )
Comment: @hockey2112 Rock on, way to stick with it!


Typedef for pointer to a template class method

Question: The title sums my question up - I need a generic typedef for a pointer to a template class method, as explained in the code below. The typedef needs to be generic.
template<typename TYPE>
struct MyClass {
    const TYPE& get() const {}
};

// this is okay:
typedef void (MyClass<int>::*ParticleMethodPtr)( int );

// now I just need to typedef this so I can
// use the pointer on methods other than int

// using typedef, not okay:
template< TYPE >
typedef void (MyClass<TYPE>::*ParticleMethodPtr)( TYPE );

Answer: In C++11:
template<typename TYPE>
using ParticleMethodPtr = const TYPE&(MyClass<TYPE>::*)() const;

ParticleMethodPtr<int> p = &MyClass<int>::get;

Answer: That is not allowed, as you've seen yourself.
You can do this:
template<typename T>
struct member_pointer
{
    typedef void (MyClass<T>::*function_type)(T);
};

Now you can use this as:
member_pointer<int>::function_type memfun = &MyClass<int>::some_func;

(obj.*memfun)(100);

You can use C++11 template alias to make it simpler as:
template<typename T>
using mem_function = typename member_pointer<T>::function_type;

then use it as:
mem_function<int> memfun = &MyClass<int>::some_func;

(obj.*memfun)(100);

Hope that helps.


Using text file as pattern preg_match_all PHP

Question: error:preg_match_all(): Compilation failed: nothing to repeat at offset 59

Hello everyone, I am trying to make a word filter for a client and i have ran into the issue that my code witch is pulling words from a text file can not read the $  character from my text file, mycode is below.
 $lines=array();
  $fp=fopen('/opt/lampp/htdocs/Comments/Classes/Bad.txt', 'r');
  while (!feof($fp))
  {
    $line=fgets($fp);
    //process line however you like
    $line=trim($line);
    //add to array
    $lines[] = preg_quote(trim($line));
  }
    fclose($fp);
    $string = Input::get('comments');
    $matches = array();
    $matchFound = preg_match_all(
      "/\b(" . implode("|", $lines) . ")\b/i",
            $string,
            $matches
          );
    if ($matchFound) {
      $this->addError("The following is not allowed please change it.");
      $words = array_unique($matches[0]);
      foreach($words as $word) {
        echo "
" . $word . "
";
      }
      echo "
";
    }

Answer: Use preg_quote on each line while reading in the text file.
Answer: You're probably inserting text that happen contains regex metacharacters, and inserting them in such a way that they're actually treated as metachars. You need to preg_quote() the text you're inserting to escape all of those metachars.
Comment: So it would would look something like this $line=trim(preg_quote($line));
Comment: don't manipulate the quoted string in any way once it's been quoted. trim() won't do much, but any other manipulations COULD undo the quoting. trim THEN quote.
Comment: I did the preg_quote after the then and it still isnt searching for the special character i may be doing something wrong i updated the code above
Comment: You've got `preg_quote($lines)`, and `$lines` is your array. so you're using an array in string context, and PHP will politely convert that array into the literal word `Array`. try `$lines[] = preg_quote(trim($line))`.
Comment: So i echo the array and found that the line isn't $$ in the array it is \$\$ could that be because of my sanitize function htmlentities(string)($string, ENT_QUOTES, 'UTF-8');
Comment: $ isn't an html entity. and html entities don't use backslashes. an htmlentity-ified $ would be more like `$`
Comment: `$` is, however, a regex metachar, and after preg_quote would become `\$`.
Comment: could it possibly be my implode ?
Comment: no idea. put your pattern into a string so you can echo it out for inspection.e.g. `$pat = "/\b" . implode(...) . "/"; echo $pat;`
Comment: I have no idea anymore its in the search as a\$\$   (word sensor) and its still not finding it and when i remove the pre_quote so its just a$$ it tells me error at offset 59


How sensitive is a site's UUID?

Question: I have set up continuous integration for a Drupal site. After every git push the site is built remotely from scratch in a Docker container. It runs a site install, sets the site's UUID to import the configs, creates some sample content and then runs Behat tests.
For simplicity I'd simply commit the UUID in a text file and then import it like so:
drush cset system.site uuid "$(< sites/default/uuid.txt)"

The underlying question I have is if it's OK – from a security point of perspective - to commit a site's UUID into a public Git repo? Does that create any vulnerabilities to my site?
Answer: Sorry, only after I created the question it came to my mind that the UUID itself already may be present inside a config YAML. And indeed it is. Inside system.site.yml. So, I guess when it's OK to have it printed inside a config YAML – with the purpose to get version tracked – it's no problem to have it inside a text file as well.
Comment: I guess the only difference being that most web servers will be default-configured to allow the contents of txt files to be served, but not yml files. So there's probably still a slight security concern for some people

How sensitive is a site's UUID?


Interpretation and usage of Kolmogorov-Smirnov Test in Python

Question: I have a sample of data, and I want to know weather it is Gaussian-distributed or not. The mean of my data is not zero.
To check weather I'm using the K-S test correct, I generated some Gaussian-distributed data and added some bias:
<code>data = stats.norm.rvs(size=10000) + 1
print stats.kstest(data, 'norm')
</code>
This gives a p-value of 0.0. If I subtract the bias, I get something like 0.7-0.8, depending on the seed, of course. Does the data need to have $\mu = 0$? If so, does $\sigma^2 = 1$? What if my distribution has a different, unknown $\sigma^2$?
Answer: According to this SO question and the docs, it seems that the Python KS test default reference distribution is a normal distribution with $\mu = 0$ and $\sigma = 1$ ($N(0,1)$).  See the SO question for more instructions on how to change the reference distribution.
In answer to your more specific question, you were using it correctly.  In your first analysis, you had $p \approx 0.7$ when comparing 2 distributions which were $N(0,1)$.  You then added 1 to all the terms in one distribution, so that the means were different, and $p = 0$.
Comment: I'm sorry to bother you again, but i really don't understand how i have to renorm my data in order to get $\mu = 0$ and $\sigma = 1$. For the $\mu$ its easy, but how to do it for the $\sigma$?
Comment: So, the general approach is subtract the mean and divide by the standard deviation; that is, $x_{new} = \frac{x - \mu}{\sigma}$.  You can use the code in [this SO answer](http://stackoverflow.com/a/4544459/1046075) to standardize your data.
Comment: But why do you want to standardize your data, in the context of using the KS test?


Stem para Twitter

Question: Caros colegas,
Estou tentando fazer uma análise de twitters de uma timeline e precisava stemiatizar os textos para análise. Estou tentando o seguinte procedimento:
setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)
tweets <- userTimeline("Pragmatismo_", n = 3000)
tweets.df <- twListToDF(tweets)
myCorpus <- Corpus(VectorSource(tweets.df$text))
removeURL <- function(x) gsub("http[[:alnum:][:punct:]]*", "", x)
removeNumPunct <- function(x) gsub('[[:punct:]]', '', x)
myCorpus <- tm_map(myCorpus, content_transformer(removeNumPunct))
myCorpus <- tm_map(myCorpus, content_transformer(removeURL))
myCorpus <- tm_map(myCorpus, ptstem)

A questão é que mesmo após o último comando myCorpus <- tm_map(myCorpus, ptstem) o texto não aparece stemizado.
Alguma dica?
Muito obrigado!
Comment: Será que isso te ajuda? https://github.com/dfalbel/ptstem
Comment: Vou tentar. Obrigado!
Comment: A função `ptstem` que é utilizada em `tm_map` a princípio nao está definida na questão, e nem nas bibliotecas mais comuns para esse fim. Voce poderia indicar de qual pacote você retirou a mesma? Ou insira a função que tu programou...
Answer: A principal função de stemming empregada na tm_map é a stemDocument, mas como é apresentado na resposta dessa pergunta, não é possível utilizar a mesma para o português devido a um bug.
O que eu fiz para contornar a situaçao foi utilizar o pacote quanteda:
library(quanteda)
my_dfm <- dfm_wordstem(myCorpus, language = "pt")

Outra opção, seria adaptar se possível o uso da função ptstem::ptstem_words no seu contexto (não testei).

Stem para Twitter


What are the logical underpinnings of the epsilon- delta definition of limits?

Question: I'm having trouble getting my head around the epsilon-delta defintion of limits. I learned about conditional statements and I know that in order for a conditional to be true , one of the following cases have to hold

both antecedent and consequent are true, or
both are false, or
the consequent is true and antecedent is false.

Now in proving the definition of a limit say
$$\lim_{x \rightarrow 4} x^2 + x -11 = 9 $$
which case do we prove? Is it the same for every limit or are different limits proved by different cases?
How does the concept of conditional truth-tables relate to this definition?
Thank you in advance.
Comment: If antecedent is false then consequent is irrelevant
Answer: If you're proving that $P\implies Q$, for some statements $P$ and $Q$, you don't care which of these
$$\underbrace{P\textsf{ true},\;Q\textsf{ true}}_{\large P\land Q}\qquad\qquad \underbrace{P\textsf{ false},\;Q\textsf{ true}}_{\large (\lnot P)\land Q}\qquad\qquad \underbrace{P\textsf{ false},\;Q\textsf{ false}}_{\large (\lnot P)\land(\lnot Q)}$$
is true. Your only task is to prove that
$$\underbrace{P\textsf{ true},\;Q\textsf{ false}}_{\large P\land(\lnot Q)}$$
is false. In other words, these statements are logically equivalent:
$$(P\implies Q)\iff\lnot(P\land(\lnot Q))$$

Given a function $f:\mathbb{R}\to\mathbb{R}$, and real numbers $a,L\in\mathbb{R}$, the meaning of the statement
$$\lim_{x\to a}f(x)=L$$
is that
$$\textsf{for any }\epsilon>0,\textsf{ there exists some }\delta>0\textsf{ such that for all }x\in\mathbb{R},\\
|x-a|<\delta\implies|f(x)-L|<\epsilon$$
Let's take a look at your example of
$$\lim_{x\to 4}x^2+x-11=9$$
so $\,a=4$, $\,L=9$, $\,f(x)=x^2+x-11$. If $\epsilon=1$, then you want to find a $\delta>0$ such that
$$\textsf{for all }x\in\mathbb{R},\;\;|x-4|<\delta\implies|x^2+x-11-9|<1$$
An example of such a $\delta$ would be $\delta=\frac{1}{10}$. In order to prove that
$$\textsf{for all }x\in\mathbb{R},\;\;|x-4|<\tfrac{1}{10}\implies|x^2+x-20|<1$$
all we care about is proving that there is no $x\in\mathbb{R}$ such that
$$|x-4|<\tfrac{1}{10}\textsf{ true},\;|x^2+x-20|<1\textsf{ false}$$
For example,
$$|5-4|<\tfrac{1}{10}\textsf{ false},\;|5^2+5-20|<1\textsf{ false}\qquad\quad \textsf{great! }\checkmark$$
$$|4.109-4|<\tfrac{1}{10}\textsf{ false},\;|(4.109)^2+(4.109)-20|<1\textsf{ true}\qquad\quad \textsf{great! }\checkmark$$
$$|4.05-4|<\tfrac{1}{10}\textsf{ true},\;|(4.05)^2+(4.05)-20|<1\textsf{ true}\qquad\quad \textsf{great! }\checkmark$$
Answer: Just to make sure we are on the same page, let me write out that conditional:
For all $\epsilon > 0$ there exists a $\delta > 0$ such that for all $x$
$$
|x - 4| < \delta \implies |(x^2 + x - 11) - 9| < \epsilon\\
$$
Note that the conditional appears AFTER you choose $\epsilon$, $\delta$, and $x$. For any choice of $\epsilon$, there needs to be some choice for $\delta$, such that for any choice of $x$, the conditional is true.
Which of those three cases holds will depend on the $\epsilon$, $\delta$ and $x$ you choose!
Say you take a small $\epsilon$, choose a $\delta$ small enough for the $\epsilon$, and take an $x$ that is very far from 4 (like 100 or something). Then:
$$
|x - 4| > \delta
$$
$$
|(x^2 + x - 11) - 9| > \epsilon
$$
So the antecedant is false, the consequent is false, and the conditional is true.
Now say you take an $x$ that is very far from 4, but very close to -5. Then:
$$
|x - 4| > \delta
$$
$$
|(x^2 + x - 11) - 9| < \epsilon
$$
(Because -5 is the other solution to the equation $x^2 + x - 11 = 9$.)
So the antecedant is false, the consequent is true, and the conditional is true.
Finally, say you take an $x$ that is very close to 4. Then:
$$
|x - 4| < \delta
$$
$$
|(x^2 + x - 11) - 9| < \epsilon
$$
So the antecedant and consequent are both true, and the conditional is true.
So the answer is: you use all three cases for every problem! Which one holds depends on which $x$ you are looking at.
Answer: This is a good question!
Strictly speaking, there is a logical implication here. But once you understand that we only care about one case, your job becomes much easier (and less confusing). Why do we only care about one case?
Another way to look at the statement "$Q$, whenever $P$" (equivalently, "If $P$, then $Q$") is that the statement is true if

Both $P$ and $Q$ are true, or if
$P$ (the antecedent) is false.

The latter case, when the antecedent is false, we often say that the statement "$Q$, whenever $P$" is vacuously true (and I think of it as "true by a technicality", or "true by default"). It's similar to the statement "If I go to the store, I'll bring you something". You wouldn't consider me a liar if I don't go to the store (regardless of whether I've brought you anything), would you? It's essentially the same kind of "true by a technicality." If I don't go to the store, then you don't even need to ask "Have you brought me anything?" because I technically couldn't possibly have lied to you.

Aside: My thoughts are that the logic we use in calculus isn't really the same kind of logic that's done in a purely logical setting; at least, we don't have the same goals. We really don't care about implications with false antecedents because we're only interested in using implications (and properties defined with implications, like continuity) to deduce things. For that we need modus ponens, which requires our antecedents to be true.
For this reason, I think a truth table will generally be nearly useless in a calculus setting.

So, when confronted with the task of showing that

for all $\epsilon > 0$, there exists a $\delta >0$ such that $\lvert f(x) - 9 \rvert < \epsilon$ whenever $\lvert x - 4 \rvert < \delta$,

we do a few things:

Pick a generic $\epsilon > 0$.
Find some $\delta > 0$ so that when we assume $\lvert x - 4 \rvert < \delta$, we can show that $\lvert f(x) - 9 \rvert < \epsilon$.

Step 2. is where all the work happens. It usually involves using some unknown $\delta$, assuming $\lvert x - 4 \rvert < \delta$, and seeing what kind of relationship we can derive, algebraically, between $\epsilon$ and $\delta$.
(Notice that I said "we assume $\lvert x - 4 \rvert < \delta$". This goes back to the fact that, if $\lvert x - 4 \rvert \nless \delta$, then our antecedent is false, and the "$Q$, whenever $P$" statement involving the two inequalities is true by default -- there's literally nothing to show, so we don't even bother talking about it.)
Once we have our $\delta$, actually showing that $\lvert f(x) - 9 \rvert < \epsilon$ using our $\delta$ and $x$ such that $\lvert x - 4 \rvert < \delta$ can happen almost as if by magic: We generally don't even need to say how we found such a $\delta$ (although we had to go through the algebra in step 2. to do so), only that we've found one, and shown that it works.


Why do we need confidence intervals?

Question: I am following a video lecture on Statistics, which introduces the concept of confidence intervals in the following way:
"A bank vice president is interested in the average checking account balance for all personal accounts. A random sample of 500 accounts is selected, and the average is calculated. What level of "confidence" for the mean will the VP be satisfied with?"
What I don't understand is, why are confidence intervals even necessary? I learned previously that the mean of the sampling distribution of the sample mean is exactly equal to the population mean. So, wouldn't it be better for the VP to do that i.e. take many samples and calculate the mean of the sampling distribution of the sample mean (and thus get the exact population mean), instead of just using one sample to calculate confidence intervals (which will not give you the exact population mean)?
Thanks.
(Question source: https://www.youtube.com/watch?v=9GtaIHFuEZU)
Comment: It takes effort to look at every single account to get the exact answer with certainty, so taking a sample saves effort at the cost of some possible sampling error.  The bigger the sample is, the  smaller the sampling error is likely to be, but requires greater effort.
Comment: @amoeba In some corners (mainly medical ones) where the word "sample" is used also outside of a statistical context (say, "take _a_ sample of blood"), the world "sample" means _one item_ (one observation/measurement) (hence, "take 3 samples of blood" means simply "take blood three times"), not as in "sample = set of many observations/measurements". This difference in the meaning assigned to the word "sample" is a recurring source of confusion in communication.
Comment: I wonder if the OP thinks that taking the mean of multiple sample means is what is meant by "mean of the sampling distribution of sample means". It isn't -- that's just the mean of a bigger sample!
Comment: The expected value of the sample mean may be the population mean, but more often than not the sample mean will not be exactly equal to the population mean, and different samples are likely to have different sample means.
Comment: @Henry Thanks. So, when people say that the mean of the sampling distribution of the sample mean is exactly equal to the population mean, are they assuming that there are infinite samples taken? I.e. if you took, say, 100 samples (or any number of samples less than infinity), then the mean of the sampling distribution of the sample mean would not equal the population mean?
Comment: Yes. They might be equal by chance, but probably would not be.
Comment: The "sample mean" and "the mean of the sampling distribution of the sample mean" are two different things! Also, the word "sample" refers to all 100 observations (in your example). So if you take a sample of 100 observations, then its sample mean is not equal to the population mean. But if you take many different samples, then their means form a distribution (called "sampling distribution") and the mean of that distribution is equal to the population mean.
Answer: You forget that accuracy comes at the cost of effort. He'd need to gather all the data from thousands of accounts. And what if five accounts have billions while the rest are in the hundreds? The confidence interval is a faster way to give you a reasonable answer in a reasonable amount of time.
Confidence interval is even more apparent with surveys of a city or country. You can't put a gun to everyone's head and yell at them to complete the survey.
Answer: One answer is that the exact population may not be so easily reachable. The example is kind of weird in that the population of bank accounts is easily knowable to the banker and thus he should/would indeed just use that information. A better question would be what is the average sum a person is expected to deposit to the bank, although a random sampling would not be available.
Even more classic example would be the banker surveying the population what they think about his bank. In this case it would be very difficult to reach everyone and thus the concept of a confidence interval makes more sense.

Why do we need confidence intervals?


How to override menu link href to be something like javascript:jQuery....?

Question: I need to override one menu link in footer to be href like this javascript:jQuery - after click the modal window will open.
Drupal not allowed me to insert it directly to the menu link form.
Thanks a lot!
Comment: Its not for me but for the company they decided :D
Comment: You could use [menu_attributes](https://www.drupal.org/project/menu_attributes) and set an id or class on the menu item. Then in your theme or custom module include a javascript that binds to the id or class.
Comment: Yes this is good opion I have it in my mind, but I was wondering for something without module. Does exist any hook alter function or anything I can do it by?
Comment: Using javascript in the `href` is a bit 'outdated' and discouraged (no reference, sorry). I have not seen any module or function to achieve it in Drupal.
Comment: I understand dude.
Answer: There is a way to make this happen with the assistance of a module called Special menu items. Essentially, the module allows you to fill in placeholder / separator menu items to work around the menu item system (the module overrides theme_link() when you give it a special path). Per the project page:

If you use module than require to output a link, go in
  /admin/config/system/special_menu_items and use  as the HTML tag.
  You can even use  if you need to.

In your instance, you could use  in the configuration page, then add your menu item with the <nolink> path to have produce a menu item with the javascript embedded href.


How to populate data with haveInDatabase in _beforeSuite in Codeception

Question: What I am trying to do is to insert some data to the Database before the suite starts. The dump is a clean Db and I have multiple suites that will need different starting data.
this is my code right now
namespace Helper;

// here you can define custom actions
// all public methods declared in helper class will be available in $I

use Codeception\TestInterface;

class Contacts extends \Codeception\Module
{
    public $started = false;
    public function _beforeSuite($settings = [])
    {

        parent::_beforeSuite($settings); // TODO: Change the autogenerated stub
        $dba = $this->getModule('Db');

        $dba->haveInDatabase('table1',array('is_published'=>'1','created_by'=>'1',
            'created_by_user'=>'Automated User'));

    }

I don't get any errors but the data is not there when codeception starts
Answer: Have you got cleanup: true in parameters of Db modules?
It cleans data before each test, so in order to keep data inserted in _beforeSuite, you have to disable cleanup by changing it to cleanup: false.


Why is there no $S_1$ and $S_2$ partition $\{1,\cdots,n\}$ with some conditions?

Question: For even integer $n\geq6$, let $S_1\cup S_2=\{1,\cdots,n\}$, $S_1\cap S_2=\varnothing$, $\vert S_1\vert=\vert S_2\vert$.
Why does not such $S_1$ and $S_2$ exist if $\prod S_1-\prod S_2=2$?
I obtained $pm\in S$ iff $pm'\in S$ with a prime $p$ and small integers $m$ and $m'$.
Also, one of $S_1$ and $S_2$ has exactly one even number but not other even numbers.
I don't know what I should do now.
Answer: If $n\ge12$, then one set contains all the multiples of 3, including $6$ and $12$, so it also contains all but one of the even numbers.  That is too many.
Comment: You still have $n=6,8$ and $10$ to go


How to animate two divs in html / css to make a semi-circle transition?

Question: I make this css/html logo, and it has 2 semi-circles, I want to animate them... I want they "change" postion, but not making a linear moviment, I want a semi-circle transition
Can anyone help me? A suggestion?
HTML:

Regulus

Here is: http://jsfiddle.net/6cY7x/
Comment: How exactly do you want them to move? Away from the center at separate angles?
Comment: I want to animate them around the title, making a semi-circle moviment, and stopping in the place there is the other div...
Answer: Here is the CSS
.halfCircle{
     height:60px;
     width:27.5px;
     border-radius: 90px 0 0 90px;
     -moz-border-radius: 90px 0 0 90px;
     -webkit-border-radius: 90px 0 0 90px;
    background:black;
    position: absolute;
    left: 60px;
    top: 30px;
    -webkit-transition: -webkit-transform 1s;
    transition: transform 1s;
}
.right{
  transform:rotate(55deg)  translateY(70px);
  -webkit-transform:rotate(55deg)  translateY(70px);
}
.left{
    -webkit-transform:rotate(-125deg)  translateY(70px);
    transform:rotate(-125deg) translateY(70px);
}
.logo:hover .right {
    transform:rotate(235deg)  translateY(70px);
    -webkit-transform:rotate(235deg)  translateY(70px);
}
.logo:hover .left{
    -webkit-transform:rotate(55deg)  translateY(70px);
    transform:rotate(55deg) translateY(70px);
}

.logo{
    width:150px;
    height:100px;
    position: relative;
}

.name h1{
  font-family: serif;
  margin: 0px;
    text-align: center;
    padding-top: 40px;
}

demo
Answer: Here's how I went about it. Instead of rotating the .halfCircles individually, I put them in a container as big as the logo. The logo itself I gave 60px of padding on the top and bottom to give space for the .halfCircles around the text.
HTML:

Regulus

CSS:
.logo{
  width:150px;
  position: relative;
  padding: 60px 0;
}

.Circle {
  position: absolute;
  top: 0; bottom: 0; left: 0; right: 0;
  transform: translateZ(0);
  transition: all 800ms ease-out;
}

The half circles are then positioned absolute to their spots, instead of with float and margin. This will make it easier to adjust and scale the logo later.
.halfCircle{
  position: absolute;
  height: 60px;
  width: 27.5px;
  border-radius: 90px 0 0 90px;
  background: black;
}
.right{
  top: 0;
  right: 0;
  transform:rotate(55deg);
}

.left{
  transform:rotate(-125deg);
  left: 0;
  bottom: 0;
}

Then, on :hover, we just rotate the .Circle element 180deg and presto! No magic numbers, just a simple half rotation.
.logo:hover .Circle {
  transform: rotate(180deg);
}

Demo: http://codepen.io/shshaw/pen/Hrlqx


How do I get the node ID from the view's URL?

Question: I want to add a comment form to my view by using the following code:
print drupal_get_form('comment_node_project_form', array('nid' => $node->nid), 'comment');

However, it shows up a error saying that the variable $node->nid is undefined. I then realized it is a view, not a node.
In the view, by choosing the right URL, e.g. /projects/{username}/{nodeid}, I only show one content.
I guess I can get the node ID by parsing the 3rd argument of the URL. How do I get the node ID from the view's URL?
Answer: If the view uses fields, you can add a field labeled Content: Nid - and either build the link in the template by yourself or try to Rewrite the field in the Views admin UI. If the View uses display views, such as Teaser, you should be able to use the $node variable in the teaser (in this case) tpl file.
Comment: I add a field labeled content:nid in the view, but nothing happens.
Comment: Wait, now that the question has been re-edited I kinda get a better understanding that it's not the individual nodes that you want to allow comments for, but the entire view. Is that correct?


AttributeError: 'unicode' object has no attribute 'pop' - Python list of dictionaries

Question: I have
    my_dict = {
          "test_environments": [
            {
              "Branch Coverage": "97/97(100%)",
              "project" : 'ok'
            },
            {
              "Branch Coverage": "36/36(100%)",
              "project" :'ok'
            }
          ]
        }

How could I delete the key Branch Coverage? I'm trying with this code:
   for index, _ in enumerate(my_dict['test_environments']):
        for key, values in my_dict['test_environments'][index].items():
            key.pop("Branch Coverage")

Comment: `test_environments` have an invalid syntax. some curly braces are not closed, and no comma separating them. Please edit your question and post the code you are actually running.
Answer: You can use the del keyword to remove keys from a dictionary in-place.
my_dict = {
          "test_environments": [
                {
                  "Branch Coverage": "97/97(100%)",
                  "project" : 'ok'
                },
                {
                  "Branch Coverage": "36/36(100%)",
                  "project" :'ok'
                }
        ]
    }

for element in my_dict["test_environments"]:
    del element["Branch Coverage"]

# Prints {'test_environments': [{'project': 'ok'}, {'project': 'ok'}]}
print(my_dict)

Comment: You can still use `pop`; you  just have to call it on the correct object (the dict, not the key).
Answer: You call pop on the dict object you want to modify, not the key you want to remove.
for d in my_dict['test_environments']:
    d.pop("Branch Coverage")


Rephrasing Double Integral Equation

Question: Suppose we have a equation $$\iint_{-\infty }^{\infty} xy \frac{1}{2\pi \sigma^2}\exp\left(-\frac{(x-\mu_x)^2+(y-\mu_y)^2}{2\sigma^2}\right) dx dy$$
What property of the integral has been used to rephrase the equation like below:
$$\int_{-\infty }^{\infty} x \frac{1}{2\pi\sigma^2} \exp\left(-\frac{(x-\mu_x)^2}{2\sigma^2}\right)dx \cdot \int_{-\infty }^{\infty} y \frac{1}{2\pi\sigma^2} \exp\left(-\frac{(y-\mu_y)^2}{2\sigma^2}\right)dy $$
I would like to know the detailed steps between them.
Thank you so much for your help.
Answer: This is Fubini theorem. You can inverse integrating over $x$ and $y$, then intergating first over $y$ and terms in $x$ are seen as constant and can move out from the integral.
$$\int_X \int_Y f(x) g(y) dx dy=\int_Xf(x) \int_Y  g(y) dydx = \int_X f(x) dx \int_Y g(y) dy$$

Rephrasing Double Integral Equation


How to classify a unbalanced dataset by Convolutional Neural Networks (CNN)?

Question: I have a unbalanced dataset in a binary classification task, where the positives amount vs negatives amount is 0.3% vs 99.7%. The gap between positives and negatives are huge. When I train a CNN with the structure used in MNIST problem, the testing result shows a high False Negative Rate. Also, the training error curve goes down quickly in couple of epochs at the beginning but remains a same value in the following epochs.
Could you please advise me a way to process this problem? Thanks!
Answer: This happens because when you take a mini-batch, it is very very less likely (given the ratio of the proportions here) that a mini batch will contain samples of your positives at all. So it will end up learning the pattern for the negative class and after a couple of epochs, everything just gets classified as negative class.
There are two possible ways to handle such a situation.

Given the proportions as 0.3% to 99.7%, this is a very highly skewed data set. You hardly have 3 samples of positive classes for every 1000 samples. I would say you should look at balancing the data set by getting more positive classes. Go and get as many positive samples as you can. Then, you can use a more balanced dataset. For example, you could get 1000 positive samples and then pick a random set of 1000 negative samples and build the classifier. Now, it should be able to learn both the classes.
Use a weighted error measure when updating the weights after a mini-batch. The weights are updated in proportions to the number of samples of the positive and negative classes during any mini-batch. Now, in the current situation given the proportions as 3:1000, even this trick may not work. So you may try getting the proportions to something like 300:1000 by getting 297 more positive samples and combining them with 1000 negative samples. Then with 300:1000 ratio you should weight the error during mini-batches based on number of samples in each of the classes. This should work.
Comment: Do you know what the optimal proportion is here? is it 3:10, or something else?
Answer: Why do you want to use CNNs here? Have you considered other models that actually handle imbalanced data?
For example, I've found the following two techniques have worked really well for me:

Random Forests w/ SMOTE Boosting: Use a hybrid SMOTE that undersamples the majority class and over-samples the minority class by adjustable percentages. Select these percentages depending on the distribution of your response variable in the training set. Feed this data to your RF model. Always cross-validate/perform grid-search to find the best parameter settings for your RFs.
XGBoost w/ hyper-parameter optimisation: Again, cross-validate or perform gird-search to find the best parameter settings for the model. Additionally, xgboost allows you to balance positive and negative class weights using scale_pos_weight. See the parameter documentation for a complete list.

I should also add that the data-set I was working on had the same percentage of skew and I was able to obtain Sensitivity score of 0.941 and a Specificity of 0.71 with xgboost, which means the model is predicting the true positives quite accurately and that bodes well for me.
(Sorry, I can not leave a comment, not enough reputation and I really wanted to know why you chose CNNs)
Answer: Unbalanced dataset is a common issue in all areas and does not specifically concern computer vision and problems dealt by Convolutional Neural Networks (CNNs).
To tackle this problem you should try to balance your dataset, either by over-sampling minority classes or under-sampling majority classes (or both). Arguably, a good choice would be SMOTE (Synthetic Minority Over-sampling Technique) algorithm, as mentioned above. Here you can find a comparison of different over-sampling algorithms. If you're a Python user, imbalanced-learn is a nice library that implements many useful techniques for balancing datasets.
On the other hand, if you're trying to classify images, a nice way to increase your dataset size is to augment it (i.e. by creating reasonable synthetic examples, e.g. similar images but rotated/shifted tiny bit with respect to original ones). You might sometimes find it useful to augment the minority classes to achieve better balance. Keras ImageDataGenerator class is a good tool for this purpose.


How to make users with administrator role member of all organic groups?

Question: Is there a way to make all administrator users a member of all the Organic Groups?
For the moment, if an administrator creates an organic group, the only one who will be member of the newly created group will be this particular Admin user.
Maybe I should just find a way to do it throught a Rule ?
UPDATE
I FOUND HOW:
Answer: I found How,
go to your content type and change the widget for your field "og_group_ref" from OG_Reference to Select List


A question on the permutations of arranging 6 people around a table.

Question: I'm working on a question that involves six people needing to be seated around a table and asks how many arrangements are possible if it is only interested in who sits next to whom, not where they sit at the table?
I wasn't able to come up with the correct answer based on their solution, but I'm having trouble understanding the thought behind what they've done. I've included the image of the solution to this problem and was wondering if someone can help me understand what they've done?
Answer: You're basically asking for a distribution which doesn't depend on the absolute position, rather the relative position. That's easy.
Assume a random person $p_1$ sat at the start of a straight line. Then there are $5$ possiblities for the person who sits on his right, $p_2$, then $4$ possiblityies for the one who sits on his right, $p_3$, and so on. So, we get
$\begin{align}p_1\quad&p_2\quad&p_3\quad&p_4\quad&p_5\quad&p_6\\1\quad&5\quad&4\quad&3\quad&2\quad&1\end{align}$
So, the number is $5*4*\cdots*1=120$.
But, ABCDEF is the same as FEDCBA, since, in both, A is between B and F, B is between A and C, and so on. So, the final number is ${120\over2}=60$


Can I add active class when url starts a string in react Navlink?

Question: How do I add an active class in react navlink when url starts with a "/teacher/create/" string?
Answer: <NavLink to={"/teacher/create/"} exact activeClassName={this.props.location.pathname.startsWith("/teacher/create/") && "activeLink"}>{title}</NavLink>

and add css:
.activeLink {
    color: red;
}

Answer: <NavLink to={"/teacher/create/2"} exact activeClassName={this.props.location.pathname === "/teacher/create/2" && "activeLink"}>{title}</NavLink>

and add css:
.activeLink {
    color: red;
}

react-router-dom: "^4.1.2"
Answer: I don't know how the other solutions above work but in my case I had no access to this.props.location. You can do this by trying something like that:
export const LooseNavLink = props => (
  <NavLink {...props} isActive={(match, location) => location.pathname.startsWith(props.to.pathname)}/>
)


How to simulate from a t copula?

Question: This is a question related to: How to simulate from a Gaussian copula?
Suppose that I have two univariate marginal distributions, say $F$ and $G$, which I can simulate from. Now, construct their joint distribution using a t copula, denoted $C(F,G;R,\nu)$. All the parameters are known.
How can I simulate from this bivariate distribution? Should I do, as a commenter said: Generate $(X,Y) \sim t_2(0,R,\nu)$ and the take $F^{-1}(t_{\nu}(X))$ and $G^{-1}(t_{\nu}(Y))$?
where $t_2(0,R,\nu)$ is a bivariate t distribution with parameters $(0,R,\nu)$, and $t_{\nu}(\cdot)$ represents the univariate t CDF with $\nu$ degrees of freedom.
Answer: Look at this example in MATLAB, it has t copulas too. Basically, the steps are:

generate the pairs of $(x_i,y_i)$ from the copula.
apply the inverse CDFs to get the new pairs: $(F^{-1}(x_i),G^{-1}(y_i))$

You'll see that copula produces the pairs where $x_i,y_i\in[0,1]$, and the domains of inverse CDFs are also $[0,1]$. This works out nicely.
Answer: If you're using R, you can follow this example:
<code>library(copula)
set.seed(101)
#construct the copula object:
mytCop <- tCopula(.4, dim = 2, dispstr = 'un')
#specify the marginal distributions (here a gamma and a t-distribution):
multiV_dist <- mvdc(copula = mytCop,margins=c('gamma', 't'),
                paramMargins = list(list(shape = 2, scale = 1), list(df = 4)))
#sample from the defined copula:
multiV_sample <- rMvdc(2000, multiV_dist)
</code>
Comment: you got to edit the post, use markup for code
Comment: Jordan, What you needed to do was select the code and click the $\{\}$ button. I've fixed it now. But your answer is little use to non-R users -- please explain what the code is doing before you present the code, in such a way that someone using another language could implement it.

How to simulate from a t copula?


In Apple Logic 9, what are "GM Device" tracks?

Question: I'm trying to open a MIDI file in Logic Pro 9, and the tracks are being placed on "GM Device" tracks, which don't have any inserts, meaning I cannot add instruments or effects to them.
What are these GM Device tracks, and is the only way to fix it to create new Software Instrument tracks and move the regions across manually? This is quite a tedious process in large MIDI files.
Comment: I don't know Pro Logic, but I'm guessing that your MIDI files contain [General MIDI](http://en.wikipedia.org/wiki/General_MIDI#Parameter_interpretations) instrument type information and that Pro Logic acts upon that. Guess this doesn't help you though...
Comment: They do, yes. But I'd rather they opened on Software Instrument tracks so that I can add instruments to them, if possible.
Comment: I don't know logic pro either, but this might help : http://www.logicprohelp.com/forum/viewtopic.php?t=48799
Answer: These are tracks that are not configured for use with virtual instruments running on your computer. Rather, they are configured to be used with an external hardware synthesizer or sample player or keyboard, connected using MIDI cables. The MIDI note data that is output from Logic is given patch number or instrument assignments according to the General MIDI Specification.
The reason that they don't have any inserts or effects set up for them is that these tracks don't work with audio: they just send MIDI signals from Logic to the MIDI Out ports on your MIDI interface to an external keyboard or rack-mount hardware device. You would typically take the audio outputs from such a device, run them into the audio inputs of your multi-channel external hardware audio interface, and record the audio from them onto other tracks, and on those other tracks you could add inserts or effects within Logic.
Answer: Logic uses these tracks to send information to an external device. If you want to record an external device then you would probally record this onto another track.
For example:
I recently recorded an Oberheim, the information on the "device track" sents the info to th oberheim, next, the oberheim sends the info to a Logic audio track.
How to solve your problem: just create a softsynh track and copy your midi info into this track.
Don't get confused in the use of midi though, midi is just "info for playing a device" this "device" can be hosted in your computer or externally.


Can the blues scale be used over iim7 V7 Imaj7?

Question: In typical blues using V7 IV7 I7 the blues scale can be used - and as I understand - without concern for avoid tones.
Can I do something similar over jazz iim7 V7 Imaj7?
I made this chart of chord tones for minor and major blues scales...
<code>------------------------------------------
MINOR BLUES SCALE           CHORD TONES

                           R   3   5   7
1 ♭3 4 ♯4 5 ♭7 1    iim7 : -  E♭   -   B♭
B♭ D♭ E♭ E♮ F A♭ B♭  V7   : F (A♭)  -   E♭
                    IΔ7  : B♭ (D♭)  F   A

------------------------------------------
MAJOR BLUES SCALE           CHORD TONES

                           R   3   5   7
1 2 ♭3 ♮3 5 6 1     iim7 : C   -   G   B♭
B♭ C D♭ D♮ F G B♭    V7   : F   -   C   -
                    IΔ7  : B♭  D   F   (G)

------------------------------------------
</code>
...and my thought is that I should be able to use the blues scale by using the matching chord tones as starting/target notes.
When I tried this last night, it seemed OK to me.
Is this approach used in jazz?
Is there another approach that is more typical from a jazz perspective?
Comment: I think there is some misunderstanding in your statements.  It is not the case that the blues is played "without concern for avoid tones", many blues players synchronize their licks to "avoid" certain tones except in passing.  Also, it is customary to modulate with the changes (adding the maj 3rd on I7, then flattening it on IV7, etc).  As for Jazz (and music in general) anything is possible.  There is no need to justify choice of mode using theory.  If it sounds good it is good.  Jazz players use all sorts of embellishments in their improv.
Comment: @ggcg, I agree with what you say about 'avoid tones' there are surely avoid tones, the #4 seems the most obvious case, yet I've seen many 'methods' says any note of the blues scale can be used any time... or words to that effect.
Comment: @ggcg I disagree that "it is customary to modulate with the changes" when using the blues scales. The blues scale tends to be used to created a "bluesy" sound that comes from it's particular melodic contour. Do you have any examples?
Comment: Just because people say it doesn't make it true.  Statements like that are too broad to be useful.  I think what they mean is that if you're playing licks you don't need to check that the 4th is missing from the lick (but that's true of any music, classical too).  Really, the 4th is to be avoided as a strong note in a melody.  And blues players respect that most of the time too.
Comment: @Peter, What I'm saying is that is is customary to modulate with the changes using the Mixolydian mode that matches the chord.  This is one of many possible choices.  The Blues scale can be melded with Mixolydian to create exotic melodies.  There are plenty of players who move the "blues scale" from one chord to the next to add more to their improv.
Answer: Of course you can use the major blues scale when improvising over a II-V-I progression in major, because the notes are simply a subset of the major scale, plus the b3, which makes for the bluesy character. Over the I and II chords, the b3 is dissonant but if used with care, it can give a bluesy feel to your lines. Over the V chord, it is actually a nice altered tension, namely the b13.
The minor blues scale is a bit more problematic, and I would only use it over the V chord, where it gives you a few (altered) tensions that you don't get from the major (blues) scale:

scale tone      tension over V
  b3                b13
   4                  7
  b7                 #9 (b10)

The answer to your question

Is there another approach that is more typical from a jazz perspective?

is 'yes', but whole books have been written about that topic, so I don't see how I can even start to answer it here.
Comment: Sorry for the late accept. Major blues seems the way to go.
Answer: Because the blues scale has such a unique melodic character, it is generally used to skate over without specific regard to the harmonic motion underneath it.
As an example, check out this transcription/analysis of Herbie Hancock's solo on his tune Driftin: http://www.actualproofblog.com/2016/11/driftin_14.html
At the start of his second chorus, he plays exclusively on the Eb major blues scale (or C minor blues scale) for the first two A sections. The chords move around quiet a bit underneath him, but he just sticks to the scale with most of his lines ending on the note Eb. Once he gets to the bridge he goes back to a more harmonic style of improve.
There are definitely other ways to do it, but that's the first example that came to my mind.


Erro o acessar o projeto no laravel4

Question: Estou recebendo um erro ao acessar o projeto no laravel4.
Estava funcionando, mas quando reiniciei meu computador e tentei acessar novamente. Recebo o seguinte erro:

ErrorException
      unserialize(): Error at offset 0 of 197 bytes
Answer: Já tive um erro parecido, quando tentei acessar a aplicação após trocar a "key" nas configurações.
Por isso todo projeto que inicio, gero a "key",e no arquivo
"app/config/session.php" altero a chave "cookie" para "onomedoprojeto_session".
Espero ter ajudado.

Erro o acessar o projeto no laravel4


Hypothesis test for a linear combination of coefficients $c_0\beta_0 +c_1\beta_1$

Question: I'm given a sime linear regression model $$y_i=\beta_0+\beta_1x_i+\epsilon_i$$
where $\epsilon_i\sim N(0,\sigma)$, and I have to create a hypothesis test to contrast
$$H_0: c_0\beta_0+c_1\beta_1=h$$
where $c_0,c_1, h$ are given constants.
My try:
We know that the estimator $\hat{\beta_0}\sim N\left(\beta_0,\sigma^2(\frac{1}{n}+\frac{\bar{x}^2}{S_{xx}})\right)$ and that the estimator $\hat{\beta_1}\sim N\left(\beta_1,\frac{\sigma^2}{S_{xx}}\right)$,so the combination $c_0\beta_0+c_1\beta_1$ follows a normal distribution (since it is a linear combinatin of normal distributions) with mean
$$\mu=c_0\beta_0+c_1\beta_1$$
and variance
$$Var[c_0\hat{\beta_0}+c_1\hat{\beta_1}]$$
I don't see any reason why $\hat{\beta_0}$ and $\hat{\beta_1}$ must be uncorralated, so I can't say that the variance of the sum is the sum of the variances. Then
$$Var[c_0\hat{\beta_0}+c_1\hat{\beta_1}]=Var[c_0\hat{\beta_0}]+Var[c_1\hat{\beta_1}]+2Cov(c_0\hat{\beta_0},c_1\hat{\beta_1})$$
Now
$$Cov(c_0\hat{\beta_0},c_1\hat{\beta_1})=c_0c_1Cov(\bar y-\hat{\beta_1}\bar x,\hat{\beta_1})=c_0c_1\left(Cov(\bar y, \hat{\beta_1})-\bar xCov(\hat{\beta_1},\hat{\beta_1})\right)$$
But $Cov(\bar y, \hat{\beta_1})=0$ and $Cov(\hat{\beta_1},\hat{\beta_1})=Var[\hat{\beta_1}]$ so
$$Var[c_0\hat{\beta_0}+c_1\hat{\beta_1}]=c_0^2\sigma^2\left(\frac{1}{n}+\frac{\bar x^2}{S_{xx}}\right)+c_1^2\frac{\sigma^2}{S_{xx}}-2c_0c_1\bar x \frac{\sigma^2}{S_{xx}}$$
If I haven't made any mistakes, that should be the expression for the variance, but I can't simplify this in order to get a $N(0,1)$ distribution.
My question is then if there is a simpler way to get the hypothesis test, so any hint that points me in the right direction would be highly appreciate.
Note: I have seen a similar question asked here, but the answer involves matrix notation, and that is something we haven't covered in class yet.
Comment: The algebra becomes easier if you use the matrix version of the distribution theory!
Comment: Why does it seem wrong? If you know that $c_0 \hat{\beta}_0 + c_1 \hat{\beta}_1$ is normally distributed and you know its mean and variance, isn't that enough (together with knowledge of how $\sigma^2$ is usually estimated and t-distributions)?
Comment: @kjetilbhalvorsen That's what I've heard, but since we haven't covered the matrix version yet, I don't think I'm allowed to use it.
Comment: "If I haven't made any mistakes..." - did you check for mistakes?
Comment: @mark999 I was concerned about whether I computed the term $Var[c_0\hat{\beta_0}+c_1\hat{\beta_1}]$ right. I think it is corrected now, but the problem at simplifying is still there.
Comment: What sort of simplification do you think is necessary?
Comment: @mark999 Well, the point is to find a quantity with known distribution in order to do the hypothesis test. And the usual path comsists in typifying to get  a $N(0,1)$. But with that variance, it seems like it is the wrong way to do it.
Comment: I agree with @marc999: if you note $c_0 \hat{\beta}_0 + c_1 \hat{\beta}_1$ as $X$ then you know that $X$ is normal with a mean $\mu_X$ that you have computed and a variance $\sigma_X^2$ that you also computed.  If you have all this then is is rather ''trivial'' to test the hypothesis $H_0: \mu_X = h$ versus $H_1: \mu_X \ne h$ don't you think ? And as $\mu_X = c_0 \beta_0 + c_1 \beta_1$ this is the same as the test $H_0: c_0 \beta_0 + c_1 \beta_1= h$ versus $H_1: c_0 \beta_0 + c_1 \beta_1 \ne h$
Answer: Straightforward method
If you'd covered a bit more you'd be there. Start by assuming the null:
$$
h=c_0\beta_0+c_1\beta_1
$$
then, as you've pointed out $c_0\hat\beta_0+c_1\hat\beta_1-h\sim\mathcal{N}(0,V)$ where $V$ is some unknown variance. So you need to find $V$. The equation you've started with is also correct:
$$
V=\text{Var}[c_0\hat\beta_0+c_1\hat\beta_1-h] = c_0^2\text{Var}[\hat\beta_0]+c_1^2\text{Var}[\hat\beta_1] +2c_0c_1\text{Cov}(\hat\beta_1,\hat\beta_2)
$$
To complete this straightforward method, one thing you could use is the variance-covariance matrix of regressors, which I'm guessing you haven't covered yet (if you have this should be straightforward, the covariance you need is just an entry). Alternatively, you can derive $\text{Cov}(\hat\beta_0,\hat\beta_1)$. To derive $\text{Cov}(\hat\beta_0,\hat\beta_1)$, note that:
$$
\hat\beta_1 = \dfrac{\sum_{i=1}^N x_i-\bar x}{\sum_{i=1}^N (x_i-\bar x)^2}y_i
\quad \text{and} \quad
\hat\beta_0 = \frac{1}{n}\sum_{i=1}^N y_i -\hat\beta_1 \bar x
$$
and figure out where you can go from there given the assumptions you've been handed. Unfortunately, you can't assume that $\text{Cov}(\bar y, \hat\beta_1)=0$. $\text{Cov}(\bar y, \hat\beta_1)$ need not be zero, as both are estimates, so their covariance may in general be non-zero. If you can justify why they should be 0, then you're done. One way to approach this would be to think about what would happen if you ran this regression on demeaned data, and go from there to a formal argument.
In the future you'll also learn it's much easier to do this kind of restriction with a Wald test or an $F$ test. This method, once you've used the variance-covariance matrix, is algebraically equivalent to a Wald test and asymptotically equivalent to an $F$ test.
Tricky method may be easier
Right now you're model is:
$$
y_i = \beta_0 + \beta_1 x_i + \epsilon
$$
Under the null, remember that $\beta_1- (h-c_0\beta_0)/c_1=0$. Try manipulating the regression equation, and redefining the variables in your regression by adding and subtracting things to the regression equation that are equal, such that you can test this exact restriction as a $t$ test on a slightly different regression formed with the same data and equivalent to your original model. While you're doing this, remember that $h$, $c_0$ and $c_1$ are known constants, not random variables. Without giving you the answer, that's a trick that could make your life easier.
The method you will arrive at if you use this trick right is also algebraically equivalent to a Wald test.
Answer: Using the Sum of normal distribution, knowing that  $\epsilon_i \sim N(0,1)$
If
$$y_i=\beta_0+\beta_1x_i+\epsilon_i$$
Then given two points,
$$y_i-y_j-\beta_1(x_i-x_j)=\epsilon_i-\epsilon_j$$
Where $\epsilon_i-\epsilon_j \sim N(0,2)$
Thus, you can test if $$y_i-y_j-\beta_1(x_i-x_j)\sim N(0,2)$$


How to compare two elements of a list using testng assert method

Question: <code>@DataProvider(name="StringData")
public Object[][] StringProvider(){
    return new Object[][]{{"This is a real project with real "
            + "code to do real things to solve real "
        + "problems in real world real"}};
}

@Test(dataProvider="StringData")
public void testRepatedStrings(String s){
    List<String> lst = ReadString.RepeatedString(s);
</code>
"lst has two strings. i want to compare both elements of list lst using Assert.
anyone help me how to compare two elements of a list using assert function in testng"
Comment: Hi, what is this ReadString.RepeatedString(s); ? are you loading string twice into list and planning to compare both?
Answer: The list class already has an implementation of equals method.  So list1.equals(list2) will result true if all the elements in the list match
<code>Assert.assertEquals(list1,list2, "Lists don't match");
</code>
will also achieve the same purpose.
Answer: if you want to compare more than one value in assert go with assert true , instead of assertEquals.
Ex :
Assert.assertTrue((x==4) || (x==5)),"Unexpected Error");
in the above example, it will be Pass if the value of x is either 4 or 5 else gives unexpected Error


Carácter estranho no lugar do diretorio

Question: Quando executo esse código no lugar onde que era para aparecer o diretório aparece Ó²o\formula.exe, alguém poderia me ajudar a resolver ?
<code>#include <iostream>
#include <Windows.h>
#include <lmcons.h>
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <string>

using namespace std;

string teste() {
    char buffer[MAX_PATH];
    GetModuleFileName( NULL, buffer, MAX_PATH );
    string::size_type pos = string( buffer ).find_last_of( "\\/" );
    return string( buffer ).substr( 0, pos);
}

int main(){
        TCHAR teste[MAX_PATH];
        cout << teste << endl;

        string cmd = "reg add HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Run /t REG_SZ /v formula /d ";
        cmd += teste;
        cmd += "\\formula.exe";

       cout << cmd.c_str() << endl;

        system(cmd.c_str());

}
</code>
Comment: Onde é que o conteúdo de `TCHAR teste[MAX_PATH];` é inicializado?
Comment: Você queria mesmo `cmd += teste;` ou deveria ter sido `cmd += teste();`? Ter duas coisas distintas com o nome `teste` é pedir para ter confusão.
Comment: Conseguir resolver da seguinte maneira
Answer: <code>#include <iostream>
#include <Windows.h>
#include <lmcons.h>
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <string>

using namespace std;

string dir() {
    char buffer[MAX_PATH];
    GetModuleFileName( NULL, buffer, MAX_PATH );
    string::size_type pos = string( buffer ).find_last_of( "\\/" );
    return string( buffer ).substr( 0, pos);
}

int main(){

cout << dir() << endl;
        string cmd = "reg add HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Run /t REG_SZ /v formula /d ";
        cmd += dir();
        cmd += "\\formula.exe";

        cout << cmd.c_str() << endl;

        system(cmd.c_str());

    return 0;
</code>

Carácter estranho no lugar do diretorio


Execução de procedure com argumento do tipo data em uma dblink

Question: ao executar a procedure abaixo, via DBLINK no Oracle
corporativo..UP_COP025_ASS_DMANDA_JURID_I 17444968, 1, "1", "20150806", "RJ", " ", " ", "000020"

recebo o erro:

erro: Syntax error during implicit conversion of VARCHAR value
  '"20150806"' to a DATE field.

Devo transcrever o comando da procedure, substituindo "(aspas) por plics (')?
corporativo..UP_COP025_ASS_DMANDA_JURID_I 17444968, 1, '1', '20150806', 'RJ', ' ', ' ', '000020'

Abraços,
Answer: Como se tratava de uma dificuldade de um fornecedor, eu quis compartilhar a dúvida antes de apresentar a sugestão que eu achava mais correta.
Pois bem, a chamada no Sybase funciona plenamente neste formato:
corporativo..UP_COP025_ASS_DMANDA_JURID_I 17444968, 1, "1", "20150806", "RJ", " ", " ", "000020"
Onde o campo "20150806" é um tipo data na assinatura da procedure.
Se for chamar do mesmo modo no Oracle, através de uma DBlink dá erro neste campo data, com a mensagem Syntax error during implicit conversion of VARCHAR value '"20150806"' to a DATE field.
Sugeri, que ao invés de usar aspas duplas neste campo "simulando" uma data, usar aspas simples ('). Me recordava de algo assim na minha época de oracle.
O fornecedor implementou a sugestão e funcionou sem problemas.
Ficando assim:
corporativo..UP_COP025_ASS_DMANDA_JURID_I 17444968, 1, "1", '20150806', "RJ", " ", " ", "000020"
Abraços, e obrigado.
Answer: Teria que saber a estrutura da sua Procedure, e da tabela, mas tente adicionar o comando to_date, pois assim você converte sua string em date:
corporativo..UP_COP025_ASS_DMANDA_JURID_I 17444968, 1, '1', to_date('20150806','yyyymmdd'), 'RJ', ' ', ' ', '000020'


Stratify a train / test split according to some categorical variables

Question: I would like to train / test split a dataset in such a way that all categories of categorical variables are in both train and test split.
I tried ( using sickit learn ) :
df_moto_train , df_moto_test = train_test_split( df_moto , test_size = 0.15 , stratify = df_moto[ cols_obj ] )

( where cols_obj is a list of categorical variables from the dataframe df_moto )
but I got the message :
ValueError: The least populated class in y has only 1 member, which is too few. The minimum number of groups for any class cannot be less than 2.

Thanks.
Answer: The error massage says it all: in some subgroups you have insufficient data. You need to gather more data or split it differently.
Comment: Right : one combination of categories has only one record. The more categorical variables you stratify on, the more you could meet that case. I wonder if there is a way to collapse categories such as to ensure stratification is possible.


IsWebSocketRequests not working in mono

Question: i connected to both HTTP and webSockect in .NET on windows system,its working fine then i moved the same project to mono to test on linux system using Mono but it giving me a false case that is webSocket connection IsWebSocketRequest is becoming false
<code> if (httpListenerContext.Request.IsWebSocketRequest==true)
 {
   //some logic
 }
</code>
Answer: <code> httpListener.Prefixes.Add("http://127.0.0.1:8080/");
 httpListener.Start();
</code>
I tried in this way it worked for me

IsWebSocketRequests not working in mono


Reference for the bounded linear transformation theorem?

Question: I was checking nice theorems on wikipedia and I stumble on this one (BLT theorem): https://en.wikipedia.org/wiki/Continuous_linear_extension
It says that every bounded linear transformation $T$ from a normed vector space $V$ to a complete, normed vector space $W$ can be uniquely extended to a bounded linear transformation ${\tilde T}$ from $\bar V$, the closure of $V$, to $W$.
The article cites Reed's book, but there is no BLT theorem in this one. I usually check the proofs after the wikipedia page, but I cannot find any trace of this theorem anywhere. Does someone know a reference for this ?
Thank you
Answer: This result is true in a more general context:

Theorem. Let $X$ and $Y$ be metric spaces, $A$ a dense subset of  $X$ and $f\colon A\rightarrow Y$ a uniformly continuous map. If $Y$ is complete, then there exists a unique continuous map that extends $f$ to $X$.

Sketch of a proof. Let $a\in A$, there exists $(a_n)_n$ a sequence of elements of $A$ converging to $a$, then $(f(a_n))_n$ is a Cauchy sequence in $Y$, which is complete, so that it converges to a point $y\in Y$. The key point is that $y$ does not depend on the sequence $(a_n)_n$ but just on $a$, so that $\widetilde{f}(a):=y$ is a valid definition. The rest of the proof consists in seeing that $\widetilde{f}$ is (uniformly) continuous, which should be clear.
The uniqueness part follows from the fact that two continuous map agreeing on a dense subset are equal. $\Box$
A continuous linear map is Lipschitz so that it is uniformly continuous and the above result can be applied, notice that the process used to defined $\widetilde{f}$ is linear.
Comment: Completeness and uniform continuity aren't topological notions. You need *uniformities* to define them. Moreover, in this generality you cannot argue with sequences.
Comment: @Jochen I assumed it was clear that when I say that $f$ is uniformly continuous, then both $X$ and $Y$ are endowed with metrics because as you said, this makes no sense otherwise. Anyways, I edited to make it explicit!
Answer: An Introduction to Banach Space Theory, Robert E. Megginson, page 70.
The crucial step is to justify the well-definedness of the bounded linear operator $\overline{T}:\overline{V}\rightarrow W$ defined by $\overline{T}(v)=\lim_{n\rightarrow\infty}T(v_{n})$ where $(v_{n})\subseteq V$ is such that $v_{n}\rightarrow v$.


What do these brackets mean?

Question: What do the brackets in the image shown below mean?:
Comment: The given answers are surely correct. But could  we also ask what sense it makes a) to notate it b) to put the paranthesis as the left hand has nothing else to fo?
Comment: It makes sense to notate it, when two independent voices converge temporarily on the same pitch.  The notation indicates that the same note "belongs" equally to both voices even if it is only played by one hand.  For example, I have seen the same notation in an edition of Bach two-part inventions (invention #8 in particular)
Answer: My guess (I cannot really see the notes for the upper voice): The same note is already played by your right hand. Both voices share the same note but it obviously only needs to be played once. That's why it is written in brackets for the left hand.
Comment: Yeah it is the same note now that I look at it. Thanks
Answer: Your image is clipped, but I'm guessing the top note is two ledger lines below the staff?
That would make the right hand note A.  The A in parenthesis is telling you it's the same note as the left hand top staff line.  So it's an optional way to play it, using the left hand instead of the right.

What do these brackets mean?