I don't understand Gödel's incompleteness theorem anymore

Question: Here's the picture I have in my head of Model Theory:

a theory is an axiomatic system, so it allows proving some statements that apply to all models consistent with the theory
a model is a particular -- consistent! -- function that assigns every statement to its truth value, it is to be thought of as a "concrete" object, the kind of thing we actually usually think about. It's only when it comes to models that we have the law of the excluded middle.

My understanding of Gödel's first incompleteness theorem is that no theory that satisfies some finiteness condition can uniquely pin down a model.
So I am not really surprised by it. The idea of theories being incomplete -- of not completely pinning down a particular model -- is quite normal. The fact that no theory is complete seems analogous to how no Turing machine can compute every function.
But then I read this thread and there were two claims there in the answers which made no sense to me:

Self-referential statements as examples of unprovable statements -- Like "there is no number whose ASCII representation proves this statement".

A statement like this cannot be constructed in propositional logic. I'm guessing this has to do with the concept of a "language", but why would anyone use a language that permits self-reference?
Wouldn't that be completely defeat the purpose of using classical logic as the system for syntactic implications?
If we permit this as a valid sentence, wouldn't we also have to permit the liar paradox (and then the system would be inconsistent)?

Unprovable statements being "intuitively true/false" -- According to this answer, if we found that the Goldbach conjecture was unprovable, then in particular that means we cannot produce a counter-example, so we'd "intuitively" know that the conjecture is true.

How is this only intuitive? If there exist $\sf PA$-compatible models $M_1$, $M_2$ where Goldbach is true in $M_1$ but not $M_2$, then $\exists n, p, q$ such that $n= p+q$ in $M_1$ but not in $M_2$. But whether $n=p+q$ is decidable from $\sf PA$, so either "$\sf{PA}+\sf{Goldbach}$" or "$\sf{PA}+\lnot\sf{Goldbach}$" must be inconsistent, and Goldbach cannot be unprovable. Right?
In any case, I don't know what it means for the extension to be "intuitively correct". Do we know something about the consistency of each extension or do we not?
Further adding to my confusion, the answer claims that the irrationality of $e+\pi$ is not such a statement, that it can truly be unprovable. I don't see how this can be -- surely the same argument applies; if $e+\pi$'s rationality is unprovable, there does not exist $p/q$ that it equals, thus it is irrational. Right?
Comment: Regarding the first thing, the point is fairly simple: languages we would actually use in mathematical logic don't directly permit this kind of self-reference, but with the level of expressive power granted to us by sufficiently strong mathematical theories, we can bootstrap up such self-reference (e.g. by building a statement that contains its own Godel number). Regarding the second thing, I've never gotten a very clear picture of what the true-but-unprovable theorems in a consistent-but-not-provably-consistent full-featured arithmetic theory actually look like (but I am not a logician).
Comment: @HagenvonEitzen "If Goldbach ... is unprovable in PA it is necessarily true in all models" Not at all - indeed, that would contradict the completeness theorem. All we can conclude is that it is true in the *standard* model.
Comment: "Unprovable" is the wrong formulation ! If Goldbach's conjecture is false, it is also unprovable. What we need is that it is undecidable within PA (in particular not disprovable), then it cannot be false since PA would be able to disprove it.
Comment: "No theory is complete" is incorrect. Godel showed that a theory which is consistent, recursively definable, and can do elementary arithmetic on $\Bbb N$ must be incomplete.
Comment: If Goldbach (and a class of similar statements that can be disproved by an explicit counterexample to a simple statement) is unprovable in PA it is necessarily true in all models, by the argument you gave. -- If $e+\pi$'s rationality is unprovable, the situation is differnet. For a concrete rational $\frac pq$, it is" easy" to show $e+\pi>\frac pq$ and easy to show $e+\pi<\frac pq$, whichever is true: just use good enough aproximations; showing $e+\pi=\frac pq$  however *may* be impossible.
Comment: @HagenvonEitzen Your assertion about Goldbach is not entirely accurate. I explain myself in an answer below.
Comment: In fact , the situation in the case of the question whether $e+\pi$ is irrational is worse. This is not even necessarily semidecidable. Similar situation in the Collatz conjecture.
Answer: This answer only addresses the second part of your question, but you asked many questions so hopefully it's okay.
First, there is in the comments a statement: "If Goldbach is unprovable in PA then it is necessarily true in all models." This is incorrect. If Goldbach were true in all models of PA then PA would prove Goldbach by Godel's Completeness Theorem (less popular, still important).
What is true is:
Lemma 1: Any $\Sigma_1$ statement true in $\mathbb{N}$ (the "standard model" of PA) is provable from PA.
These notes (see Lemma 3) have some explanation: http://journalpsyche.org/files/0xaa23.pdf
So the correct statement is:
Corollary 2: If PA does not decide Goldbach's conjecture then it is true in $\mathbb{N}$.
Proof: The negation of Goldbach's conjecture is $\Sigma_1$. So if PA does not prove the negation, then the negation of Goldbach is not true in $\mathbb{N}$ by Lemma 1.
Remember that $\mathbb{N}$ is a model so any statement is either true or false in it (in our logic). But PA is an incomplete theory (assuming it's consistent), so we don't get the same dichotomy for things it can prove.
Now, it could be the case that PA does prove Goldbach (so its true in all models of PA including $\mathbb{N}$). But if we are in the situation of Corollary 2 (PA does not prove Goldbach or its negation) then Goldbach is true in $\mathbb{N}$ but false in some other model of PA. (This would be good enough for the number theorists I imagine.) This is also where the problem in your reasoning is. It is NOT true that if Goldbach fails in some model $M$ of PA then there is a standard $n$ in $\mathbb{N}$ that is not the sum of two primes. Rather the witness to the failure of Goldbach is just some element that $M$ believes is a natural number. In some random model, this element need not be in the successor chain of $0$.
On the other hand, the rationality of $\pi+e$ is not known to be expressible by a $\Sigma_1$ statement. So we can't use Lemma 1 in the same way.
Edited later: I don't have much to say about the question on self-referential statements beyond what others have said. But I'll just say that one should be careful to distinguish propositional logic and predicate logic. This also goes for your "general picture of Model Theory". Part of the interesting thing with the incompleteness theorems is that they permit self-reference without being so obvious about it. In PA there is enough expressive power to code statements and formal proofs, and so the self-referential statements about proofs and so forth are fully rigorous and uncontroversial.
Comment: @AbhimanyuPallaviSudhir: Correct about "standard model". But as kalrankard said, nothing here has anything to do with intuition. All rigorous mathematics can be done within some chosen foundational system MS (usually ZFC), including mathematical logic. $\mathbb{N}$ is defined within MS as the closure of $0$ under some successor function, so every member is "$0$" or is of the form "$1+\cdots+1$". This is related to why PA is Σ1-complete, but you'll have to actually go through the proof to understand properly.
Comment: Superb answer (+1) !
Comment: I see ... and the construction of the model $\mathbb{N}$ -- where the Goldbach conjecture is true (if undecidable in PA) -- is in ZFC, which is a stronger theory. So the notion of a "standard model", and of "intuitively knowing" something is within the framework of a stronger theory. Correct?
Comment: Yes the construction of $\mathbb{N}$ as a model of PA can be done in ZFC (probably something weaker suffices). Though you have to be a little careful what you mean by "ZFC is stronger than PA" as these are theories in different vocabularies. And I emphasize that while the meaning of Lemma 1 can be explained in an intuitive way, it is in fact a rigorous result (within ZFC or whatever other framework you choose to get your hands on $\mathbb{N}$ and the proof system). So one can explain why the independence of Goldbach from PA implies its truth in $\mathbb{N}$ without appealing to intuition.
Answer: Let me try to get at the heart of your misunderstanding as concise as possible:
1. We are not deliberately choosing to use a language that permits self-reference, we are forced to do so.
The only choice we made is that of a logic that is sufficiently strong to include integer arithmetic. What Gödel then proves is that access to the integers automatically allows us to construct somewhat self-referential statements. If we want integers, then we have to accept self-referentiality. The same is true in the theory of computability. Turing machines are not chosen because they can emulate themselves, they are chosen because they allow all operations we expect a general computer to do, which just happens to include emulating turing machines.
2. We are self-referential with respect to the theory, not the model.
The kind of sentences that Gödels procedure allows us to construct are of the form "X can not be infered from Y", as the integers are only used to build a copy of logical reasoning. If we pick the set of axioms of a given theory as Y, then we can construct sentences like "X is not provable in the theory" which is what leads to the incompleteness theorem if X is the sentence itself. There is no way to access a specific model of the theory and thus no way of constructing sentences like "X is false", which would be needed for the liar's paradoxon.
Answer: Allow me to start by pointing out that Gödel's theorems are usually studied in the context of first-order logic, whereas you are describing propositional logic in your understanding of theory and model.
While a theory is roughly the same idea of a collection of sentences and inference rules (although some people define a theory as also being closed under deductions), a model is very different. It is not just an assignment of truth values. So while propositional logic deals with a lot of "switches" that have true and false, first-order logic deals with collections of objects, some relations, some functions, and some named constants, and what statements a collection of objects interpreting these syntactic ideas will satisfy.
The two things, models and theories, are connected by Gödel's completeness theorem which states the first-order logic is complete (which is not the same as a theory being complete). So a statement is provable from a theory if and only if it is true in every model of the theory. And it is important to stress, "most theories" have a lot of different models, either by reasons like cardinality (if a theory has an infinite model, it has one of every infinite cardinality) or incompleteness (if a theory is not complete it has completely different models even in the same cardinality), or by other reasons (e.g. maybe the theory is complete, but there are things beyond the scope of the language that are not decided).
And while we utilise this deep connection all the time in mathematics, without even thinking about it most of the time, syntax and semantics are separate. Theories are not models, and models are not theories.
When you get to analyse these definitions, you will see that a first-order language cannot be self-referential. It cannot talk about its own model, because the tools to do so are simply not syntactic.
But, and here is the importance of the conditions of Gödel's incompleteness theorem, some languages are sufficient for internalising the whole of first-order logic, and under some basic assumptions a theory can provably do so.
In other words, if $T$ is a theory in a language which is "rich enough" (where "rich enough" is really quite poor: a binary relation or a binary function would suffice), and $T$ can internalise first-order logic, then it is not complete.
The key idea is that once we have formulas which we can prove to be an interpretation of first-order logic, we can make all sort of weird constructions. This is not self-referential as much as it is "self-aware". But even that is a misnomer.
The subtle point of the incompleteness theorem is that in different models of the same theory, the internalisation may be very different. It will always include a faithful copy of the actual first-order logic used "outside" the theory, but it may include new bits and pieces which may or may not be "reasonable".
Moreover, since the notion of "finiteness" is not captured internally by first-order logic, once we interpreted first-order logic, and found a predicate to represent the interpretation of a theory $T'$, if $T'$ had infinitely many axioms, if the internalisation process adds "new bits", it will invariably add new sentences to its own interpretation of $T'$.
So between different models of the theory $T$, we may get very different copies of first-order logic and different copies of $T'$. Gödel utilises this to construct a sentence which is not provable from $T$ itself.
But this is not the liar's paradox. At no point a sentence really refers to itself. It simply talks about an interpretation of itself. Because "true/false" is not the same as "provable/unprovable", unless you can quantify over all models, which you can't, since they are not part of your language.
Gödel wanted to avoid people looking at all of this and saying "Oh, those crazy logicians... good things we actually care about the natural numbers and not all of this formalism around it". So in the process he showed that all of this coding can be done in an extremely robust way by using the natural numbers and some very basic number theoretic results. Now mathematicians had to pay attention, this can no longer be ignored.
Finally, as to the remarks on the Goldbach Conjecture, I will direct your attention to Decidability of the Riemann Hypothesis vs. the Goldbach Conjecture.
Answer:

Self-referential statements as examples of unprovable statements -- Like "[there is no number whose ASCII representation proves this statement][1]".

A statement like this cannot be constructed in propositional logic. I'm guessing this has to do with the concept of a "language", but why would anyone use a language that permits self-reference?

Here's the crux of the issue. Actually, such a statement can be constructed. (Or, at least, a statement which acts like such a statement can be constructed.)
As you know, it's not possible to take the sentence "This sentence cannot be proved in ZFC" and simply translate it directly into the language of ZFC. This is because, as you know, there is nothing in the language of ZFC that means "this sentence".
What we can do, however, is create a sentence G that is true if and only if G cannot be proved in ZFC. How can we do this?
Well, take a look at the following English sentence:

If you write down the following, and then write it again in between quotation marks, then the resulting statement cannot be proved in ZFC: "If you write down the following, and then write it again in between quotation marks, then the resulting statement cannot be proved in ZFC:"

Notice that the part within quotation marks is identical to the part outside of quotation marks, and so "the resulting statement" is identical to the original statement. This statement refers to itself without ever using the phrase "this statement"!
It is possible to do something similar to the above "tricky sentence" in the language of ZFC. The desired sentence is "The sentence with Gödel number $N$ cannot be proved in ZFC", where $N$ is a particular number which is chosen in a way similar to the above "tricky sentence", so that $N$ is the Gödel number for a sentence which is logically equivalent to "The sentence with Gödel number [$N$] cannot be proved in ZFC".
The reason that this can't be extended to form the liar paradox is that the predicate "the statement $p$ cannot be proved in ZFC" can be defined in the language of ZFC, whereas the predicate "the statement $p$ is false" cannot. (In fact, the liar paradox you mention is the proof that the predicate "the statement $p$ is false" cannot be defined in the language of ZFC.)
Answer: The proof of Gödel's first incompleteness theorem relies on inventing a proposition-to-integer mapping. The theories it considers are able to describe this, as a function from strings of symbols to integers. It turns out that, even without direct self-reference, propositions can even talk about their own Gödel numbers. (There's no way to prohibit this in the theories of interest.) And some are equivalent to their own unprovability. Such statements are either true but unprovable, or false but provable.
If Goldbach's conjecture is false, it has a counterexample, so is decidable. Therefore, if a theory $T$ proves the conjecture is undecidable in $T^\prime$, $T$ also proves the conjecture true.


