Relationship between cross entropy and average negative log likelihood

Question: I'm trying to understand some machine learning theory background: specifically, the relationship between cross entropy loss and "negative log likelihood".
To start, I already fully understand these definitions:

Entropy of a probability distribution $p$ with $K$ classes:

$$
H(p) = - \sum_{k=1}^{K} p_k \log p_k
$$

Cross entropy between two probability distributions $p$ (ground-truth) and $q$ (predicted):

$$
H(p, q) = - \sum_{k=1}^{K} p_k \log q_k
$$
My specific confusion comes from reading Kevin Murphy's 2021 book "Probabilistic Machine Learning: An Introduction". He says something like this about Kullback-Leibler divergence (it's a paraphrase summarization of sections 4.2 and 6.2):

$KL(p||q) = \sum_{k=1}^{K} p_k \log p_k - \sum_{k=1}^{K} p_k \log q_k$
We recognize the first term as the negative entropy and the second term as the cross entropy. The first term is a constant with respect to our predictions $q$, so we can ignore it.
Let us suppose the $p$ distribution is defined with a delta function $\delta$ like this:
$ p(x) = \frac{1}{N} \sum_{n=1}^{N} \delta(x - x_n)$ .
Then the KL divergence becomes
\begin{align}
KL(p||q) &= -H(p) - \frac{1}{N} \sum_{n=1}^{N} \log q(y_n)\\
         &= constant + NLL
\end{align}
This is called the cross entropy objective, and is equal to the average negative log likelihood of q on the training set.

Questions:

The term $\frac{1}{N} \sum_{n=1}^{N} \log q(y_n)$ mentions one distribution $q$. How can it be a cross-entropy term when cross entropy is defined for two distributions $p$ and $q$?

How does a log-likehood expression in terms of $N$ training instances ($\frac{1}{N} \sum_{n=1}^{N})$ turn into a cross-entropy expression in terms of $K$ classes ($\sum_{k=1}^{K}$)?

Is the author's use of a delta function $\delta$ just another way of saying a one-hot distribution?

I'm still confused even after reading other posts like this one, this one, and this one.
Answer:
This is because of the claim about delta distributions. Now $p_k$ is $0$ for all $k$ but one. Those $p_k \log q_k$ terms in the sum over $k$ can be skipped. For the other, $p_k$ is $1$, so there's no need to write that you multiply by $1$.
I think this is answered in (1). The $0$-valued terms are skipped. In a sense, you have a double sum over $n$ and over $k$, but then the sum over $k$ goes away.
Yes, that's right. "One-hot" is terminology from the area of digital circuits; the delta function is from mathematics.
Comment: @stackoverflowuser2010 perhaps the paper [Why the logistic function? A tutorial discussion on probabilities and neural networks](https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.476.1842&rep=rep1&type=pdf) by Michael I. Jordan, 1995, could help.
Comment: It's equivalent—try taking the log of it.
Comment: Yes, ML borrowed it from circuits. Why bring up delta functions in an ML book? Because probability is mathematical, and it's the focus of the book! Which delta depends on the data but is a longer discussion answered elsewhere on this site. To your last question: without going a rabbit hole of nuance, yes.
Comment: Don't forget the word "average" in Murphy's description, which you quoted in the original question. It'll fix the discrepancy.
Comment: I've done further reading online (e.g. http://www.awebb.info/probability/2017/05/18/cross-entropy-and-log-likelihood.html), where I've come across loose terminology stating that the likelihood of a training instance is $\prod_{k}^{K} \hat{y_{k}} ^{y_k}$, where $K$ is the number of classes, $\hat{y}$ is the predicted probably, and $y$ is the one-hot true probability. I have not seen that formulation anywhere in any text book. Is it standard, and if so, do you  know of a good reference?
Comment: Thank you. For 3, I've only seen the term "one-hot" in ML books and websites. Why do you think this author would bring up delta functions in a book about machine learning? And *which* delta function? I know of the Dirac and Kroneker delta functions. Does it matter?
Comment: For 1, I know that to compute total cross-entropy loss over a data set, you compute the mean cross-entropy loss over each training instances, like $H_{total}(P, Q) = -\frac{1}{N} \sum_{n=1}^{N} \sum_{k=1}^{K} p_{k,n} \log q_{k,n}$ for class $k$ and training instance $n$. So the relation between cross-entropy and negative log likelihood is that the true distribution $p$ is implicitly one-hot, so you can remove the summation over $K$ ? Is that all there is???
Comment: For 1, can you please point me to an understandable reference that explains whatever nuance I'm missing?
Comment: "Understandable" and "nuance" tend to be opposed to each other; the details are more academic than practical. It's enough if you're reading an intro book to take the answer as "yes".
Comment: @stackoverflowuser2010 Was your question answered? If not, what can be clarified?
Comment: @AryaMcCarthy: I understand that computing the MLE is equivalent to finding the `argmin` of the negative log likelihood (NLL), which is equivalent to minimizing the cross-entropy loss. But I find there's a discrepancy with a $\frac{1}{N}$ term, where $N$ is the number of training instances. NLL is defined as $-\sum_{i}^{N} \log P(y_i | x_i)$, but total cross entropy is defined as $- \frac{1}{N} \sum_{i}^{N} \log q_i = - \frac{1}{N} \sum_{i}^{N} \log P(y_i | x_i)$. Why does cross-entropy loss have that $\frac{1}{N}$ term (for computing the mean), while NLL does not? How can they be equivalent?
Comment: @AryaMcCarthy Thanks.


How to perform a $and search with Python and MongoDB

Question: Let's say I have a list of keywords and I want to match against MongoDB documents in which they all appear in the keys field (that is, a AND clause).
How can I do that in Python?
I've tried:
keywords = ['a', 'b', 'c']
keywords_dict = {'keys' : keywords}
results = collection.find(keywords_dict)

But it seems to return no result.
I'm using Python3.5 with PyMongo.
Any hints?
Comment: I've figured it out: I need to make a list of dictionaries, e.g. `{ '$and' : [ {'keys' : key1} , {'keys' : key2} ] }` - any hints on other ways to do this is welcome!
Answer: You can use the $all operator which is also equivalent to an $and operation of the specified values; i.e. the following statement:
{ 'keys': { '$all': ['a', 'b', 'c'] } }

is equivalent to:
{ '$and': [ { 'key': a }, { 'key': b }, { 'key': c} ] }


Using @property and @synthesize with ivar implicit creation

Question: Ok, so lately I got this presumable bad habit of declaring my class properties with @property create getters and setters with @synthesize and simply use them anywhere I need them as self.name without having a reference to the inner ivar (let @synthesize do its work)
Now, of course this doesn't allow me to access the inner ivar (eg. name_) but from what I've coded so far, I didn't really need it. For read-only properties I don't use @synthesize but implement the getter myself.
Everything seems fine, but somehow I've got the feeling that this is not right because all the open source libraries I've had a look over, also declared the ivar and used it throughout the code. @property + @synthesize and no ivar its definitely the lazy choice, but what are the drawbacks? Can someone give me some advice?
Also, I've read that, as a general advice, it's OK to use self.propertyName anywhere in your class code except the dealloc and init methods. But as long as you make sure the object is initialize:
-(id) init
{
    if( (self=[super init] )) {

    }
    return self;
}

and you remove all key-value observers before calling [super dealloc] everything should be fine. Right?
Comment: If you mark the property as readonly, synthesize will only generate a getter. There is nothing wrong with being lazy as long as the behaviour is correct.
Answer: well the @synthesize will create the inner ivar with the same name as the property:
@synthesize name;

- (void) dealloc {
   [name release], name  = nil;
   [super dealloc]
}

Or give the inner ivar an other name:
@synthesize name = _name;

- (void) dealloc {
   [_name release], _name  = nil;
   [super dealloc]
}

I use the self.name in the init but not in the dealloc and seems to work great.
Comment: @Jano I don't know if your comment is directed at me or at @rckoenes, but I don't follow it either way.  Not setting ivars to nil in `-dealloc` is not generally going to lead to frequent crashes, unless you are in the habit of referencing your ivars in that method after you release them.
Comment: @Seamus Well, bugs shouldn't happen but they do. After much discussion, some nil http://zathras.de/blog-defensive-coding-in-objective-c.htm and others don't http://www.red-sweater.com/blog/1423/dont-coddle-your-code I do both with a release macro.
Comment: Upvoted, but there's really no reason to set your ivars to nil in `-dealloc`.
Comment: For iOS that could mean waiting for week(s) until your new version is uploaded, and getting horrible ratings. And it requires having a crash report system. I'm not sure if crashing early and often in production is a good idea. It also depends on the type of app I guess.
Answer: It comes down to which version of the Objective-c runtime you are building for: Modern or Legacy.

The Modern version supports "instance variable synthesis for declared properties".  ie. there is no need for you to define an instance variable for each @property. The runtime will take care of creating one for you.  By default the ivar will be named the same as the @property.  You can change the ivar name by using the format @synthesize propertyName = iVarName;
The Legacy version does not support ivar synthesis and therefore you must define an ivar for storage of your @property.

iPhone applications and 64-bit programs on Mac OS X v10.5 and later use the modern version of the runtime.  You should assume that every other platform uses the legacy version.
What are the drawbacks?  Well, the biggest one is portability - By not defining the ivars your code can run on fewer platforms e.g. it won't work on OSX 10.4.  You also lose some control/flexibility & might inadvertently run into name collisions with sub- & super- classes.
It's important to remember that you should deallocate properties that you have synthesized with @synthesize.  Properties are not automatically released - you should release any synthesized property that is not marked assign (don't release any properties marked assign).
N.B. For extra credit you can tidy up your read-only properties.  There's little reason to write your own accessor and not use @synthesize.  Simply define your property as read-only & optionally what to call the getter @property(getter=isIntReadOnlyGetter, readonly)
Comment: If your property is readonly, why would you declare the getter explicitly?  Is is just so that the ivar and the getter have different names?
Comment: There are a few reasons to set the name of the getter to be different from the ivar or property name. One of the most common is readability.  Say you have an instance variable that stores whether or not an object is `BOOL green`.  Rather than the getter just being `- (BOOL) green` it is more understandable if use use `- (BOOL) isGreen`.
Comment: I understand why you might have getters and setters with different names; I'm asking about the case where you have only a getter, because you have declared you property to be readonly -- the "property name" is then not actually useful as a property name.  If your purpose is just as a way to name your ivar, I can understand, though personally I would be inclined to say `@property (..., readonly) BOOL isGreen` and `@synthesize isGreen=green`.


Verificação de url's - Django

