Multiple Audio starting on

Question: I'm trying to start an audio whenever a user clicks on the page (since autoplay doesn't work anymore on most browsers), and I've managed to do so by using an eventlistener on ngOnInit which works fine on the first click. My problem is that when I click multiple times the audio starts playing multiple times, so I'd need to remove the listener from it. I've tried a basic solution like using the once: true flag or removing the listener after the audio played, but with no success. This is the code snippet I'm using:
  ngOnInit() {
  document.querySelector('ion-content').addEventListener('click', this.playAudio);

  }

  playAudio(){
   console.log('started');
   let audio = new Audio();
   audio.src="http://192.168.1.15:8000/example1.ogg";
   audio.load();
   audio.play();
   document.querySelector('ion-content').removeEventListener('click', this.playAudio);
   console.log("removed")

}

Why wouldn't this work?
Comment: set global variable IsClicked as false. Then wrap code in playAudio() with if(!isClicked){...} . and inside 'if' also set variable IsClicked to true.
Answer: I think what you need is a debounce method to not delay your functions calls when called multiple times :
private clickTimeout = null;

//better way of doing is attach a click event handler on the ion-content in the HTML

ngOnInit() {
  document.querySelector('ion-content').addEventListener('click', this.getItem());

  }

public getItem(): void {

  if (this.clickTimeout) {

      this.setClickTimeout(() => {});

  } else {

    // if timeout doesn't exist, we know it's first click
    // treat as single click until further notice

    this.setClickTimeout((itemId) =>
       this.handleSingleClick());
  }
}

// sets the click timeout and takes a callback
// for what operations you want to complete when
// the click timeout completes

public setClickTimeout(callback) {
  // clear any existing timeout

  clearTimeout(this.clickTimeout);

  this.clickTimeout = setTimeout(() => {

    this.clickTimeout = null;
    callback();

  }, 200);

}
public handleSingleClick(itemId: string) {
   //The actual action that should be performed on click
   this.playAudio()
}

You should check this link to get better understanding of how this code works.

Multiple Audio starting on


Qual a diferença entre o preg_split e o explode?

Question: Qual a diferença entre as funções preg_split e explode? Quando usar uma ou outra?
Me deparei com esse código e estou querendo entender o seu funcionamento.
function consulta_header_grid($campos){
  $linha = preg_split('/[\n]/', $campos, -1, PREG_SPLIT_NO_EMPTY);
  foreach($linha as $linha){
    $colunas = explode("    ", $linha);
    $header[$colunas[0]] = $colunas[0] . ";" . $colunas[1] . ";" . $colunas[2];
  }

  return $header;
}

Answer: A principal diferença entre a explode(); e preg_split(); é que você pode usar expressão regular na preg_split();, porém, segundo o stack.com, o explode normalmente é mais rápido (segue a referencia).
Em resumo, se for uma filtragem mais complexa use o preg_split();, se for algo mais simples use o explode();.
Nesse seu exemplo, ele usa o preg_split(); com o "\n" parar quebra a cada linha, e o explode("   "), para gera um vetor com base no espaços.
Comment: Muito obrigado, me ajudo muito.
Answer: Ambos são usados para dividir uma string em um array, a diferença é que split() usa o "pattern", enquanto explode() usa string. explode() é mais rápido do que split() porque não corresponde à string com base na expressão regular.
Comment: Vlw brother, agora deu para entender o codigo


SORT CRITERIA by GROUP BY datepart in Views

Question: In views I have my items being sorted by date and then relevancy .  Although unless the items are posted at exactly the same time this means that the relevancy score is made redundant.
I can't seem to add a date boost to the relevancy but have figured if I could Sort the date in blocks of time ( i.e. a day ) this would give me the results that I'm looking for.  Then items to be sorted by the day they are posted; and then by relevancy.
I understand that in SQL I can use ' GROUP BY datepart' to do this.  Has anyone done anything like this in Views.
Answer: I couldn't find any way to do this using the options in the View UI. One way to accomplish it, however, might be to add an additional field to your content type that stores only the day/month/year for the date (set the default to 'now' so that you don't have to manually enter the date, and set the field to 'hidden' on the display options), and then you could use that field to group by in the view.


SetWinEvent on a separate Class

Question: I have a little problem with Qt , here is my code :
SessionSnatcher.h:
#ifndef SESSIONSNATCHER_H
#define SESSIONSNATCHER_H

#include <Windows.h>
#include <QThread>
#include <QtCore>
#include <QDebug>

class SessionSnatcher : public QThread
{
public:
    SessionSnatcher();
    ~SessionSnatcher();
    void run();
    void SetPID(int pid);
    void SetTID(int tid);
    int GetPID();
    int GetTID();
    HWND GetCOProductControl();

private:

    //HWND GetCOProductControl();
    void CALLBACK SessionSnatcher::handler(HWINEVENTHOOK hook , DWORD event , LONG idChild , LONG idObject , DWORD dwEventThread , DWORD dwmsEventTime);
    HWINEVENTHOOK hook;
    int PID , TID;

};

#endif // SESSIONSNATCHER_H

SessionSnatcher.cpp :
#include "sessionsnatcher.h"

SessionSnatcher::SessionSnatcher()
{

}

SessionSnatcher::~SessionSnatcher()
{
    UnhookWinEvent(hook);
}

void CALLBACK SessionSnatcher::handler(HWINEVENTHOOK hook , DWORD event , LONG idChild , LONG idObject , DWORD dwEventThread , DWORD dwmsEventTime)
{

}

void SessionSnatcher::run()
{
    hook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND , EVENT_SYSTEM_FOREGROUND , NULL , &SessionSnatcher::handler , PID , TID , WINEVENT_OUTOFCONTEXT);
}

HWND SessionSnatcher::GetCOProductControl()
{
    HWND TPanel = FindWindow(L"TPanel" , NULL);
    return TPanel;
}

void SessionSnatcher::SetPID(int pid)
{
    PID = pid;
}

void SessionSnatcher::SetTID(int tid)
{
    TID = tid;
}

int SessionSnatcher::GetPID()
{
    return PID;
}

int SessionSnatcher::GetTID()
{
    return TID;
}

And now i keep getting this error :
D:\TT\sessionsnatcher.cpp:25: error: C2664: 'HWINEVENTHOOK SetWinEventHook(DWORD,DWORD,HMODULE,WINEVENTPROC,DWORD,DWORD,DWORD)' : cannot convert argument 4 from 'void (__stdcall SessionSnatcher::* )(HWINEVENTHOOK,DWORD,LONG,LONG,DWORD,DWORD)' to 'WINEVENTPROC'
There is no context in which this conversion is possible

Before this i was using "handler" instead of "&SessionSnatcher::handler" but i had a problem saying it was a member and it cant be used like that.
Now i cant work it out.
It worked well on Visual Studio When i tried it , all i had to do was cast like (WINEVENTPROC)handler , but i think since im using a separate class i cant do that anymore.
Any tips ?
Answer: You have fallen victim to the hidden this parameter in every non-static class methods. A modern solution is to use std::bind, but that won't help you with a C-based API or a C++ API that predates C++11.
The typical solution when std::bind isn't available is a free function or static method that

meets the prototype you need to supply,
maps the callback to the object, and
invokes the desired method on the object found in 2.

Often Win32 API calls have a pointer to a user argument you can use to make this easy: just cast the pointer to SessionSnatcher * and call the function. I don't see a user argument in WinEventProc's parameter list, so you'll have to get creative and map one of the event ids or handles WinEventProc does provide and uniquely identifies a SessionSnatcher.
Or if you only have one SessionSnatcher you can write the function to always use that one SessionSnatcher.
Here is a generic example:
#include <iostream>
#include <map>

typedef int (*funcp)(int id, int p1, int p2);
// using typedef instead of using on purpose because that's what'll be in
// the Windows API

class test
{
private:
    static std::map<int, test*> tests; // our map of id -> test objects
    int id; // this test's id. Only included for proof
public:
    test(int id):id(id)
    {
        tests[id] = this; // put in map. In the real world test for collision first
    }

    // function that will eventually be called
    int func(int p1, int p2)
    {
        (void) p1;
        (void) p2;
        std::cout << "Called func for " << id << std::endl;
        return 0;
    }

    // static method that can be called by API
    static int staticfunc(int id, int p1, int p2)
    {
        // look up object and call non-static method
        // I recommend at() instead of [] because you get a catchable exception
        // rather than a crash over null pointer if id is unknown.
        return tests.at(id)->func(p1, p2);
    }
};

// allocating storage for static map
std::map<int, test*> test::tests;

// function mocking up API. Takes a function and an identifier used by called function
int caller(funcp func,
           int id)
{
    return func(id, 100, 20); // just calls the function
}

// tester
int main(void)
{
    test a(1); // create a test
    test b(2); // create a test

    try
    {
        caller(test::staticfunc, 2); // call test 2 through API
        caller(test::staticfunc, 1); // call test 1 through API
        caller(test::staticfunc, 10); // throw exception over unknown id
    }
    catch (std::exception &e)
    {
        std::cout << "Failed call: " << e.what() << std::endl;
    }
}

Typical output:
Called func for 2
Called func for 1
Failed call: map::at

"map::at" isn't much of an exception message, but it can be converted into a useful log message. At any rate it beats a segfault with no information.
Comment: Thank you for your answer , i have tried using a free function for the callback (i hope thats what you meant) but i still got the same error !
Comment: But it worked with the original `handler` function, yes? Currently `SessionSnatcher::handler` appears to be missing `WinEventProc`'s third parameter: `HWND hwnd`. A free function that is also missing this parameter will generate the exact same error message. Sorry I did not spot that earlier.
Comment: You know i've just tried to remove (WINEVENTPROC) from handler and i still get an error , and BTW ive noticed i missed HWND's parameter from the Handler Callback i dont know how i missed it ! Current Error : `sessionsnatcher.obj:-1: error: LNK2019: unresolved external symbol __imp__SetWinEventHook@28 referenced in function "public: virtual void __thiscall SessionSnatcher::run(void)" (?run@SessionSnatcher@@UAEXXZ)`
Comment: Have you added `User32` to Project->Properties->Configuration Properties->Linker->Input->Additional Dependencies?
Comment: Wow ! even though that was the first thing i always do but i forgot it this time ! That was it , Just `LIBS += -luser32` solved it. Thanks for you help @user4581301

SetWinEvent on a separate Class


Two Way ANOVA repeated measurement