How to ignore text between quotes in JavaScript?

Question: I make my own language in JavaScript using regular expression. How can you recommend to ignore text in string variables?
for example, my code:
if (flag) {
    print("Yes")
} elif (!flag) {
    print("Elif is worked")
}

will transpile to
if (flag) {
    console.log("Yes")
} else if (!flag) {
    console.log("else if is worked")
}

I want to ignore text in quotes:
if (flag) {
    console.log("Yes")
} else if (!flag) {
    console.log("elif is worked")
}

Comment: What do you mean by saying "ignore" ? Do you want to replace the texts with string variables ?
Comment: I dont want to replace my features. For example "elif" converts to "else if". I don't want to change it in string variable.
Answer: Without wanting to discourage you: Don't use regular expressions, because the problem domain is too complex for them to work properly. Instead, implement a tokenizer and a parser. But generally speaking, this topic is too broad to be answered here.
There is a tutorial online here which shows how to implement a programming language in javascript.


Prove that $\int\sum k\chi_{f^{-1}}<\infty$

Question: For a Lebesgue measurable sets and functions problem, I need to prove this statement:

Being $A\subset\mathbb{R}$ a measurable set with $m(A)<\infty$, and $f:A\rightarrow[0,\infty)$ a Lebesgue measurable function:
  $$\sum_{n=0}^\infty(m(\{x\in A:f(x)\geq n\}))<\infty\Longleftrightarrow \int_{\mathbb{R}}\sum_{k=0}^\infty (k\cdot\chi_{f^{-1}([k,k+1))}) dm<\infty$$