Question: Ao aprender a desenvolver aplicações utilizando Django é comum chegar a seguinte URL:
<code>locahost:8000/minhaentidade/deletar/id
</code>
Porém um usuário mal intencionado pode alterar esse id e acabar deletando outra entidade. Sabendo disso, como posso verificar se os parâmetros da minha URL foram alterados? O django já possui alguma ferramenta para lidar com a situação? Ou existe alguma abordagem melhor para lidar com a situação?
Answer: A abordagem mais correta para lidar com a situação é verificar se o usuário tem permissão para deletar o objeto antes de fazê-lo. Para isso, você precisa de um sistema de autenticação e de controle de acesso. O próprio Django fornece mecanismos prontos pra isso, mas também existem diversos outros apps de terceiros que implementam mecanismos mais complexos. As palavras chaves que você deve procurar, associada com Django, são: authentication backend, permissions, object permissions.
Sua preocupação com esse controle de acesso é um sinal que você está com as preocupações corretas. Mas, a ideia de controlar as URLs não é uma boa ideia. Lembre-se que o usuário sempre pode entrar manualmente com as URLs, sem ter recebido ela da sua aplicação.
Em programação WEB você precisa considerar que o usuário sempre tem controle total das requisições feitas a sua aplicação e que ele não é limitado pelo o que é exibido pra ele.

Verificação de url's - Django


cakephp 1.3 routing question

Question: I'm make a static/dynamic mixed site.

if a user accesses to xxx.com/about, I want to use pages controller to show about.ctp.
it seems it's done like /pages/about but if i want to hide /pages, do I have to write like:

Router::connect('/about', array('controller' => 'pages', 'action' => 'display', 'about'));
for all static pages? or any better way?

some static pages may be hierarchical. like /about/history, /about/address, ... I don't know what the easiest way is for maintenance later, like whether i should make a controller w/o model for each static directory or use page controller and keep all files in views/pages/. any advice?
Answer: If you don't mind having a .html extension on the end of your urls and have rewrite control over your server (mod_rewrite), you can add a rewrite rule that will directly serve your static content and totally bypass Cake. This could be the simplest for you, since all you have to do is create the static content somewhere under webroot/
For example, you could add the following for lighttpd (making sure that you have the mod_rewrite module loaded):
url.rewrite-once = (
    "^/.*\.html$"  => "$0"
)

Which translates to: take every request ending in .html (such as "/page.html" or "example/page.html") and serve it unmangled. The $0 is a keyword for the original request.
Or, for Apache (either in your config or in the app/webroot/.htaccess) make sure your rewrite rules start with these two, which say to serve any existing files or directories:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

Note that this rule has to come before your CakePHP rewrite rule in order to short-circuit it and serve the pages first, or else CakePHP routing will take over!

cakephp 1.3 routing question


Comparison of total counts vs. comparison of mean counts per participant in exploratory data analysis

Question: I already came across this question: Difference in means vs difference in proportions, but could not answer my question based on the answers and comments there.
My question would be this: Suppose there are two groups, A and B, whose participants are asked to write texts. Let's say the participants of group A have produced a total of $90,000$ of characters and the participants of group B have produced $100,000$. I also know the number of characters per participant. Now I want to compare the results regarding the number of characters between the groups. Two things are not clear to me:
(1) What would, in this case, a binomial test check with an assumed probability of $0.5$ (and a present probability of $90,000 / 190,000 = 0.47$)?
(2) In comparison, what would a $t$-test check based on the individual character counts of the participants?
Possibly, Fisher's exact test or a $\chi^2$-squared test could take the place of the binomial test. Still, I wonder from what different "perspectives" the data are being viewed here, when either the total counts or the mean counts per participant are compared.
Comment: what do you mean by frequency here? And what's your end goal? to test whether there is a significant difference between the two groups in terms of characters generated per person?
Comment: Thank you for checking back and sorry for the imprecise wording. By frequency, I mean counts. My end goal would be to test if there is a significant difference between the two groups in terms of characters generated in general, so to speak. And I wonder if both tests would be useful, and if so, how they would have to be interpreted. I realize that the binomial test would look more at the ratio of total counts and the t-test would look at means and variances, but I'm not clear under what circumstances one test would be preferable to the other.
Comment: I should perhaps mention that I am conducting an exploratory data analysis, not a hypothesis testing study.
Comment: If you find a significant difference between the two groups in terms of characters generated per person that directly translates into a significant difference between the groups in terms of characters generated in general when the groups have the same sample size. Binomial test does not make sense to me at all in your problem (unless I misunderstood your question).
Comment: Thank you so much for highlighting that a binomial test is not useful at this point! Which I now fully realize after some thought. I was completely on the wrong track. Thanks also for clarifying that differences in characters generated per person also mean group differences, given equal group sizes.
Comment: Let me also briefly explain how my understanding that a binomial test is not useful came about (which I hope makes sense): For one thing, it is probably hard to regard the generation of a single word as a random event, at least if the subjects' task is, say, to formulate sentences. For another, multiple unbalanced measurements of most persons would be present, if one considers the generation of a word (yes/no) as one measurement. I really hope that this is not an overly fanciful explanation. I am still in an early learning phase concerning statistics. Thanks again for responding!
Answer: If I understand correctly, the "binomial test" is simply examining whether both groups have the same probability of producing a character. The way you stated the test, there is no accounting for differences among individuals within each group. You could have 1 individual in each group or 10,000 in each group and would get the same result.
The t-test takes the variance among individuals within a group into account. That should be more generally useful than just analyzing the overall number of characters per group.
Comment: Thank you very much for your reply and highlighting that a $t$-test is generally more useful here! Through your explanations and also through your highlighting ("binomial test", _groups_) as well as through @Amin's comments I now understand that actually only a $t$-test, in any case not a binomial test, comes into question in this case.


how to view data row table related to user only?

Question: Good day, I need to create a view, which only shows data related to the user logged in user.
When I have a user called "john smith", I create a contents as I like, BUT I don't want other users to see them.
A content can have multiple input from different users, so ONLY admin can see all content.
while, each user, can only see his OWN content which he posted him self.
Think of it like a database system, where each student can add projects for his own records, but each student can see ONLY his own.
I tried looking around first, and I came across this question https://stackoverflow.com/questions/20840490/drupal7-filter-view-based-on-current-users-roles which seemed bit related, but I got lost in the too much explanation because they not entirely related to what I want to achieve.
In user roles permission, I perfectly found (add content, edit own content) but its missing one more thing which I want.."view own content"!!
I think it has something to do with filed permission, or something like when adding a content, set its permission to be view my that user only.
In coding the view to show ONLY table rows related to the that user, maybe something like this:
global $user;
if (in_array (student_field_name  == $user))
//view the row related to that user;

if (in_array('administrator', $user->uid))
//show ALL data rows available

To summarize, I want the user to be able to SEE his own data only.
while admin will be able to see ALL content view of that page.
Comment: So add a filter for the `author` field of a Node is equal to the logged in user. The `author` created the content -- nobody else.
Comment: hmm, i see..maks sense..im newbie in drupal actually, so im not quite sure how its done..can u guide me on how its done? do i go to contextual filter? from there i only find "tracker - user" then , choosing tracker -user ID..which seems to be the one..after that do i choose default value and insert php code to suite the condition? (im not sure wht to type there to match the values together as condition, like if $user == field name then excute or something like that) thanks
Answer: To create a view that is limited to content created by the current user (logged in) and Admin:

create the view as you normally would
Under Advanced, Relationship, create a relationship to Content:Author
Create a Filter, User:Current, Logged in Yes, using the author relationship
Create a filter, Author ID = admin
Create a Filter Group (click on arrow next to Add on Filter Criteria)
'Create a new filter group' and make it look something like this:

This will limit the view to content only created by the currently logged in user and/or Admin, but does not limit access to the nodes themselves, for that you will need a module such as Node Access:

Nodeaccess is a Drupal access control module which provides view, edit
  and delete access to nodes. Users with the 'grant node permissions'
  permission will have a grant tab on node pages which allows them to
  grant access to that node by user or role. Administrators can set
  default access controls per content type, and also define which roles
  are available to grant permissions to on the node grants tab.
Comment: God bless u man..it worked, this is what i was looking for.
but i have 1 problem..the administrator, which is supposed to see everything now cannot see the contents created by other users!! how can i add this to the current filter to allow admin to see everything?
can the code access module do that for me?
the module is new to me actually, so i'll test to check how it works.
Comment: for NodeAccess you should be able to allow the admin to see everything (actually an Admin should have bypass access control permission set which would do that as well). For the View it gets more complicated - you will need to create a new View without the Logged-in user restriction and limit it's Access to Admin only under Page Settings or Block Settings (center top of the Views UI)
Comment: with your suggested filter and node access.. i stil couldnt get it done! i've spent long hours trying to figure out why is it not viewing everything for admin while limitting the non admin..its like it either all of them get self access only, or all get to see all nodes..
worst case is creating 2 views 1 for admin and one for the nons..but its trouble some cuz i have 10s of contents to edit their views and make them two..is there  any alternative way to get the job done? i may have missed something..do u know specific methods to make admin see all content while non admin see their owns? thanks
Comment: @AbdulazizThedragon see my edit
Comment: thank u for ur effort but no success bro. just like the previous method, it doesn't show ALL nodes of that content!! it seems that once i add the filter content author or the current, it restricts the nodes to ones created by that user, even for admin :( "knowing that admin is supposed to have full access whether given perm or not" i even checked my permissions (i had previously checked everything), but noticed that i mistakenly chose the ones with "own"content i removed everything related to "own", but i still have same result..no change, still cant get admin to see all existing nodes.
Comment: well, I tested it and it works on a dev site of mine, so there's something else going on that is interfering with the normal functionality on your site
Comment: hmm, since u r sure..i guess there's no other way to explain this.
i'll post a new question for this matter..again, thank you for your time and advice. :)
Answer: In the View, add a contextual filter on Author: Uid, and use the following settings:
WHEN THE FILTER VALUE IS NOT AVAILABLE:
Provide default value
Type: PHP Code
Use the following code:
global $user;

if (
    in_array('administrator', $user->roles) ||
    in_array('editor', $user->roles)
)
{return -1;}
else
{return $user->uid;}

WHEN THE FILTER VALUE IS AVAILABLE OR A DEFAULT IS PROVIDED
Specify validation criteria
Validator: PHP Code
Use the following code:
if ($argument == -1)
{return FALSE;}
else
{return TRUE;}

Action to take if filter value does not validate: Display all results for the specified field (this is what will give admins access to all results)
And that's it!
Notes:
Compared to my initial settings, the relationship (Content: Author) isn't needed anymore; neither is the "Author" filter (which was brought in by the relationship anyway).
Apparently, for Drupal 6 the condition in the first PHP snippet should rather be if (in_array('super user', array_values($user->roles))).
Comment: i saw this answer..u merely copied it.. but my concern is the user ID rather than the role..because the data fields i want to view should be based on user name. thats why i said i feel lost..Please refer to my question, i have added some extra info im looking for which are related to row, or content. thanks


Connect Linode servers to a disk on another server

Question: I have three servers with Linode: A DB server and two web servers running Wordpress. The DB server also has a disk image called 'Shared Media', which I'd like to be accessible from the web servers (they're in the same data center).
How do I go about this?
Answer: NFS via the private linode network. Simple, easy to set up, no fuss.


To Use Django-Haystack or not?