Question: I am trying to compare the three replicate samples taken at different time point to see if the replicate one differ from replicate two and three and visa verse. My interest is to see if the three replicates represent the same community by have the same species abundance or not? From the reading I have done, I think that Two Way ANOVA repeated measurement would do the job for me but I am not quite sure that would be right?
Please see the link of my data by following the link:
https://gist.github.com/plxsas/9ec2a528f94bd2dff46d
Can anybody advice me on that and how to achieve it in R, please?
Comment: Have you tried Google? http://ww2.coastal.edu/kingw/statistics/R-tutorials/repeated.html
Comment: Or, if you do not have any experience with R. Please tell me more about your variables.
Comment: I have conducted study by collecting samples from three sites on monthly basis and for a period of one year. Within each site, I have taken three replicates. After looking at the sampling, I have found that each of the replicate sample has about 4-5 species with roughly the same percentage of abundance. My goal is to prove statistically that the three replicate within each site have been sampled from the same assemblage based on the similarity in the species composition and there is no much difference if you samples 2 meter away from the original sample.
Comment: Why is "BranC" at May-12 missing? It do not exist, or you are posting only a fragment from full dataset?
Comment: Yes, I have just posted a small portion of my data but the same concept follow for the rest of the year.Your help would be appreciated.
Comment: Maybe [this](http://stats.stackexchange.com/questions/97863/one-group-four-conditions-and-six-tests-testing-the-effect-of-altitude/97892#97892) link is useful for your purposes.
Comment: I have looked at the link but the problem is as more you read as more you became confused: Do you think the following procedure will be correct for me: lme<- lme(fixed=Species ~ Sites + Months, random=~1|replicates, data=data)
Answer: At first, you must reorganize your table:
Months  Replicate2      Species.1   Species.2  Species.3   Species.4
Jan-12  BranA           778         41         19          1
Jan-12  BranB           800         18         18          2
Jan-12  BranC           537         48         20          2
Feb-12  BranA           465         29         19          1
Feb-12  BranB           444         45         14          1
Feb-12  BranC           671         13         10          2
...

# reorganisation procedure
# (need to be optimalized according to your full datatable!!!)
# create vectors...
my.data <- read.table("spec.csv", header = TRUE, sep = ",") # load your data
spec_abund <- c(my.data$Species.1, my.data$Species.2,
                my.data$Species.3, my.data$Species.4) # species abundance
sites <- rep(my.data$Replicate2, times  = 4) # sites
my.date <- rep(my.data$Months, times = 4) # date of collection
spec.no <- rep(c("spec1","spec2","spec3","spec4"),
                 each = nrow(my.data)) # spec.ID
# ...and bind them into new data.frame
my.reorg <- data.frame(abundance = spec_abund, sites = sites,
months = my.date, spec.no = spec.no) # final table

It should looks like this:
my.reorg
   abundance sites months spec.no
1        778 BranA Jan-12   spec1
2        800 BranB Jan-12   spec1
3        537 BranC Jan-12   spec1
4        465 BranA Feb-12   spec1
5        444 BranB Feb-12   spec1
6        671 BranC Feb-12   spec1
...

Than try to fit the model:
aov.out <- aov(abundance ~ sites * months + Error(spec.no/months), data=my.reorg)
summary(aov.out)

It looks like that sites are non-significantly different (in species composition) from each other during the "whole time".
Is this what you need?
Comment: btw I do not use last two rows in your data table...due to "BranC" at May-12 missing.
Comment: Yes, that is what I really want , thank you very much for your help

Two Way ANOVA repeated measurement


How to store a mutable borrow with a specified lifetime in an enum?

Question: I want to be able to store a mutable borrow &mut Foo<'a> inside an enum variant. I can work with the mutable borrow directly just fine, but not with an enum containing the mutable borrow. Extract of simplified code below ('full' playground version with 'broken' code commented out here).
#[derive(Debug)]
struct Foo<'a> {
    shared_ref: &'a usize,
    child_foos: HashMap<usize, Foo<'a>>,
}
impl<'a> Foo<'a> {

    // Get a 'naked' mutable reference to the child foo - works fine!
    pub fn get_handle(&mut self, id: usize) -> &mut Foo<'a> {
        self.child_foos.get_mut(&id).unwrap()
    }

    // !!! Get an 'enclosed' mutable reference to the child foo - doesn't work !!!
    pub fn get_handle_in_enum(&mut self, id: usize) -> Handle {
        Handle::Foo(self.child_foos.get_mut(&id).unwrap())
    }
}

// Note: Compiler demands &'a mut Foo<'a>. Q: why is the &'a mut needed
// (i.e. why can't it be &mut Foo<'a>)?
enum Handle<'a> {
    Foo(&'a mut Foo<'a>),
}