The notation I'm using is: $m$ for the Lebesgue measure and $\chi_B$ the characteristic function of $B$, to be said, $\chi_B(x)=1\Leftrightarrow x\in B$ and $\chi_B(x)=0\Leftrightarrow x\notin B$.
I've started calling $S=\sum_{k=0}^\infty (k\cdot\chi_{f^{-1}([k,k+1))}$, so that $S$ is in fact a simple function, and that means (by characterisation of simple functions)
$$\int_RS=\sum_{k=0}^\infty (k\cdot m(f^{-1}([k,k+1)))$$
I clearly see that $\sum_{k=0}^\infty (m(f^{-1}([k,k+1)))=m(f^{-1}([0,\infty)))=m(A)=<\infty$, because the problem says $m(A)<\infty$. My problem is that I'm struggling with the multiplying $k$ inside of the sum. I don't know what to (guess using $\sum_{n=0}^\infty(m(\{x\in A:f(x)\geq n\}))<\infty$, but I don't know how) do with that $k$ to prove that $$\sum_{k=0}^\infty (k\cdot m(f^{-1}([k,k+1)))<\infty.$$
I will thank any answer or comment.
Answer: Hint: Let $a_n=m\{x: f(x) \in [k,k+1)\}$ and $s_n=a_n+a_{n+1}+...=m\{x: f (x) \geq n\}$. Then $\sum k a_k =a_1+2a_2+3a_3+...=s_1+s_2+...=\sum m(f(x) \geq n)$. To finish the proof you have to know that the sum and integral on RHS can be interchanged. But that is true by non-negativity.
Comment: Thanks for your answer! I think I've figured it out thanks to your hint.


Run python in background from other python program and continue

Question: How to run a python program in between another python program
os.system(r'pythonw.exe D:\text.py')

I have tried this but it had pause execution between the main program
and also tried
subprocess.run(r'pythonw.exe D:\text.py')

suppose I have program text.py which notify me every hour
I have main program i.e.
code...
os.system(r'pythonw.exe D:\text.py')
code...
print("hello world")

my command should run text.py in the background and execute the following code.
Answer: You can use Python Library named "Soldier". This is very easy to use and implement.
https://pypi.org/project/soldier
import soldier

firefox_process = soldier.run('firefox', background=True)
firefox_process.pid


Write continuously to output file using Python lxml

Question: I'm writing big chunks of data from a database down to an XML file. I'm using Python and its lxml library to create the file.
I notice that it's generating the whole XML file in memory and then outputs it to the file at the end, is there's a way to dump the xml file for every X number of database objects?
import lxml.etree as etree
import os

root = etree.Element('root')
db_obj1 = etree.SubElement(root, 'item')
db_obj2 = etree.SubElement(root, 'item')
db_obj3 = etree.SubElement(root, 'item')
et = etree.ElementTree(root)
et.write(sys.stdout)

I've tried with ElemenTree().write(), but can't find any setting or best-practice on how to accomplish this in the documentation at http://lxml.de/api/lxml.etree._ElementTree-class.html.
Looking to decrease the total memory footprint.
Comment: Why would you want to dump a database to XML at all?  You might consider writing to a more natural rectangular format like CSV or HDF5 or something else which would avoid this issue completely, as well as provide a more space-efficient and regular format for your data on disk.
Comment: Integration with a legacy system @JohnZwinck, can't change this.
Answer: From the lxml docs:

It is a common pattern to have one or more nested element() blocks,
  and then build in-memory XML subtrees in a loop (using the ElementTree
  API, the builder API, XSLT, or whatever) to write them out into the
  XML file one after the other. That way, they can be removed from
  memory right after their construction, which CAN LARGELY REDUCE THE
  MEMORY FOOTPRINT of an application, while keeping the overall XML
  generation easy, safe and correct. [Emphasis mine].

filename = "/tmp/somefile.xml"
with ET.xmlfile(filename, encoding='utf-8') as xf:
    xf.write_declaration(standalone=True)
    xf.write_doctype('<!DOCTYPE root SYSTEM "some.dtd">')
    with xf.element('root'):
        for value in '123':
            # construct a really complex XML tree
            el = ET.Element('item', attr=value)
            xf.write(el)
            # no longer needed, discard it right away!
            el = None

writes
<?xml version='1.0' encoding='utf-8' standalone='yes'?>
<!DOCTYPE root SYSTEM "some.dtd">
<root><item attr="1"/><item attr="2"/><item attr="3"/></root>

to the file.


How can I hook into the system Contact form Message Sent process and alter the HTML generated?

Question: I have a Drupal site where I am using the sited-wide system Contact to allow visitors to send messages to the site owner.  Once a visitor submits the form, the user is redirected to the home page and a sticky appears at the top reading:
Your message has been sent.
My question then, is it possible to change this message and add some HTML to the output?  Ideally I would add some javascript to create a virtual page view for the Google Analytics account so that I can track it as a Goal Conversion.
I imagine this would be a hook_ function sitting in my template file, but I'm relatively new to this process.
Answer: Sure, just use hook_form_alter() and add a callback to $form['#submit']. The easiest way to find your form's id is inspecting it using firebug. This article has a lot more detailed info: http://www.lullabot.com/articles/modifying-forms-drupal-5-and-6.
In short:

Find the id for the contact form, "contact_mail_page" or something (notice how hifens turn to underscores)
In a new custom module implement either: yourmodule_form_alter($form_id, &$form, 'contact_mail_page') or yourmodule_form_contact_mail_page_alter($form_id, &$form)
Modify the contact form as you want using the $form variable passed by reference
Comment: thanks for your response.  I've read through that article and I'm no closer to this solution.  Do you have an example? That article shows how to write a hook for a module, but the Contact form is a system module so if I make the change their then a core update would blow that change away.  Maybe I'm still missing something.
Comment: I've edited the first post to include more details
Comment: thanks for the update.  I'm still not clear on how to implement your solution.  So I create a new module and put the code where?  As I mention above, I'm new enough to Drupal that I've never had to write a module. Maybe I should just use that webform module.  Thanks again for any help.
Answer: The webform module might be more suited to your needs if it's just a general site wide form.
Comment: I meant to add... the webform module allows you to specify a redirect url which is used when the form is successfully submitted. This also means you can manage the success page easily as a regular content type.

Hope this helps.

James
Answer: @shanabus, you're understanding of "write a hook for a module" is incorrect. The main goal of creating a custom hook for any existing module including a system module is to over-ride the behaviour. This is how you customise Drupal to a specific sites needs. You'll not edit the existing code but rather add new code that uses the "hook" system to add new/replacement functionality.
Typically a custom hook function lives in a custom module created specifically for the site by the author of the site. Consider it a place to store your site specific customisations that you'll not be sharing with the greater Drupal community. A classic use would be a hook_form_alter as Alex suggests above.
Consider looking at the hook API documentation for more details: http://api.drupal.org/api/drupal/includes--module.inc/group/hooks. This is often a concept that new Drupal developers struggle with so don't be discouraged if it takes some extra effort to get the concept figured out. We've all been here :)


Can a goalkeeper be guilty of DOGSO via a six-second-rule offence?

Question: I'm trying to understand whether the letter of the law does allow for a goalkeeper to be potentially sent-off for denying the opposing team an obvious goal-scoring opportunity by breaking the six-second rule, or if I'm missing something in the text or there is some other source I'm unaware of that prevents this possibility (e.g. an official communication from the IFAB).
The situation I'm envisioning may sound absurd at first, seeing how a goalkeeper who's keeping the ball in his hands (for less or more than six seconds) can't possibly be stopping a goal-scoring opportunity either way—as the ball is firmly in his hands the whole time. Furthermore, there is this passage here (Law 12.1 Direct Free Kick):

The goalkeeper has the same restrictions on handling the ball as any
other player outside the penalty area. If the goalkeeper handles the
ball inside their penalty area when not permitted to do so, an
indirect free kick is awarded but there is no disciplinary
sanction.

However, this passage is very specific in saying 'handling the ball', which refers to the act of touching the ball with one's own hands. And if we look just a little beyond, we find that (Law 12.2 Indirect Free Kick):

A goalkeeper is considered to be in control of the ball with the
hand(s) when: [...] bouncing it on the ground or throwing it in the
air.

Now, follow me on this: am I correct in saying this covers the entire action of throwing the ball into the air up until the moment when it is kicked? We do see it very often: players are penalised all the time for interfering with the goalkeeper while he's throwing the ball in the air to kick it, proving how the ball is indeed considered to be in the keeper's possession until the kick takes place.
Therefore, consider this scenario: at 5 seconds, the goalkeeper throws the ball into the air—and it's kind of a vertical throw, maybe also due to wind or other conditions. If he let it be, the count would just stop at five. But an attacking player from the opposing team sees the chance and runs towards the ball, and he's alone and he could clearly score on an empty goal. So the keeper notices this, and he immediately runs towards the ball and kicks it away before the attacker could reach it. When he touches it with his foot, 7 seconds have elapsed in total. In this situation:

the goalkeeper was in control of the ball with his hand(s) for 7 full seconds, using the same wording from Law 12.2, but at the same time he did not commit an offence by handling the ball (as per Law 12.1), since the final touch is the one that realised the offence, and that was a kick;
the goalkeeper has artificially extended his control of the ball from 5 to 7 seconds: it is only due to his kick that his personal six-second clock kept running (otherwise it would have stopped at 5), meaning that him breaking the six-second rule was a deliberate act and the offence that directly caused him to deny the opponent an obvious goal-scoring opportunity.

And by the way, I know someone will say: 'There's no way the referee is gonna keep track of seconds so precisely'. But I should point out that if this is indeed DOGSO, then this is a potential red card situation which would therefore call VAR into action as sending-off offences are part of the protocol (VAR protocol Principles). Furthermore, counting seconds is an objective fact (and it's also easy to do on a computer), meaning that, according to the protocol once again, the whole check could start and end without even involving the referee (VAR Protocol Procedures):

For factual decisions e.g. position of an offence or player (offside),
point of contact (handball/foul), location (inside or outside the
penalty area), ball out of play etc. a 'VAR-only review' is usually
appropriate

So, what do you think? Is a red card indeed appropriate in this situation, and if not, why?
Comment: There simply is no goal scoring opportunity with the goalie having the ball. So nope, not possible,
Answer: You are unfortunately following in the footsteps of many spectators, fans, and even players, and not just in football.
Stop overthinking it.
The laws are clear enough: "being in control of the ball with the hand(s)" includes when throwing it in the air. The goalkeeper cannot be given a card for the offence of doing this too long in the box. No VAR will ever intervene in a situation like this because it is entirely outside their remit, except if an overzealous referee gave a red card or penalty kick for this offence alone.


word problem in probability

Question: i would like to exactly what is asked in this problem:
The probability that A can solve the problem is $1/4$ and that B can solve it is $1/3$. If both of them try, what is the probability that problem will be solved.
$A. 1/4$
$B. 7/12$
$C. 1/3$
$D. 1/2$
$E. 1/12$
i will say what is a point of my confusion,when we are   trying to find probability of two independent events,we are multiplying  probability of each  other to get probability that both event occur .now first it is what i have tried and got $1/12$,but  it shows me that it is not correct,now  i have this question and please could help me,how can i translate if both of them will try into probability language?
$P(A or B)$ is not correct because it means that one or second will solve,so it means that $P(A and B)$,but they are independent are this would not be product of their probabilities?let us denote probability that $A$ will solve by $P(A)$, and  probability that  $B$ will solve by $P(B)$
$P(A)=1/4$
$P(B)=1/3$
now what would be $P(A and B)$?would not it be $1/4*1/3$?
or i am calculating wrongly and it would be $P(A or B)$
Comment: It's 1 minus probability of "not A and not B".
Comment: but in terms of $A$ and $B$ itself?
Comment: probability of not-A is 1 minus probability of A.
Comment: @GerryMyerson this one i know,i am asking different thing
Comment: OK: what thing is it that you are asking?
Answer: You're trying to find the probability that either A or B solves the question. An easier way to think about it is this - the only way that the problem does not get solved is if both cannot solve it. We can find the probability that A and B cannot solve it, and subtract this from one.
As you mentioned, the probability of A not solving AND B not solving is simply the product of these. Hence, we find the probability that A cannot solve, which is $1 - \frac 14 = \frac 34$. Similarly, we find the probability that B cannot solv, which is $1 - \frac13 = \frac 23$. Multiplying, we have $ 1- \frac34 \times \frac 23$. This gives us an answer of $ 1- \frac12 = \frac 12$.
Hope that helps. Cheers!
Comment: @dato: The question asks for the probability that the problem gets solved. This can happen in any of three ways: A solves it and B does not, B solves it and A does not, or both of them solve it. The formula for P(A and B) only gives you the probability of the third "way".
Comment: i see,i understood  thanks very much
Comment: only one thing is why i can't use formula for $P(A and B)$
Comment: for such problem,we are saying that it is equal to  1- neither A nor B,but why it could not be simply product?
Comment: Because the product obviously gives the wrong answer. The product is **smaller** than either of the individual probabilities, whereas the answer must be **bigger** than either of the individual probabilities.
Comment: Can anyone suggest me a book where these probability problems are discussed clearly?
Answer: If you want to do it directly (though the proposed way by Gerry is both easier to understand and easier to carry on, imo), you can argue as follows:
The event whose probability we want is "$A$ solves the problem or $B$ solves the problem", and this in numbers is given by "prob. that $A$ solves + prob. that $B$ solves minus the prob. that they both solve
$$\frac14+\frac13-\frac1{12}=\frac6{12}=\frac12$$
Can you see why we have to substract the probability that they both solve the problem?
Comment: but last one probability that  they both solve it is asked exactly,i am confused
Comment: @dato, the fact that both $\,A,B\,$ solve the problem **already** is contained or observed in the "either $A$ solves the problem or $B$ solves the problem" part, since "or" in mathematics is inclusive, not exclusive.
Comment: i see ,so in other  word,probability that if they both will try,problem would be solved is the same  as either A or B will solve?
Comment: consider that it has answer  $1/12$ for confusion
Comment: Yes to your first question, as long as we're clear that saying "either A solves the problem or B solves the problem" contains within it the even "Both A and B solve the problem".

word problem in probability


Deploy ecommerce website

Question: How I can deploy my Asp.NET web application without affect the current application? My application is an e-commerce web site and I need it to be the shortest possible time offline.
Thanks.
Comment: This is more of a ServerFault question...
Answer: Set it up on the production box with dev headers (ie dev.website.com) in IIS and then when you've tested and ensured it will work, change the headers to be www.website.com and website.com

Deploy ecommerce website


Expose "all changes since" via wordpress JSON api

Question: I'm struggling to find a solution to what I thought would be a common requirement so I'm hoping someone can help me with some pointers on what to search for/areas to explore.
Background
I'm building an iOS mobile app. I'm storing data locally using realm.io. The app is preinstalled with a snapshot of the content of a Wordpress mySQL database (it uses custom types). The content of the WP database is only written via the Wordpress install, the mobile app cannot write data.
Objective
So, I want to be able to check for changes since a given date (whenever the local database was last updated) and send the changed records to the mobile app (via the wp JSON api?).
I think I can fetch "posts since a date" but I need a full list of all create, update and delete operations since a given date.
Since the app is read-only I thought this type of one-way sync would be pretty straight forward but I can't find a common solution.
Any ideas to point me in the right direction would be great. Obviously, if anyone has any experience of doing this sort if thing with realm.io then that would be amazing :-)
Answer: Realm doesn't support yet any sort of synchronization mechanism across different files. We have an issue about that though, but you're likely searching rather for a solution in the immediate future.
Update: Realm launched the Realm Mobile Platform. This offers synchronization functionalities and would greatly simplify the solution for this use case.
You could use e.g. the server-side Node.js binding to pull new data from your MySQL Wordpress installation and push them to a global Realm served by the Realm Object Server. This can be read-only synchronized from the mobile apps, which would automatically receive the deltas and provide updated data to your users.

Whatever mechanism you come up yourself though in the meantime, it would require that you have read-write access from your iOS application to the realm database, so that you can update it with new data.
Pushing changed records as you describe is likely not going to work.
Apple's Push Notification service (APNS), which is the only back communication channel that works when your app is in the background or suspended, allows you to send very small payloads. You would use that to signalize your iOS app, that something changed on the server-side and there is new data to load. You would then initiate a request to a JSON-based API, wait for the response, map the returned JSON to Realm objects and store them in your database.
You want probably read more in the "Downloading Content in the Background" section of background execution chapter in the official App Programming Guides for iOS.
While pre-seeding the database from the app bundle seems like a nice idea, because the user wouldn't need to wait initially after downloading the app, that will enlarge the app itself with data, which might become in the future completely irrelevant.
Comment: **Update**: We introduced the Realm Mobile Platform, which brings synchronization to your Realms. I updated the answer accordingly as I think it greatly simplifies this use-case.
Comment: thanks for your comments. I've not been clear; the ios app **will** be writing to the local realm db but won't be sending any updates back to the 'master' database on the server.

The scenario is something like this: 1000 records in realm pre-loaded with the app, 'mirroring' a master mySQL db on the server. After a week, the app is opened and 4 records have been added, 3 records have been changed and 1 deleted from the master db. I only want to expose details of these 8 records rather than re-send a new snapshot of all 999 records to the app.
Comment: I had seen that issue/thread about syncing and then spent some time looking at Firebase which has great support for real-time syncing. However, because I only need one-way syncing I think the benefits of Realm outweigh the negatives of having to roll my own sync logic. I'm now looking at Dreamfactory which seems like it might do what I need. I'll post an update if it does :-) Thanks.