Question: So this might be an obvious answer to some but I'm not sure what the right answer is. I have a simple donation application where Donor objects get created through a form. A feature to be added is to allow of a search for each Donor by last name and or phone number.
Is this a good case to use django-haystack or should I just create my own filters? The problem I may see with haystack is that a few donations are being submitted every minute so could indexing be a problem? There are currently around 130,000 records and growing. I have started to implement haystack but have realized it might not be necessary?
Answer: Don't use haystack -- that's for fast full-text search when the underlying relational database can't handle it easily.  The use case for haystack is when you store many large documents with huge chunks of text that you want indexed by words in the document so you can easily search.
Django by default already allows you to easily index/search text records.  For example, using the admin backend simply specify search fields and you can easily search for name or telephone number.  (And it will generally do case insensitive contains searches -- this will find partial matches; e.g., the name "John Doe" will come up if you search for just "doe" or "ohn").
So if your models.py has:
class Donor(models.Model):
    name = models.CharField(max_length=50)
    phone = models.CharField(max_length=15)

and an admin.py with:
from django.contrib import admin
from mysite.myapp.models import Donor

class DonorAdmin(admin.ModelAdmin):
    model = Donor
    search_fields = ['name', 'phone']

admin.site.register(Donor, DonorAdmin)

it should work fine.  If an improvement is needed consider adding an full-text index to the underlying RDBMS.  For example, with postgres you can create either a text search indexes post 8.3 with a one liner in the underlying database, which django should automatically use: http://www.postgresql.org/docs/8.3/static/textsearch-indexes.html
Comment: Thanks! That seems like the more logical way to go. Right now users are using admin to search for donors but I've heard that it's not a great idea to allow a large number of searches to be executed there as it could lead to performance issues. The main reason why I want the search is because I need to allow a "Same donor" feature where if makes another donation with the same information (address,name,phone,etc) with just a different pickup date.After the search I want them to be able to click the donor and bring up the donation form with prepopulated info. Also why I also hesitate to use admin

To Use Django-Haystack or not?


Combinatorics: Sitting Order Thought Process

Question: Let's say we have a group of 7 children and 13 adults (each one is unique and can be picked once), and 20 chairs(each one is unique and can be picked once).
Question 1)
How to find the number of ways the group can be seated, if at least one adult must be seated between two children?
Question 2)
How to find the number of ways the group can be seated, if the children all sit next to each other?
Question 3)
How to find the number of ways the children can sit, if none of the adults sits?
Now, to answer Question 1, i thought of this: If A is an adult and C is a child, we can have either the pattern ACA, the pattern CAA or the pattern AAC.
So, we have $C_3(20) = \frac{20!}{(20-3)!} = \frac{20!}{17!} = 20\cdot19\cdot18 = 6840$  ways for each pattern, so in total we got $6840 \cdot 3 = 20520$ combinations.
To answer Question 2, i thought of this: the children can sit in $ P_7 = 7!$ different ways, and the adults can sit in $P_{13} = 13!$ different ways, so we got a total of $P_7 + P_{13} = 7! + 13!$ different ways in which they can sit.
To answer Question 3, i thought of this: we have $C_7(20) = \frac{20!}{(20-7)!} = \frac{20!}{13!}$ different ways the children can sit.
Am I getting this right?
Answer: For 2, you should multiply the $7!$ and $13!$ because for each order the children sit in you have $13!$ orders that the adults can sit in.  You also have to multiply by $14$ for the leftmost seat occupied by a child because sitting the children in seats $1-7$ is different from sitting them in seats $3-9$
For 3, your answer is the number of ways to choose the $7$ seats the children sit in, but does not account for the assignment of children to seats.  You should multiply by $7!$
For 1, we have to find the number of ways to choose seats for the children, then multiply by $7!$ and $13!$ as in 2.  It is easier to imagine a $21^{st}$ seat filled by an adult.  Then we can attach an $A$ to the right of each $C$ and have $14$ things to put in $14$ seats.  $7$ of them are $A$s and $7$ of them are pairs of $CA$.  We can choose seats for the $CA$s in $14 \choose 7$ ways.
Comment: That was a typo.  It should have been $7!$ for the orders of the children.  Fixed
Comment: No, it would just be $7!\cdot 13!$ because you can always rotate the table so the most clockwise child is in seat $1$.
Comment: Thank you for the response. For q3, why multiply by 71 specifically?
Comment: Oh I see.  Thanks again
Comment: I have another question for 2, if the chairs where in a circle, would the number of possible orders be $7!\cdot13!\cdot(\frac{13!}{7!\cdot6!})$ ?


O que é segmentation fault?

Question: Esse erro ocorre com frequência em códigos com problemas. Não vejo ele ocorrendo em outras linguagens. Por que ele ocorre? O que significa?
Comment: @WallaceMaxters provavelmente é erro do PHP :)
Comment: Sempre me perguntei sobre isso, já vi acontecendo esse erro em PHP quando você roda um código na linha de comando que gera uma recursão infinita.
Answer: Isso ocorre por acessar um ponto da memória não autorizado para sua aplicação.
Quando sua aplicação está rodando, o sistema operacional libera acesso à memória virtual. Ao contrário do que muita gente pensa, você não tem acesso à memória física (RAM) diretamente, na verdade pode ser que nem esteja na RAM. É um problema do sistema operacional colocar onde quiser, ele organiza a memória como quiser. Ele colocará seu código, área estática de dados, a pilha e depois vai alocando outros trechos dinamicamente conforme a necessidade, o heap. A memória física nem costuma estar em sequência, mas parece estar. Quando sua aplicação acessa parece que a memória toda do computador é dela, por isso é chamada de memória virtual, os endereços não são os físicos. Claro que a memória precisa estar reservada para seu uso.
Então qualquer endereço que seja acessado pela aplicação e não estava reservada é considerado um endereço inválido. Assim há uma proteção às outras aplicações. A memória é dividida em segmentos, então a tentativa de acessar um segmento que não lhe pertence causa uma falha de segmentação.
Em C ou C++ é comum ter este tipo de problema porque o uso dos ponteiros é muito livre e direto. É relativamente fácil fazer uma variável ponteiro ter um valor inválido. Principalmente por esquecer de inicializar o ponteiro, mas também:

por fazer uma aritmética errada ou pegar valor aleatório (wild pointer),
tratar um outro dado como se fosse ponteiro,
usar um endereço que era existente e não é mais (dangling pointer),
stack overflow,
acessar endereço além de uma sequencia (array), que em outras linguagens é o out of range, especialmente em string sem terminador,
modificar área estática, especialmente literais strings,
entre outros.

É muito comum ocorrer um segmentation fault por causa de um comportamento indefinido deixado no código.
Mas sabe qual é o endereço que mais causa esse problema? É o 0, ou seja, o nulo. Nesse ponto é igual à exceção Null Pointer ou Null Reference de outras linguagens. Essas linguagens não são tão liberais com o uso de ponteiros, costumam gerenciar o acesso à memória, mas costumam permitir que um objeto possa ser nulo. Linguagens mais modernas estão evitando isso e só deixando o nulo ocorrer por escolha do programador onde faça sentido.
Hoje os compiladores possuem opções para ajudar identificar potenciais problemas de ponteiros e nem deixam fazer operações que criarão problemas. Existem ferramentas extras de análise estática ou dinâmica que ajudam ainda mais.
Aparecendo esse erro já comece ver seus ponteiros, o problema está em um deles.
Coloquei no GitHub para referência futura.

O que é segmentation fault?


Why is it more useful to use inverse normal for qq plot?

Question: Just ran this command qnorm in Stata to plot a qqplot of my data.
Y Axis = my observations
X Axis = quantiles of Normal distribution