First question: Why am I forced to use &'a mut Foo<'a> in the enum definition (I would like to be able to use &mut Foo<'a> instead). I'm sure there is a good reason, but I suspect that this is responsible for the resulting compiler errors.
Second question: When I compile the code I some very confusing errors, including one where the compiler says it found what it was expecting?!
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
  --> src/main.rs:39:9
   |
39 |         Handle::Foo(self.child_foos.get_mut(&id).unwrap())
   |         ^^^^^^^^^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime defined here...
  --> src/main.rs:38:31
   |
38 |     pub fn get_handle_in_enum(&mut self, id: usize) -> Handle {
   |                               ^^^^^^^^^
note: ...so that the types are compatible
  --> src/main.rs:39:9
   |
39 |         Handle::Foo(self.child_foos.get_mut(&id).unwrap())
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: expected `Handle<'_>`
              found `Handle<'_>`
note: but, the lifetime must be valid for the lifetime `'a` as defined here...
  --> src/main.rs:30:6
   |
30 | impl<'a> Foo<'a> {
   |      ^^
note: ...so that the expression is assignable
  --> src/main.rs:39:21
   |
39 |         Handle::Foo(self.child_foos.get_mut(&id).unwrap())
   |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: expected `&mut Foo<'_>`
              found `&mut Foo<'a>`

For more information about this error, try `rustc --explain E0495`.
error: could not compile `enum_mut_borrow` due to previous error

Any explanation or suggestions for how to use enums properly in this situation would be hugely appreciated (in my real code I need more variants of the enum each with its own mutable borrow)!
Comment: Does this answer your question [Why does this mutable borrow live beyond its scope?](/q/66252831/2189130) If not that directly, perhaps one of the linked questions. Basically `&'a mut Foo<'a>` is almost always wrong.
Comment: @kmdreko Thanks for the link - I was aware that `&'a mut Foo<'a>` was likely cause issues so I was trying to avoid it. It was just that this was the compiler's fairly insistent suggestion (I should have been more sceptical!).
Answer: You are not forced to put &'a mut Foo<'a>. You are forced to have an explicit lifetime: all borrows (exclusive or not) have an associated lifetime, but most of the time Rust doesn't require you to explicitly write it. Type definition doesn't fall in that category, so you need to explicitly write it:
enum Handle<'a, 'b> {
    Foo(&'b mut Foo<'a>),
}

this way, you dissociate the borrow's lifetime from Foo's.
To make this work, you also need to explicitly name the lifetime of &mut self in get_handle_in_enum:
fn get_handle_in_enum<'b>(&'b mut self, id: usize) -> Handle<'a, 'b> {
    ...
}

See the playground.
Comment: Exceptionally helpful answer - many thanks! Was not expecting to need multiple lifetimes, but it makes sense. I think I read too much into the compiler's suggestion to use `&'a mut Foo<'a>` in the enum definition and just ran with it.
Comment: @tokoloshe countrary to most other aspects of the compiler, is not smart at all when it comes to lifetimes. It doesn't try to "make them work" or anything, it just has a preset of simple rules that allow implicit lifetimes, and a couple of error messages that reuse existing lifetimes when possible and that's it. Usually, it's not a good idea to follow its indication (for lifetimes) if these indications don't make it work right away, because the situation is probably too complex for these simple rules.


Is there any difference between these two terms, Population and Probability Distribution?

Question: I am learning trying to learn more about statistics and probability theory, but I am having trouble understanding some of the terms that I feel have same or similar semantics just different name. For example, in picture 1. there is a contour that represents PDF function over two random variables F (body fat) and B (beer). On the right side of the picture there is marginal probability distribution for random variable F, and on the bottom of the picture there is marginal probability distribution for random variable B.

So basically this is the way how we calculate an exact Marginal P. Distribution for entire population, correct? But because we might not know the date for entire population, we sample it. So we have an approximate curve that is show on picture 2.

Does that mean when we say "population" we are referring to the EXACT probability distribution of the world that we wish to model mathematically? And because the data is not available, we sample the population instead and approximate it?
EDIT: Also, isn't the definition of probability distribution the following: "Probability distribution represents all possible states, and their probabilities which particular random variable can acquire". If that is true, then Probability distribution for some discrete random variable X, lets say tossing a coin, can be {HEAD, TAILS} both with probability of 0.5 of happening. What would we say a population is then in this context?
Sorry, I am just confused.
NEW EDIT:
Okay so basically, I am wondering are the following statements true:

Sample space of some random variable X is basically a population (e.g. {heads, tails})
Probability distribution is defined over a sample space (i.e. population) and it represents probabilities of all possible sample values
In theory, we assume and analyze some probability distribution, which we believe to be true for the "world" that we are modeling
In practice, we sub sample the sample space (i.e. population), create a histogram, based on which we APPROXIMATE prob. distribution that we believe to be true for the entire population
Also, in theory we use notion of random variable to say that X can represent any possible value from sample space, while in practice (i mean statistics) notion of random variable does not exist. Instead, we refer to X= {some particular value} as a sample.
Answer: You've basically got it!
Here's some answers to your questions:

No; a sample space is not a population. The sample space is the set of all the possible outcomes of an experiment.  The population is the set of all events which is of interest. The set of outcomes of a coin flip {heads, tails} is a sample space, but not a population.  The population would be all tosses of the coin. (Note the subtle difference: the sample space is the set of outcomes for each toss, the population is the actual results of all future tosses).
Correct, probability distribution is defined over a sample space and it represents probabilities of all possible sample values.  But the sample space is not the population.  The probability distribution tells us what the probability is of observing an outcome for each member of the population. (In your example, the probability distribution would be P(heads) = 1/2, P(tails) = 1/2, which applies to each coin flip).
Correct, we assume there's a probability distribution that applies to all members of the population.
We take a sample, which is a small group of the population, from which we compute statistics or use other methods to approximate the distribution. For example, if we didn't know that the coin were fair, we might flip the coin a few times and use the outcomes (the sample) to estimate the probability of heads.
Random variables are a way of representing the elements of a sample space as numbers.  For example, we might say the random variable $X$ is 1 if the coin flip is heads and 0 if it is tails.  But you're correct in that we use the random variables while we're doing statistics, but we don't use them in our final analysis.  The whole point of random variables is to get numbers we can do math with.  For example, you wouldn't say $P(X= 1) = 0.5$ to someone, because they won't know what that means.  You'd say, "The probability of flipping heads is 1/2". (I hope this answers your question.)

To recap:

You find a coin, that may be fair or may be unfair.  But you'd like to be able to say what the outcome of heads is on any given flip ("any given flip" is your population; you want to be able to apply your analysis to all flips of the coin).
The sample space for each coin flip is {heads, tails}.  You can encode this using a random variable $X$ where $X = 1$ if the coin flip is heads and $X = 0$ if the coin flip is tails.  We use the random variable because then we have numbers we can do math with.
Then you take a sample, by flipping the coin several times.  You record the results for your random variable, which will be a sequence of 1's and 0's.
Now you use statistics, performed on the sequence of 1's and 0's from your sample, to estimate the probability of flipping a heads.
Say your estimate is 0.6.  You can then say that your estimate for the probability of flipping heads each time the coin is tossed is 0.6 (or more technically, the future flips of the coin are from a Bernoulli(0.6) population).  Note that this applies to all members of your population - all future tosses of the coin.
Comment: Yes, the probability distribution and population are different concepts.  The population is the group that the probability distribution applies to.  And you don't *find* a population, you choose your population up front.  Then the goal is to approximate a distribution that applies to each member of the population. I can tell you that all statisticians, and probably everyone on this site, has dealt with this confusion around the terminology when they started out.  As you continue to study, these concepts will become clear in time.  It just takes immersion and practice.
Comment: Hi Nick. Thanks for the comment, please read my edit.
Comment: Hi Stefan.  Check out the new response.  Let me know if it answers your questions.
Comment: Nick, thank you so much for the help! I really appreciate it! I learned a lot!
Comment: So what you mean basically when you say "population is the set of all possible events", you are alluding to that population is probability distribution over all the events that are of interest (e.g. N flips of a coin). Or population does not have anything to do with probabilities at all, only outcomes of the past that we are analyzing? If that is true, then probability distribution and population are different concepts, correct?
Comment: And if all the above holds true. When we find population, we have to calculate probabilities for each sample in population in order to have probability distribution? Again, SORRY FOR ALL THE TROUBLE, I just really want to learn this.
Comment: I honestly wish I could give you like +500 rep, because I would. Thank you! Any recommended readings regarding the fields in general?


Can categorical data only take finitely or countably infinitely many values?

Question: I wonder if categorical data by definition can only take finitely or countably infinitely many values? And no more i.e. not uncountably many values?
Related question: is the distribution of a categorical variable always a discrete distribution or a continuous distribution?
Comment: What do you mean by "Countably infinite"? Using the definition from here: http://mathworld.wolfram.com/CountablyInfinite.html I can't actually conceive of an applied example. Do you just mean a variable that can range toward + or - infinity?
Comment: No. I mean the cardinality of the set of the values which the categorical variable for the categorical data can take.
Comment: Not sure that added clarity ;) I'm going to take a swing anyway.
Comment: I believe that the question should be rephrased can there be finite ***levels*** (or countably infinite) for a categorical ***variable***.  Categorical *data* can include infinite values - there's no reason to assume that an observation has a given cardinality.
Answer: "Categorical" is not a well-defined mathematical term, so to answer this question we have to look to how this word is intended to be used.  It is employed in contrast to "ordinal," "interval," and "ratio."  One way to understand the primary distinctions is in terms of the groups of allowable re-expressions of the values.  In the case of the latter three, there is an order that must preserved, whence all re-expressions must be monotonic (order preserving).  For categorical variables, any bijection (including permutations) is ok:

Beyond that, anything goes with the nominal [categorical] scale.

(Stevens, quoted in the Wikipedia article.)
Another concept of "categorical" is that each outcome must be distinguishable from every other.  This strongly suggests that any probability measure must be totally discrete: that is, all subsets are measurable, implying that each category will have its own well-defined probability. (This is not the case for continuous distributions.)
This would seem to indicate that the number of categories should be finite or at most countable, but that is not evident in the literature.  For instance, an archetypal example of a categorical variable is a set of names.  The set of all possible names on any finite alphabet is countable but not finite.  It is therefore useful to allow countably infinite sets to be categorical.  For example, if we are studying names given to babies, it is convenient to let the sample space consist of all possible names (rather than all names that we know of).
A slightly less realistic, but still conceivable, example of a categorical variable would be one that uses real numbers for names.  In effect, such a variable would ignore all the usual mathematical structure on this set.  I don't see any problem with such a usage, but it's worth observing that the axioms of probability imply that any probability distribution valid in this context would (a) assign a non-negative value to each real number and (b) would assign a non-zero value to at most a countable infinity of the reals.
One application involving an uncountable sample space that supports categorical random variables of infinite, even uncountable, support is the study of random graphs.  To understand the rate of growth of some property of graphs, we would want to contemplate graphs on 0, 1, ..., $n$, ... nodes, so it's convenient to allow graphs to have countably many nodes.  Random variables defined on this set can have various types.  For instance, the mean vertex degree (if finite) could be considered of ratio type; the total vertex degree could be considered of ordinal type (and, therefore--by forgetting the ordering--is a nice example of a countable discrete variable).  If we also allow a graph to have arbitrarily many edges and are interested in, say, its connected components, then we would have a naturally occurring category that is uncountable (because each connected component determines the subset of nodes it contains and there are uncountably many subsets of a countable set).
To summarize, it is reasonable to allow categorical values to attain an uncountable infinity of possible values, while recognizing that at most a countable number of them could ever have positive probabilities.  This must be a discrete distribution, because all subsets are measurable, which is not the case for continuous distributions.
Comment: "it is convenient to let the sample space consist of all possible names (rather than all names that we know of)" - how is that convenient?
Comment: I'm not sure why you say that "categorical" is not well-defined mathematically.  I believe that you may need to clarify the meaning of category, level, and data.  These terms must be defined mathematically for mathematical statistics to be relevant. :)  Nonetheless, it's quite an interesting answer.
Comment: @Iterator I have no disagreement with you and appreciate your remarks.  To clarify where I'm coming from, it appears to me that the scale "categorical, nominal, interval, ratio" is not a mathematical one, but rather is a qualitative description.  One could indeed come up with mathematical definitions that capture most of the concept of "categorical," but that might even be counterproductive.  I do not view this hierarchy as being any part of mathematical statistics, and in fact find it to be useless except as a suggestive guide in some applications.
Comment: @whuber It sort of matters for issues like constructing hypothesis tests, and then for applications such as estimating necessary sample sizes.  :)  In any case, the OP was vague.  Categorical is an adjective, which noun it modifies is the issue to address.  Whether that noun is countable or describes something countable is the source of ambiguity.  I am contemplating editing the question to reflect something that's actually answerable, because the vagueness yields an unanswerable question.
Comment: @Iterator I think you're on to something here.  I have been wondering whether "categorical" refers to a sample space or to a random variable or to neither.  In his [original article](http://www.sciencemag.org/content/103/2684/677), Stevens addresses *scales of measurement.*  As such, it would seem he was interested in an additional property of a random variable.  I would characterize this extra property in terms of the group of transformations of $\mathbb{R}$ that would be considered inconsequential for the data analysis.  However, I'm unaware of any published definition of this sort.
Answer: Alright, here's my attempt at an answer, from my (admittedly imperfect) understanding of your question.
"Categorical data" is something of a fuzzy and problematic term to begin with. Similar to the "I know it when I see it" definition of obscene material. There are some very clear cases of categorical data, where the values of a variable fall into a small number of clearly defined categories.
Beyond that, there be dragons.
At some point you get enough categories that your "categorical" variable could likely be treated as a continuous variable. Or alternately, using either subject matter knowledge or a description of a distribution, you can break a continuous variable up into categorical chunks and treat it as categorical.
So there's really two answers to your question:
Theory Answer: No. You could have infinitely many categories, but for some reason decide not to call it a continuous variable. If you allow for decimal-based categories of an utterly unbounded variable, there's no reason I can see it wouldn't be be uncountably infinite.
I'm not sure how often this ends up coming up. In my experience at least, fairly rarely.
Applied Answer: The cardinality of most things that would ever reasonably be called categorical data have a cardinality vastly below the cardinality of N. As noted above, there are exceptions, often subject to vague judgement calls.
Comment: @EpiGrad: I don't consider your "silly" answer quite so silly. There is no particular reason I see that an infinite number of categories should somehow be equated with some form of continuous random variable. Indeed, the difference seems to hinge on whether or not the variable can be represented in a meaningful, natural, and unambiguous way as a real number.
Comment: @PeterFlom: Cantor showed that the real numbers are *uncountable*. :)
Comment: What would "vastly below the cardinality of **N**" mean?  Is this just a long way of saying "finite"?  If so, that would rule out many interesting applications involving categorical variables.
Comment: Thanks! So the silly answer is that the cardinality can be any, while the applied answer is that mostly it is finite?
Comment: Hi @cardinal yes, I know Cantor showed the reals are uncountable. But is there any set of categories that needs more than the rationals, which *are* countable? I have trouble imagining a set of categories that requires use of irrationals.
Comment: @Tim That's my take on it. Someone may very well disagree.
Comment: Building on @EpiGrad 's silly answer, didn't Cantor prove that the decimal numbers are countably infinite, rather than uncountably infinite?
Comment: @whuber Not necessarily just finite, but - compared to **N** - quite small. Even the most fiendishly clever uses of categorical variables I've seen don't have a particularly high cardinality.
Comment: @Epi, there is no cardinality between finite and countably infinite (the cardinality of the natural numbers).  I think you may need to review the theory of cardinality.  It's crucial for understanding modern probability, which makes a fundamental distinction between finite, countable, and uncountable unions of events.
Comment: @whuber But you can calculate - and compare - the cardinality of two finite sets. Which is my point. In practice, even variables where there is theoretical argument for them being categorical, as the number of possible values the variable can take rises, they tend to end up being treated as continuous. So not only are they finite, but even among other finite variables, *most* categorical variables *in practice* don't have a particularly high cardinality.
Comment: @Epi In my examples of (a) names and (b) random graphs, exactly how do you propose to use continuous distributions to model them?  Note that these are well-known practical applications, not theoretical pathologies.  Note, too, the sharp distinction between properties of a *model* and properties of the reality it represents.  For instance, all Normal distributions are unbounded, but very few of the data they model in practice are truly unbounded.  So please don't confuse finiteness of data with boundedness ("finite cardinality") of their distributions.
Comment: Another example: if you have ever used a Poisson distribution to model counted data, you have treated the counts as a categorical variable with a *countable infinity* of categories.
Comment: @whuber And herein we discover why I said "most", not "all". I can provide well-known practical applications in support of my argument as well. Age, CD4 Cell Count come to mind. *The whole point* of the post is that what is clearly categorical is sometimes clear (you examples) and sometimes not (mine) in ways not strictly governed by cardinality. I've edited my answer slightly, hopefully that will assuage you.
Comment: @whuber Frankly, our answers are *largely in agreement*. Namely, that as 'categorical' lacks a well-defined mathematical definition, the answer will be somewhat fuzzy and based on usage. I can dredge up examples for either argument, but in my experience, I encounter categorical variables modeled with a continuous distribution once they reach a "large" number of categories more than I encounter countably infinite categorical variables. You've just been...oddly insulting about it in at least two places.
Comment: @cardinal Fair point. I've edited away from silly ;)
Comment: @Epi I apologize for any suggestion of insult; it was not intended.  I do have a sense, though, from your answer and previous comments that many of your remarks may be based on a shaky (or at least unconventional) idea of cardinality (as intimated, among other things, by the implicit suggestion that there are cardinalities between finite and countably infinite), and that your replies could be improved by further study of the concept.  Please take that as a constructive suggestion and not an implied slight.
Comment: @Whuber I'm still puzzled as to your notion that I suggested there are cardinalities between finite and countably infinite. My assertion is that finite, bounded sets can be compared on their cardinality. For example, a categorical & binary variable (0,1) has a lower cardinality than a categorical variable that can take (0,1,2,3,4,5). Both are finite. That's the point. There is big finite, there is tiny finite, and in my experience the vast majority of categorical variables used as such in practice have a fairly low cardinality.
Comment: @Peter: Perhaps I misunderstood your use of the phrase *decimal numbers*. (**??**) When I read that phrase, I don't think of restricting to *rational* numbers. At any rate, I do not see any *a priori* justification for limiting one's self to the finite or countable cases, even if it seems hard to any particular person (or group) to think of an example outside of those classes. :)
Comment: @cardinal well... maybe you're right. I agree the phrase "decimal numbers" does not exclude the irrational numbers. The whole question seems sort of whimsical.
Answer: Categorical data is discrete - otherwise it would be hard to assign categories to the data.
My take is this: the natural numbers are discrete and thus categorical. They are also ordinal and interval data, but also categorical. Since the natural numbers are countably infinite we see that there are categorical variables that can take countably infinite values. That does not mean, though, that this applies for all categorical variables.
Comment: I believe that the usage is ambiguous.  Categorical variables have *levels*.  Data is merely observations *of* a categorical variable, with some specified *level*.  One could abuse the notion of levels to produce infinitely many.  A level itself could, say, count the infinities encountered in a data set (e.g. $X/Y$ where both are drawn from the two element set ${0,1}$.  I do not disagree with your answer, but suggest clarifying to address the issue of levels (or categories).


How to create Chord progressions that make sense

Question: I've been reading over Walter Piston's Harmony book, and it has been an eye-opener to say the least.  I understand concepts like tension and release, voice leading and so forth, but I still don't quite understand the process of generating harmonic patterns other than by trial and error, or composing as a whole.  I understand many things that I didn't before, but there seems to be so much more when I listen to a piece by Sibelius, Mahler, or Mozart. To be honest, I expected a kind of harmonic revelation in the sense that I would have a way to understand (in a very limited sense) and analyze works, but this is simply not the case.
It makes sense now that you can make a piece using simply I V I, and I have seen many such examples, but I still don't really understand what the composer is thinking from when he starts at I and makes his way to V.  I still don't really understand how to think about harmonic progressions in a way that is useful (meaning in a way that will help me write my own music).  These pieces that I hear seem like they have written themselves...  so my conclusion is that I am doing it wrong.
Maybe I'm putting way too much emphasis on harmony.  Should I be looking into musical form?
Comment: By the way; don't bother comparing yourself with famous classical composers. They were so good, their works are still now world-famous. It is just unfair to compare yourself to these half-gods, especially if you haven't had any substantial education in composing (which they all had. Guess what the profession of the fathers of - for example - Bach and Mozart was).
Comment: If you want to look at the "why" of chord progressions, think about the concepts of departure and return.  If you look at musical form, you'll see that the concept of departure and return is very much a part of it too.
Answer: Great question - I remember when I myself was confused about this very same thing many years ago, and indeed at first, it all seems completely random.  In order to answer your question, there needs to be a little background:
Historically, thinking about music in terms of harmonic progression is one that has really only come to complete prominence in the last 150 years or so (not very long when considering the past 2000 years...)  Mozart was coming out of the Baroque period, and during that time, many people thought of music linearly - as in, vertical harmonies are the result of linear motion, not the other way around.  Mahler and Sibelius are a little more difficult to pin-down - Sibelius wrote a lot of programmatic and nature-oriented music, and through his technique of teleological genesis could be argued that in his own way, he was thinking linearly (or perhaps by phrase modules) moreso than harmonically, but I digress.
It is important to remember that in addition to the composers you mentioned, many other composers thought about larger implications of key relationships, and as a result, invented a myriad number of devices to facilitate movement between these relationships.  These devices (tonicization, modulation, chromatic saturation) and many others would take too long to describe accurately in a simple answer.  But, it is important to remember that they used these devices, and you have not begun to use them.  Therefore, your music will not sound like there music.
When writing music, it comes differently for some people in terms of inspiration, and should come differently for people depending on the project.  For example, if you're writing a song for your band, you'll probably come up with chord progression first.  However, if you're setting text for an art song or working on a sonata for tuba or something, you'll probably come up with a couple of riffs you like before you add chords.
Moral: Don't panic; start with whatever you hear first.
Yes, there are some "canned" progressions.  For example:

I-IV-V7-I
I-vi-ii6/5-V7-I
I-vi-IV-V7-I

A lot of composers (especially the famous ones) were regarded for their economic use of chords - they repeated them over and over again.  This use provided harmonic consistency throughout the work, and locally, phrases.

Substitution
You may or may not be familiar with scales and keys.  With popular music, often keys are ignored, which can make it challenging if you want to put chords together that sounds like they should go together.
If you are reading the Piston (good text, through incredibly dry) then you should know that chords in major keys can be represented by each degree of the scale like this:

I, ii, iii, IV, V, vi, vii°

In a common progression (I-IV-V-I for example), a chord or chords may be substituted with chords that share like notes.  IV may be substituted with ii or vi, I may be substituted with vi or iii, V may be substituted with iii or vii° for example. (It should be noted that iii is an incredibly weak chord and cannot be effectively used as a substitute for V.)

Cadences
It would be a good idea to learn about the different types of cadences and how / when to use them.  You can then use the canned progressions to direct your subordinate harmonies to point toward the cadences and to give your piece forward motion.

My Advice
Start with a simple canned chord progression and make it an exercise to write some music using only those chords.  Do this in all 12 keys.  Once you feel comfortable, begin occasionally substituting some of the chords for more interesting harmonic possibilities.  You will eventually develop your own style about what you like to hear and how it should be notated.
Answer: I think it goes without saying that there are a lot of different approaches to creating meaningful chord progressions, but I wanted to share a useful "trick" I learned while taking music theory in college. This trick does not require an extensive knowledge of theory, so it is an easy way to get started creating progressions that sound good and have some idea about what you're doing. Also, let me start off by mentioning that all of the canned progressions jjmusicnotes mentioned in his great post actually follow this trick to the letter, so this is a really neat trick to have under your belt. :)
The trick is to use chord movements that always sound like they are moving forward and these movements are:

Up a 2nd
Down a 3rd
Up a 4th

The theory behind this is that the notes with the chord either:

Move to the next note in the scale (move forward in the scale)
Stay the same

Here is an example of each chord movement in the key of C major:
Up a 2nd: C major to D minor
All of the notes (C,E,G) in the C major chord move up to the next note in the scale.
C --> D (move up)
E --> F (move up)
G --> A (move up)

Down a 3rd: C major to A minor
The G note in the C major chord moves up to the next note in the scale. The C and E notes stay the same.
C --> C (stay the same)
E --> E (stay the same)
G --> A (move up)

Up a 4th: C major to F major
The notes E and G in the C major chord move up to the next note in the scale. The C note stays the same.
C --> C (stay the same)
E --> F (move up)
G --> A (move up)

And finally, I mentioned at the beginning that the canned chord jjmusicnotes mentioned in his post follow this trick, so here is the breakdown of those chord progressions (for simplicity I'm omitting the chord extensions jjmusicnotes included):
I IV V I
- up a 4th
- up a 2nd
- up a 4th

I vi ii V I
- down a 3rd
- up a 4th
- up a 4th
- up a 4th

I vi IV V I
- down a 3rd
- down a 3rd
- up a 2nd
- up a 4th

Give this a try and you'll find that the chord progressions created with this method have the same feeling of moving forward that all of these chord progressions have.
As an added bonus, if you REVERSE all of these chord movements to "Down a 2nd", "Up a 3rd", and "Down a 4th" you will create the exact opposite effect. Instead of moving forward you will now be moving backwards. This gives you another flavor to work with when you want to use it.
Answer: This is a very good question that all people who set out to write music (in any harmonic system, be it tonal or atonal) struggle with. The conclusion that I have settled on at present is that it all comes down to voice-leading.
I will admit to not having read Mr. Piston's text (though I admire his music), but from the way you phrase your question I am lead to believe that he is presenting harmonic theory in the usual vertical, way. There is nothing technically wrong with this approach, however I personally believe this way of thinking is much better suited to analysis than composition.  The whole business of thinking about harmonies as vertical structures is rooted in the thinking popularized by French baroque theorist and excellent composer Jean-Philippe Rameau, and has been prominent in analytical thought ever since. This theory follows the notion that the patterns of root motion in the bass dictate the harmonic progression. I beleive, however, that Rameau missed the mark and merely made useful observations about the results of good writing and not the principles behind it.
To get a better picture of how harmony works, I think it is useful to think about it in its historical context. Prior to the advent of the Baroque, in the Middle Ages into the late Renaissance, composed music was a fundamentally polyphonic art. The dominant way of conceiving music was of multiple voices singing separate lines that were fully independent but interacted with each other in harmony. Each voice (alto, soprano, bass, etc.) sang in what was essentially a slightly more florid chant like gregorian style that conformed to a common tactus or pulse.  The conception of voices of a composition as separate entities was so pervasive that until the advent of the baroque, scores quite literally did not exist. Even while composing pieces in as many as 9 voices (40 in one famous case!), the composer would write out each voice separately in its own partbook. Organists were expected to be able to read 5 part motets written in this way from 5 different books at sight to be considered for a post in a respectable church!
Now, to relate this to tonal harmony. If one spends some time looking at the development of polyphonic music over time, as voices become more numerous and relations between them start to get more complex, the harmonic structures that result begin to sound more and more like common practice harmonic progressions.  Of course, all this music was basically modal in character; true major minor tonality did not truly emerge until the baroque period proper, but it was in essence a sort of patternization of common trends in Renaissance voice leading. Bach chorales, the most commonly analyzed 4 part harmony known to man, make much more sense viewed from a linear, polyphonic stance than a vertical, chordal one.
To get a better feel for writing harmonic progressions, find a 16th century counterpoint textbook and make a point of following all the rules. It will feel restrictive at first but I think it's instructive. Try to write 2 voice counterpoint with equally interesting melodic lines in both voices, then begin adding voices. I think you'll be surprised by how close your results are to good common practice harmony without thinking about vertical structures at all.
Of course, not all music is contrapuntal in nature, since Rameau published his treatise his ideas have been in the general musical consciousness. Much music written after him has been at least partially based on his thinking. I believe very strongly, however, that the greatest composers (Sibelius, Mahler, and Mozart among them) understood very well this deep universal relevance of voice leading, and it is this that I think contributes in part to the wonderful organicism of their harmony, a rich interplay of contrapuntal and Ramellian thinking.
Voice leading and good controll of tension and release and an awareness of the flow of time and momentum (all of which, coincidentally, are not exclusive to tonal music, read Hindemith's theory textbook and look at some analyses of Bartok!) in your music are the most important things to consider. Tonal progressions are not and never have been a goal, but an outgrowth of the ways people have dealt with these fundamental issues over the centuries. I think that if you try to think about music in these terms, not only will you write better music, you will also be more likely to hit upon new and original ways to address them, and create a more personal and less contrived style.
Good luck composing!
Answer: Great question Sketchyfish, and the reason that your question has yet to be answered in other forums, is because there isn't really a clear answer. Theory is great to know. I consider it a must-have for composers.
Good and interesting chord progressions are based on the melody. From your question it seems as if you're trying to come up with chord progressions before a melody, or just come up with a standalone chord progression. Not that there is anything wrong with that method, but the progressions that seem to manifest on their own are because the melody called for those specific chords.
I highly doubt that any composer deliberately thinks, I must play these sequence of notes so that I can resolve to V chord. More often than not a composer will come up with an interesting melody, and then use the chords to harmonize the melody. It's easy to run into some progressions that make no sense, because composers can and do use chords that belong to a very distant key. The thing with harmony is that there are so many ways to harmonize a melody and note. When harmonizing you have to decide do you want the melody to harmonize with the root, third, or fifth of the chord, or even an extended note like a 6th or flat 13th.
I think that you should try writing a melody and then adding chords to it afterwards. Start with the simplest chords, that belong to the key, but then start using chords that are from other keys. For instance if you're playing in the key of Cmajor, and your melody lands on a D note, harmonize it with a Bb chord. Bb is the IV chord of Fmajor which is very closely related to Cmajor, and it could make your melody more interesting, because it contains a D note which will go with your D note, but it also contains a B flat, which does not belong to the key of Cmajor.
To sum it up, I really don't think that composers like Mozart, were so methodical that they were thinking, "I have to properly resolve this so I need to make my way to V by doing this theoretically correct passage". I think they kind of let their melody and emotions take the tune where it needed to go and then they harmonized it in ways that were interesting. There is a lot of trial and error in the early stages of music, but eventually you start develop an ear for it and it becomes easier to achieve a desired sound.
Comment: Actually, I highly disagree with your third and fifth paragraph.  Many composers (if writing tonal music) think *precisely* that - how can I get out of this mess I've gotten myself into and get to that V chord? MANY composers thought about all of these very carefully because they often attributed larger structural connotations as well as local ones.
Comment: I have to say that I disagree for just the reasons that jjmusicnotes does.  I've written a few things of my own, and I usually start with some sort of melodic theme and some idea of what form I'm going to use.  I often get into just the sort of "messes" that he mentions.  Also, you might have a look at [this](http://www.bl.uk/learning/timeline/large105241.html).  Beethoven's sketches have a lot of detail represented as lines, which give the general direction of the notes.  This suggests that he often conceptualized a larger structure first, and filled in the details later.
Comment: I appreciate your advice, MrTheBard.
Comment: I don't see the difference between BobRhodes approach and MrTheBard's. BobRodes claims Beethoven conceptualized the direction (ascending, descending) then filled in the details.  That sounds pretty much the same as coming up with a melody first and then working in a chord progression to support it.  What am I missing?
Answer: To answer the posters question, you should study "the circle of fifths", and embrace it as the center-piece for musical theory composition.  There is a mathematical reason, looking at "A" at 440 Hz, and working down of up from there, that certain chords in a circle of 5ths ascending, or 4ths descending, will 'resonate' correctly in the ear and make for 'correctness' to the ear drum and nervous system.  This explains the 'must get to V' notion in composing.
All of the chords I-XII, even above with 13th and 14ths, relative to the root chord, can be reached within the circle of fifths, or the progression it derives.
All composers will assert that 'artistic' license allows for atonal and other progressions outside of the circle of fifths - I am certainly one of them.  But relied on too much, such music will generally sound discordant, poorly written, and though achieving tension and resolve, will not always sound 'sensible' or particularly appealing.
I'd say it is for mathematical reasons more so than for 'artistic' reasons that I-V-I or I-II-V-I are 'correct' progressions. Though a composer may 'play by ear' or write 'emotionally' or 'with inspiration', those human motivations do not change the laws of physics and therefore how harmonies form a progression.  A good composer embraces these natural laws, not fight them and pretend they don't exist.
Of course, tension and resolve can be fabricated, for instance whole tone intervals, or just random chords.  And, like many good song writers, I think these 'affects' when used well, particularly for transition and bridge sections, are crucial to good writing. But I doubt a serious composer will abandon the circle of fifths altogether.
Comment: I respectfully disagree with the large majority of this answer. The circle of fifths is created by tetrachords and intervallic proportionality, Hz ratios merely determine the *temperament* in which the notes are produced. Paragraphs 3-5 are just simply untrue. Look at Wagner, look at Bartok, Stravinsky, Copland, Glass, Reich, Stockhausen, Feldman, and **hundreds** of other *very successful* composers that did not adhere to the circle of fifths. Most music throughout history has been made without the circle of fifths. Your assertions are made out of ignorance and are short-sighted. Please edit.
Comment: With due respect, I think you would be quite surprised how many composers and people enjoy non-tonally-functional music. You said it yourself: taste is subjective. Your own taste is obviously biased toward tonally functional music. While that is perfectly okay, it is *wrong* to pass it along as the be-all end-all. You do not seem to be very well educated in this subject, and as such, should be cognizant of your limitations. I would not attempt to make wild, ridiculous arguments about Android coding, and neither should you concerning music. It is only respectful and mature.
Comment: I respect that composers and listeners often want more (far more) than what a circle of fifths offers. That my song writing abilities exceeds my software coding abilities is irrelevant. I at least address the poster's desire: "...understand how to think about harmonic progressions in a way that is useful". Atonal random chords is not useful. I also address his query: "Maybe I'm putting way too much emphasis on harmony. Should I be looking into musical form?"  I give an highly educated answer to this.  Not some half-baked "dude, just do what feels right, man".   Perhaps, you should do the same.
Comment: You gave a slanderous, over-generalized answer with short-sighted scope and little to none factual or anecdotal support. You are merely purporting your opinion of artistic aesthetic as concrete fact, and therein lies the crux of my point with my first comment. You and I have having two different conversations here - I am speaking about factual accuracy and providing best information; you are speaking about artistic aesthetic and who is "right". The internet is not a constructive place and one-sided conversation is not conversation. Such things are best left to amicable parties with open minds.
Comment: Yes, hundreds of very successful composers admonish the circle of fifths. There are thousands of EVEN MORE successful composers that embrace it. Music ultimately is an acquired taste, which can be quite subjective. I think I answered the question correctly. I am not going to edit the answer because a smallish subset of composers were able to create tolerable sounding music admonishing the circle of fifths. To use a math example, you can add two digit numbers tens column first, as taught by Common Core math. Yet, this is not an approach I would recommend if you want easy success adding numbers.


Members of "Remote Desktop Users" group cannot logon to Windows Server 2019 domain controller

Question: I installed a Windows 2019 domain controller. I added a user to the Remote Desktop Users group. The user cannot logon to the server, unless I add them to the Administrators group.
All I see in other answers here are to do just that (or to create a local user - which is not possible on a DC - only a standalone server).
How do I configure a Windows Server 2019 domain controller to allow a domain user account, to logon to the server using remote desktop?
I am unable to find a method and don't see any default policies for RDP anywhere. Perhaps I didn't install something when setting it up?
For years I've set up remote users, but never to a DC, so I'm not sure how to proceed as the obvious adding of users to RDP group doesn't work.
Comment: You don't mention whether you have purchased the Terminal Services license. You need TS in order to allow non-administrators to remote in to your server.
Comment: Done, and thank you for your consideration... and that one bit me too, years ago, which is why I remember it.
Comment: I have 25 CALs. I thought TSLs came with CALs (my ignorance)
Comment: There's supposed to be a 120 day grace period - I just built the thing.
Comment: https://learn.microsoft.com/en-us/windows-server/remote/remote-desktop-services/rds-client-access-license
Comment: oops - the grace period is only for device CALs
Comment: tsc_chazz, put your comment in as the answer. I can't believe I forgot about TS (but never had to use it until now after 20 years).
Answer: Without the Terminal Services add-on, a Windows 2019 server only has remote access available to administrators. In order to get non-administrative users to have RDP access, you have to purchase the TS add on and CALs for the users who will be connecting remotely.


Understanding and proving prop 2.1.16 in Tao's Analysis I concerning recursive definitions

Question: Notes: Tao's axioms and original proposition statement and proof are given below. Similar questions have been asked here and here, but I was not able to resolve my issues. In particular, I have realized, it seems, there is a great deal of set theory going on "underneath the hood", and I find it rather conceptually and logically problematic that Tao introduces this proposition in a chapter before any set theory is even discussed. He remarks in a footnote how the proposition can be formulated more rigorously in the language of set theory, but he leaves that as the last exercise in a section in the chapter on set theory. This seems pretty sloppy to me, but I'm doing the best I can to follow what he's trying to communicate and the how/why of his proof. I found this blog post from Keith Devlin to be interesting but a little over my head. Is there any real good way to understand Tao's proposition without a thorough grounding in axiomatic set theory?
EDIT: I have reformulated my question as largely a request for people to comment on or critique my attempt at "a more fleshed out inductive proof." Since the proof is by induction, I'll try to outline my thoughts according to the typical way an induction proof works. Any comments as to where I am going wrong or what needs to be fixed would be greatly appreciated.

Proof attempt:
Base case: The procedure gives a single value to $a_0$, namely $c$. My understanding is that we basically assume a natural number $c$ exists due to Assumption 2.6, and we simply make the definition $a_0\colon=c$. None of the other definitions $a_{n++}\colon=f_n(a_n)$ will redefine the value of $a_0$ because the way in which a natural number $a_n$ is assigned to another natural number $n$ is by the assignment $a_{n++}\colon=f_n(a_n)$ which involves the successor, and Axiom 2.3 tells us that $n++\neq0$ for every natural number $n$. Hence, it's impossible to define (again) $a_0$ once the procedure has begun.
Inductive step: Suppose inductively that the procedure gives a single value to $a_n$. We want to show that a single value is then given to $a_{n++}$, namely $a_{n++}\colon=f_n(a_n)$. We should first note that $a_n$ being single-valued is critical because $a_{n++}$ is going to be defined to be $a_{n++}\colon=f_n(a_n)$, and it would not be sensible to start thinking about $a_{n++}$ being single-valued if $a_{n++}$ were defined in terms of a function whose argument was multi-valued (i.e., if $a_n$ were multi-valued, then we could reasonably expect $f_n(a_n)$ to return multiple distinct values as well). It may seem as though we can now safely rely on $a_{n++}$ being single-valued, but there is a concern we must address, namely the possibility that for a later natural number $m$ the corresponding definition $a_{m++}\colon=f_m(a_m)$ could somehow cause $a_{n++}$ to be multi-valued. How?
If we came across an $m$ for which $n++=m++$ but $f_n(a_n)\neq f_m(a_m)$, then we would have an issue  because $n++=m++$ implies that $a_{n++}=a_{m++}$. Why would this be a problem? Well, with $a_{n++}\colon= f_n(a_n)$ and $a_{m++}\colon= f_m(a_m)$, we would have $a_{n++}=f_n(a_n),f_m(a_m)$ even though $f_n(a_n)\neq f_m(a_m)$, indicating that $a_{++}$ would not have a single value assigned to it, as desired. How do we resolve this potential issue?
Suppose that later in the process we came across an $m$ corresponding to the definition $a_{m++}\colon= f_m(a_m)$, where we also had $n++=m++$. Since $n++=m++$, it must be the case that $n=m$ by Axiom 2.4. Hence, we must also have $f_n(a_n)=f_m(a_m)$, thus showing that none of the other definitions $a_{m++}\colon= f_m(a_m)$ will redefine the value of $a_{n++}$, and hence only a single value is assigned to $a_{n++}$.
This completes the induction, and so $a_n$ is defined for each natural number $n$, with a single value assigned to each $a_n$.

Content by Tao:

Axiom 2.1. $0$ is a natural number.
Axiom 2.2. If $n$ is a natural number, then $n{++}$ is also a natural number.
Axiom 2.3. $0$ is not the successor of any natural number ; i.e., we have $n{++} \ne 0$ for every natural number $n$.
Axiom 2.4. Different natural numbers must have different successors ; i.e., if $n, m$ are natural numbers and $n \ne m$, then $n{++} \ne m{++}$. Equivalently, if $n{++} = m{++}$, then we must have $n=m$.
Axiom 2.5. (Principle of mathematical induction). Let $P(n)$ be any property pertaining to a natural number $n$. Suppose that $P(0)$ is true, and suppose that whenever $P(n)$ is true, $P(n{++})$ is also true. Then $P(n)$ is true for every natural number $n$.
Assumption 2.6. (Informal) There exists a number system $\mathbb{N}$, whose elements we will call natural numbers, for which Axioms 2.1-2.5 are true.
Proposition 2.1.16 (Recursive definitions). Suppose for each natural number $n$, we have some function $f_{n} : \mathbb{N} \rightarrow \mathbb{N}$ from the natural numbers to the natural numbers. Let $c$ be a natural number. Then we can assign a unique natural number $a_{n}$ to each natural number $n$, such that $a_{0} = c$ and $a_{n{++}} = f_{n} (a_{n})$ for each natural number $n$.
Proof. (Informal) We use induction. We first observe that this procedure gives a single value to $a_0$, namely $c$. (None of the other definitions $a_{n{++}}:= f_{n} (a_n)$ will redefine the value of $a_0$, because of Axiom 2.3.) Now suppose inductively that the procedure gives a single value to $a_n$. Then it gives a single value to $a_{n{++}}$, namely $a_{n{++}}:= f_{n} (a_n)$. (None of the other definitions $a_{m{++}}:= f_{m} (a_{m})$ will redefine the value of $a_{n{++}}$, because of Axiom 2.4.) This completes the induction, and so $a_n$ is defined for each natural number $n$, with a single value assigned to each $a_n$.
Comment: @MauroALLEGRANZA I get that Tao is concerned with *analysis* and not set theory proper, but it seems disjointed and inconsistent with some of his explicitly stated goals early on in the text, namely "We will try to introduce these concepts one at a time and identify explicitly what our assumptions are as we go along--and not allow ourselves to use more "advanced" tricks such as the rules of algebra until we have actually proven them" [p. 14]. It seems disjointed is all. Also, my question is now not identical to the linked questions, given my latest edit of actually trying to furnish a proof.
Comment: Tao's book is titled *Analysis* and not *Set Theory*: this is the reason why he do not spend many page regarding set theory topics and try to recap only the basic concepts and results needed in his exposition.
Comment: *Base case* : Ok.
Comment: *Inductive step*: the i nitial discussion about multi-valued is superfluous: $f_n$ is a *function* and this it cannot have more than one-value for input $a_n$. This is slightly different with respect to : assuming that $a_n$ has been defined (and thus it is a single specific value) it is not possible that subsequently we can re-define it. This is waht you have discussed in thenext paragraphs, where we want to enusre that there is no $m$ for which $n++=m++$ but $f_n(a_n)≠f_m(a_m)$.
Comment: @MauroALLEGRANZA Thanks for the clarification! It's starting to make more sense now. Final question: where in your proof sketch do you explicitly use the inductive hypothesis $P(n)$ in proving that's $P(n++)$ follows? I'm so used to explicitly referencing how $P(n++)$ follows from $P(n)$ Andrew $P(0)$ that Iran throwing me off a bit. Is it simply the ability to even define $a_{n++}$, since it depends on $a_n$, that the inductive hypothesis is being explicitly used?
Comment: I used it implicitly assuming that, up to $a_n$, there are no "loops". Assuming it, I've showed (sketched) that adding $a_{n++}$ will not cause a "loop".
Answer: The key characteristic of a function is that for every input it produces a single output, i.e.

$ \text {for every } x,y : \text { if } f(x) \ne f(y), \text { then } x \ne y$.

Thus, the step-by-step construction described by Tao is aimed at satisfying this property.
We start with $a_0 := c$ for some $c$ and, as you noted, the procedure will never re-define $a_0$.
Regarding the inductive step, that defines : $a_{n++} := f_n(a_n)$; so, how can we be sure that there is no "previous" $m$ such that $a_{n++}=a_m$ ?
Assume that there is such an $m$.
Two cases : $m=0$. In this case : $a_{n++}=a_0$ contradicting what we have said above.
Second case : $m \ne 0$. This means that $a_m$ is a successor of some $r$, i.e. $a_m=a_{r++}$.
But then $a_{r++}=a_m=a_{n++}$, and this can be so only if $r=n$.
Thus $a_{r++}=a_{n++}$ and we are done.
Note also Tao's comment :

Note how all of the axioms had to be used here. In a system which had some sort of wrap-around, recursive definitions would not work because some elements of the sequence would constantly be redefined. For instance, in Example 2.1.5, in which $3++ = 0$, then there would be (at least) two conflicting definitions for $a_0$, either $c$ or $f_3(a_3))$. In a system which had superfluous elements such as $0.5$, the element $a_{0.5}$ would never be defined.

For the definition of addition of natural numbers in a standard textbook, see : George Tourlakis, Lectures in Logic and Set Theory. Volume 2 : Set Theory, Cambridge UP (2003), page 246.

Another way of formulating the above proof is [see Landau, page 4] :
Let $a_y$ and $b_y$ be defined for all $y$ as follows :
$$a_1=c \text { and } b_1=c,$$
$$a_{y++} = f_y(a_y) \text { and } b_{y++}=f_y(b_y), \text { for every } y.$$
Let $A = \text { the set of all } y \text { for which } a_y=b_y$.
Clearly $a_1=c=b_1$ and thus $1 \in A$.
If $y \in A$, then $a_y=b_y$, and therefore : $f_y(a_y)=f(b_y)$.
Thus : $a_{y++}=f_y(a_y)=f(b_y)=b_{y++}$ and so $y++ \in A$.
Hence - by induction - $A$ is the set of all natural numbers, i.e. for every $y$ we have $a_y = b_y$.
Answer: I think that it actually wants to prove that functions are well-defined, i.e.
$(\forall m,n \in \mathbb{N})((m=n) \implies (a_m=a_n))$.
By induction,
$(\forall n \in \mathbb{N})(0 \neq n++)$ by Axiom 2.3, so it is true that $(\forall n \in \mathbb{N})((0 = n)\implies(a_0 = a_n))$. Since there is no natural nubmer other than $0$ can be equal to $0$, antecedent is false and implication is always true.
If case $n$ is true, i.e. $(\forall m \in \mathbb{N})((m = n)\implies(a_m = a_n))$, then by Axiom 2.4, $(\forall m \in \mathbb{N})\left((m++ = n++)\implies(m = n)\implies(a_m = a_n)\implies(a_{m++} = f_m(a_m) = f_n(a_n) = a_{n++})\right)$. Therefore, case $n++$ is true.


Normal Subgroup which is Subgroup of Normal Subgroup - Normal Subgroup of that Subgroup

Question: There appears in Seth Warner's "Modern Algebra" Exercises 12.15 what seems to be an obvious truth:

If $H$ and $K$ are normal subgroups of a group $G$ such that $H \subseteq K$, then $H$ is a normal subgroup of $K$, ...

Indeed it is so:
$\forall g \in G: g K = K g$ by definition of a normal subgroup
$\forall g \in G: g H = H g$ by definition of a normal subgroup
$ \leadsto \forall g \in K: g H = H g$ as $K \subseteq G$
That seems too easy.
In fact, it even appears that if $H$ is a subset of any subgroup $K$ -- or even if $K$ is merely a subset -- of $G$, then $H$ is a normal subgroup of $K$.
This result is rarely seen in the elementary literature on group theory and abstract algebra, or at least, in any of the works I've seen. But so obvious is it, and so useful is it, that I wonder whether I'm made a silly mistake in understanding this, and in fact it is not such a trite and trivial result after all.
Can my thinking be confirmed -- that in fact a normal subgroup $H$ of a group $G$ is also a normal subgroup of any subset $K$ such that $H \subseteq K \subseteq G$? And if not, where am I going wrong in my thinking?
Comment: Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackexchange.com/rooms/134900/discussion-on-question-by-prime-mover-normal-subgroup-which-is-subgroup-of-norma).
Answer: Your argument is correct.
The result appears in many books. For example:

J.J. Rotman, Introduction to the Theory of Groups, 4th Edition. The 9th Exercise in the section on normal subgroups (the section only contains the definition), page 31, Exercise 3.33: "If $K\leq H\leq G$ and $K\triangleleft G$, then $K\triangleleft H$."

W.R. Scott's Group Theory, from 1964. Page 29, Exercise 2.1.24(c): "If $H\triangleleft G$ and $H\subset K\subset G$, then $H\triangleleft G$." (Scott uses $\subset$ to denote subgroups that need not be proper, reserving $\lt$ for proper subgroups).

Likely you have not spotted it because the more general version that is most useful is the following:

Let $G$ be a group, and $N\triangleleft G$ a normal subgroup of $G$. Then for any subgroup $H$ of $G$, $N\cap H\triangleleft H$.

In fact, this statement is part of the Second Isomorphism Theorem (which states that $NH/N\cong H/(N\cap H)$). In the special case where $N\leq H\leq G$, you get your result: $N\cap H=N\triangleleft H$.
In this form, it appears in:

Dummit and Foote, Abstract Algebra Second Edition. Exercises on Chapter 3, Quotient Groups and Homomorphisms, has Exercise 24, p. 89.

Lang's Algebra, Revised 3rd Edition, mentions en passant on page 17: "Let $G$ be a group, and let $H,K$ be two subgroups. Assume that $H$ is contained in the normalizer of $K$. Then $H\cap K$ is obviously a normal subgroup of $H$[.]"  This yields this result when $K$ is normal and $H$ contains $K$.

Derek Robinson's A Course in the Theory of Groups 2nd Edition, 1.4.4 (on page 19), the Second Isomorphism Theorem: "Let $H$ be a subgroup and $N$ a normal subgroup of a group $G$. Then $N\cap H\triangleleft H$ and $(N\cap H)x\longmapsto Nx$ is an isomorphism from $H/(N\cap H)$ to $NH/N$."

Jacobson's Basic Algebra I, Theorem 1.9 (page 64): "Let $H$ and $K$ be subgroups of $G$, $K$ normal in $G$. Then $HK=\{hk\mid h\in H, k\in K\}$ is a subgroup of $G$ containing $K$, $H\cap K$ is normal in $H$, and the map $hK\mapsto h(K\cap H)$, $h\in H$, is an isomorphism of $HK/K$ with $H/(H\cap K)$.

van der Waerden's Algebra, volume I, Section 7.3, page 147; he uses "divisor" rather than subgroup: "If $N$ is a normal divisor in $G$, $K$ is a subgroup $G$, then the intersection $N\cap K$ is a normal divisor in $K$[.]"

etc. Since this fact is key to one of the Isomorphism Theorems, I dare say that any book on Group Theory that includes the Isomorphism Theorems will necessarily include it, your result being a special case.
Comment: @PrimeMover: The statement "If $N\triangleleft G$ and $K$ is **any** subgroup of $G$, then $N\cap K\triangleleft K$" is more general than "If $N\triangleleft G$ and $K$ is a subgroup of $G$ with $N\leq K\leq G$, then $N\triangleleft K$." Not the Second Isomorphism Theorem,, but just the statement about $N\cap K$ being normal in $K$.
Comment: Why is the S.I.T. "more general"? I'm missing something.


Solving a puzzle: Graph where each node has degree 3

Question: I am trying to solve this puzzle:
Problem:
The  Park is covered by a network of hiking paths.
Goal: Find an algrithm by which you'll find (in finite time) the crossing at which the restaurant is located.
Rules:

The paths form a finite connected graph in which each crossing (that is, each crossing of paths) is of degree 3.
The crossings are indistinguishable one from another: it is not possible to know which crossing one has arrived at, to identify the paths meeting there, or to recognize whether one has been there before.
Strict rule against leaving markings in the park. It is not ok to mark crossings or path in any way.
If you enter a crossing by one path you must leave it by a different path. You have initially arrived at some node by one of its incident paths.

My questions:
For rule 1: I cannot find any graph where every node has a degree of 3. At least one node has degree 2.
2nd question: If I cannot mark the paths where I have been, what can I do to find a sufficient solution? Just go right, right right and one time left?
Comment: $K_4$ is the simplest graph where every node has degree 3.
Comment: I assume the park is planar (no trails crossing by bridges), which might be required to find an answer. (Both $K_4$ and the example from @BrianM.Scott are planar, although that picture is drawn with intersections.)
Comment: @ml0105: There is no requirement that the path be an Euler circuit; it just has to be guaranteed to reach every vertex.
Comment: Also, a practical question: is the solution allowed to be based on a map of the park, or should it be a single sequence that works for parks of any shape and size?
Comment: @MishaLavrov yes, of any shape and size...
Comment: What's the goal, to visit every junction?
Comment: If the goal is to visit every edge and to end up where you started, then you are describing an Euler Circuit. A graph $G$ has an Euler Circuit if and only if every vertex has even degree.
Comment: You can see another $3$-regular graph [here](https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/3-regular_graph.svg/425px-3-regular_graph.svg.png).
Comment: @aschepler  I added the goal.
Comment: @ml0105 The graph obviously does not have an Euler circuit.
Comment: @aschepler yes, it is planar... but does that have some advantages?
Comment: @aschepler: I suspect that it doen't matter: about the only thing that one can use is the fact that at each junction one has a choice between going left and going right, and that's the case even if there are bridges.
Comment: @ml0105: why must the solution be an Euler circuit?
Comment: I confess this remark is a little flippant: we can never find the restaurant if rule 2 holds, since if the restaurant isn't a distinguishing feature of the junction that is our goal, how can we know when we've found that junction?
Comment: Any repeating pattern of left/right choices is not a solution, since we can construct an antagonist graph which makes that period a cycle without visiting all vertices. I suspect a sequence which contains every finite substring infinitely many times would do the trick, but I haven't been able to prove it.
Comment: My apologies for the confusion! I was having some trouble parsing out what the goal of the problem was.
Comment: What is the source of this problem? It reminds me of a talk I've given, and I'm curious if one of the people listening to it wrote the problem.
Comment: https://math.stackexchange.com/questions/55221/need-an-efficient-algorithm-to-visit-all-nodes-of-a-graph-revisiting-edges-and
Answer: Define a hypothesis to consist of the following:

a $3$-regular graph representing a hypothetical map of the park;
a vertex $r$ in that graph representing a hypothetical location of the restaurant;
a vertex $s$ in that graph representing a hypothetical starting location;
an edge incident to $s$ representing the hypothetical path by which you arrived at $s$.

There are infinitely many hypotheses, but countably many: for each natural number $n$, there are only finitely many hypotheses in which the park has $n$ vertices. So we may number the hypotheses $H_1, H_2, H_3, \dots$.
One of the hypotheses represents the true map of the park, location of the restaurant, and your starting position: call that hypothesis $H_N$. You don't know which hypothesis this is, but $N$ is some finite number.

Now, here is the strategy that will get you to the restaurant. For each $i=1,2,3,\dots$, you:

Assume hypothesis $H_i$ is true for the time being.
Given the steps you've taken so far, determine the location in $H_i$'s graph where you would now be, if hypothesis $H_i$ were true.
From that location, find directions in $H_i$'s graph that would take you to $H_i$'s restaurant location $r$.
Follow those directions in the actual physical park.

For the first $N-1$ iterations of this procedure (for $i=1, 2, \dots, N-1$), you're going to be following directions based on a false premise, and they're unlikely to get you to the restaurant except by chance.
On the $N^{\text{th}}$ iteration of the restaurant, you'll assume a true hypothesis: the graph you take will be the correct graph, with the restaurant and your starting location and starting direction marked correctly. Then the location you work out in step 2 will happen to be your actual location in the park, and the directions you work out in step 3 will actually take you to the restaurant.
Comment: @StevenStadnicki Unfortunately, the rules don't let us turn around, and so returning to the start is also not something we can do without making some assumptions about the map.
Comment: I'm not sure I understand. If each vertex has degree $3$, and you can't go back the way you came, there are only two options. We can call them "left" and "right".
Comment: This could be a bit more "efficient" by checking at each new hypothesis $H_i$ if it predicts I would have already seen the restaurant. If so, toss out that hypothesis, skipping the walking around, and consider the next untested hypothesis.
Comment: the breaking point would be to left or right... but why do take the left or right?
Comment: exactly, but do choose left or right randomly?
Comment: sorry, I dont understand with the tokens? we are not allowed to mark trails. You violate rule 3.
Comment: We are not marking the trails. The algorithm I am giving is used beforehand to generate a sequence of directions to follow; then, you just follow that sequence of directions. We don't choose left or right randomly; on each map, we can look at that map to see how to get to the restaurant on that map, and give those directions.
Comment: how would you define a token?
Comment: The token is just a marker to represent a position and direction on a map. You can imagine drawing the map on paper and putting some coins on the paper and moving them around. If implementing this algorithm in a computer, they'd be a bunch of variables.
Comment: "Repeat until you've guided every token to the restaurant." Does not make sense to me, because I stop, when I find the restaurant, right?
Comment: To know the position of the restaurant I need to find all junctions, right?
Comment: @jester I have tried to rephrase my answer avoiding the terminology that confused you.
Comment: @MishaLavrov It might be easier to illustrate by backtracking to the starting position after each hypothesis is tested; this doesn't require any more ability than the procedure outlined here and has the possible visualization advantage that one tests each hypothesis from a 'clean slate'.
Comment: there is a missing path, how to verify if the hypthesis is correct. You need to save left, right etc. until to the restaurnt, right? For that you need test every combination in a graph with start and endpoint and compare within one hyptohtesis. I think that you have described that in your previous answer.
Comment: What do you mean, how to verify if the hypothesis is correct? If the hypothesis is correct, it gets you to the restaurant, and then you win.
Comment: Different possible graphs, or even the same graph with a different start and endpoint, are different hypotheses.


IV and Exogeneity Tests

Question: Read something about the Hausman test that didn't sound right in some grad course handout online.
It stated that the Hausman null of OLS and IV not being statistically different, if not rejected, means there is no evidence of endogeneity.
It also states that under the null it is assumed that OLS is consistent+efficient and that IV is consistent+inefficient.
That doesn't sound right to me. If OLS is biased and IV is correctly identified, wouldn't rejecting the null be evidence of no endogeneity? And why would we assume OLS is consistent for the null?
Comment: you have received an excellent answer by Dan. Please consider upvoting and/or accepting it if you are happy with his explanation. The model of this site depends on the contribution of everyone. Writing a good answer takes a good amount of time which is time that is devoted to help others. Accepting/upvoting only costs you a click with the mouse :)
Answer: The Hausman test is given as: $H=\left(\hat{\beta}_{OLS}-\hat{\beta}_{IV}\right)'\left[var\left(\hat{\beta}_{IV}\right)-var\left(\hat{\beta}_{OLS}\right)\right]^{-1}\left(\hat{\beta}_{OLS}-\hat{\beta}_{IV}\right)\sim\chi^{2}\left(M\right)
 $ where $\hat{\beta}_{IV}
 $ is the $M\times1
 $ vector of IV estimates. The test is built with OLS being consistent under the null because: $\left[var\left(\hat{\beta}_{IV}\right)-var\left(\hat{\beta}_{OLS}\right)\right]
 $ will always be a positive definite matrix since $var\left(\hat{\beta}_{IV}\right)>var\left(\hat{\beta}_{OLS}\right)
 $. (The variance of the OLS estimator is given as: $Avar\left(\hat{\beta}_{OLS}\right)=\hat{\sigma^{2}\left(X'X\right)^{-1}}$
  while the variance for the IV estimator is given as: $Avar\left(\hat{\beta}_{IV}\right)=\hat{\sigma^{2}\left(Z'X\right)^{-1}Z'Z\left(Z'X\right)^{-1}}$
 ). What we want to do is to test the difference between the two estimators. If we do not have endogeneity then both the OLS and the IV estimates are consistent but the IV estimate is inefficient.The null hypothesis is that we do not have any endogeneity!! This means that under the null of no endogeneity, the OLS estimate is consistent and efficient while the IV estimator is consistent although inefficient. Under the alternative hypothesis only the IV estimate is consistent (since $E\left(X'u\right)\neq0
 )$, correlation between $x_{i}
 $ and $u_{i}
 $ makes the OLS estimator inconsistent. This can be seen as:
$\hat{\beta}=\left(X'X\right)^{-1}X'y$
$\hat{\beta}=\left(X'X\right)^{-1}X'\left(X\beta+u\right)$
$\hat{\beta}=\beta+\left(X'X\right)^{-1}X'u$
We can divide through by N
  and take probability limits so we get
$plim\left(\hat{\beta}\right)=\beta+plim\left(N^{-1}X'X\right)plim\left(N^{-1}X'u\right)
 $
since we assume a random sample we can rewrite this equation by the law of large number as:
$plim\left(\hat{\beta}\right)=\beta+E\left(X'X\right)^{-1}E\left(X'u\right)=\beta
 $
since we assume $E\left(X'u\right)=0
 $. If we have endogenity, i.e. $E\left(X'u\right)\neq0
 $ then OLS will be inconsistent ($plim\left(\hat{\beta}\right)=\beta+E\left(X'X\right)^{-1}E\left(X'u\right)
 $). However, IV is by assumption $cov\left(z,\epsilon\right)=0
 $ and $cov\left(z,x\right)\neq0
 $ ($z
 $ is the instrument while $\epsilon
 $ is the error term),hence it'll be consistent. The IV estimator can be shown to be consistent from: The IV estimator can be shown to be consistent from: $plim\left(\hat{\beta_{IV}}\right)=\beta_{IV}+E\left(Z'X\right)^{-1}E\left(Z'u\right)=\beta_{IV}
 $. So to answer your question: Rejecting the null would indicate that we have endogeneity!
Comment: +1, maybe as a caveat: not rejecting the null does not necessarily mean that we do not reject exogeneity but simply the test may not detect the difference between OLS and IV if IV is too imprecise. This can happen for instance when the first stage does not explain much in the variation of the endogenous variables, i.e. the instrument isn't very strong. Then the standard errors of IV will be larger.

IV and Exogeneity Tests


Woocommerce checkout page reload after change shipping method

Question: Once i change Shipping option on checkout page i want to show/hide some field but it worked once i manual reload page then i added a jquery code to reload page -
jQuery('form.checkout').on('change','input[name^="shipping_method"]',function() {

Complete Code-
<?php
// Add custom Theme Functions here

add_filter('woocommerce_checkout_fields', 'xa_remove_billing_checkout_fields');

function xa_remove_billing_checkout_fields($fields) {
     $first_shipping_method ='free_shipping:1'; // Set the desired shipping method to hide the checkout field(s).
     $second_shipping_method ='free_shipping:2'; // Set the desired shipping method to hide the checkout field(s).
     $third_shipping_method ='local_pickup:3'; // Set the desired shipping method to hide the checkout field(s).

    global $woocommerce;
    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    $chosen_shipping = $chosen_methods[0];

    if ($chosen_shipping == $first_shipping_method || $chosen_shipping == $third_shipping_method) {
        unset($fields['billing']['billing_address_1']);
        unset($fields['billing']['billing_address_2']);
    }
    if ($chosen_shipping == $second_shipping_method || $chosen_shipping == $third_shipping_method) {
        unset($fields['billing']['billing_pick_up']);
    }

    ?>

    <?php
    return $fields;
}

But it's not working for me, can you guide me?
Answer: The page is refreshed before the changes are applied. Reboot after the updated_checkout event runs.


Error when I try to play a sound

Question: I am trying to make a alarm clock sort of thing but when I try and run the thing I am using i get this error:
 File "C:\Python27\Salty.py", line 2, in <module>
    winsound.PlaySound('siren.wav')
 TypeError: PlaySound() takes exactly 2 arguments (1 given)

Here is the code I am running.
import winsound
winsound.PlaySound('siren.wav')
I know this is probably a simple fix but it is late. Thank you for your help!
Answer: winsound.PlaySound takes two parameters: sound and flags. sound

may be a filename, audio data as a string, or None

(from the docs), while flags is a bitwise-OR'd combination of winsound.SND_FILENAME (the sound parameter is the path to a .wav file), winsound.SND_ALIAS (the sound parameter is a name for a builtin Windows sound, see the docs), winsound.SND_LOOP (play the sound in a loop), winsound.SND_MEMORY (the sound parameter is a memory image of a .wav file), winsound.SND_PURGE (stop all playing instances of the specified sound, not supported on modern Windows), winsound.SND_ASYNC (return immediately, allowing sounds to play asynchronously), winsound.SND_NODEFAULT (do not play default sound if the sound cannot be found), winsound.SND_NOSTOP (do not interrupt other sounds currently playing) and winsound.SND_NOWAIT (return immediately if the sound driver is busy)
What you want is the winsound.SND_FILENAME flag:
import winsound
winsound.PlaySound('siren.wav', winsound.SND_FILENAME)

or, as a function:
import winsound
def playSiren():
    winsound.PlaySound('siren.wav', winsound.SND_FILENAME)

Edited to add:
It may be necessary to give the whole path (e.g. 'C:\\whatever\\siren.wav') instead of just the file name (as discussed in the comments).
Comment: How could I turn that into a function?
Comment: Not even the default system sound? What does `winsound.PlaySound("SystemHand", winsound.SND_ALIAS)` do (it should play the default error sound)?
Comment: It plays a noise, but not the one i put in there. It plays like sort of an error noise.
Comment: Good, so `winsound` works. Is the `siren.wav` file in your current working directory (try printing `os.getcwd()` in your script)?
Comment: I don't know if I put it in right but if i did, it did not work.
Comment: Try giving the whole file path `PlaySound` (ie. instead of `'siren.wav'` use `'C:\\whatever\\siren.wav'`)
Comment: Amazing! Thank you soo much for helping me! (The siren was extremley loud and scared the crud out of me)

Error when I try to play a sound


req.body do node.js vazio no rest da angular+

Question: Boa tarde
Estou criando uma API em node.js e estou com problemas no req.body recebido pelo express. O problema se resume a não conseguir pegar as variáveis rest post enviada pelo angualr9, quando eu crio um formulário e direciono para a rota do arquivo node.js ele acha as variáveis porem no rest do angular as variáveis vem no arquivo (consigo vela no debug do navegador) mas não chega ao servidor. Desde ja agradeço a atenção dos colegas.

const bodyParser = require('body-parser');
const express = require('express');

const config = require('../../config/config');
const port = config.server.port;

const servPontoDeVenda = require('../../api/pages/loja/pdv');

const app = express();

app.use(bodyParser.urlencoded({ extended:true}));
app.use(bodyParser.json());

app.use(function (req, res, next) {

    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
    res.setHeader('Access-Control-Allow-Headers', 'content-type');
    res.setHeader('Content-Type', 'application/json');
    res.setHeader('Access-Control-Allow-Credentials', true);
    next();
});

app.get('/get', (req, res) => {
    res.json(servPontoDeVenda.get(req));
});

app.post('/post', function (req, res) {
    console.log('bod', req.body); //esta vindo vazio
    res.json(servPontoDeVenda.post(req));
});

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`);
});

Comment: não tem nenhum erro no código nodejs, executei aqui, e enviando corretamente:
`$ curl -d '{"teste": 42}' -H 'content-type: application/json' http://localhost:3000/post `
os dados chegam:
`bod { teste: 42 }`

o seu erro está na camada frontend.
Comment: Testei o código tb, quando envio json está ok, o problema é quando envio formdata. Provavelmente este é o método de envio via formulário html.
Comment: Poderia me mandar o formato que vcs estão enviando no front-end? O engraçado é que eu ja utilizo este método no front-end para uma api em PHP.
Comment: faz o contrario, diz como você tá enviando.. qual o tipo? tá indo form data?
Comment: Cara achei o erro para a api php eu tenho um método tratado para receber e enviar os dados todos passa por este método e ele utiliza JSON.stringify(objJSON) e isto estava danificando ou algo do tipo no node.js. Muito obrigado pela dica.
Answer: Como está realizando sua requisição? Tente adicionar esses cabeçalhos.
fetch(`${url}/teste`, {
  method: 'POST',
  headers: {
     'Accept': 'application/json',
     'Content-Type': 'application/json'
  },
  body: JSON.stringify({"a": "teste"})
})

Comment: Como está escrito atualmente, sua resposta não é clara. Por favor, pode [edit] para adicionar mais detalhes, que ajudarão outras pessoas a entender como isso resolve a pergunta feita. Você pode encontrar mais informações sobre como escrever boas respostas [na Central de Ajuda](/help/how-to-answer).
Answer: Acredito que o problema esteja em quem está dando o cast para json no objeto enviado.
Você alterando de:
app.use(bodyParser.json());

para
app.use(express.json());

deve funcionar.