How to host multiple websites on one server...with one public IP...with Apache

Question: I am a new to the server world, and I need some assistance setting one up.
Here is my question: How can I host multiple websites on one server...with one public IP address...with Apache, without having to have registered domain names for each website?
I would like to be able to type in my public IP address from anywhere...and have the ability to view any particular website on the server. Perhaps...by typing this into a browser address bar for one website--http://xx.xxx.xxx.x:80, and this into the address bar for another website--http://xx.xxx.xxx.x:81.
I'm not sure how to do this; I haven't been able to find tutorials or guides for this goal.
I am using Apache 2 on Ubuntu Server 12.04. My server computer is going through a home router (is using the router's public IP address).
Any advice is greatly appreciated.
Comment: How is it possible not to find this? http://httpd.apache.org/docs/2.2/en/vhosts/ Do you consider your problem (not finding the most basic document) "professional IT"? Only that is on-topic here.
Comment: Welcome to ServerFault.  Unfortunately, your question is probably going to be closed, because it relates to computers in a home setting, and hence is offtopic.  Please check out the [faq]
Comment: Not to mention, domain names are quite cheap. If you're a developer, you should already have one. If not, go get one.
Answer: It's a <VirtualHost ipaddr:port> line in the Apache config files. For ubuntu, I believe there is a directory for these in /etc/apache2/vhost.config/ or something like that. You'd use a VirtualHost section for each of your sites, but on a different port for each.


Uninstalling an entity "BaseFieldDefinition" on module uninstall

Question: I'm attempting to create my first module, which doesn't do much. I'm looking to:

Add a couple of extra fields to a menu item.
Save them in to the menu_link_content_data table.
Access them within theme templates.
Remove the items from the menu_link_content_data table when the module is uninstalled.

I've managed (after A LOT of reading through the awful docs) to get 1-3 completed, but 4 is giving me issues.
Does anyone know how to do this?
I've tried the following in my_module.module file:
function detailed_menu_uninstall() {
  // Deletes the field storage for the 'detailed_menu_heading' base field.
  $definition = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions('menu_link_content')['detailed_menu_heading'];
  \Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionDelete($definition);
}

Where detailed_menu_heading is the name of the table that was added to the menu_link_content_data table (using HOOK_entity_base_field_info(), with $fields['detailed_menu_heading'] = BaseFieldDefinition::create()). But it doesn't seem to work.
If anyone could give some advice (while I still have a few homer-esque strands of hair left), that would be awesome!
Answer: Okay,
After a few more hours of head scratching and hair pulling, I think I've found (or so it would seem) the answer... I'm writing this for myself, as well as anyone else who might stumble across it by route of the all-mighty Google.
From what I can gather, the EntityManager Service has been split up in to several parts. As a result, the bit we're interested in lies within the Entity Field Manager service / class.
We can use this to grab the definition (which I believe is Drupal for - the set of instructions that provide relevant information about an entity [or part thereof]) of our custom entity field.
Now that we have a Drupal readable way of referencing the entity field we're interested in manipulating, we need to actually manipulate it.
Soo... There are a few ways to do this, but I believe the best way is to make use of Drupal's Field Definition Listener service, that seems to do the job we're after. What this listener will do is handle getting rid of all the places that the entity field has been buried away within Drupal itself. From the sounds of the service name it should be using the event dispatcher system somewhere, somehow; although, looking at the source I can't find anywhere that actually does this? (this is in contrast to field_storage_definition.listener, which does dispatch an event, but didn't seem to work, and would white screen with an SQL error, complaining that it can't find the given field when used...)
With these two pieces of the puzzle, it's just a case of putting them together to get the functionality working correctly.
I ended up with:
function detailed_menu_uninstall() {

  // Grabs the entity Field Manager service.
  $entityFieldManager = Drupal::service('entity_field.manager');

  // Grabs the Field Definition listener.
  $fieldDefinitionListener = \Drupal::service('field_definition.listener');

  // Ensures(?) that the field storage for the 'detailed_menu_heading' base field is deleted
  // properly when the module is uninstalled.
  $definition = $entityFieldManager->getFieldStorageDefinitions('menu_link_content')['detailed_menu_heading'];
  $fieldDefinitionListener->onFieldDefinitionDelete($definition);
}

One thing to note is that if I add more definitions to be installed, and consequently uninstalled in this hook, they don't actually need to be put in the uninstall hook, and I'm not sure why... It's as if Drupal just needs a nudge to be told that some things need to be removed from the entity properly, and Drupal does the rest to check what that is? I'm really not sure.
Comment: **Don't do this.** Drupal automatically deletes basefields when you uninstall the corresponding module. Doing a manual uninstall like this will break your site, because Drupal now want's to delete a database table that no longer exists due the manipulations done in the uninstall hook.


Interpolating function for a nested radical $y_x=\sqrt{a+y_{x-1}}$

Question: Is it possible to find an explicit expression for a function, which at integer points takes values:
$$y_1=\sqrt{a}$$
$$y_2=\sqrt{a+\sqrt{a}}$$
$$y_3=\sqrt{a+\sqrt{a+\sqrt{a}}}$$
etc.
We can take $y_0=0$. Then, defining $y$ for negative integers is not a problem:
$$y_n=\sqrt{a+y_{n-1}}$$
$$y_{n-1}=y_n^2-a$$
Thus, for $n=-1,-2,-3,\dots$ we have a quadractic map:
$$y_{-1}=-a$$
$$y_{-2}=a^2-a$$
$$y_{-3}=(a^2-a)^2-a$$

Now I have no idea how to define $y_x$ for $x \notin \mathbb{Z}$. We have an obvious functional equation:

$$y_{x+1}=\sqrt{a+y_x}$$
For $x \to 0$ we have $ y_x \to 0$. But how to find an explicit expression? Maybe in terms of an integral or some special functions? Or how to even approximate this function?

At $x \to 0$ we should have $ y_x \to cx$ for some constant $c$.
This task seems hopeless to me even though such things as Gamma function or general Harmonic numbers exist, despite the fact that interpolation there is not obvious at all.

For the special case $a=2$ @NgChungTak gave an interpolation:
$$y_{x}=2\cos \dfrac{\pi}{2^{x+1}}$$
The plots of the sequence and the function are shown below:

However this kind of function doesn't work with the other values of $a$, even for $x>0$. For $a>0$ and $x>0$ the rate of convergence increases with $a$.
We can plot normalized sequences for different $a>0$.
$$f(n)=\frac{2 y(n)}{1+\sqrt{1+4a}}$$

I think it makes sense to consider a Quadratic map first, since a lot of exact solutions for special cases were found for it, and then extrapolate the solutions to negative indices.
Comment: In particular, $a=2$ and $x\in \mathbb{R}^{+}$, $y_{x}=2\cos \dfrac{\pi}{2^{x+1}}$
Comment: I think you'll have to settle for recursion. But you _can_ find a closed form for the limit.
Comment: I had no doubt that you knew. Was just pointing that out for the general audience. But for the expression itself, I would forget about closed form.
Comment: well, in formal power series, if you move the attracting fixpoint to $0$, your transformation is conjugate to a linear map (in a unique way), and you can then interpolate in an obvious way. This also gives you power series that perform "half a step" (or other fractional steps). I have no idea how to prove anything about their radius of convergence though
Comment: if $g(x) = x^2-a$, and $z$ is the fixpoint, then you can probably define $y_k = \lim_{n \to \infty} g^{\circ n}(z - \exp((1-k)\log (z-y_n) + k\log (z-y_{n+1})))$
Answer: Let $f(z) = z^2-a$ with $a \neq - \frac 14$.
If $a>0$, $f$ has a repulsive fixpoint at $\tau = (1+\sqrt{1+4a})/2$ (and most of the time, the other fixpoint is repulsive too, but let's take this one). I will note $\lambda$ the derivative of $f$ at $\tau$, which is $\lambda = 2\tau$ (larger than $1$), so near this fixpoint, $f$ acts almost like the dilation $g(z) = \lambda \tau$.
On a small disk $D$ around $\tau$ we can define $f^{-1}$ such that it sends $D$ to itself, and then according to Koenigs, there is a (unique) holomorphic injective function $h$ on $D$ into a small neighbourhood of $0$ such that $h(\tau) = 0, h'(\tau) = 1$, and forall $z \in D$, $h ( f^{-1} (z)) = g^{-1}(h(z))$.
Since $h$ is injective we can define $t = h^{-1}$ on $h(D)$, and then the functional equation can be put in the form $t(g(z))
 = f(t(z))$ for $z$ close enough to $\tau$.
Now, since $g$ is a simple dilation and $f$ is entire, this means $t$ has an analytic continuation to the entire complex plane. $t$ has an essential singularity at infinity, so it takes every value infinitely often (with no exception because of the functional equation).
Let $w \in \Bbb C$ be a zero of $t$. It has a corresponding sequence $(y_n)_{n \in \Bbb Z}$ defined by $y_n = t(\lambda^{-n}w)$.
Then $y_0 = 0, y_1 = \pm \sqrt a, y_2 = \pm \sqrt {a \pm \sqrt a} \ldots$ and so on.
If you choose a value for $\log \lambda$, this allows you to interpolate the sequence to all of $\Bbb C$, defined by $y_n = t(w\exp(-n\log(\lambda)))$.
Suppose two zeroes $w_1,w_2$ give rise to the same sequence. Then, by the identity theorem we have $t(w_1z) = t(w_2z)$ forall $z \in \Bbb C$, and differentiating this relation at $z=0$ gives $w_1 = w_2$.
This work for any nonzero value too, so the points $(w,y_0)$ are in a bijective correspondance with the sequences $y_n = f(y_{n+1})$ that eventually converge to $\tau$.
To compute the interpolation approximately, you can plunge into the sequence by applying $f^{-1}$, then once you are close enough to the fixpoint, approximate $f$ with $g$ to interpolate however you need, then go back by iterating $f$ :
$y_n = \lim_{m \to \infty} f^{\circ m}(\tau+\lambda^{-n}(y_m-\tau))$.
This works in theory, but you may run into precision issues for both iterations.
You may want to instead use a power series to approximate $t$ and $h$ once you're close enough to $\tau$.
If you wonder what the fractional iterates of $f^{-1}$ would look like geometrically, it seems they aren't necessarily algebraic : they should have branch points at all the critical values of $t$.
Differentiating the functional equation of $t$ gets you $g'(z).t'(g(z)) = f'(t(z)).t'(z)$, which means $\lambda t'(g(z)) = 2t(z).t'(z)$, so the critical points of $t$ are the the zeroes of $t$ (critical points of $f$) along with their images by any number of dilations.
Furthermore, $t'$ only has simple zeroes because iterating $f$ on $0$ will never loop back to $0$.
So the critical values are $0,f(0),f((0)),\ldots$, and there $t^{-1}$ (and thus all the fractional iterates, and well everything really) have a square root-like singularity.


submit handler not working when using hook_form_alter

Question: I am using hook_form_alter to add validation and submit handlers to the registration form:
<?php
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'user_register_form') {
    $form['#validate'][] = 'blah_register_validate';
    $form['#submit'][] = 'blah_register_submit';
  }
}
?>