However, Stata uses "inverse normal" as the X axis of the qqplot. Why is it more useful to use the inverse normal than the normal itself?
Comment: Note the command is `qnorm` not `qqnorm` as first posted.
Answer: A small misunderstanding here: "inverse normal" in this context is just jargon for selected quantiles from the quantile function.
Fuller wording would be "inverse of the normal cumulative distribution function".
The first relevant line of the associated help file (accessible to all via https://www.stata.com/help.cgi?qnorm) gives a definition in other terms:

qnorm plots the quantiles of varname against the quantiles of the
  normal
      distribution (Q-Q plot).

In general, as you say, a quantile-quantile plot necessarily plots quantiles versus quantiles.
I am not clear what else you're imagining that "inverse normal" could mean, however.
Comment: (+1) Possibly the OP had https://en.wikipedia.org/wiki/Inverse_Gaussian_distribution in mind.
Comment: @whuber Good point, although I don't recall that distribution being called "inverse normal". Anything is possible....


list of controls for each form

Question: iam using vb.net 2008 ,i have 2 listboxes, one is for listing all forms name in my project and works fine, but what i need is when i click in the forms list, the other listbox should show me all the controls in the selected form name in listbox1! i have been trying and no luck.please help. this the code for listing all forms
Dim myAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
        Dim types As Type() = myAssembly.GetTypes()
        For Each myType As Object In types

            If myType.BaseType.FullName.ToString.ToUpper = "SYSTEM.WINDOWS.FORMS.FORM" Then
                ListBox1.Items.Add(myType.Name)
            End If
        Next

and this what i was trying in list2
 Dim f As New Form
        f.Name = ListBox1.SelectedItem.ToString
        For Each c As Control In f.Controls
            ListBox2.Items.Add(c.Name)
        Next

Comment: This is dodgy: `If myType.BaseType.FullName.ToString.ToUpper = "SYSTEM.WINDOWS.FORMS.FORM" Then`.  It would be far better to use this: `If myType.BaseType Is GetType(Form) Then`. Using `Strings` to do things other than work with text should always be your last resort.
Comment: As for the question, creating an instance of the `Form` class and changing its `Name` doesn't magically make it a different type.  You have to actually create an instance of that type in the first place.
Comment: Also, that would exclude forms that are derived from some type other than `Form`.  It would be better still to use this: `If GetType(Form).IsAssignableFrom(myType) Then`.
Comment: Dim ddb As String = ListBox1.SelectedItem
        Dim x As String = System.Reflection.Assembly.GetExecutingAssembly.GetName.Name()
        Dim ProjAndForm As String = x + "." + ddb
        Dim objType As Type = Type.[GetType](ProjAndForm)
        Dim objForm As Control = DirectCast(Activator.CreateInstance(objType), Control)
Comment: `Dim f As New Form` should create an instance of a generic blank form, not whatever form you want to poll
Answer: OK! After several hours hunting - I hope this works in vb2008. It works in vb2015, but let's hope! The code to get a list of all forms came from another source. My problem in finding an answer has always resulted in a error in runtime casting- but finally this seems to work. Hopefully, it works for you as well.
Public Class Form1
    Dim allforms() As Form
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        allforms = (From t As Type In Me.GetType().Assembly.GetTypes()
                    Where t.BaseType Is GetType(Form)
                    Let f = DirectCast(Activator.CreateInstance(t), Form)
                    Select f).ToArray
        For Each frm As Form In allForms
            ListBox1.Items.Add(frm.Name)
        Next
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        For Each c As Control In allforms(ListBox1.SelectedIndex).Controls
            ListBox2.Items.Add(c.Name)
        Next
    End Sub
End Class

Comment: You might need to add Imports System.Linq

list of controls for each form


Missing file extension "tsx" for "./reducer" error after using eslint-config-airbnb

Question: I am using eslint and prettier code formatters for my project. After using airbnb I am getting errors like

Missing file extension "tsx" for "./reducer"
Missing file extension "tsx" for "./saga"

reducer has index.tsx and so does saga folder.
I have tried import/extensions: [".js", ".jsx", ".tsx"] in my .eslintrc.yml file but still getting the error.
Answer: eslint-config-airbnb has this rule enabled:
    'import/extensions': ['error', 'ignorePackages', {
      js: 'never',
      mjs: 'never',
      jsx: 'never',
    }],

See here.
If want to turn it off, you could override it in your own eslint config file like so:
'import/extensions': 'off'

More information on how you could configure this specific rule can be found here
Comment: Could you add that to your original question with the formatting applied?

Anyway, the `import/extensions: off` should be put under `rules`.
Comment: Thanks for your reply. I tried setitng 'import/extensions' off and tsx to off too but still the error persists
Here is my .eslintrc.yml file content
parserOptions:
  ecmaFeatures:
    jsx: true
  ecmaVersion: 2018
  sourceType: module
parser: babel-eslint
plugins:
  - react
  - prettier
settings:
  import/parsers:
    "@typescript-eslint/parser": [".ts", ".tsx"]
  react:
    version: detect
  import/extensions: ["error", "always", { "tsx": "never", "ts": "never" }]
  import/resolver:
    node:
      paths: ["src"]
      extensions: [".js", ".jsx", ".tsx"]
rules:
  {...}
Comment: Thank you so much Jackyef. At most of the places import/extensions are under settings and not rules. I was also doing the same mistake. You saved my day.
Thanks


Number of hours per day when the database is running

Question: In the calculator, I can select the number of hours per day when the database is running.
But when creating Cloud SQL, there is no option to select the number of hours.
How can I indicate the number of hours?
Answer: The number of hours that you select in the pricing calculator correspond to the "Number of hours per day the database instance will be available for client connections". Therefore, this is not something you can set.
At this point your billing will vary depending on the usage of the instance and the number of hours that the instance will be available.


Como remover a hashtag (#) da URL?

Question: Por padrão o AngularJs formata a URL da seguinte maneira:
http://dominio.com.br/#/login
Como faço para deixá-la da seguinte maneira?
http://dominio.com.br/login
Answer: São dois passos bem simples:

Habilitar o HTML5 no $locationProvider
Configurar o caminho base da aplicação

Habilitar HTML5
angular.module('app', [])
  .config(function($routeProvider, $locationProvider) {
    $routeProvider
      .when('/', {
        templateUrl : 'home.html',
        controller : homeController
      })
      .when('/sobre', {
        templateUrl : 'sobre.html',
        controller : sobreController
      });

      // habilitar o uso da API HTML5 History
      $locationProvider.html5Mode(true);
    });

Definir url base no Index da aplicação

    <base href="/">

Fonte: URLs bonitas em AngularJS: Removendo o # (Em Inglês)

Como remover a hashtag (#) da URL?


How to post a link with a picture to Facebook using the Graph API?

Question: I have tried two different methods.
The first is to specify the link in the /PROFILE_ID/feed link argument (as described under "publishing" here).  The problem is that if I specify anything other than my application URL i get an error saying the URL is invalid.
The second is to use the /PROFILE_ID/links method and specifying a picture URL.  The problem is that when it posts it only shows the message and the URL.  I've specified values for picture, name and message but none of them show.
How can I Post a link, with a name, message and picture?
Answer: Your first approach is the correct one. Is your url starting with http://? What SDK are you using?
As described on this page, the example suggest it should work.
http://developers.facebook.com/docs/reference/api/post
curl -F 'access_token=...' \
 -F 'message=Check out this funny article' \
 -F 'link=http://www.example.com/article.html' \
 -F 'picture=http://www.example.com/article-thumbnail.jpg' \
 -F 'name=Article Title' \
 -F 'caption=Caption for the link' \
 -F 'description=Longer description of the link' \
 -F 'actions={"name": "View on Zombo", "link": "http://www.zombo.com"}' \
 -F 'privacy={"value": "ALL_FRIENDS"}' \
 -F 'targeting= {"countries":"US","regions":"6,53","locales":"6"}' \
 https://graph.facebook.com/me/feed

Comment: I am using an unsupported open source C# SDK.  I was able to fix this using the method I posted below.
Comment: Right, is it the SSL certificate verification you had to disable?
Comment: Being an old post, this answer helped me to post image with message, link and url using new version in nodejs. Thanks
Answer: I ended up solving this problem by using the /PROFILE_ID/feed method and then disabling Stream post URL security in my application security settings.  Hope this helps someone!
Comment: Where do you disable that settings?


Protocols with generic parameter types

Question: I'm trying to create a protocol which accept objects that are subclassed from NSObject:
protocol ProtocolExample {
    func willDisplay<T: NSObject>(object: T?)
}

class TableViewCell: UITableViewCell,ProtocolExample {
    func willDisplay<T>(object: T?) where T : NSObject {

    }

    func willDisplay(object: AMDObject?) {

    }
}

class AMDObject: NSObject {}

So my question is how can I apply that I only need to add the second willDisplay method instead of the first. How can I tell that in that case of tableviewcell my T is currently AMDObject type (like T is now AMDObject)
Comment: Your protocol is not generic. It is a normal protocol that requires a generic method. You cannot resolve the generic method; you must implement it exactly as your requirement dictates. To make a generic protocol you need an associated type.
Answer: The signature of the requirement in ProtocolExample should be read to mean, "a function willDisplay which accepts object, a parameter which can be of any (optional) type T? as long as T inherits from NSObject". This definition guarantees that I could write a function like this:
func displayThings(in example: ProtocoExample) {
    example.willDisplay(NSArray())
    example.willDisplay(NSObject())
}

However, with your implementation, what would happen if I wrote:
let cell: TableViewCell = // ...
displayThings(in: cell)

Then, displayThings would be unable to call example.willDisplay(NSArray()) since TableViewCell can only handle willDisplay for and AMDObject.
If you have control over the protocol yourself, you could implement this with an associatedtype:
protocol ProtocolExample {
    associatedtype DisplayObject: NSObject

    func willDisplay(object: DisplayObject?)
}

class TableViewCell: UITableViewCell, ProtocolExample {
    // Implicit typealias DisplayObject = AMDObject
    func willDisplay(object: AMDObject?) {

    }
}

Depending on how you are using ProtocolExample, though, adding this associatedtype may not be a straightforward solution.
Comment: Can you please explicate why is this not a straightforward solution? I mean I trying to learn this kind of best practices for protocols and generic types, but sometimes its hard to understand the syntax behind the scenes.
Comment: And what If I'm using the generic protocol function. Should I guard object is AMDObject else return ? Then work with that object? Or what is the most effective way to solve issues like this
Comment: In general, if you want your function to only accept one type as determined by the conforming type/method, having a generic function in your protocol is the wrong tool. The non-straightforwardness has to do with the fact that if you add an `associatedtype` to a protocol, you will no longer be able to use the protocol as what is called an "existential type", which essentially means that it can't be used as the type of a variable as in `var x: ProtocolExample = AMDObject()`.

Protocols with generic parameter types


What is the number of parameters for AIC if some coefficients are zero?

Question: I am estimating a linear regression model. I have n independent variables. One of the estimated coefficients is exactly zero. Does this coefficient count as a parameter when calculating the AIC?
Comment: *Exactly* zero or very small and close to zero? I ask because doubt they are exactly zero.
Comment: Exactly zero, of course.
Answer: [The first two paragraphs address an earlier version of the OP.]
For AIC comparisons, you cannot change the dependent variable without accounting for it. So you cannot directly compare the AIC of a model for the original variable with the AIC of a model for the z-score of the original variable.
Also, you are not quite right when you say I could arbitrarily increase the AIC by including further zero-coefficients for fictional variables without efficiently changing my model. These coefficients are fixed as opposed to being estimated, so they do not add extra penalty to AIC. The penalty is intended to account for overfitting, which involves estimation. If you are not estimating but rather imposing coefficient values, you do not overfit, so there is no need to penalize.

Does an estimated coefficient of zero counts as a parameter for AIC calculation?

I do not have a definite answer. Intuitively, I think it should, as the potential to overfit was there, although it did not materialize. But what are the chances of getting a coefficient value exactly equal to zero?
On the other hand, for example, in LASSO models an unbiased estimator of the degrees of freedom is the number of nonzero coefficients. So if the coefficient ended up being estimated at zero, it does not add up in the degrees of freedom calculation. So probably the answer depends on the model at hand?
Comment: (+1) I think problems with AIC are rather the other way round - when the model's not big enough to encompass some description close to the truth the approximation of no. degrees of freedom as a bias-correction factor for the expected KL divergence becomes unreliable.
Comment: @Scortchi, thanks, I don't know enough about this, but it sounds reasonable. But the question even as posed now seems interesting to me.
Comment: Yes, you are right with your points. I should have not included my two thoughts in the question which distract from the point of the real question. So, the question remains: Does an estimated coefficient of zero counts as a parameter for AIC calculation?
Comment: @RichardHardy: I think I can picture the outline of a more rigorous argument, but haven't the time to research & write it. (I'll add a bounty when I can.)


Not able to access index on local LAMP server on Linux Mint

Question: I'm starting to play with Zend FrameWork, but I have had troubles with it. I try the follow the super guide From Zero To Zend Framework in 10 minutes, but it amounted to nothing.
I also was getting that annyoing message
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
... waiting apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName twice.
And while I managed to fix that, this is how my local server looks like. .
This is how my /etc/hosts looks like.
127.0.0.1   localhost
127.0.0.1   prueba.local
127.0.1.1   dradis

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

And, according to 1, how my vhost configuration should be. This is in /sites-avaliable but symlinked to /sites-enabled.
prueba.local :
<VirtualHost *:80>
    ServerName prueba.local
    DocumentRoot /home/seba/solartekka/test/prueba/public
</VirtualHost>

/home/seba/solartekka/test/prueba is where I have my project.
I have completed two days fighting with this, and my spirit is shattered. Does anyone have an idea of what may be happening?
I tried to give as much relevant information as I thoguht, but should you require more, I would happily deliver it.
Thanks!
Comment: At times, I have run into permission problems. As a test, try setting `chmod 0777 public/index.php`. Also, you could try browsing directly to `http://prueba.local/index.php`.
Comment: I'll add it as an answer. ;-) Keep your spirit up. ZF can sometimes feel like a steep climb, but it's a great framework - ZF2 is even better - from which I learned a lot.
Comment: That was it! The index.php! Weid. Thank you, unfortunately I can accept your comment as an answer.
Answer: You should look into apache logs messages file, they will give you a hint:
tail /var/log/apache2/error.log

Frequent problem is missing Zend libraries.
Try to add this code to your vhost file:
<Directory "/home/seba/solartekka/test/prueba/public/">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride all
        Order allow,deny
        Allow From all
</Directory>

Do not forget to restart apache.
Comment: Look at: http://serverfault.com/questions/388378/what-attempt-to-serve-directory-error-means-in-apache-log
Comment: Vhost file udpated. Apache restarted. No changes.
This appears in error.log:

  `[Tue Feb 05 20:48:32 2013] [error] [client 127.0.0.1] Attempt to serve directory: /home/seba/solartekka/test/prueba/public/`

Repeated at least 4 times.
Comment: "/home/seba/solartekka/test/prueba/public is where I have my project." - Your project should be in /home/seba/solartekka/test/prueba/ and index.php in /home/seba/solartekka/test/prueba/public regarding to vhost content.
Comment: Yes, I didn't express myself correctly. That's is correct.
Answer: At times, I have run into permission problems. As a test, try setting chmod 0777 public/index.php.
Also, you could try browsing directly to http://prueba.local/index.php.


Background process for sending data to server

Question: Is it possible to run a background task on iOS that uploads data to a server every minute or so, like a service on Android?
Comment: Do you mean in the background like a separate thread or when the app is closed?
Answer: Not like Android. Only when the app is running in the foreground.
Comment: So basically its no possible?
Comment: Impossible unless you jailbreak or run a VOIP app.
Answer: Nothing like a service but there are certain categories of apps that can be run in the background for an extended period- In addition to VOIP that @lorean mentioned, there are others- audio, location, newsstand , external accessory -https://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html
. So if your app fits into these categories, then you are set.
Otherwise, you could also run your app as a background audio app by playing silent audio in a loop in the background- but there may be issues trying to get it through the App Store. For instance, you may be required to explicitly inform the user that audio will be played.
Answer: There's no way to do it as service except some special cases provided by ios. But you can simulate it. This post answer how to:
https://stackoverflow.com/a/19121250/2835520
I hope it's gonna help!


multivariate control chart

Question: I am trying to build a multivariate control chart but I did not find any good explanation of how to do that.
Starting with $x_0$ a part of the samples that are in control I compute $\mu_0$ and $\Sigma_0$ the mean and the covariance matrix.
Now with new data $x_n$ I compute $d= (\bar{x}_n-\mu_0)\Sigma^{-1}(\bar{x}_n-\mu_0)$ .
After this step I do not really know how to go further.
The best answer will be a theoretical explanation and an R or python code.
Answer: I'm not sure, if you're still interested in the topic, but I will provide a brief answer for you and other people that are interested in working with quality control charts (QCC), using R language. For a theoretical introduction to the topic, I would suggest reviewing the corresponding section of StatSoft's nice electronic textbook: http://www.uta.edu/faculty/sawasthi/Statistics/stquacon.html. For much more advanced treatment of the topic, I'd suggest this relevant thesis, titled "An investigation of some characteristics of univariate and multivariate control charts" (see links to PDF chapters).
Traditionally, R ecosystem offers a wide variety of packages to choose from for a specific domain. This applies to the QCC analysis as well. Most frequently used package for QCC analysis is qcc (Quality Control Charts), however there many other packages with varying ranges of functionality:

IQCC: Improved Quality Control Charts
MSQC: Multivariate Statistical Quality Control
qcr: Quality control and reliability
qualityTools: Statistical Methods for Quality Science
SPCadjust: Functions for calibrating control charts
CMPControl: Control Charts for Conway-Maxwell-Poisson Distribution
edcc: Economic Design of Control Charts
MetaQC: Objective Quality Control and Inclusion/Exclusion Criteria for Genomic Meta-Analysis
graphicsQC: Quality Control for Graphics in R
QCGWAS: Quality Control of Genome Wide Association Study results
GWAtoolbox: GWAS Quality Control
qAnalyst (removed from CRAN)
SixSigma: Six Sigma Tools for Quality and Process Improvement
qicharts: Quality Improvement Charts

For qcc there is a hard-to-find vignette by Luca Scrucca, which can be complemented by this blog post. For those, considering using QCC in educational setting, there is an interesting paper, describing the process (no code, though). Finally, anyone, interested in using QCC in a larger context of SixSigma and in R environment, the book "Six Sigma with R: Statistical engineering for process improvement", published by Springer, might be helpful.
Comment: +1, that a great list for reference, thank you! I recently found out that `qicharts` is now superceded by `qicharts2` by the same author. Also, `qualityTools` seems to have been removed from CRAN.
Comment: @COOLSerdash It's my pleasure. I'm happy that you found my answer useful.

multivariate control chart


Webform module issue - content type not displaying

Question: By accident I deleted the Webform content type in my Drupal 7 installation. It was a complete accident and thought that I was deleting a different content type. I tried going through a couple of tutorials that I saw online but was unable to get the content type back. Does anyone have any ideas on how to do this?
I stumbled across this: http://www.drupalgardens.com/documentation/feature/webforms/recreate which was a small step forward, but it still doesnt work. Has this happened to anyone before? I have tried uninstalling the and re-installing the module, clearing my cashe, and running cron. Still nothing.
Can anyone provide any suggestions? I can't be the only one that this has happened to before and there has to be so sort of fix to this... Also, please note that I am a relatively new Drupal developer.
Comment: Drupal 7? Can't you simply create a new content type called "Webform" with the machine name "webform" and enable it to be used as webform? That should get you back all the previously created webform content.
Comment: Just saw, that this is basically what they say in the link you posted. Can you explain what exactly is not yet working as expected?
Comment: So it seems that the user interface that comes with the webform module does not come back into play. So I cant designate who gets an email when the form is submitted and all of that stuff. Instead what happens, is that what would normally be on the UI is just displayed on my page and I would have to submit the stuff that way. But I dont have any of that other behind the scenes access from before...
Comment: Can you post a screenshot?
Comment: In addition to the missing details already mentionned on comment, the title of the question is too broad.
Comment: I actually just fixed it... It was kinda dumb but if it works i wont complain. I had to remove the file clear my cashe. Install the file again. Clear my cashe... deselect it in my modules. clear the cashe. Then went to modules>uninstall... then re-selected it again. I appreciate your interest in helping me though.
Comment: @PierreBuyle - sorry for it being too broad... I wasnt too sure how to exactly explain the issue. I hope that is more precise for your taste.
Answer: Drupal can act funny when you start deleting things and then trying to reinstall them. Do you have access to your database?
If so uninstall webform and delete it - then head into your database and delete any webform tables that might still be hanging around.
Dont even try this without backing up your database first though incase you happen to delete anything important.
Then re-install webform.
This is a pretty drastic step and you will lose all data surrounding your webforms.
I have had to do this with other modules that have left tables behind when uninstalled and then they mess when reinstalling said modules.
Once again though if you take this route be careful what you delete.
Comment: You could also add just the missing database elements back by looking at the schema of the Webform install.
Answer: For anyone looking, this exact same thing happened to me. It's easy to forget, but if you update a module to the latest version you need to update the DB. This is best achieved using Drush. For me, it was simply a case of running:
<code>drush updb
</code>
All my webforms reappeared and started working correctly.


Storing and retrieving custom field value of a webform in/from database?

Question: I have altered a webform by adding a date field in different language like chinese in Drupal 7. But what steps are required to store custom field value in database and retrieving value when webform results are accessed?
I used hook_form_alter() to add custom date field in the existing webform, but its value doesn't get stored in database. Do I need to add some extra code to store value of custom field in db?
I am sharing my code.
function tekege_grievanceform_form_alter(&$form, &$form_state, $form_id) { //print_r($form_id);exit;
switch ($form_id) {
    /* for chinese */
    case 'webform_client_form_60224':
        $form['submitted']['dob'] = array(
            '#type' => 'fieldset',
            '#title' => t('出生日期'),
            '#weight' => 4,
            '#prefix' => '
',
            '#suffix' => '
',
            '#required' => 'true'
        );

        $form['submitted']['dob']['dob_month'] = array(
            '#type' => 'select',
            '#default_value' => array('month' => '月'),
            '#options' => array('month' => t('月'),
                1 => t('1 月'), 2 => t('2 月'), 3 => t('3 月'), 4 => t('4 月'), 5 => t('5 月'), 6 => t('6 月'), 7 => t('7 月'), 8 => t('8 月'), 9 => t('9 月'), 10 => t('10 月'), 11 => t('11 月'), 12 => t('12 月'),
            ),
            '#description' => t('Select month.'),
            '#required' => true,
        );

        /* for 31 days */
        $form['submitted']['dob']['dob_day_31'] = array(
            '#type' => 'select',
            '#default_value' => array('day' => '日'),
            '#options' => array('day' => t('日'), 1 => t('1'), 2 => t('2'), 3 => t('3'), 4 => t('4'), 5 => t('5'), 6 => t('6'), 7 => t('7'), 8 => t('8'), 9 => t('9'), 10 => t('10'), 11 => t('11'), 12 => t('12'),
                13 => t('13'), 14 => t('14'), 15 => t('15'), 16 => t('16'), 17 => t('17'), 18 => t('18'), 19 => t('19'), 20 => t('20'), 21 => t('21'), 22 => t('22'), 23 => t('23'), 24 => t('24'),
                25 => t('25'), 26 => t('26'), 27 => t('27'), 28 => t('28'), 29 => t('29'), 30 => t('30'), 31 => t('31')),
            '#description' => t("Thirty-One Days."),
            '#required' => true,
        );

        $form['submitted']['dob']['dob_year'] = array(
            '#type' => 'select',
            '#default_value' => array('year' => '年'),
            '#options' => array('year' => t('年'), 1 => t('2013'), 2 => t('2012'), 3 => t('2011'), 4 => t('2010'), 5 => t('2009'), 6 => t('2008'), 7 => t('2007'), 8 => t('2006'),
                9 => t('2005'), 10 => t('2004'), 11 => t('2003'), 12 => t('2002'), 13 => t('2001'), 14 => t('2000'), 15 => t('1999'), 16 => t('1998'), 17 => t('1997'),
                18 => t('1996'), 19 => t('1995'), 20 => t('1994'), 21 => t('1993'), 22 => t('1992'), 23 => t('1991'), 24 => t('1990'), 25 => t('1989'), 26 => t('1988'),
                27 => t('1987'), 28 => t('1986'), 29 => t('1985'), 30 => t('1984'), 31 => t('1983'), 32 => t('1982'), 33 => t('1981'), 34 => t('1980'), 35 => t('1979'), 36 => t('1978'), 37 => t('1977'), 38 => t('1975'),
                39 => t('1974'), 40 => t('1973'), 41 => t('1972'), 42 => t('1971'), 43 => t('1970'), 44 => t('1969'), 45 => t('1968'), 46 => t('1967'), 47 => t('1966'),
                48 => t('1965'), 49 => t('1964'), 50 => t('1963'), 51 => t('1962'), 52 => t('1961'), 53 => t('1960'), 54 => t('1959'), 55 => t('1958'), 56 => t('1957'),
                57 => t('1956'), 58 => t('1955'), 59 => t('1954'), 60 => t('1953'), 61 => t('1952'), 60 => t('1951'), 61 => t('1950'),
            ),
            '#description' => t('Select year.'),
            '#required' => true,
        );
    }
 }

Comment: How did you altered webform ? using form alter ? You can add a field date to webform which can store and retrieve results like any other fields...
Comment: @AnilSagar I have added custom field in webform by form alter for some different requirements.Kindly let me know how its value can be stored and retrieved?
Comment: Please explain your requirements in your question.. Otherwise it's difficult to answer your question.. Please update your question with all details...
Comment: @AnilSagar I have updated my question.Please have a look.
Comment: Additionally you can add  webform.api functions (like `_webform_submit_component` or `hook_webform_submission_presave` or `hook_webform_submission_insert`...)
Comment: I see you are doing above just for translation .. Correct me if i am wrong.. If that's the case you can use Drupal Translation and https://drupal.org/project/webform_localization module for same..
Comment: @AnilSagar Yes I am doing it for translation. But I dont want to use any module for translation.
Answer: Solved: I was using devlopment version of webform module that was restricting my custom date fields of webform to get stored in database. It was required to be updated.
Anyways thanks for replying.


Derivative and graph mismatch

Question: Using the implicit function $(x^2+y^2-1)^3=x^2y^3$ it can be shown that $y'=\frac{2xy^3-6x(x^2+y^2-1)^2}{6y(x^2+y^2-1)^2-3x^2y^2}$ but when I evaluate it for the point (1,0) I get $y'(1,0)=\frac{0}{0}$ even though the slope of the tangent line is 2 at that point.
Any ideas?
Garth
Comment: have you drawn a plot of the curve?
Comment: Wow lovely heart.
https://www.wolframalpha.com/input/?i=%28x%5E2%2By%5E2-1%29%5E3%3Dx%5E2y%5E3
Comment: That is a pretty impressive graph.
Answer: Assuming you did all the algebra correctly, I haven't checked: You made an error in the algebra! Heh.
When you differentiated you got $$Q(x,y)y'+P(x,y)=0,\quad(*)$$where $P$ and $Q$ are two nasty polynomials. Then you converted that to $$y'=-\frac{P(x,y)}{Q(x,y)}.\quad(**)$$
But going from (*) to ($**$) is only valid if $Q(x,y)\ne0$. So you haven't actually shown that (**) holds, you've shown that $(**)$ holds at points where $Q\ne0$. Again assuming that you did everything else right, at $(1,0)$ equation (*) becomes $$0(2)=0,$$which is no problem since $0$ times $2$ does equal $0$. But $0(2)=0$ does not imply $2=0/0$.
Comment: Prof. Ullrich, welcome to MSE. Could you please take a look at my questions: http://math.stackexchange.com/questions/1263869/definition-functions-integrals-on-mathbb-rn-mathbb-r-mathbb-r  and
Comment: And http://math.stackexchange.com/questions/1306846/metrizability-models-of-non-standard-reals  ?
Comment: Don't think I can help much with those two questions, sorry. Looks like you're already getting reasonable feedback - given that the questions are a little vague (at least the first) you can't expect something definitive. Hmm. _If_ $\mathbb{R}^{\mathbb N}$ means what I think it means it may or may not be helpful to point out that there's no problem defining something more or less like Lebesgue measure on $[0,1]^{\mathbb N}$.
Answer: Instead $(x^2+y^2-1)^3=x^2y^3$ we can take $x^2+y^2-1=x^{2/3}y$ near of $(1,0)$, hence
\begin{align*}
\left(y-\frac{1}{2}x^{2/3}\right)^2&=1-x^2+\frac{x^{4/3}}{4}\\
y&=-\left(1-x^2+\frac{x^{4/3}}{4}\right)^{1/2}+\frac{x^{2/3}}{2}\\
y'&=-\frac{1}{2}\left(1-x^2+\frac{x^{4/3}}{4}\right)^{-1/2}\left(-2x+\frac{x^{1/3}}{3}\right)+\frac{1}{3}x^{-1/3}
\end{align*}
Then
\begin{align*}
y'(1)&=-\frac{1}{2}\left(1-1+\frac{1}{4}\right)^{-1/2}\left(-2+\frac{1}{3}\right)+\frac{1}{3}\\
&=-\frac{1}{2}(2)\left(-\frac{5}{3}\right)+\frac{1}{3}\\
&=2
\end{align*}
Comment: Thank you. I also came to this conclusion. It takes away the need to use the chain rule which forces the $y^2$ factor. Much appreciated.

Derivative and graph mismatch


How Can I Exclude Specific Records Correctly - SQL Server

Question: I have a table with Clients and their labels (saved in a single column separated by comma).
I need to find a way to EXCLUDE clients with SDRC label.
CREATE TABLE FakeData
(
    ClientId INT,
    ClientLabels VARCHAR(MAX)
);

INSERT INTO FakeData
VALUES (1, 'ABA, SDRC, California'),
       (2, 'SLP, Texas, SDRC (0-3)'),
       (3, 'ABA, SDRC (0-3), SDRC, Virginia'),
       (4, 'OT'),
       (5, 'ABA, SDRC (6+)'),
       (6, 'SLP, SDRC, Texas, SDRC (12+)')

SELECT * FROM FakeData

ClientId
ClientLabels

1
ABA, SDRC, California

2
SLP, Texas, SDRC (0-3)

3
ABA, SDRC (0-3), SDRC, Virginia

4
OT

5
ABA, SDRC (6+)

6
SLP, SDRC, Texas, SDRC (12+)

The output I need:

ClientId
ClientLabels

2
SLP, Texas, SDRC (0-3)

4
OT

5
ABA, SDRC (6+)

I was thinking of using STRING_SPLIT, however it did not work.
SELECT *
FROM FakeData
WHERE 'SDRC' NOT IN (SELECT VALUE
                     FROM STRING_SPLIT(FakeData.ClientLabels, ','))

And obviously below code will not work as well.
SELECT *
FROM FakeData
WHERE FakeData.ClientLabels NOT LIKE '%SDRC%'

Comment: Just don't store data as delimited strings.
Answer: How about this:
SELECT *
FROM FakeData
WHERE 'SDRC' NOT IN (SELECT TRIM(VALUE) FROM STRING_SPLIT(FakeData.ClientLabels, ','))

http://sqlfiddle.com/#!18/717fc/3
Comment: As it's currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer).
Answer: You can use STRING_SPLIT as a table:
SELECT ClientId, ClientLabels
  FROM @FakeData
    CROSS APPLY STRING_SPLIT(ClientLabels, ',') s
 GROUP BY ClientId, ClientLabels
 HAVING MAX(CASE WHEN TRIM(value) = 'SDRC' THEN 1 ELSE 0 END) = 0

Re-aggregating it after making your comparisons. To alter the filter just add or remove values in the case statement.
Answer: The simplest way if you must store data like this, would be to use a like match
select *
from FakeData
where Concat(', ', ClientLabels, ', ') not like '%, SDRC, %';


net links on meteor ios simulator can not click in

Question: When I do the meteor run iOS in my project it threw no error:
1.Started proxy.
 2.Started MongoDB.
 3.Started your app.
App running at: http://localhost:3000/
Started app on iOS Simulator.
But the links on the ios simulator screen can not click in,it has no action at all when click the links of the blue characters,the detail screen is in the attached picture. ios simulator screen of the project
Answer: have you been sure that the click button is enabled
Comment: the click button is able,when click it it can add one time,but the links in blue characters can not link in,when click the net links it can not forward to the link adress.
Comment: it seems that you'd like to push to an new view /webview controller or call  a new UIView after you pressed the links?
Comment: but the blue characters links can be clicked in and forward to the link dress when open this page in internet explore,so it just can not click in on simulator, whether android or ios simulator it won't work.
Comment: have you added` App Transport Security Settings `to Info.plist?
Comment: the HTML content is like below:
Comment: no,i did not add ` App Transport Security Settings `to Info.plist,how should i add it,can you give me an example? thanks a lot.
Answer: I mean the following:
yourProject/Info.plist
Comment: sorry,i just first use meteor and don't know where the Info.plist is,can you tell me?Thanks very much.
Comment: I find it under /platforms/ios/myapp/myapp.plist,is this to be set?
Comment: I added myapp-info.plist with: Allow Arbitrary Loads YES,and rerun meteor run ios,but the links still can not be clicked in.
Comment: And I tested on real android and ios phone, the app can be loaded on phone successfully, but also the links can not be clicked in and be opened and forward to the link address, only in internet explore the links can click in and forward to the linked address.
Comment: I looked into the meteor document,maybe need to set the mobile-config.js in one meteor project about the access rule, but i can not be sure,can you give me some example of mobile-config.js about access rules?
Comment: did you have  a gitHub account (https://github.com/ :you can push your code the website and it will be easier to manager your project )?you can show me your code ,then the error will be easier to be found .or you can email your code to me.(my email address:email@example.com)


Change staff position of hi-hat

Question: This is what it currently looks like:

This is what I want it to look like:

My code currently looks like this:
\version "2.22.2"

\header {
  title = "abc"
}

\drums {
  \clef percussion
  \numericTimeSignature
  \time 4/4

  << {
    hh4 4 4 4
  } \\ {

  } >>
}

Comment: Most seem to put hi-hat above the top line, although others will put cymbal there, and use top line itself for 'charlie' (hi-hat). Unless it's played with the foot, when 'x' is below the bottom line. Agostini seems to be the most used,
Answer: LilyPond has few different "styles" that set the positions for the various instruments on a percussion staff.   See 2.51 Common notation for percussion - Percussion staves.
For example, to change to the "agostini" style, place
\set DrumStaff.drumStyleTable = #agostini-drums-style

in the \new DrumStaff or \drums block.
(LilyPond v2.23 also has a "weinberg" style.)

Alternatively, you might like to define your own custom style, such as:
#( define my-drum-kit-style
    '(
        ( hihat         cross   #f   5 )
        ( snare         default #f   1 )
        ( bassdrum      default #f  -3 )
        ( crashcymbal   cross   #f   6 )
        ( ridecymbal    cross   #f   4 )
        ( himidtom      default #f   3 )
        ( lowmidtom     default #f   2 )
        ( highfloortom  default #f  -1 )
    )
)

This style table can be called with:
\set DrumStaff.drumStyleTable = #(alist->hash-table my-drum-kit-style)

Comment: @JonasHardt That should be opened as a separate question.
Comment: I didn't know that I was still running an old version of lilypond. The weinberg style did it for me in terms of positioning. But now the stems of hihat and bass drum are disconnected: https://i.imgur.com/VHCpJVw.png. Is there any way to fix this?
Comment: @JonasHardt
That's happening because of the (now slightly larger) space between the notes.
It's not really about percussion notation though; the same thing happens with notes on a regular staff.
I'm sure there is a way to fix it, but I haven't figured it out yet.
It would make a good seperate question.
Comment: @JonasHardt, just combine 8. Happy to see another Weinberg user! :D
Answer: To do this, redefine the DrumStaff.drumStyleTable.
\version "2.22.2"

#(define mydrums '(
         (hihat           cross     #f           5)))

\drums {
  \clef percussion
  \numericTimeSignature
  \time 4/4

  << {
    \set DrumStaff.drumStyleTable = #(alist->hash-table mydrums)
    hh4 4 4 4
  } \\ {

  } >>
}

For more, see the LilyPond manual entry on Custom percussion staves.

Change staff position of hi-hat


How is my book completing the square?

Question: In my book I see:

To integrate the given function we complete the square in the denominator:

$$4x^2 - 4x + 3 = (2x-1)^2 + 2$$
How is it doing this? When I complete the square I get:
$$x^2 - x + \frac{3}{4} = 0$$
$$x^2 - x + \frac{1}{4} = \frac{-1}{2}$$
$$\left(x-\frac{1}{2}\right)^2 = \frac{-1}{2}$$
$$x - \frac{1}{2} = \sqrt{\frac{-1}{2}}$$
now I'm stuck. can someone help to show me how my book gets the $(2x-1)^2 + 2$?
Comment: Why have you set the denominator equal to zero and divided through by 4? This is not a valid procedure.
Comment: @PeterForeman I thought that's how I complete the square...
Comment: @Jwan622 Completing the square is used in this case to simplify an expression, not to solve an equation. These are different procedures.
Comment: That's the method used for solving quadratic equations, which is something completely different.
Comment: This looks like a quadratic no?
Comment: Note that $(2a + b)^2 = 4a^2 + 4ab + b^2$.  In this case, $4a^2 = 4x^2$ and $4ab = -4x$, so $a = x$ and $b = -1$.  Hence, you want $4x^2 - 4x + 3 = 4x^2 - 4x + 1 + 2 = (2x - 1)^2 + 2$.
Answer: $$4x^2-4x+3=4x^2-4x+1+2=(2x-1)^2+2.$$
Comment: Why not? I wrote your expression as a square plus a number that should be added so that the equality holds.
Comment: so this isn't really completing the square right?
Answer: $4x^2-4x+ 3 = 4x^2-4x +  1 + 2 =  (2x-1)^2+2$
Answer: This is exactly the same as the other answers, but I'll try to explain a bit more:
Given expression:
$$4x^2 - 4x + 3$$
$$=4\left(x^2-x+\frac{3}{4}\right)$$
$$=4\left((x^2)-2\left(\frac{1}{2}\right)(x)+\left(\frac{1}{2}\right)^2-\left(\frac{1}{2}\right)^2+\frac{3}{4}\right)$$
$$=4\left(\left(x-\frac{1}{2}\right)^2-\frac{1}{4}+\frac{3}{4}\right)$$
$$=4\left(\left(x-\frac{1}{2}\right)^2+\frac{1}{2}\right)$$
$$=4\left(x-\frac{1}{2}\right)^2+2$$
$$=4\left(\frac{2x-1}{2}\right)^2+2$$
$$=(2x-1)^2+2$$
Because this expression is in the denominator of the integrand, we can further express this as
$$(2x-1)^2+(\sqrt2)^2$$
And proceed with the integration, depending on the expression in the numerator.

How is my book completing the square?


Marcar Todas Checkbox em um groupbox

Question: Tenho um programa que em um groupbox tem várias checkbox, como são muitas e pensei em fazer um botão para selecionar todas de uma vez, para ser mais prático, mas ai surgiu uma duvida, como posso fazer com que ao clicar no botão automaticamente selecione todas, e que eu não precise fazer um CheckBox.Checked = true; para todas?
Comment: Deu certo, a resposta?
Answer: No botão busca esse GroupBox (no meu exemplo é groupBox1) e pegue os seus controles que são do tipo CheckBox (Cast<>) e faça um ForEach setando a sua propriedade Checked = true, exemplo:
private void Button1_Click(object sender, EventArgs e)
{
    groupBox1.Controls.Cast<CheckBox>()
        .ToList()
        .ForEach(x => x.Checked = true);
}

Answer: Mais uma solução caso o groupbox não contenha apenas checkboxs:
groupBox1.Controls.OfType<CheckBox>().All(chk => chk.Checked = true);

Answer: Geralmente faço assim:
foreach (Control ctl in Gbx.Controls)
{
  if (ctl is CheckBox)
  ((CheckBox)ctl).Checked = true;
}

Marcar Todas Checkbox em um groupbox


Ordering array according to index ~ text

Question: Please, put in R these datas:
S.names <- c("FXI", "XLB", "GLD", "IWM", "XLE", "XLF", "EWZ", "GDX", "XLK",
"TLT", "IYR", "QQQ", "SLV", "EWJ", "XLV", "DIA", "XHB", "EEM",
"USO", "VWO", "SPY", "EFA")
strike_vec <- structure(list(Strike = c(152, 43, 61, 11, 56, 37, 36, 159, 96,
74, 71, 27, 163, 128, 35, 44, 30, 40, 81, 19, 31, 48)), .Names = "Strike", row.names =
c("DIA", "EEM", "EFA", "EWJ", "EWZ", "FXI", "GDX", "GLD", "IWM", "IYR",
"QQQ", "SLV", "SPY", "TLT", "USO", "VWO", "XHB", "XLB", "XLE",
"XLF", "XLK", "XLV"), class = "data.frame")

As you can see, strike_vec row names are equal to S.names elements.
I would like to order strike_vec elements according to the order of S.names, that is associating each strike_vec element to the position in which you find the corresponding S.names row name.
The final result should be something like
> strike_vec.new
    [,1]
FXI   37
XLB   40
GLD  159
IWM   96
   ...
   ...
   ...

where rownames(strike_vec.new) follows exactly the order of S.names.
How may I do?
Answer: Just use :
strike_vec[S.names,,drop=FALSE]

Which gives :
    Strike
FXI     37
XLB     40
GLD    159
IWM     96
XLE     81
XLF     19
EWZ     56
GDX     36
XLK     31
TLT    128
IYR     74
QQQ     71
SLV     27
EWJ     11
XLV     48
DIA    152
XHB     30
EEM     43
USO     35
VWO     44
SPY    163
EFA     61

This works because if you index the rows of a data frame with a character vector, indexing will be based on row names.


What does p.overall mean in the compareGroups package in R?

Question: I have done a cluster analysis for customer data using compareGroups, but I don't understand what p.overall means. Does it show each that variable in clusters are the same, or the value each variable from clusters are different?
Here are some of the results
library(compareGroups)
data1 = data.frame(x,cluster = g1$cluster)
group<-compareGroups(cluster~.,data= data1)
clustab<-createTable(group)
clustab

--------Summary descriptives table by 'cluster'---------

                  1             2            3            4             5       p.overall
                N=233          N=9          N=37         N=94         N=67

¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Channel:
1            229 (98.3%)    1 (11.1%)    2 (5.41%)    6 (6.38%)    60 (89.6%)     <0.001
Fresh        11236 (9863) 20696 (15256) 8817 (18414) 7815 (7349)  21120 (17473)   <0.001
Milk         2001 (1344)  37557 (20033) 16097 (7131) 7545 (3067)   6587 (3667)    <0.001

¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Answer: Here, p.overall simply lists the p-values for t-tests (or Mann-Whitney U tests, if your data are non-normal) performed between each of your clusters. Looking at the documentation for compareGroups, we see that it apparently corrects for multiple comparisons when necessary.
In this case, if you choose your significance level to be alpha = 0.05, then your reported p-values would indicate that the groups differ from one another in both the "Fresh" and "Milk" variables.
This question is not really appropriate for StackOverflow, but I figured I'd help you anyway. If you have other questions about statistical analysis, try: https://stats.stackexchange.com/
Comment: Ouuuh... i see thank u for the answer, the reason i asked here... Because kinda difficult to get an answer there.


How to describe and present the issue of perfect separation?

Question: Folks who work with logistic regression are familiar with the issue of perfect separation: if you have a variable specific values of which are associated with only one of the two outcomes (say a binary $x$ such that all observations with $x=1$ have outcome = 1), the likelihood blows up, and the maximum likelihood estimates run off to infinity. glm in R may or may not handle that terribly well, as the perfect prediction error message can show up for reasons other than perfect prediction/separation. logit in Stata identifies such variables and problematic values, and discards them from the analysis.
My question is different from what to do if you have a perfect separation. That I can handle by recoding my variables (they are all categorical, so I can simply combine categories), or with Firth version of logistic regression if I want to be fancy.
Instead, I wonder what the common ways are of describing this. I have a data set with circa 100 patients with about 50% proportion "positive", and some categories of the demographic variables produce this perfect prediction. Let's just say that all 7 green-eyed people have a "positive" outcome. This may be a small sample peculiarity that would disappear if I had sample size of 1000 and 70 green-eyed people, but it may be clinically meaningful, as in that larger sample I could have 60 out of 70 green-eyed people that would have a "positive" outcome with high odds ratios.
So it's nice to say that I used a Bayesian or some other shrinkage method, but in describing how I got there, I would need to admit that I had perfect prediction/separation, and had to find a more sophisticated technique to get any results at all. What would be a good language to use here?
Comment: See here for one viewpoint:  https://stats.stackexchange.com/questions/254124/why-does-logistic-regression-become-unstable-when-classes-are-well-separated/254205#254205
Comment: Related: [Seeking a Theoretical Understanding of Firth Logistic Regression](http://stats.stackexchange.com/questions/88734/seeking-a-theoretical-understanding-of-firth-logistic-regression)
Answer: While performing my excavation activities on no-answer questions, I found this very sensible one, to which, I guess, by now the OP has found an answer.
But I realized that I had various questions of my own regarding the issue of perfect separation in logistic regression, and a (quick) search in the literature, did not seem to answer them. So I decided to start a little research project of my own (probably re-inventing the wheel), and with this answer I would want to share some of its preliminary results. I believe these results contribute towards an understanding of whether the issue of perfect separation is a purely "technical" one, or whether it can be given a more intuitive description/explanation.
My first concern was to understand the phenomenon in algorithmic terms, rather than the general theory behind it: under which conditions the maximum likelihood estimation approach will "break-down" if fed with a data sample that contains a regressor for which the phenomenon of perfect separation exists?
Preliminary results (theoretical and simulated) indicate that:
1) It matters whether a constant term is included in the logit specification.
2) It matters whether the regressor in question is dichotomous (in the sample), or not.
3) If dichotomous, it may matter whether it takes the value $0$ or not.
4) It matters whether other regressors are present in the specification or not.
5) It matters how the above 4 issues are combined.
I will now present a set of sufficient conditions for perfect separation to make the MLE break-down. This is unrelated to whether the various statistical softwares give warning of the phenomenon -they may do so by scanning the data sample prior to attempting to execute maximum likelihood estimation. I am concerned with the cases where the maximum likelihood estimation will begin -and when it will break down in the process.
Assume a "usual" binary-choice logistic regression model
$$P(Y_i \mid \beta_0, X_i, \mathbf z_i) = \Lambda (g(\beta_0,x_i, \mathbf z_i)), \;\; g(\beta_0,x_i, \mathbf z_i) = \beta_0 +\beta_1x_i + \mathbf z_i'\mathbf \gamma$$
$X$ is the regressor with perfect separation, while $\mathbf Z$ is a collection of other regressors that are not characterized by perfect separation. Also
$$\Lambda (g(\beta_0,x_i, \mathbf z_i)) = \frac 1{1+e^{-g(\beta_0,x_i, \mathbf z_i)}}\equiv \Lambda_i$$
The log-likelihood for a sample of size $n$ is
$$\ln L=\sum_{i=1}^{n}\left[y_i\ln(\Lambda_i)+(1-y_i)\ln(1-\Lambda_i)\right]$$
The MLE will be found by setting the derivatives equal to zero. In particular we want
$$ \sum_{i=1}^{n}(y_i-\Lambda_i) = 0 \tag{1}$$
$$\sum_{i=1}^{n}(y_i-\Lambda_i)x_i = 0 \tag{2}$$
The first equation comes from taking the derivative with respect to the constant term, the 2nd from taking the derivative with respect to $X$.
Assume now that in all cases where $y_1 =1$ we have $x_i = a_k$, and that $x_i$ never takes the value $a_k$ when $y_i=0$. This is the phenomenon of complete separation, or "perfect prediction": if we observe $x_i = a_k$ we know that $y_i=1$. If we observe $x_i \neq a_k$ we know that $y_i=0$. This holds irrespective of whether, in theory or in the sample, $X$ is discrete or continuous, dichotomous or not. But also, this is a sample-specific phenomenon -we do not argue that it will hold over the population. But the specific sample is what we have in our hands to feed the MLE.
Now denote the abolute frequency of $y_i =1$ by $n_y$
$$n_y \equiv \sum_{i=1}^ny_i = \sum_{y_i=1}y_i \tag{3}$$
We can then re-write eq $(1)$ as
$$n_y = \sum_{i=1}^n\Lambda_i = \sum_{y_i=1}\Lambda_i+\sum_{y_i=0}\Lambda_i \Rightarrow n_y - \sum_{y_i=1}\Lambda_i = \sum_{y_i=0}\Lambda_i \tag{4}$$
Turning to eq. $(2)$ we have
$$\sum_{i=1}^{n}y_ix_i -\sum_{i=1}^{n}\Lambda_ix_i = 0 \Rightarrow \sum_{y_i=1}y_ia_k+\sum_{y_i=0}y_ix_i - \sum_{y_i=1}\Lambda_ia_k-\sum_{y_i=0}\Lambda_ix_i =0$$
using $(3)$ we have
$$n_ya_k + 0 - a_k\sum_{y_i=1}\Lambda_i-\sum_{y_i=0}\Lambda_ix_i =0$$
$$\Rightarrow a_k\left(n_y-\sum_{y_i=1}\Lambda_i\right) -\sum_{y_i=0}\Lambda_ix_i =0$$
and using $(4)$ we obtain
$$a_k\sum_{y_i=0}\Lambda_ix_i -\sum_{y_i=0}\Lambda_ix_i =0 \Rightarrow \sum_{y_i=0}(a_k-x_i)\Lambda_i=0 \tag {5}$$
So : if the specification contains a constant term and there is perfect separation with respect to regressor $X$, the MLE will attempt to satisfy, among others, eq $(5)$ also.
But note, that the summation is over the sub-sample where $y_i=0$ in which $x_i\neq a_k$ by assumption. This implies the following:
1) if $X$ is dichotomous in the sample, then $(a_k-x_i) \neq 0$ for all $i$ in the summation in $(5)$.
2) If $X$ is not dichotomous in the sample, but $a_k$ is either its minimum or its maximum value in the sample, then again $(a_k-x_i) \neq 0$ for all $i$ in the summation in $(5)$.
In these two cases, and since moreover $\Lambda_i$ is non-negative by construction, the only way that eq. $(5)$ can be satisfied is when $\Lambda_i=0$ for all $i$ in the summation. But
$$\Lambda_i =  \frac 1{1+e^{-g(\beta_0,x_i, \mathbf z_i)}}$$
and so the only way that $\Lambda_i$ can become equal to $0$, is if the parameter estimates are such that $g(\beta_0,x_i, \mathbf z_i) \rightarrow -\infty$. And since $g()$ is linear in the parameters, this implies that at least one of the parameter estimates should be "infinity": this is what it means for the MLE to "break down": to not produce finite valued estimates. So cases 1) and 2) are sufficient conditions for a break-down of the MLE procedure.
But consider now the case where $X$ is not dichotomous, and $a_k$ is not its minimum, or its maximum value in the sample. We still have complete separation, "perfect prediction", but now, in eq. $(5)$ some of the terms  $(a_k-x_i)$ will be positive and some will be negative. This means that it is possible that the MLE will be able to satisfy eq. $(5)$ producing finite estimates for all parameters. And simulation results confirm that this is so.
I am not saying that such a sample does not create undesirable consequences for the properties of the estimator etc: I just note that in such a case, the estimation algorithm will run as usual.
Moreover, simulation results show that if there is no constant term in the specification, $X$ is not dichotomous but $a_k$ is an extreme value, and there are other regressors present, again the MLE will run -indicating that the presence of the constant term (whose theoretical consequences we used in the previous results, namely the requirement for the MLE to satisfy eq. $(1)$), is important.
Comment: I agree with you. My post does not argue otherwise. What I tried to do was to "predict" when the actual implementation of the MLE _algorithm_ through the attempt to satisfy the usual conditions, will numerically break down under perfect separation, and when it won't. But my post does not discuss the usefulness/meaningfulness of the actual estimates we will obtain, in the case where the MLE algorithm does provide results, and I have wrote this clearly in my post. I see my examples as a warning: "beware -even if perfect separation exists, don't trust the MLE algorithm to tell you so".
Comment: No I haven't, so the question stands open. However, I would need to stop you right at the normal equations (1) and (2). The practical issue of perfect separation is that the likelihood optimizer wants to send the $g(\cdot)$ to $\pm \infty$, and consequently the predicted probabilities to zero/one. You are not in the interior of the parameter space anymore, so the normal equations may not work. Also, for continuous variables, I would characterize perfect separation as $x_i > a$ implies $Y_i = 1$ and $x_i \le a$ implies $Y_i = 0$, say, rather than an exact equality to a given value.
Comment: The normal equations is what the MLE will try to satisfy -and in order to do so, it may send the logit to infinity, certainly. What do you mean by "the normal equations may not work?" I am not sure I understand. Regarding continuous variables, yes your characterization of perfect separation is essentially in accord with what I found, namely that the "$a_k$" value is extreme in the sample, for the MLE to break down. Your inequalities essentially describe the same thing.
Comment: ML does not work when regularity conditions are violated. See e.g. [this](http://www.unc.edu/depts/statistics/postscript/rs/ISI89.pdf).
Comment: OK. With that in mind, it's a great, thorough analysis, and a valuable contribution to our understanding of perfect separation. But strictly speaking it's not an answer to my question which was on reporting the phenomenon (and you recognize that in your answer, too). May I also suggest that you expand your answer a little bit and describe what happens with finite precision arithmetics -- (5) will be deemed "small enough" (e.g., < $10^{-8}$ or something like that) in absolute value, and the software may happily spit out non-sensically large values (as `glm` in R does, to my knowledge)?


Intuitive explanation of desirable properties (Unbiasedness, Consistency, Efficiency) of statistical estimators?

Question: From literature I understand that the desirable properties of statistical estimators are

Unbiasedness - we want the estimator to give the correct parameter value theta, on an average, irrespective of the sample size--defined by

Consistency - we want larger sample sizes to give progressively better estimates of the correct parameter value theta and asymptotically converge to theta in probability--defined by

Efficiency - we want the unbiased estimator to have the lowest possible variance--as determined by the Cramer-Rao bound. Efficient estimators, however, need not exist in all situations.

I don't really understand each of these properties and the difference between them. Please explain the intuitive meaning of these properties followed by the math behind it.
Refernce Link - https://www.cs.utah.edu/~suyash/Dissertation_html/node6.html
Answer: Unbiasedness means that under the assumptions regarding the population distribution the estimator in repeated sampling will equal the population parameter on average. This is a nice property for the theory of minimum variance unbiased estimators. However, I think unbiasedness is overemphasized.  The mean square error is a good measure of the accuracy of an estimator. It equals the square of the estimator's bias plus the variance.  Sometimes estimators with small bias have smaller mean square error than unbiased estimators that have large variances.
Biased estimators can be asymptotically unbiased, meaning the bias tends to 0 as the sample size gets large.  If the estimator is both asymptotically unbiased and the variance goes to 0 as the sample size gets large, then the estimator is consistent (in probability).  Technically, in measure theory, there is a difference between convergence in probability and convergence almost surely.  The Cramer-Rao lower bound is a mathematical result that shows, in a particular parametric family of distributions, that no unbiased estimator can have a variance less than the bound.  So if you can show that your estimator achieves the Cramer-Rao lower bound, you have an efficient estimator.
Answer: Think of firing at a target. If you consistently hit the target too low you have a bias. If your arrows are closely grouped you have an efficient estimate. You might be interested in or amused by Maurice Kendall's poem on the subject http://www.columbia.edu/~to166/hiawatha.html


Algorítmo para plano de corte

Question: Não sei se esse é o melhor local para fazer esta pergunta, mas eu procurei muito na internet por algum algorítmo para plano de corte - que informando as dimensões das peças ele mostra a melhor arranjo para que se disperdice o mínimo possível.
Alguém conhece algum algorítmo próximo dessa realidade?
Comment: eu já vi um sistema desses funcionando em uma marcenaria, então com certeza existe já pronto. Você já fez alguma busca sobre priorização por tamanho? Acho que pode ser um bom ponto de partida.
Comment: Eu encontrei esse material: http://www.dct.ufms.br/~eah/pli/planos-de-corte-4p.pdf - extremamente acadêmico e pouco didático
Answer: O nome desse tipo de algoritmo em inglês é Bin Packing, uma variante do problema da mochila.
Uma árvore binária é utilizada para selecionar, em vários passes, a melhor distribuição (optimal distribution) de um conteúdo por ordem  e percentual de utilização de superfície.
Uma excelente implementação do algoritmo pode ser encontrada aqui:
http://codeincomplete.com/posts/2011/5/7/bin_packing/
Exemplo de uso:
http://codeincomplete.com/posts/2011/5/7/bin_packing/example/
Eu tomei a liberdade de testar as áreas da sua imagem de exemplo na demo do algoritmo. Este é o resultado (eu dividi as dimensões por 10 e arredondei os valores):
Comment: Ótimo ponto de partida! Obrigado

Algorítmo para plano de corte


AWS Lightsail Load Balancer and Too Many Redirects

Question: I have spent several hours trying to figure out why once I put my instance containing my website behind a load balancer, I keep getting the "Too Many Redirects" error. I have a Windows Server instance behind my load balancer with an SSL certificate on my load balancer. Originally, I had an SSL certificate on my instance because we weren't sure about load balancing yet, and was handling the http -> https redirects on my server with url rewrite. However, now I am getting this error. I have disabled the rewrite rules on my server and yet I'm still getting all my responses with a 307 status. Any help is much appreciated, as I'm almost out of hair to pull out. Also please let me know if you need more information, I just wasn't sure what else to include in the question.
Update
After using Failed Request Tracing, it seems that the module setting the response header to 307 is the "AspNetCoreModuleV2".
Comment: I'm in the process of trying to work through a similar issue. Try enabling Failed Request Tracing (google about it and how to install and configure). Maybe it will tell you what is happening. Here's my issue, if it helps at all: https://serverfault.com/questions/1034456/arr-works-for-non-ssl-but-fails-for-ssl?noredirect=1#comment1346089_1034456
Comment: Also check out this article, it talks about SSL and redirect loops a bit: https://kevinareed.com/2015/11/07/how-to-deploy-anything-in-iis-with-zero-downtime-on-a-single-server/
Answer: So now I feel really silly, but I figured out what the problem was. Someone had added httpsredirection middleware to the project and I was unaware of it. Once I removed the middleware, it works like a charm. However I wouldn't have figured that out if Chad Richardson hadn't mentioned Failed Request Tracing. Thank you very much!