They were both working fine until I added the following hook:
<?php
function MYMODULE_user_login(&$edit, $account) {
  drupal_goto('user');
}
?>

The validation function still works but the submit function gets bypassed completely - it doesn't get called at all by form_execute_handlers() in form.inc. As soon as I remove hook_user_login, it works again. Anyone have an idea why?
Update:
hook_user_login() gets called by user_login_finalize() in the User Module. Unfortunately,  this happens before my submit function gets called - so obviously when I use drupal_goto() the submit function will never get called. Can anyone confirm if I am adding the submit handler correctly, is there another way?
Answer: If you check out drupal_goto() you can see it calls drupal_exit() which is not something you want to do in a hook. The comments for hook_user_login() discuss this a little, finishing with this snippet:
function mymodule_user_login(&$edit, $account)
{
  // Your logic will set $redirection to the desired location
  $redirection = 'node/394';

  // Unless there is already a redirection going, or the user is trying to reset his password, we redirect to $redirection.
  if (empty($_GET['destination'])
    && !is_null($redirection)
    && (!isset($_POST['form_id']) || $_POST['form_id'] != 'user_pass_reset'))
  {
    $_GET['destination'] = $redirection; // Should we use $edit['redirect'] instead..?
  }
}

Also mentioned is the login destination module.
Comment: This is the right answer: As many other hooks, `hook_user_login()` is not supposed to call `drupal_goto()`, which would stop other hooks (and other form handlers) from being executed.
Answer: read about drupal_get_destination function. Used to direct the user back to the referring page after completing a form.
You should try something like this(If I understand the question) :
 drupal_goto('user', array('query'=>drupal_get_destination()) );
If you trying to add a new Submit handler to an existing user_login_form you should try this:
 $form['new_submit'] = array(
        '#type' => 'submit',
        '#value' => 'Submit',
        '#submit' => array('blah_register_submit'),//name of your submit function
    );

Comment: Ah sorry, maybe I didn't explain my problem very clearly. The problem is that my submit function is being called 'after' hook_user_login. So if I add drupal_goto, the page redirects and the submit function doesn't get called at all. So nothing to do with the destination GET variable.
Comment: This plain wrong: To add a submission handler to an existing submission button, you don't add a new submission button.


Why this runs in the same time as the secvential version?

Question: I have the folowing code:
   #pragma omp parallel for private(dot,sum,c,cc)
   for(r=0;r<rows;r++){

       for(c=0;c<cols;c++){
       dot = 0.0;
       sum = 0.0;

       for(cc=(-center);cc<=center;cc++){
        if(((c+cc) >= 0) && ((c+cc) < cols)){
           dot += (float)image[r*cols+(c+cc)] * kernel[center+cc];
           sum += kernel[center+cc];
        }
     }
     tempim[r*cols+c] = dot/sum;
  }
}

This code runs with the same speed as secvential version. What i do wrong? The rows , col values are like 4000+ and center something near 10-30.
Comment: Possibly a stupid question, but have you told your compiler to work with OpenMP?
Answer: You could use profiling tools to check how many threads it uses actually. If it is single thread, you could check

if the OpenMP support is enabled in for compile options
if environment variables or OpenMP APIs have disabled multi-threading, i.e, OMP_NUM_THREADS, OMP_DYNAMIC, omp_set_num_threads(), ...


Kalman filter vs. smoothing splines

Question: Q: For which data is it appropriate to use state-space modeling and Kalman filtering instead of smoothing splines and vice versa? Is there some equivalence relationship between the two?
I'm trying to get some high-level understanding of how these methods fit together. I browsed through Johnstone's new Gaussian Estimation: Sequence and Multiresolution Models. It surprised be that there was not one mention of state-space models and Kalman filtering. Why wouldn't that be in there? Isn't that the most standard tool for these sort of problems? The focus, instead, was on smoothing splines and wavelet thresholding. I'm now very confused.
Answer: Regarding your question on the equivalence, fitting a univariate local linear trend model using a Kalman filter is equivalent to fitting a cubic spline; see
Time Series Analysis by State Space Methods, Section 3.11 for instance.
I think you are right in pointing that the Kalman filter and smoother are sometimes neglected when they could be put to good use. In particular, I find that the Kalman smoother is much more convenient with irregularly spaced and/or missing data.
Comment: State-space based algorithms are very powerful to cope with splines with a scalar argument, or even with tensor product splines. An example is in  my answer to [this question on smoothing with derivatives](https://stats.stackexchange.com/q/428558/10479). Due to the non-stationary processes involved - known as "Intrinsic Random Functions"- a diffuse initial state is generally required, as now implemented in several toolboxes or packages devoted to SS and Kalman.
Comment: @Tusell. Thx for the reply. I'm going to have to check out the book you pointed out. Not easy to find books that put it all together like that.
Comment: @Yves, a very thorough answer indeed, that I saw a couple of days ago and quickly bookmarked. Thank you anyway for bringing this to my and everyone else's attention.

Kalman filter vs. smoothing splines


XLST to Transform an XML Dataset of Arbitrary Recursive Structure to an Organization Chart

Question: I answered my own question, but would appreciate constructive feedback on how I could improve it for speed, clarity, or a more idiomatic solution. :)
Please skip to ~EDIT~ or read through to it if you'd like to understand the original context of my question.
In a nutshell, I have an XML dataset that looks something like this:
<org>
  <dep>
    <nm>Department</nm>
    <emps>
      <emp>

      </emp>
      <emp>

      </emp>
    </emps>
    <deps>
      <dep>
        <nm>Sub Department</nm>
        <emps>
          <emp>

          </emp>
        </emps>
      </dep>
    </deps>
  </dep>
</org>

That's a very small sample, but the key take away is that a department can can any number of employees and any number of sub-departments.  Usually, such a data-set would be bottom heavy, having few people at the top (leaders), and more and more as you navigate further down into the sub-departments.  What I have been trying to do--with a semi-breakthrough just today, actually--is figure out how to transform that XML Dataset into a somewhat no-frills, HTML Organization Chart.
What I originally came up with spat out nested div's with strategically placed "float: left;" and "clear: both;" styles, and looked something like this:
+-------------------------------------------------------+
|                         Dep                           |
| +------------------------+                            |
| | +--------+ +---------+ |                            |
| | |  Emp   | |   Emp   | |                            |
| | +--------+ +---------+ |                            |
| +------------------------+                            |
| +---------------------------------------------------+ |
| | +----------------------+ +----------------------+ | |
| | |         Dep          | |          Dep         | | |
| | | +--------+ +-------+ | | +--------+ +-------+ | | |
| | | |  Emp   | |  Emp  | | | |  Emp   | |  Emp  | | | |
| | | +--------+ +-------+ | | +--------+ +-------+ | | |
| | +----------------------+ +----------------------+ | |
| +---------------------------------------------------+ |
+-------------------------------------------------------+

Apologies for my crude ASCII art.
I had wracked my brain trying to come up with an XSLT transform that could produce this, instead:
+-------------------------------------------------------+
|                         Dep                           |
|             +------------------------+                |
|             | +--------+ +---------+ |                |
|             | |  Emp   | |   Emp   | |                |
|             | +--------+ +---------+ |                |
|             +------------------------+                |
| +---------------------------------------------------+ |
| | +----------------------+ +----------------------+ | |
| | |         Dep          | |          Dep         | | |
| | | +--------+ +-------+ | | +--------+ +-------+ | | |
| | | |  Emp   | |  Emp  | | | |  Emp   | | Emp   | | | |
| | | +--------+ +-------+ | | +--------+ +-------+ | | |
| | +----------------------+ +----------------------+ | |
| +---------------------------------------------------+ |
+-------------------------------------------------------+

Can't really do that with floating div's, and while absolute positioning works, the only div's that have a fixed width are the employee div's, so I needed to somehow get the total width of all contained elements to properly size each department, departments, and employees div.  Today, I had the idea that all I'd need to do is get a count of all the bottom-most employees div's at each department div, since organization charts tend to fan out from top to bottom:
<xsl:template match="dep">

  <xsl:variable name="emp_count"
    select="count(descendant::emp[parent::emps/parent::dep[count(child::deps)=0]])"/>

  <xsl:variable name="width" select="$emp_count * $emp_width"/>
  <xsl:variable name="mleft" select="$width div 2"/>

    <xsl:apply-templates/>

</xsl:template>

In a nutshell, while the above omits much, it works, barring any typos I have.  But!  Here's the problem.  That ASSUMES that the organization follows the time-honored tradition of being bottom-heavy.  How do I transform a org document that should output something like this?
+---------------------------------------------------------------+
|                             Dep                               |
| +-----------------------------------------------------------+ |
| | +--------+ +---------+ +--------+ +---------+ +---------+ | |
| | |  Emp   | |   Emp   | |  Emp   | |   Emp   | |   Emp   | | |
| | +--------+ +---------+ +--------+ +---------+ +---------+ | |
| +-----------------------------------------------------------+ |
|     +---------------------------------------------------+     |
|     | +----------------------+ +----------------------+ |     |
|     | |         Dep          | |          Dep         | |     |
|     | | +--------+ +-------+ | | +--------+ +-------+ | |     |
|     | | |  Emp   | |  Emp  | | | |  Emp   | | Emp   | | |     |
|     | | +--------+ +-------+ | | +--------+ +-------+ | |     |
|     | +----------------------+ +----------------------+ |     |
|     +---------------------------------------------------+     |
+---------------------------------------------------------------+

Ah...  Nothing like a corner case to ruin what I initially thought would have solved the problem that wracked my brain all week.  In truth, that corner case might not ever emerge.  In truth, maybe instead of using div's, using nested table's might have been easier.
But, now I'm here, and I'm wondering...
How would one go about calculating the width of a top-level department div with this corner case in mind, also keeping in mind that this corner case could occur more than once and  arbitrarily deep within the hierarchy?
~EDIT~
I want to turn this XML Source Document:
<?xml version="1.0" encoding="UTF-8"?>

<dep>
  <nm>Dep</nm>

  <emp>
    <nm>Emp</nm>
  </emp>

  <emp>
    <nm>Emp</nm>
  </emp>

  <emp>
    <nm>Emp</nm>
  </emp>

  <dep>
    <nm>Sub Dep 1</nm>

    <emp>
      <nm>Emp</nm>
    </emp>

    <emp>
      <nm>Emp</nm>
    </emp>

    <dep>
      <nm>Sub Dep 1-1</nm>

      <emp>
        <nm>Emp</nm>
      </emp>

      <emp>
        <nm>Emp</nm>
      </emp>

      <emp>
        <nm>Emp</nm>
      </emp>
    </dep>

    <dep>
      <nm>Sub Dep 1-2</nm>

      <emp>
        <nm>Emp</nm>
      </emp>

      <emp>
        <nm>Emp</nm>
      </emp>
    </dep>
  </dep>

  <dep>
    <nm>Sub Dep 2</nm>

    <emp>
      <nm>Emp</nm>
    </emp>

    <emp>
      <nm>Emp</nm>
    </emp>

    <emp>
      <nm>Emp</nm>
    </emp>

    <emp>
      <nm>Emp</nm>
    </emp>

    <dep>
      <nm>Sub Dep 2-1</nm>

      <emp>
        <nm>Emp</nm>
      </emp>
    </dep>

    <dep>
      <nm>Sub Dep 2-2</nm>

      <emp>
        <nm>Emp</nm>
      </emp>

      <emp>
        <nm>Emp</nm>
      </emp>
    </dep>
  </dep>

  <dep>
    <nm>Sub Dep 3</nm>

    <emp>
      <nm>Emp</nm>
    </emp>
  </dep>

  <dep>
    <nm>Sub Dep 4</nm>

    <emp>
      <nm>Emp</nm>
    </emp>

    <emp>
      <nm>Emp</nm>
    </emp>

    <dep>
      <nm>Sub Dep 4-1</nm>

      <emp>
        <nm>Emp</nm>
      </emp>

      <emp>
        <nm>Emp</nm>
      </emp>

      <emp>
        <nm>Emp</nm>
      </emp>

      <dep>
        <nm>Sub Dep 4-1-1</nm>

        <emp>
          <nm>Emp</nm>
        </emp>

        <emp>
          <nm>Emp</nm>
        </emp>

        <emp>
          <nm>Emp</nm>
        </emp>

        <emp>
          <nm>Emp</nm>
        </emp>

        <dep>
          <nm>Sub Dep 4-1-1-1</nm>

          <emp>
            <nm>Emp</nm>
          </emp>
        </dep>

        <dep>
          <nm>Sub Dep 4-1-1-2</nm>

          <emp>
            <nm>Emp</nm>
          </emp>
        </dep>
      </dep>
    </dep>

    <dep>
      <nm>Sub Dep 4-2</nm>

      <emp>
        <nm>Emp</nm>
      </emp>

      <emp>
        <nm>Emp</nm>
      </emp>
    </dep>
  </dep>
</dep>

...into this XML Result Document:
<?xml version="1.0" encoding="UTF-8"?>

<deps emps-wide="16">
  <dep emps-wide="16">
    <nm>Dep</nm>

    <emps emps-wide="3">
      <emp>
        <nm>Emp</nm>
      </emp>

      <emp>
        <nm>Emp</nm>
      </emp>

      <emp>
        <nm>Emp</nm>
      </emp>
    </emps>

    <deps emps-wide="16">
      <dep emps-wide="5">
        <nm>Sub Dep 1</nm>

        <emps emps-wide="2">
          <emp>
            <nm>Emp</nm>
          </emp>

          <emp>
            <nm>Emp</nm>
          </emp>
        </emps>

        <deps emps-wide="5">
          <dep emps-wide="3">
            <nm>Sub Dep 1-1</nm>

            <emps emps-wide="3">
              <emp>
                <nm>Emp</nm>
              </emp>

              <emp>
                <nm>Emp</nm>
              </emp>

              <emp>
                <nm>Emp</nm>
              </emp>
            </emps>

            <deps emps-wide="0"/>
          </dep>

          <dep emps-wide="2">
            <nm>Sub Dep 1-2</nm>

            <emps emps-wide="2">
              <emp>
                <nm>Emp</nm>
              </emp>

              <emp>
                <nm>Emp</nm>
              </emp>
            </emps>

            <deps emps-wide="0"/>
          </dep>
        </deps>
      </dep>

      <dep emps-wide="4">
        <nm>Sub Dep 2</nm>

        <emps emps-wide="4">
          <emp>
            <nm>Emp</nm>
          </emp>

          <emp>
            <nm>Emp</nm>
          </emp>

          <emp>
            <nm>Emp</nm>
          </emp>

          <emp>
            <nm>Emp</nm>
          </emp>
        </emps>

        <deps emps-wide="3">
          <dep emps-wide="1">
            <nm>Sub Dep 2-1</nm>

            <emps emps-wide="1">
              <emp>
                <nm>Emp</nm>
              </emp>
            </emps>

            <deps emps-wide="0"/>
          </dep>

          <dep emps-wide="2">
            <nm>Sub Dep 2-2</nm>

            <emps emps-wide="2">
              <emp>
                <nm>Emp</nm>
              </emp>

              <emp>
                <nm>Emp</nm>
              </emp>
            </emps>

            <deps emps-wide="0"/>
          </dep>
        </deps>
      </dep>

      <dep emps-wide="1">
        <nm>Sub Dep 3</nm>

        <emps emps-wide="1">
          <emp>
            <nm>Emp</nm>
          </emp>
        </emps>

        <deps emps-wide="0"/>
      </dep>

      <dep emps-wide="6">
        <nm>Sub Dep 4</nm>

        <emps emps-wide="2">
          <emp>
            <nm>Emp</nm>
          </emp>

          <emp>
            <nm>Emp</nm>
          </emp>
        </emps>

        <deps emps-wide="6">
          <dep emps-wide="4">
            <nm>Sub Dep 4-1</nm>

            <emps emps-wide="3">
              <emp>
                <nm>Emp</nm>
              </emp>

              <emp>
                <nm>Emp</nm>
              </emp>

              <emp>
                <nm>Emp</nm>
              </emp>
            </emps>

            <deps emps-wide="4">
              <dep emps-wide="4">
                <nm>Sub Dep 4-1-1</nm>

                <emps emps-wide="4">
                  <emp>
                    <nm>Emp</nm>
                  </emp>

                  <emp>
                    <nm>Emp</nm>
                  </emp>

                  <emp>
                    <nm>Emp</nm>
                  </emp>

                  <emp>
                    <nm>Emp</nm>
                  </emp>
                </emps>

                <deps emps-wide="2">
                  <dep emps-wide="1">
                    <nm>Sub Dep 4-1-1-1</nm>

                    <emps emps-wide="1">
                      <emp>
                        <nm>Emp</nm>
                      </emp>
                    </emps>

                    <deps emps-wide="0"/>
                  </dep>

                  <dep emps-wide="1">
                    <nm>Sub Dep 4-1-1-2</nm>

                    <emps emps-wide="1">
                      <emp>
                        <nm>Emp</nm>
                      </emp>
                    </emps>

                    <deps emps-wide="0"/>
                  </dep>
                </deps>
              </dep>
            </deps>
          </dep>

          <dep emps-wide="2">
            <nm>Sub Dep 4-2</nm>

            <emps emps-wide="2">
              <emp>
                <nm>Emp</nm>
              </emp>

              <emp>
                <nm>Emp</nm>
              </emp>
            </emps>

            <deps emps-wide="0"/>
          </dep>
        </deps>
      </dep>
    </deps>
  </dep>
</deps>

For the sake of completeness, here is the XLST Transformation I used to produce most of that XML Result Document:
<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <xsl:call-template name="recurse-deps"/>
  </xsl:template>

  <xsl:template name="recurse-deps">
    <deps emps-wide="">
      <xsl:for-each select="dep">

          <dep emps-wide="">
            <nm><xsl:value-of select="nm"/></nm>

            <emps emps-wide="">
              <xsl:for-each select="emp">

                  <emp>
                    <nm><xsl:value-of select="nm"/></nm>
                  </emp>

              </xsl:for-each>
            </emps>

            <xsl:call-template name="recurse-deps"/>
          </dep>

      </xsl:for-each>
    </deps>
  </xsl:template>

</xsl:stylesheet>

Note that I have generalized my original question.  Let's not even care about how this will be displayed as an organization chart.  That really wasn't the problem for which I was seeking to learn to solve in XSLT.
Also note that I had to edit the XML Result Document because I--obviously--do not quite know how to write the XSLT Stylesheet to produce what I want.  Otherwise, I would not be here asking questions.  :)
Here is what I am finding so much difficulty trying to express in XSLT, and why I had to manually edit the XML Result Document after running that transformation:

Assume every emp element is 1 unit wide, whatever that unit may be.  Let's call it an inch, just for the sake of being able to visualize it.
Every emps element should be as wide as the number of emp elements in it.  (So, an emps element with three emp elements in it would be three inches wide.)
Every dep element should be as wide as its emps element, or its deps element, whichever is the widest of the two.  (So, a dep element with a 3-inch-wide emps element and an 8-inch-wide deps element would be 8 inches wide.  A dep element with a 5-inch-wide emps element and a 3-inch-wide deps element would be 5 inches wide.)
Every deps element should be as wide as the sum of all its dep elements' widths. (So, a deps element with a 3-inch-wide dep, a 5-inch-wide dep, and another 5-inch-wide dep would then be 13 inches wide.)
A dep (or department) can have any number of emp elements (or employees) and any number of dep elements (or sub-departments), arbitrarily deep.

The problem, then, that I want to solve in XSLT is how to transform a generic and recursively structured XML Source Document to an XML Result Docment where each (or most) element(s) should have some kind of calculated value attached to them that depends on the same or similar calculated value of its children... and so on... and so on... all the way down to whatever might be the "leaf node".
Quick Edit: Thinking about it more, a similar example of this kind of problem would be to transform a "filesystem" XML Source Document to an XML Result Document in which a "file-count" attribute is added to every "folder" showing the total number of files in it and its "sub-folders", and their "sub-folders" and so on.
Any help in learning how to do this--Even...  No. Most especially if there is more than one idiomatic way to do it--would be deeply appreciated.
Comment: There is nothing XSLT/XML/XPath here -- no specific source XML document, no specific wanted result document, no specific requirement. Please, edit the question and provide these.
Comment: `Can't really do that with floating div's` Why? What's the problem? It would be much easier to answer if you provided the resulting HTML + CSS for each of your ASCII arts, preferably posted on JSFiddle or something alike.
Comment: As Tom and Dimitre have already mentioned, please can you provide an example of the desired output markup. Also you might want to consider a different visualisation tool for this sort of thing such as this: http://thejit.org/demos/
Comment: Dimitre and Kev: Yes, I will post back to this with a very specific input source XML document and specific result HTML document.  Thank you all for the suggested alternatives to ASCII art.  Apologies that my question and provided XML and XSLT snippets were unclear.
Comment: I have clarified/generalized my question, and provided full XML Source, XML Result, and XSLT Tranformation documents.
Answer: I figured out a way of doing what I have been trying to do.
This XSLT Stylesheet transforms the XML Source Document to the desired XML Result Document (found in my Question after my ~EDIT~).
It seems to me that there could be a better or more idiomatic solution.  I found that iterating over the XML Source Document several times, pipelining the result tree fragment from one transformation through the next, and making only simple changes through each iteration helped me find this solution.  I can hardly imagine how much more complex this XSLT Stylesheet would have been had I not taken this approach.  I dare say I would not have been able to do it in one fell swoop.
Even so, I am sure there is room for improvement (for speed and/or clarity).  Here it is to help anyone else that might be dealing with a problem similar to mine, or for constructive critique and improvement, which I would likewise appreciate:
<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version     = "1.0"
                xmlns:xsl   = "http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl = "urn:schemas-microsoft-com:xslt"
                xmlns:exsl  = "http://exslt.org/common"
                extension-element-prefixes = "exsl">

  <xsl:output method               = "xml"
              omit-xml-declaration = "no"
              indent               = "yes"
              encoding             = "UTF-8"/>

  <xsl:template match = "/">
    <xsl:variable name = "boxed-emps-and-deps">
      <xsl:call-template name = "box-emps-and-deps"/>
    </xsl:variable>

    <xsl:variable name = "added-emp-count-to-emps">
      <xsl:call-template name = "add-emp-count-to-emps">
        <xsl:with-param name = "last-pass" select = "$boxed-emps-and-deps"/>
      </xsl:call-template>
    </xsl:variable>

    <xsl:variable name = "added-emp-count-to-dep">
      <xsl:call-template name = "add-emp-count-to-dep">
        <xsl:with-param name = "last-pass" select = "$added-emp-count-to-emps"/>
      </xsl:call-template>
    </xsl:variable>

    <xsl:variable name = "added-emp-count-to-deps">
      <xsl:call-template name = "add-emp-count-to-deps">
        <xsl:with-param name = "last-pass" select = "$added-emp-count-to-dep"/>
      </xsl:call-template>
    </xsl:variable>

    <xsl:variable name = "recurred-dep-and-deps-emp-counts">
      <xsl:call-template name = "recur-dep-and-deps-emp-counts">
        <xsl:with-param name = "last-pass" select = "$added-emp-count-to-deps"/>
      </xsl:call-template>
    </xsl:variable>

    <xsl:copy-of select="exsl:node-set( $recurred-dep-and-deps-emp-counts )"/>
  </xsl:template>

  <xsl:template name = "box-emps-and-deps">
    <deps>
      <xsl:for-each select = "dep">
          <dep>
            <xsl:copy-of select = "nm"/>

            <emps>
              <xsl:for-each select = "emp">
                <xsl:copy-of select = "."/>
              </xsl:for-each>
            </emps>

            <xsl:call-template name = "box-emps-and-deps"/>
          </dep>
      </xsl:for-each>
    </deps>
  </xsl:template>

  <xsl:template name = "add-emp-count-to-emps">
    <xsl:param name = "last-pass"/>

    <xsl:for-each select = "exsl:node-set( $last-pass )/node()">
      <xsl:copy>
        <xsl:if test = "name( . ) = 'emps'">
          <xsl:attribute name = "emp-cnt">
            <xsl:value-of select = "count( emp )"/>
          </xsl:attribute>
        </xsl:if>

        <xsl:call-template name="add-emp-count-to-emps">
          <xsl:with-param name = "last-pass" select = "."/>
        </xsl:call-template>
      </xsl:copy>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name = "add-emp-count-to-dep">
    <xsl:param name = "last-pass"/>

    <xsl:for-each select = "exsl:node-set( $last-pass )/node()|@*">
      <xsl:copy>
        <xsl:if test = "name( . ) = 'dep'">
          <xsl:attribute name = "emp-cnt">
            <xsl:value-of select = "emps/@emp-cnt"/>
          </xsl:attribute>
        </xsl:if>

        <xsl:call-template name="add-emp-count-to-dep">
          <xsl:with-param name = "last-pass" select = "."/>
        </xsl:call-template>
      </xsl:copy>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name = "add-emp-count-to-deps">
    <xsl:param name = "last-pass"/>

    <xsl:for-each select = "exsl:node-set( $last-pass )/node()|@*">
      <xsl:copy>
        <xsl:if test = "name( . ) = 'deps'">
          <xsl:attribute name = "emp-cnt">
            <xsl:value-of select = "sum( dep/@emp-cnt )"/>
          </xsl:attribute>
        </xsl:if>

        <xsl:call-template name="add-emp-count-to-deps">
          <xsl:with-param name = "last-pass" select = "."/>
        </xsl:call-template>
      </xsl:copy>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name = "recur-dep-and-deps-emp-counts">
    <xsl:param name = "last-pass"/>
    <xsl:variable name = "top-deps-emp-count" select="exsl:node-set( $last-pass )/deps/@emp-cnt"/>

    <xsl:variable name = "redid-dep-emp-count">
      <xsl:call-template name = "redo-dep-emp-count">
        <xsl:with-param name = "last-pass" select = "$last-pass"/>
      </xsl:call-template>
    </xsl:variable>

    <xsl:variable name = "redid-deps-emp-count">
      <xsl:call-template name = "redo-deps-emp-count">
        <xsl:with-param name = "last-pass" select = "$redid-dep-emp-count"/>
      </xsl:call-template>
    </xsl:variable>

    <xsl:variable name = "new-top-deps-emp-count" select = "exsl:node-set( $redid-deps-emp-count)/deps/@emp-cnt"/>

    <xsl:choose>
      <xsl:when test = "$top-deps-emp-count = $new-top-deps-emp-count">
        <xsl:copy-of select = "$redid-deps-emp-count"/>
      </xsl:when>

      <xsl:otherwise>
        <xsl:call-template name = "recur-dep-and-deps-emp-counts">
          <xsl:with-param name = "last-pass" select = "$redid-deps-emp-count"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name = "redo-dep-emp-count">
    <xsl:param name = "last-pass"/>

    <xsl:for-each select = "exsl:node-set( $last-pass )/node()|@*">
      <xsl:choose>
        <xsl:when test = "name( . ) = 'emp-cnt' and name( ./.. ) = 'dep'">
          <xsl:attribute name = "emp-cnt">
          <xsl:choose>
            <xsl:when test = "./../deps/@emp-cnt > ./../emps/@emp-cnt">
              <xsl:value-of select = "./../deps/@emp-cnt"/>
            </xsl:when>

            <xsl:otherwise>
              <xsl:value-of select = "./../emps/@emp-cnt"/>
            </xsl:otherwise>
          </xsl:choose>
          </xsl:attribute>
        </xsl:when>

        <xsl:otherwise>
          <xsl:copy>
            <xsl:call-template name="redo-dep-emp-count">
              <xsl:with-param name = "last-pass" select = "."/>
            </xsl:call-template>
          </xsl:copy>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name = "redo-deps-emp-count">
    <xsl:param name = "last-pass"/>

    <xsl:for-each select = "exsl:node-set( $last-pass )/node()|@*">
      <xsl:choose>
        <xsl:when test = "name( . ) = 'emp-cnt' and name( ./.. ) = 'deps'">
          <xsl:attribute name = "emp-cnt">
            <xsl:value-of select = "sum( ./../dep/@emp-cnt )"/>
          </xsl:attribute>
        </xsl:when>

        <xsl:otherwise>
          <xsl:copy>
            <xsl:call-template name="redo-deps-emp-count">
              <xsl:with-param name = "last-pass" select = "."/>
            </xsl:call-template>
          </xsl:copy>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

Comment: I'm coming back to this after some refactoring.  I don't like all the for-each's in it.  I think it can be made much clearer by replacing (most) all the for-each's with apply-templates's ("apply templates-ez :) ).


Direction of relationship in 2x2 contingency tables

Question: I've been studying contingency table analysis and got to experiment a little bit with Fisher's exact test and Fisher's power test. Now I want to be able to determine the direction of the relationships.
Can anyone tell me what are the best methods to determine the direction of the relationship in 2x2 contingency tables? Is it just by using the odds? Or are there any standard, formal methods to calculate it?
Answer: Welcome to the site.
2x2 tables do not have a direction of relationships.
In any case, statistics do not reveal the direction of relationships, thought and logic and substantive knowledge does. That is, you (the researcher) figures out which direction (if any) the relationship might go in and then you use statistics to examine that relationship.
Since you have 2x2 tables, you  would probably want logistic regression as a tool to do that examination. You would pick a dependent variable and an independent variable and then do the regression.
If you tell us more about your particular case, we may be able to suggest more precise solutions.
Answer: To expand a little on Peter Flom's answer, you need to know the science and what question you are trying to answer to interpret any output.  For a 2x2 table there are several possible ways to look at direction, some simple approaches include calculating a confidence interval on the odds ratio, calculating a confidence interval on the difference of 2 proportions, comparing the observed values in the table to the expected values (under a null of no relationship).
Any of the above will give a feel for direction and strength of difference, but it is meaningless to say that the odds ratio is significantly greater than 1 without understanding the odds of what given what.  I remember a consulting session that started off a little confusing because the direction of the odds ratio was opposite what was expected, until we realized that one of us was talking about the odds of death and the other was talking about the odds of surviving, once we were looking at the same concept then everything made sense.
Answer: The easiest way to describe the direction is with a sentence like "The proportion of pilots among men was 0.13 higher than among women".  You can do this descriptively even if you use Fisher or the odds ratio for the formal test.  Note that Fisher is exact for the very rare situation in which both row and column totals are fixed in advance (before the data are gathered). It can also be used for experimental data in which either the row or column totals are fixed.  It is not appropriate for any sampling situation.  For those it is extremely conservative.  Chi-squared will then be closer to the truth, though not real close. I suspect Fisher is commonly (mis)used because of the word "exact" which people read too much into.  It's an exact answer to (in most cases) another question.
By way of clarification, I think the earlier posts address the issue of choosing the direction of the alternative in a hypothesis test rather than just describing the observed direction (which is what I addressed).
Answer: Why not try a Bayesian route? For example, suppose that you want to estimate the posterior distribution of the difference or ratio of a proportion between two groups.
Step 1: Open R.
Step 2: Simulate the posterior distributions of the proportion in each group with two calls of the rbeta() function, parameterized with the success and failure counts in the focal category (i.e., the "success") of the appropriate group. You could do 1,000,000 draws from each posterior in very little time. If you have at least one observation in each category, you can use an improper beta prior with shape and scale parameters equal to 0. If you don't, you can use a flat and still somewhat uninformative prior by adding a one to the counts in each category.
Step 3: Use the - operator to subtract one empirical distribution from the other to obtain the empirical distribution of the difference. Or use the / operator if you are interested in a ratio.
Step 4: Use the quantile() function to find the 2.5th, 50th, and 97.5th pecentile. Use the hist() or plot(density()) functions to visualize the distribution.
Asymptotically, the answer you get will be the same as with a frequentist method, except this way is easier. You can do virtually the same thing with any other exponential family distribution by looking up the conjugate prior of the sampling distribution, and how to parameterize the posterior.
Disclaimer This isn't a critique of frequentist methods, just a proposal of a simpler way to solve this particular problem using Bayesian inference.


Using theme_username() with no link to user page?

Question: I use this theme function to show the username of user:
 theme('username', array('account' => user_load($record->uid)))),

It works fine but the username is also a link to the user page. I just need to the username with no link at all.
I read the doc at:
http://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_username/7
and I see that there is the argument link_options to handle this, then I guessed it was a bolean value and tried with (but not working):
theme('username', array('account' => user_load($record->uid), 'link_options' => 0))),

Answer: theme_username() will only output the username as a link if the link_path variable has been set. This is done by default in template_preprocess_username(), and will happen as long as the user viewing the username has access to view the profile.
If you want to change that behaviour you just need to add your own preprocess function which removes the link_path variable:
function MYTHEME_preprocess_username(&$vars) {
  if (isset($vars['link_path'])) {
    unset($vars['link_path']);
  }
}

Bear in mind that the above will remove the link system wide. If you want to remove just for this specific instance, I think the best way would be to pass a custom variable to the theme function and check for it in your preprocess function:
$vars = array('account' = user_load($record->uid), 'surpress_link' => TRUE);
$name = theme('username', $vars);

function MYTHEME_preprocess_username(&$vars) {
  if (isset($vars['link_path']) && isset($vars['surpress_link']) && $vars['surpress_link']) {
    unset($vars['link_path']);
  }
}

Comment: good, preprocess function in my theme fitted my needs. Thanks.
Answer: Depending on what you want to do exactly, there's also format_username() that allows you to return a properly formatted username (e.g. integrated with realname.module), without any kind of link/HTML.
Note that this function returns an unsafe string (as unsafe as $user->name is), so you need to check_plain() it.


SQL Query getting a list of items from table based on other table column value?

Question: I have these 2 tables:
BluePrint:
ID| BlueprintID| CarID

Car:
ID, Name, Color

I need to get all the cars based on blueprintID.
I am using SQLLite.
Thanks
Comment: what did you try and what are the errors you got ?
Answer: You need to join one table to the other.
SELECT c.ID, c.Name, c.Color FROM CAR c JOIN BLUEPRINT b ON b.CarID = c.ID WHERE b.BlueprintID = [YOURBLUEPRINTID]
I have given both tables an alias, it makes it easier especially when you have lots of fields.  If a field with the same name appears in both tables and you don't specify which table by using something like c.Name or Car.Name` you will get an ambiguous field name error.
Answer: This can be done with either a subquery:
SELECT *
FROM Car
WHERE ID IN (SELECT CarID
             FROM BluePrint
             WHERE BlueprintID = ?)

or a join:
SELECT Car.*
FROM Car
JOIN BluePrint ON Car.ID = BluePrint.CarID
WHERE BlueprintID = ?

(If it is possible to have multiple blueprints for one car, the second query needs SELECT DISTINCT.)
Answer: SELECT c.*,B.BlueprintID FROM CAR C,BluePrint B
WHERE  C.ID=B.CarID
Comment: You should stop using these kinds of joins  Aaron Bertrand wrote a good article on it  https://sqlblog.org/2009/10/08/bad-habits-to-kick-using-old-style-joins


Custom drawable not being rendered in Xamarin Studio

Question: I am using a custom drawable to render a border around an Android TextView.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:bottom="1dp" android:left="-2dp" android:right="-2dp" android:top="-10dp">
        <shape android:shape="rectangle" >
            <stroke android:width="1dp" android:color="@color/pale_grey" />
            <solid android:color="#00FFFFFF" />
            <padding android:left="24dp" android:right="24dp"
                android:top="12dp" android:bottom="12dp" />
        </shape>
    </item>
</layer-list>

This works fine at runtime but is not rendered in Xamarin Studio Designer (or the designer in Visual Studio) when viewing an Activity that has a TextView with that background applied e.g.
<TextView
    android:id="@+id/txtHeightSelection"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:textSize="@dimen/text_size_large"
    android:text="Height"
    android:textStyle="bold"
    android:textColor="#1f2233"
    android:background="@drawable/customborder2"
    local:MvxBind="Click AddEntryCommand, CommandParameter='Height'" />

Unsuprisingly this is a PITA as I have to Build and deploy the app just to check some simple styling. Is this a known issue?
Answer: This is a known issue that is being addressed in a coming release (flagged as 5.7):

Bug 22243 - Android designer on XS and VS does not render Drawable shapes

As a workaround, open the drawable file in a hex editor (included in Xamarin Studio) and remove the first three bytes if they are this sequence: EF BB BF.
From Bugzilla:
The issue comes from the fact that our Android rendering process doesn't like
the UTF-8 BOM sequence we forcefully add to XML files in some places.
A simple workaround is to open the drawable XML files in an editor that doesn't
write a BOM and re-save the file from there. You can also open it up in an
hexadecimal editor and remove the first three bytes of the file (before the
initial '<').


Update a content type field's allowed values programmatically

Question: I have a list field in the content type and in the config table , the field storage details saved as follows :
collection:
name: field.storage.node.field_ip_range_sec_zone
data: a:17:{s:4:"uuid";s:36:"9d20e561-e39c-4c11-b100-ff3a29a75267";s:8:"langcode";
                                            s:2:"en";s:6:"status";b:1;s:12:"dependencies";a:1:{s:6:"module";a:3{i:0;s:17:
                                        "field_permissions";i:1;s:4:"node";i:2;s:7:"options";}}s:20:"third_party_settings";
                                    a:1:{s:17:"field_permissions";a:1:{s:15:"permission_type";s:6:"public";}}s:2:"id";
                                s:28:"node.field_ip_range_sec_zone";s:10:"field_name";s:23:"field_ip_range_sec_zone";
                            s:11:"entity_type";s:4:"node";s:4:"type";s:11:"list_string";s:8:"settings";a:2:
                        {s:14:"allowed_values";a:3:{i:0;a:2:{s:5:"value";s:2:"E1";s:5:"label";s:2:"E1";}
                i:1;a:2{s:5:"value";s:2:"C2";s:5:"label";s:2:"C2";}i:2;a:2{s:5:"value";s:2:"D1";s:5:"label";s:2:"D1";}}
            s:23:"allowed_values_function";s:0:"";}s:6:"module";s:7:"options";s:6:"locked";b:0;s:11:
        "cardinality";i:1;s:12:"translatable";b:1;s:7:"indexes";a:0:{}s:22:"persist_with_no_fields";
    b:0;s:14:"custom_storage";b:0;}

As you can see there is Allowed values (E1,C2,D1) , and now i want to add an additional value to the allowed values , How can i do that ?
WHAT I HAVE TRIED

I could get the allowed values using the following :
$entity_type = 'node';
$bundle = 'ip_range';
$field_name = 'field_ip_range_sec_zone';
$ip_sec_zone_storage_values = \Drupal\field\Entity\FieldStorageConfig::loadByName($entity_type, $field_name);

But dont know how to update it , Any idea ??
Answer: Those are settings, you get them using $ip_sec_zone_storage_values->getSetting('allowed_values'),, in there you'll find those settings, you can add another one, set the settings back with setSetting('allowed_values', $allowed_values) and then ->save() to save the field storage config:
$allowed_values = $ip_sec_zone_storage_values->getSetting('allowed_values');
// The new value.
$allowed_values[$ipseczone] = $ipseczone;
$ip_sec_zone_storage_values->setSetting('allowed_values', $allowed_values);
$ip_sec_zone_storage_values->save();

Comment: Not clear on the save part.. Is it **ip_sec_zone_storage_values->setSettings("custom values")** ?
Comment: place it as an answer , i will accept it ,  iused it like : **$allowed_values = $ip_sec_zone_storage_values->getSetting('allowed_values'); $allowed_values[$ipseczone] = $ipseczone;//the new value i want to add
        $ip_sec_zone_storage_values->setSetting('allowed_values', $allowed_values);
        $ip_sec_zone_storage_values->save();**
Answer: After many hours i finally found solution. I dynamically created options for multi select field and got error message on submit that choosen select in wrong option.
It requires add new options to allowed_values for that field.
Here is the solution:
 //load your field
 $field_purchasers = \Drupal\field\EntityFieldStorageConfig::loadByName('user', 'field_purchaseres');
 //subscribe new options to this field
$field_purchasers->setSetting('allowed_values_function','module_name_allowed_values_function' );
//save configuration
$field_purchasers->save();

In your .module file write in callback function :
function module_name_allowed_values_function(FieldStorageConfig $definition, ContentEntityInterface $entity = NULL, $cacheable) {
  $options = [
    1 => 'My new option 1',
    2 => 'My new option 2',
    3 => 'My new option 3',
   ];
  return $options;
}


Vhost works in Chrome but not in firefox? (XAMPP)

Question: My vhost config appears to work in Chrome but not in Firefox?
<code>127.0.0.1 dev.test.local

<VirtualHost *:80>
    DocumentRoot "D:\xampp\htdocs\12345-test\public"
    ServerName dev.test.local
    ServerAlias dev.test.local
    <Directory "D:\xampp\htdocs\12345-test\public">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>
</code>
Chrome result: reachable
Firefox result: shows index of /
Restarting XAMPP and going to my domain on Firefox doesn't work, If i open it in Chrome it works and then also starts working in Firefox? heh?
Comment: When you load `/` Firefox shows a directory index and Chrome doesn't? That's what `Options Indexes` is meant for so I guess Chrome is doing something wrong. What does Chrome display? What are you expecting?
Comment: Chrome shows the backend page as I expect it to but Firefox shows an index. Strange thing is I have this setup on my other PC at work (the vhost I mean) and only use Chrome there but there it doesn't work at all... It's just a laravel project I already checked the index.php and it seems fine, when index is shown it doesn't reach it obviously
Comment: So you want to load `index.php` then. Nothing in the settings you've shared specifically configures that. Perhaps you had it earlier and Chrome is showing a cached version. You need to add [DirectoryIndex index.php](http://httpd.apache.org/docs/2.4/en/mod/mod_dir.html#directoryindex).
Comment: Well sure but shouldn't it already be specified in Apache at
    DirectoryIndex index.php? Because it is.
Comment: I didn't know that, you've just mentioned it for the first time. Is mod_dir itself enabled? Have you cleared Firefox cache?
Comment: Now that you mention it I did recently run CCleaner which cleaned all caches and now it does appear to work properly in FF again, I just wonder if this would also fix it for me on the other pc, but I don't have access to it right now.
Answer: I faced same error, finally I found this solution
After xampp vhost setting, then system32 localhost url register is needed.
Please check this url.
https://www.cloudways.com/blog/configure-virtual-host-on-windows-10-for-wordpress/
Sometimes, you can't edit system file because of non-permission.
On that case, please check this url.
https://www.partitionwizard.com/partitionmagic/you-dont-have-permission-to-save-in-this-location.html
This url explains how to give full control to administrators on specific folders on system.