Why do we weight the surprisals by the probabilities when computing the entropy?

Question: Let's say that a random variable $X$  takes the values $(x_1, x_2, \dots, x_n)$ over the sample space $\Omega$.
As far as my understanding goes, the entropy of a given variable is meant to give an idea of how small are the probabilities of the same variable taking the values that it takes, and thus knowledge of a variable that has a high value of entropy would significantly limit the possible values taken by other variables over the same sample space.
My question is, if we want to measure how small, on average, the probabilities $(p(x_i))_{i \in [1,n]}$ are, why don't we just calculate the result of the term:
$$\frac{1}{n}\sum_{i \in [1, n] } log(\frac{1}{p(x_i)})$$
Why do we have to weight it by the probabilities $(p(x_i))_{i \in [1, n]}$ which seem to produce a counter effect to the one we intend to measure (the higher the surprisal the smaller its weigh and thus the less important it is to the overall sum)?
I can see that the textbook formula for entropy (for lack of a better term)  is indeed the average of the variable $-log(P(X))$ over the sample space $\Omega$. But it seems more intuitive to me to calculate the expectation of the same variable over $X(\Omega)$ instead,  which translates to the formula aformentioned.
Comment: See  also https://stats.stackexchange.com/questions/66186/statistical-interpretation-of-maximum-entropy-distribution/245198#245198
Answer: First of all, intuitively, it is an expected value, because we want to know what is the "average uncertainty" of a random variable. It also tells you about the "number of bits on average required to describe the random variable". If you didn't weigh the probabilities, the "average" wouldn't make sense. Let's re-write the entropy as an expected value of the information content of the $x_i$ values
$$
H(X) = E[I(X)] = \sum_i p(x_i) I(x_i)
$$
With your formula, this would be just a (normalized) sum $\sum_i I(x_i)$, why would you care about the sum? You don't care about the "total information content" but the average. If you get a random message $x_i$, you can use the expected value to make a rough guess on how many bits you need to encode it. The probability that you observe a particular $x_i$ value is $p(x_i)$, so that should be its contribution to the average. It is just an expected value of the function (that tells us about the information content) of a random variable.
But Shannon (1948) also listed the formal requirements he wanted the entropy to follow

$H$ should be continuous in the $p_i$.
If all the $p_i$ are equal, $p_i = \frac{1}{n}$, then $H$ should be a monotonic increasing function of $n$. With equally likely events there is more choice, or uncertainty, when there are more possible events.
If a choice be broken down into two successive choices, the original $H$
should be the weighted sum of the individual values of $H$.

and, as he proves, only the expected value satisfies them.
Comment: In the formula I mentioned the sum is divided by $n$ (the number of unique values taken by $X$ i.e. $Card(X(\Omega))$. Different $x_i$ could have the same probability $p$, thus if we wanted to re-write the sum as a sum of unique probability values, each surprisal $-log(p)$ would be weighted by the number of $x_i$ having the same probability $p$ divided by $n$.
Comment: @MehdiCharife Yes, I understand, but it is inconsistent with Shannon's requirements. If this metric is useful for you for some reason, feel free to use it, but it is not what we mean by entropy.
Comment: @tim this is a pretty circular argument in your comment, I'd say a better question op could have asked is "why do we need those assumptions Claude mentioned, and what would average information achieve conceptually"


Does P(Y|X)=const. in the whole P(Y,X) imply that P(X|Y) also remains constant? Or is covariate shift the same as label shift?

Question: I research covariate shift solutions for ML models. Some papers/books (e.g. "Probabilistic Machine Learning" by Kevin Murphy) claim that one needs different solutions for covariate and label shifts, I claim they are the same.
Definitions:
Covariate shift - P(Y|X) remains the same, P(X) changes between source and target domains.
Label shift - P(X|Y) remains the same, P(Y) changes between source and target domains.
Difference between these two: causality. We speak about covariate shift when X->Y and label shift otherwise.
I claim that if P(Y|X) changes in any point of the feature space X then P(X|Y) changes in the corresponding point of the label space. Therefore, assumption that P(Y|X)=const. implies that P(X|Y)=const. as well in the whole joint P(Y,X).
Am I wrong?
Answer: No, $P(Y\mid X)$ remaining unchanged over time does not imply that $P(X\mid Y)$ also remains unchanged.
For a small example, take the two discrete distributions:
$$\begin{pmatrix}
0.2 & 0.1 \\
0.3 & 0.4
\end{pmatrix}
,\qquad
\begin{pmatrix}
0.3 & 0.05 \\
0.45 & 0.2
\end{pmatrix}
$$
with $X$ indexing the columns and $Y$ the rows.  Then (because the columns are constant multiples from one dist. to the other) $P(Y\mid X)$ is the same for the two, but $P(X\mid Y)$ is not.  So this is an instance of covariate shift, but not label shift.
Comment: It seems that I got confused on conditionals. The condition is *given* so all the events given the condition should sum up to 1.
Comment: Thank you for replying.

Let's assume X and Y can take values 1 and 2. The first joint distribution is:

P(X=1, Y=1)=0.2
P(X=1, Y=2)=0.3
P(X=2, Y=1)=0.1
P(X=2, Y=2)=0.4

Conditionals of the 1st first distribution:

P(Y|X=1) = 1 with P=0.2, 2 with P=0.3
P(Y|X=2) = 1 with P=0.1, 2 with P=0.4
P(X|Y=1) = 1 with P=0.2, 2 with P=0.1
P(X|Y=2) = 1 with P=0.3, 2 with P=0.4

2nd:

P(Y|X=1) = 1 with P=0.3, 2 with P=0.45
P(Y|X=2) = 1 with P=0.05, 2 with P=0.2
P(X|Y=1) = 1 with P=0.3, 2 with P=0.05
P(X|Y=2) = 1 with P=0.45, 2 with P=0.2

Aren't they all different?
Comment: Sorry for bad formatting of the comment. I couln't make line split (or I don't know how). I hope you will read it and share your thoughts.


Google task api authentication not working php

Question: I have a file called TasksLogin.php which lets me login
session_start();
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_TasksService.php';
$client = new Google_Client();
$client->setClientId('xxxxx.apps.googleusercontent.com');
$client->setClientSecret('xxxxxxxxx');
$client->setRedirectUri('http://xxxxxxxxx/Tasks/TaskVis.html');
$client->setApplicationName("TasksForMike");
$tasksService = new Google_TasksService($client);

if (isset($_REQUEST['logout'])) {
  unset($_SESSION['token']);
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);

} else {
  $client->setAccessToken($client->authenticate($_GET['code']));
  $_SESSION['token'] = $client->getAccessToken();

}

if (isset($_GET['code'])) {
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
?>
<?php $_SESSION['token'] = $client->getAccessToken(); ?>

That seems to work because it redirects to TaskVis.php
But in TasksVis.php I call:
$(document).ready(function() {
  $.get("tasks.php", function(data){
var json = data;
});
 });

which is the php file that gets the tasks and packages them up in a json object.
but in tasks.php, I have this code that crashes:
session_start();
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_TasksService.php';
$client = new Google_Client();
$client->setClientId('xxxxxx.apps.googleusercontent.com');
$client->setClientSecret('xxxxxxxxxxxxxxx');
$client->setRedirectUri('http://xxxxxxxxx/Tasks/TaskVis.html');
$client->setApplicationName("TasksForMike");
$tasksService = new Google_TasksService($client);

if (isset($_REQUEST['logout']))
{
  unset($_SESSION['token']);
}
 if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  echo "hh";
  die();
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);

}
?>

the "die" never runs and I get a 500 server error. Since the code is in the query string, why is the $client->authenticate($_GET['code']); failing ? I am trying to seperate the data code from the rendering.
Answer: I don't think there's any 'code' in the query string. Your 'code' value is lost when you redirect to TaskVis.php and don't include it in the redirect. Then, in that file, you're calling tasks.php here...
$.get("tasks.php", function(data){

...so if there were any _GET values, they would be included in the call:
$.get("tasks.php?code=xxxx", function(data){

I'd be interested in knowing the nature of the 500 error also. Have you tried calling this while viewing the console in Firebug?


How to get only parent term ids of a given term, without loading the terms

Question: I want to find all parent ids of a given taxonomy term id recursively, without loading them. Existing taxonomy functions like taxonomy_get_parents_all() and taxonomy_get_parents() are loading the terms.
Since I will be using this result in an API function, it is important for me to get the parents with a minimum performance overhead. All I need to get the parents in a simple array like: $parents = array(6, 3, 2);
Answer: Have a look at taxonomy_get_parents() and taxonomy_get_parents_all().
You could copy most of the functions but skip
$parents[$tid] = taxonomy_term_load_multiple($tids);

to not load the terms.
/**
 * Load a terms parent.
 */
function MODULE_taxonomy_get_parents($tid) {
  $parents = &drupal_static(__FUNCTION__, array());

  if ($tid && !isset($parents[$tid])) {
    $query = db_select('taxonomy_term_data', 't');
    $query->join('taxonomy_term_hierarchy', 'h', 'h.parent = t.tid');
    $query->addField('t', 'tid');
    $query->condition('h.tid', $tid);
    $query->addTag('term_access');
    $query->orderBy('t.weight');
    $query->orderBy('t.name');
    // $tids = $query->execute()->fetchCol();
    // $parents[$tid] = taxonomy_term_load_multiple($tids);
    $tids = $query->execute()->fetchAll();
    $parents[$tid] =  $tids;
  }

  return isset($parents[$tid]) ? $parents[$tid] : array();
}

/**
 * Load all parents for a term.
 */
function MODULE_taxonomy_get_parents_all($tid) {
  $cache = &drupal_static(__FUNCTION__, array());

  if (isset($cache[$tid])) {
    return $cache[$tid];
  }

  $parents = array();
  if ($term = taxonomy_term_load($tid)) {
    $parents[] = $term;
    $n = 0;
    // while ($parent = taxonomy_get_parents($parents[$n]->tid)) {
    while ($parent = MODULE_taxonomy_get_parents($parents[$n]->tid)) {
      $parents = array_merge($parents, $parent);
      $n++;
    }
  }

  $cache[$tid] = $parents;

  return $parents;
}

If you don't need any access control or sorting you can just query the taxonomy_term_hierarchy table directly.
$query = db_select('taxonomy_term_hierarchy', 'h');
$query->addField('h', 'parent', tid);
$query->condition('tid', $tid);
$query->condition('parent', 0, '!=');
$tids = $query->execute()->fetchAll();
$parents[$tid] =  $tids;

Comment: This returns only the first parent of a given term, not the parents of the parents recursively.
Comment: @ЕлинЙ. I updated the answer so that it returns all parents.
Comment: Thank you Marcus for your answer. Although it is a valid solution for my question, it is not an effective solution that I'm seeking. It recursively makes a database query for each parent term (which Drupal core is also doing that way). I had already done it in a similar but with a simpler query. I hoped for a solution which makes it with an only one query at all.
Answer: I managed to achieve that as follows:
function MYMODULE_taxonomy_get_parents($tid) {
  $taxonomy_hierarchy = db_query('SELECT tid, parent FROM {taxonomy_term_hierarchy}
                                  WHERE parent != 0')
                        ->fetchAllKeyed();
  $parent_tids = array();

  while (!empty($taxonomy_hierarchy[$tid])) {
    $tid = $taxonomy_hierarchy[$tid];
    $parent_tids[] = $tid;
  }

  return $parent_tids;
}

I have also added some static caching, but for the brevity I didn't include it in this answer.


Why will this not compile

Question: Newest Ubuntu (20) and swift (5.6).
Test.swift
Import WebKit
<code>Var WKit = WebKit()
</code>
swiftc test.swift
error: no such module 'WebKit'
I used apt-get libwebkit2gtk perhaps this is the suspect?
Comment: Because WebKit is not part of Swift for Linux I assume.
Answer: WebKit is not part of the Swift language or standard library. WebKit is a separate framework bundled with the OS that's only available on iOS and macOS. Hence, you cannot use it on Linux.
For more information, see the WebKit documentation.
Comment: Thank you. Looks like a path issue as import OS also not working
Comment: I'm still thinking path issue; however,
my understanding the package for WebKit ported linux is  libwebkit2gtk which I successfully installed.

Why will this not compile


WordPress - remove capabilities based on user id

Question: I current have this code written:
add_action( 'init', 'hide_user_caps' );
function hide_user_caps() {
    $user_id = 1;
    $user = new WP_User( $user_id );
    $user->remove_cap( 'edit_posts' );
}

However, this doesn't seem to be doing anything. Any ideas?
Thank you
Answer: If you want to specifically deny a capability from a user (not a role), you need to use the add_cap()[1] method and specify that the $grant parameter is false. [2].
$user_id = 1;
$user = new WP_User( $user_id );
$user->add_cap( 'edit_posts', false );

[1]: WP_User::add_cap()
[2]: The answer
Comment: Thank you for that, when would you use remove_cap then? Just curious..
Comment: @cjblue When you want to delete from within a role.


how to set a Role for Sonata Admin Block

Question: In Sonta Admin Dashboard, i want to display Blocks based on Roles.
I.e : a block for admin will not be displayed for Author and etc.
Answer: in config.yml just define the roles:
<code>dashboard:
    blocks:
        - { position: left, type: sonata.block.service.statistics.toparticles,class: col-md-6, roles: [ROLE_ADMIN,ROLE_AUTHOR] }
</code>
Comment: You do realize that there are timestamps on both your question and your answer, and that they are just one minute apart? If you're trying to tell a story, you should at least get the facts straight.
Comment: Why do you ask questions, just to answer them yourself?
Comment: I don't do that!
i write the question, and  search for the solution, when i find the solution, i simply answer my question when no one do that!
Comment: Thanks for your comments :)


RAID 5 with Windows Server 2016 R2 - 45 drives - 10 hot spare

Question: I have a 45 drives server that will be running Windows Server 2016 R2. It will be fully populated and utilizing RAID 5 and I want 10 of the drives to act as hot spares. We have been searching all over and cannot find a way to do this. Is this even possible?
Comment: PLEASE don't even think about using R5, it's been essentially dead for well over a decade, we deal with at least one person a week who comes here for help when it's killed their data completely. Please only use R1/10, R6/60 or RAID-Z if you like that kind of thing, anything else is trouble.
Comment: ...Windows Server 2016 R2?!?
Comment: `1.` There's no such thing as Windows Server 2016 R2. Please tell us the actual Operating System. `2.` What Make/Model is the server? `3.` What RAID controller are you using?
Comment: If you want to use Software: Windows RAID (which shall not be used any longer) does not support any spare configuration. Windows Storage Spaces on the other hand, do. If you are going to use a hardware RAID controller, think twise about using RAID5 and consult the documentation on how it handles spares. Also, as mentioned, Server 2016R2 does not exist.
Answer: Windows Server software RAID (i'm fairly certain) will not let you set spares.
Windows Storage Spaces will let you add hot spares to a pool.
Hardware RAID cards will, depending on capabilities, let you define spares in a RAID set or even across RAID sets.
With the size of today's disks, I'd caution you to NOT make a big RAID-5 pool, and I'm also wondering why you would want 10 spare disks.


Finding unbiased estimator for Truncated Poisson Distribution

Question:
Let $X$ be a single observation from truncated Poisson distribution having probability mass function $P(X = x) =\frac{e^{-\theta} \theta^{x}}{x!(1-e^{-\theta})} ; x = 1,2,3,$ . The estimator $T = \left\{\begin{matrix}
2, x = 1,3,5...\\
0, x = 2,4,6...
\end{matrix}\right.$ $ \ \ \ \ \ \ $ is unbiased for :

So I a have been preparing for an examination and this question popped up. I do not even know where/how to begin.
EDIT: Taking @mlofton's advice I proceeded as follows:
$$E(T) = 2 * \left [\frac{e^{-\theta} \ \theta}{1!(1-e^{-\theta})} \  + \frac{e^{-\theta} \ \theta^{3}}{3!(1-e^{-\theta})} \ + \frac{e^{-\theta} \ \theta^{5}}{5!(1-e^{-\theta})}  ..... \right ]$$
Using the Taylor series expansion of $e$ we can simplify the above as:
$$E(T) = \frac{ e^{-\theta} }{(1-e^{-\theta})} * \left [\frac{2\theta}{1!} \  + \frac{ 2\theta^{3}}{3!} \ + \frac{2\theta^{5}}{5!}  ..... \right ] = \frac{ e^{-\theta}(e^{\theta} - e^{-\theta}) }{(1-e^{-\theta})} $$
$$= \frac{ (1 - e^{-2\theta}) }{(1-e^{-\theta})}$$
Is my answer correct?
Comment: This seems correct to me, you can even simplify the last line into $1+e^{-\theta}$
Comment: This isn't a complete answer but an estimator is unbiased for its expectation so you need to calculate the expectation of your estimator $T$..
Comment: @mlofton Okay thank you. I believe I got it now
Comment: @periwinkle Yes. Thank you
Answer: Your answer looks correct to me, but it can be further simplified.  You should have:
$$\begin{align}
\mathbb{E}(T)
&= 2 \Bigg[ p_X(1) + p_X(3) + p_X(5) + \cdots \Bigg] \\[6pt]
&= 2 \Bigg[ \frac{e^{-\theta} \theta}{1! (1-e^{-\theta})} + \frac{e^{-\theta} \theta^3}{3! (1-e^{-\theta})} + \frac{e^{-\theta} \theta^5}{5! (1-e^{-\theta})} + \cdots \Bigg] \\[6pt]
&= \frac{e^{-\theta}}{1-e^{-\theta}} \Bigg[ \frac{2 \theta}{1!} + \frac{2 \theta^3}{3!} + \frac{2 \theta^5}{5!} + \cdots \Bigg] \\[8pt]
&= \frac{e^{-\theta} (e^{\theta} - e^{-\theta})}{1-e^{-\theta}} \\[8pt]
&= \frac{1 - e^{-2\theta}}{1-e^{-\theta}} \\[8pt]
&= \frac{(1 + e^{-\theta})(1 - e^{-\theta})}{1-e^{-\theta}} \\[12pt]
&= 1 + e^{-\theta}. \\[6pt]
\end{align}$$
Your original question got cut off, so it's not clear what unbiasedness property the question is asserting.  In any case, you can see that the statistic $T$ is an unbiased estimator for $1+e^{-\theta}$ and biased for other functions of $\theta$.


Probability of guessing M&M colors correctly

Question: Suppose there are 5 M and M beans of different colors. You randomly choose 3. What is the probability that you guess 2 out of the 3 numbers correctly?
Apparently the answer is $12/125$, but I don't see where this came from.
Here is my take on this. Consider the 3 selected beans. What is the probability that you guess 2 out of the 3 correctly? To guess 2 correctly, there are $\binom{3}{2}$ different pairs you can guess correctly. For each pair, there is a $\frac{1}{5} \cdot \frac{1}{4} \cdot \frac{2}{3}$ probability of guessing it correctly. So I believe the answer should be
$$
\binom{3}{2} \frac{1}{5} \cdot \frac{1}{4} \cdot \frac{2}{3} = \frac{1}{10}
$$
Am I thinking about this correctly?
Answer: In both answers, the order of the candies appears to be relevant.  The difference between the answers has to do with the strategy for guessing.  In your answer, you are not repeating your guesses.  In the given answer the guesses are made independently and guesses may be repeated (despite this lowering the probability, as shown).
Note $\frac{12}{125}=3\cdot \frac{1}{5}\cdot \frac{1}{5}\cdot \frac{4}{5}$
Which answer is "more correct?"  Without more context, I would have leaned towards your answer myself as it is commonly assumed that such guessing would be done in an optimal way that maximizes chances of success.
Comment: Ah there it is. So my strategy is slightly better, it seems? Can you come up with an even better guessing strategy?
Comment: Yes, since $\frac{12}{125}<\frac{12}{120}=\frac{1}{10}$.  As for an even better strategy?  I'm rather certain there isn't one.
Comment: This does, of course, assume that the problem should be interpreted as the candies are all five different colors and we know those five colors.  If we were instead told that a certain set of five colors are possible but each candy in the bag were randomly assigned a color independently of one another (*so it is possible that all five candies are red for instance*), then the given answer would have been correct and you incorrect.  That is not how I read it however.


How can we broaden the site's coverage of the various subfields of economics?

Question: Prompted by this recent question, "Are questions about social choice and voting on-topic here?", I am starting to worry that we might be losing potential newcomers because the site does not currently seem to cover/welcome questions from their particular subfield of economics? My questions are these:

how can we broaden the site's coverage of the various subfields of economics?
what subfields are up until now still missing from economics.stackexchange.com?
how can we "prime the pump" (so to speak) to help make these subfields seem more welcome?

For example, I haven't seen any questions that cover economic field experimentation, questions from law and economics, marketing (yes, I'm pretty sure that this belongs), environmental economics (maybe they're have been questions from this field), health economics, management science.
In general, there seems to be very little applied economics and little econometrics in general. How can we help improve this?
Comment: I have created another question that I think will help measure the site's coverage: http://meta.economics.stackexchange.com/questions/192/creating-a-list-to-measure-the-sites-coverage-of-subfields
Comment: Why the [advertising] tag?
Comment: Maybe it doesn't belong, but I put it there because I figured that an important part of both advertising and promoting the site in its early stage depends on making sure that the site's content appeals to and is relevant to a broad range of economists.
Comment: Cool. I assumed as much but wanted to ensure it wasn't in error or something like that. :-) No worries!
Comment: Oh, OK. Thanks for checking!
Answer: I'd say let it come naturally. For whatever there is demand, people will ask questions - don't regulate this market.
The one thing to speed up the process is to invite colleagues from all areas, but I see no reason why we should restrict this to topics with low participation rates.


How to move/position DIV container by changing CSS values using Javascript?

Question: I got two div containers as blocks with some fixed width and maybe height and display: block;

When you hover your mouse over container .one I need somehow to apply margin-top (or padding-top) to container .two so it moves couple pixels below while mouse is over .one and when you move mouse pointer aside, .two comes back to it's original position.
Comment: Is there a reason you want to use JS? It can be done with simple CSS.
Comment: `.one:hover ~ .two{ margin-top:10px;}` try using CSS it has simple solution if `~` is no getting use try `+` used to select adjacent elements
Answer: .one:hover + .two
 {
   margin-top: 2px;
 }

second div must be followed by first div
.one:hover ~ .two
 {
   margin-top: 2px;
 }

If some other element present between one and two, try this.
Answer: For your javascript (jQuery) solution:
$( ".one" ).hover(
  function() {
     $('.two').css('marginTop', '5px');
 }, function() {
     $('.two').css('marginTop', '0');
 });

For smoother movement:
$( ".one" ).hover(
  function() {
     $('.two').animate({
         marginTop: "5px"
      }, 500 );
 }, function() {
     $('.two').animate({
         marginTop: "0"
      }, 500 );
 });

Added a Demo
Comment: Hi, thanks for helping. But I already selected user3801433 as answer because it actually works. I didn't know there's CSS solution and it's way less complicated than JS.
Comment: you're welcome. A pure css-solution is indeed more effective. I let that stand here for other, who are seeking ;-)


KeyError: 'date' - I don't know why I keep getting this error

Question: Can anyone help with this error? I am trying to plot high-level charts to get an idea of the various developments in a Covid-19 dataset that I'm using.
# Convert date column to datetime type
df['date'] = pd.to_datetime(df.date)

# Group data by quarter and calculate total cases per million
df_total_cases_quarterly = df.groupby(df['date'].dt.quarter).agg({'total_cases_per_million': 'sum'}).reset_index() # df['date'].dt.quarter

# Create bar plot for total cases per million on a quarterly basis
sns.set_style('whitegrid')
sns.barplot(x ='date', y = 'total_cases_per_million', data = df_total_cases_quarterly)
plt.title('Total Cases per Million (Worldwide) - Quarterly')
plt.xlabel('Quarter')
plt.ylabel('Total Cases per Million')
plt.xticks(ticks=[0, 1, 2, 3], labels=['Q1', 'Q2', 'Q3', 'Q4'])  # Update x-axis labels to show quarters
plt.show()

The error code whenever I run the code given above
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/opt/anaconda3/lib/python3.9/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3628             try:
-> 3629                 return self._engine.get_loc(casted_key)
   3630             except KeyError as err:

/opt/anaconda3/lib/python3.9/site-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

/opt/anaconda3/lib/python3.9/site-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'date'

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
/var/folders/rf/8yc43r0d13l2gw8m9r17pc7h0000gn/T/ipykernel_942/1110610808.py in <module>
      3
      4 # Group data by quarter and calculate total cases per million
----> 5 df_total_cases_quarterly = df.groupby(df['date'].dt.quarter).agg({'total_cases_per_million': 'sum'}).reset_index() # df['date'].dt.quarter
      6
      7 # Create bar plot for total cases per million on a quarterly basis

/opt/anaconda3/lib/python3.9/site-packages/pandas/core/frame.py in __getitem__(self, key)
   3503             if self.columns.nlevels > 1:
   3504                 return self._getitem_multilevel(key)
-> 3505             indexer = self.columns.get_loc(key)
   3506             if is_integer(indexer):
   3507                 indexer = [indexer]

/opt/anaconda3/lib/python3.9/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3629                 return self._engine.get_loc(casted_key)
   3630             except KeyError as err:
-> 3631                 raise KeyError(key) from err
   3632             except TypeError:
   3633                 # If we have a listlike key, _check_indexing_error will raise

KeyError: 'date'

I grouped a COVID-19 dataset by quarter and calculated the total cases per million so that the result can be displayed using seaborn. Despite date column not missing which I used earlier in the code as seen below and did run successfully, I am not sure why I am getting the error code given above. I would to know what is casing the error and fix it. Thanks for your help!
This code runs successfully
# Filter data for daily new deaths per million
df_daily_new_deaths = df.groupby('date').agg({'new_deaths_per_million': 'sum'}).reset_index()

# Create line plot for daily new deaths per million
sns.set_style('whitegrid')
sns.lineplot(x = 'date', y = 'new_deaths_per_million', data = df_daily_new_deaths)
plt.title('Daily New Deaths per Million (Worldwide)')
plt.xlabel('Date')
plt.ylabel('Daily New Deaths per Million')
plt.xticks(rotation = 45)
plt.show()

Comment: I'm voting to close this question as caused by a typo / not reproducible. [code execution](https://i.stack.imgur.com/HIiZM.png)
Answer: Your code should work fine, accessing dt.quarter shouldn't change the column name. You are probably doing something else that you are not reporting here. Or maybe using an old version of pandas with a bug?
This was tested both on python 3.8 + pandas 1.5.2 and on python 3.11 + pandas 2.0.0.
Example:
import pandas as pd
import numpy as np

# set up dummy data
np.random.seed(0)
df = pd.DataFrame({'date': ['2023-01-01', '2023-04-01', '2023-07-01', '2023-10-01']*5,
                   'total_cases_per_million': np.random.random(20),
                  })
df['date'] = pd.to_datetime(df.date)

# running your exact code

import seaborn as sns

# Group data by quarter and calculate total cases per million
df_total_cases_quarterly = df.groupby(df['date'].dt.quarter).agg({'total_cases_per_million': 'sum'}).reset_index() # df['date'].dt.quarter
df_total_cases_quarterly

sns.set_style('whitegrid')
sns.barplot(x ='date', y = 'total_cases_per_million', data = df_total_cases_quarterly)
plt.title('Total Cases per Million (Worldwide) - Quarterly')
plt.xlabel('Quarter')
plt.ylabel('Total Cases per Million')
plt.xticks(ticks=[0, 1, 2, 3], labels=['Q1', 'Q2', 'Q3', 'Q4'])  # Update x-axis labels to show quarters
plt.show()

Output:
Answer: You can reassign column date by quarters before groupby:
df_total_cases_quarterly = (df.assign(date=df['date'].dt.quarter)
                              .groupby('date')
                              .agg({'total_cases_per_million': 'sum'})
                              .reset_index()

Or change index name by DataFrame.rename_axis:
df_total_cases_quarterly = (df.groupby(df['date'].dt.quarter)
                              .agg({'total_cases_per_million': 'sum'})
                              .rename_axis('date')
                              .reset_index()


Probability of winning first n games.

Question: Two players A and B  play tennis ,where they serve alternately each point.
Probability of player A winning a point on his serve is p.
Probability of player A winning a point on player B's serve is q.
Player who first wins n points will win the match.What is the probability of player A winning the match?
Player A starts serving first.n is even.
Total number of game to be played is n+k;$0\leq k \leq n-1$.
So n wins of A can divided into two parts
1.m wins when A is serving.
2.n-m wins when B is serving.

Formally stated question:
Two players A and B play tennis for several games. Player A has different probability to win at different games. At $(2k-1)$-th game, the probability that A wins is $p$, while at $2k$-th game, the probability that A wins is $q$, where $k = 1,2,3,\cdots,$. The player whose number of winning games firstly achieves $n$ will be the final winner ($n\geq 1$ is a given fixed number). The question is, what's the probability that A become the final winner.
Comment: And what work have you done on this so far?
Comment: uniquesolution, Lord Shark the Unknown, NCh, Guy Fsone, Sahiba Arora -  do any of you understand the question?
Comment: Two players A and B play tennis for several games. Player A has different probability to win at different games. At $(2k-1)$-th games, the probability that A wins is $p$, while at $2k$-th games, the probability that A wins is $q$, where $k = 1,2,3,\cdots,$. The player whose number of winning games firstly achieves $n$ will be the final winner. The question is, what's the probability that A become the final winner.
Answer: Since my answer is a complicated sum of items, I am not sure whether it's the best answer, but I am sure it's correct (even if it's almost of no use since it could be too tedious to compute that sum!).
Let's begin from a simpler setting, where $p = q$. Suppose that they keep playing even if one player wins $n$ points first, until the total number of games achieves $2n-1$. Then
{$A$ first wins $n$ points} = {$A$ wins $k$ points in the first $2n-1$ games, for $k = n,\cdots,2n-1$}.
The probability of the right hand side can be easily computed since $k$ has a binomial distribution, thus
$$P(LHS) = P(RHS) = \sum_{k = n}^{2n-1}C_{2n-1}^k p^k(1-p)^{2n-1-k},$$
where $C_b^a$ is the binomial coefficient, and is sometimes denoted by $_b C_a$ or $(^b_a)$. Now we come to deal with the original problem. The equation
{$A$ first wins $n$ points} = {$A$ wins $k$ points in the first $2n-1$ games, for $k = n,\cdots,2n-1$}
is still correct, but now the hard point is, $P(RHS)$ is harder to compute. But we can figure it out by analysing more details. Suppose that $A$ wins $k$ points in the first $2n-1$ games, and among those $k$ points, $m$ points are from odd-ordered games (i.e., whose serial number is an odd number), and $k-m$ points are from even-ordered games, then
$$P(RHS) = \sum_{k = n}^{2n-1}\left[ \sum_{m = 1}^{n} \left(C_{n}^m p^m(1-p)^{n-m}\right)\cdot \left(C_{n-1}^{k-m} q^{k-m}(1-q)^{n-1-k + m}\right)  \right],$$
where one should notice that there are only $n$ odd numbers and $n-1$ even numbers in $1,\cdots,2n-1$.
Comment: I don't think there would be some way to simplify the result... Because for the simple case n=2, the result already cannot be simplified further.

Probability of winning first n games.


What test should I be using?

Question: I'm trying to compare differences between site conditions in areas that experienced tree death for the first time between two years. I have a dataset of presence/absence of mortality at the SAME sites in 2011 and 2015. From that dataset, I would like to extract sites in both 2011 and 2015 that saw tree mortality for the first time and compare differences in environmental variables (i.e elevation, slope, aspect). This results in unequal sample sizes (more sites saw mortality in 2011 [91 sites] than 2015 [51 sites]) and I believe the data is also dependent (the same sites were sampled in 2011 and 2015- although the data isn't paired, the same site could not show up in both 2011 and 2015). Here is example dataset:

I'm quite confused as to which test I should run to compare the differences between the two years. Any assistance is very much appreciated.
Answer: You could try the Welch's t-test. This test allows for two independent samples with unequal variances and sample sizes. This test however is not as reliable for smaller sample sizes.
Comment: Thank you. But what if the samples aren't independent? For example, you wouldn't see a sample site that occurred in 2011 in 2015, so I believe my dataset is lacking independence.
Comment: Ahh I see. You might try to look at the approach discussed in this [post](http://stats.stackexchange.com/questions/90644/how-to-compare-two-non-normally-distributed-samples-with-very-different-sizes?rq=1).
Comment: Hmm but the Wilcoxon-Mann-Whitney has an assumption of independent data. Still unsure. Thanks for your help.
Comment: Non-parametric tests do not make assumptions about the underlying distribution. You may need to look to this group for your analysis. The [two-sample Kolmogorov-Smirnov test](http://itl.nist.gov/div898/software/dataplot/refman1/auxillar/ks2samp.htm)  is an option.
Comment: After further thought, you are working with unbalanced panel data. Two time periods with unequal observations in each. This will be another consideration in your analysis.
Comment: I was just reading in a statistics textbook and found that you could use a Student's t two sample test for two **dependent** samples if they are randomly selected. So you do a random selection of paired samples. Would that work for what you want to do?
Comment: Sorry for the late reply-- that sounds about right-- might you cite the statistics textbook? Thank you.
Comment: The [textbook](https://www.pearsonhighered.com/product/Triola-Elementary-Statistics-12th-Edition/9780321836960.html) is Mario Triola's Elementary statistics Section 9.4 Two Dependent Samples (Matched Pairs). Not sure if your data is not paired. The assumptions don't require equal samples sizes from what I can tell. Best of luck!

What test should I be using?


Como publicar base de dados .mdf no Azure SQL Dabases?

Question: Tenho uma solution com um projeto Asp.Net MVC 5 e estou utilizando Code First e Migrations para atualizar a base de dados .mdf gerada automaticamente na pasta App_Data. Como faço para colocar esse .mdf no Azure e poder usar o mesmo em produção?
Answer: Você terá que gerar um script desse arquivo MDF e conectar na base Azure utilizando o SQL Server Management Studio. O procedimento é meio grande e não caberia na resposta (teria que ser uma resposta em separado só pra isso). Como curiosidade, neste link tem o passo a passo de como anexar seu MDF numa instância de SQL Server (http://learningsqlserver.wordpress.com/2011/02/13/how-can-i-open-mdf-and-ldf-files-in-sql-server-attach-tutorial-troublshooting/).
Lembrando que no Azure não é possível anexar um .mdf. A forma mais simples é ter o SQL SErver Management Studio e usar a opção de deploy da sua base para o SQL Azure.
Alternativamente, você pode configurar o método Seed do seu arquivo Migrations\Configuration.cs para que os dados sejam inseridos na publicação do seu sistema na nuvem Azure:
internal sealed class Configuration : DbMigrationsConfiguration<Models.ApplicationDbContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
        ContextKey = "SeuSistema.Models.ApplicationDbContext";
    }

    protected override void Seed(SeuSistema.Models.ApplicationDbContext context)
    {
        //  This method will be called after migrating to the latest version.

        //  You can use the DbSet<T>.AddOrUpdate() helper extension method
        //  to avoid creating duplicate seed data. E.g.
        //
        //    context.People.AddOrUpdate(
        //      p => p.FullName,
        //      new Person { FullName = "Andrew Peters" },
        //      new Person { FullName = "Brice Lambson" },
        //      new Person { FullName = "Rowan Miller" }
        //    );
        //
    }
}


Why and how to play chord progression?

Question: As a pianist who have only experienced classical music yet, I recently got my hands on The jazz theory book, because I am curious about this kind of music that is totally new to me.
The theory is focused on chords progressions and the scales that complements them, which led me to wonder a few things :

Why choose to describe music with chord progressions ?

I mean, it doesn't carry that much informations. The chords themselves can be played in a million possible ways (with inversions, arpeggios, rythm, voicing, dynamics, etc.).
I understand that different chords convey differents feelings or emotions, and can be descriptive of the particular progression of a piece but there is so much more to it !

How exactly do jazz pianists use chords progressions when playing a lead sheet ?

I would be surprised to ear that they just play chords with the left hand while playing the melody in the right hand, as indicated in the sheet music.
Is the lack of informations given in a lead sheet the reason why improvisation is such a big part of playing jazz ?
In other words, do jazz pianists have to improvise over every bar of their lead sheets, by deciding on the fly of the way they are going to play all the (important ?) stuff that is missing on the sheet ?
Comment: A lead sheet contains as much information as is needed - the chords, and the basic melody. without those, there won't be a tune and accompanying harmonies. So, reading a lead sheet, playing the chords provided, in the order written, will give pretty well all the info. needed. Players will play whatever voicings they feel will best fit at the time, and perhaps put occasional extra notes, mainly from those chords, under the right hand to notes played.
Answer: As you've suggested, there's a lot of improvisation in jazz. Almost everything is open to interpretation by the musicians--including melodies, rhythms, feels, and specific voicings for each chord. For most genres of jazz, the chords are usually the most fixed/immutable component of a song. (That said, chords are not completely unchanging. Chord substitutions and playing outside both introduce on-the-fly changes to the chord progression.)
In addition, there is a rich tradition in jazz of learning by listening to famous recordings. This old tradition extends far into modern-day jazz. As a simple example, imagine you're on a jazz gig with some musicians you've never played with before. The saxophonist calls the tune Autumn Leaves. One of the first things you would ask before the group starts playing is, "Do you want to play the Miles intro?" Everyone would know this intro because it's such an iconic version of the tune.
Turning our attention back to lead sheets: you're on a gig and there are ~300 or more songs you might be asked to play. Only some of these songs have famous intros, feels, rhythms, etc. that you're expected to know from famous jazz recordings. From a publisher's perspective, it wouldn't make a lot of sense to write out all of those introductions along with the chord changes. What if a song has two different famous interpretations? What if, over time, musicians change which interpretation they favor? There is likely just too much subjectivity in picking and choosing particular arrangements to include in a book with lead sheets/chord changes. And doing so would abandon the original song's core, which the sheet music is supposed to represent.
In many cases, pianists use lead sheets to jog their memory. They know the songs well and know how the recordings sound, and can use that as a guide. But they haven't memorized every single chord and thus use the lead sheet as a memory aid when they forget a chord or two. In other cases, a band might start playing a tune that the pianist has never heard of. The lead sheet provides the essential bones/structure from which the pianist can then improvise his/her part to match the dynamic of the group he/she is playing with.
So a jazz pianist likely isn't improvising everything when he/she looks at a leadsheet. Certain things are remembered from recordings, while other things are created spontaneously. This isn't as intimidating a feat as it sounds--one achieves this on-the-spot improvisation by drawing very heavily on the practice one has done. Taking rhythm as an example: one must improvise rhythms to use when playing chords behind the melody. But many of the candidate rhythms comes from a relatively small handful of rhythmic figures that sound good (e.g., dotted quarter followed by an eighth note). So practicing these known rhythmic figures makes the left hand automatic when supporting the right hand and its melody.
Answer:
Why choose to describe music whith chord progressions ?

Keep in mind that a lot of Jazz is based on the premise of putting your own spin on existing songs (standards). The melody and chord progression is typically the only part that stays the same between those interpretations. And even those vary.
So lead sheets convey only the parts you need to know—the kernel of the tune. Sometimes that does include more than just chords and melody like an intro or bass ostinato that is an important part of the tune. The rest is open to interpretation.

Is the lack of informations given in a lead sheet the reason why
  improvisation is such a big part of playing jazz ?

It works the other way around.

In other words, do jazz pianists have to improvise over every bar of their lead sheets, by deciding on the fly of the way they are going to play

Yes, mostly. The way you comp a piece may not be true improv each time. Maybe you know generally what voicings you might play because you've played the tune before. But the rhythm is much more up in the air especially given that you should be interacting with the rest of the band and playing off of their rhythms.
But it doesn't have to be that way when you're starting out. Feel free to write out exact arrangements if that helps you.
Answer:
Is the lack of information given in a lead sheet the reason why improvisation is such a big part of playing jazz ?

On the contrary, there is plenty of information present on the lead sheet, and the chord[progression]s tell the soloists what scales are most appropriate when they improvise. As you might expect, the 1, 3, 5 and 7 (chord tones) are the most attractive tones in a melody.
Answer: " Why choose to describe music whith chord progressions ?
I mean, it doesn't carry that much informations. The chords themselves can be play in a million possible ways (with inversions, arpeggios, rythm, voicing, dynamics, etc.). "
And that, in a nutshell, is what jazz is about.  Finding the million different things that can be played over a certain chord sequence.  'Jazz' can also include sticking to the composed melody and re-harmonising.  Or in going off at tangents only vaguely suggested by the original song.  But mostly it's based on the chord sequence.  Great jazz musicians invent alternative melodies.  Lesser ones just run up and down 'the scale of the chords'.

Why and how to play chord progression?


android not creating folder in LOLLIPOP and up

Question: i seem to be having problems creating folders in LOLLIPOP and up although code works just fine for previous versions
there is no error in the log-cast it simply does not crate the folder can someone help
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

private static Uri getOutputMediaFileUri(int type){
    return Uri.fromFile(getOutputMediaFile(type));
}

private static File getOutputMediaFile(int type){
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_MOVIES), "reelyChat/vids");

    if(!mediaStorageDir.exists()){
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            mediaStorageDir.mkdirs();
            try {
                mediaStorageDir.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            if(!mediaStorageDir.mkdirs()){
                Log.d("reelyChat", "failed to create directory");
                return null;
            }
        }

    }

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if(type == MEDIA_TYPE_VIDEO){
        vid_name = "RC_"+my_user_id+"_profile.mp4";
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + vid_name);
    }else{
        return null;
    }

    return mediaFile;
}

Comment: Your code works for me on Marshmallow, are you granting the run-time permission required for Marshmallow?
Comment: If the suggestion by @SteveM does not work, look here for some other suggestions: http://stackoverflow.com/questions/4062357/mkdirs-returns-false-for-directory-on-sd-card-while-the-parent-directory-is-writ
Comment: @Steve M yes permissions are set it just doesn't create the file in lollypop but i haven't tried on marshmallow yet all other previous versions work tho
Comment: Try this it may be work stackoverflow.com/a/41221852/5488468
Answer: It looks like you have took permissions but not at run time. Device running on Marshmallow or above needs to take permissions at run time.
For more information visit Developer Site
Answer: You need write permission at run time.
//Just call this function
getWirtePermissionAndCreateDir();
private void getWirtePermissionAndCreateDir() {
        if (Build.VERSION.SDK_INT < 23) {
            createDir();

        } else {
            final String[] PERMISSIONS_STORAGE = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
            //Asking request Permissions
            ActivityCompat.requestPermissions(mActivity, PERMISSIONS_STORAGE, 9);
        }
    }

     @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

        boolean writeAccepted = false;
        switch (requestCode) {
            case 9:
                writeAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
                break;
        }
        if (writeAccepted) {
           createDir();
        } else {
            Toast.makeText(mActivity, "You don't assign permission.", Toast.LENGTH_LONG).show();
        }
    }

    private void createDir(){
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_MOVIES), "reelyChat/vids");
        mediaStorageDir.mkdirs();
    }


Can this proposition be used to prove an iff statement?

Question: Let's say I want to prove that $p\Leftrightarrow q$. My proof looks like this: $p\Leftrightarrow a\Leftrightarrow
q$. I have used two "theorems", namely, $p\Leftrightarrow a$ and $a\Leftrightarrow q$. The problem is that the theorem $p\Leftrightarrow a$ has the requirement that $p$ or $q$ should be true. Can I still use this theorem in my proof? This is not a made up case, by the way. I am actually trying to find a proof for something.
Comment: To prove that $p\implies q$, suppose $p$ is true and then show that $q$ is true. So it seems like you can use it?
Answer: Yes you can. If $p$ or $q$ hold then your theorem states that $p\Leftrightarrow a$ and your other theorem states that $a\Leftrightarrow q$. On the other hand, if neither $p$ nor $q$ holds, then this creates no problem for $p\Leftrightarrow q$. Basically you have:
If we have $p$ then we have $a$ and thus also $q$. If we have $\lnot p$ then we either have $\lnot q$, or we have $\lnot a$ and thus $\lnot q$.
Answer:

The problem is that the theorem $p\Leftrightarrow a$ has the requirement that $p$ or $q$ should be true.

The theorem $$p\Rightarrow a$$ is indeed not useful when $p$ is false,    for then no conclusion can be derived;
however, the theorem $$p\Leftrightarrow a\\\equiv\;\; p\Rightarrow a \;\text{ and    }\; a\Rightarrow p \;\text{ and }\;  \lnot p\Rightarrow \lnot
a    \;\text{ and }\; \lnot a\Rightarrow \lnot p,$$ is effectively
saying    that $p$ and $a$ have the same truth value, and so is
useful    even when $p$—and consequently, $a$—is
false.

So, yes, proving $$p\Leftrightarrow q$$ is equivalent to proving
$$(p \Leftrightarrow a) \;\text{ and }\; (a \Leftrightarrow q).$$

A technical note: a strict logician would construe the statement
$$p\Leftrightarrow a\Leftrightarrow q$$ to mean $$p \Leftrightarrow
(a \Leftrightarrow q),$$ which is not
equivalent to $$(p
\Leftrightarrow a) \;\text{ and }\; (a \Leftrightarrow q),$$ which is
what you really mean.


where to place distortion pedal in signal chain for electric guitar and amplifier

Question: I play my electric guitar through an amp (actually a bass amp). I am looking for a distortion pedal and am curious as to where to place it: In front of amp or in FX loop, and additionally do all pedals work fine in front of the amp vs effects loop? I'm asking cause my amp does not have an effects loop. I've been watching reviews of different pedals and for example the boss mt-2 sounds weird from the front but when played from the effects loop, it sounds amazing.
Comment: The Boss MT-2 is a very extreme pedal. Some would argue that it's rather a poor kind of hi-gain amp simulator than a distortion pedal.
Answer: The chain is guitar > pre-amp > power amp> speaker.
Putting any effects here - Guitar> fx > pre-amp > power amp > speaker means that the pre-amp (tones mainly) will affect the fx.
Putting any effects here - Guitar > pre-amp > fx > power amp > speaker means that the fx are subjected to whatever settings the pre-amp has.
Modulation, delay, tremolo are usually in the second scheme, and most others are found to work better in the first scheme. Having said that, it's very much down to personal choice, along with what amp. and guitar and effects are being used.
Some pros will have their board wired so that some go on one route, others the other way, and most switchable for changing. Gets a bit complicated, with extra cables, etc., but gives the flexibility they need.
Whatever works better in your situation is what you will sort out - some distotion pedals work better one way than the other. Only you know what sound you prefer, so experiment. Be aware that the vol. pot. on the guitar as well as all other settings on the amp will have an effect on the overall sound.
Answer: Better is subjective, the pedals will sound different when sent through an FX loop than direct. The difference is where the distortion is created in the amplification chain.
There are two types of effects loop, parallel and serial which will have different sound and application also.
An effects input in an amplifier is usually after the pre-amp stage of the amp, so the effect is getting applied to a clean, boosted signal from the guitar, giving you more signal and tone to add distortion to.
The pedal directly from the guitar is distorting the guitar's low level signal and sending the distorted signal into the pre-amp of the amplifier which then makes the distorted signal louder.
You can get a similar sound to using an effects loop by having a clean boost or a signal buffer pedal in chain with your distortion pedal.
Comment: the type of effects loop really has nothing to do with the case of a drive pedal which is almost always put in front of the amp.
An FX loop is between the PreAmp and PowerAmp or it would have no purpose.

Based on his question serial/parallel loops are meaninglessly different and more complicated than worth getting into here, especially as parallel effects loops are becoming exceedingly rare as they are often misused and most pedals aren't well designed to flow with them ( from Dave Friedman's lecture at CME )
Comment: also your comment about simulating it with a buffer or clean boost pairing in front of amp doesn't makes sense.  buffer does nothing but prevent tonal loss.

clean boost before or after the distortion will be slightly different, but either case the limiting factor still becomes the preamp, I wouldn't compare that to a distortion in fx loop. Your presumption that the output of the preamp is clean is not necessarily true..  but as anything give it a try and see what you like. much like jazz we only learn rules to break them and everyone comes up with their own sounds.
Comment: I mentioned serial/parallel because it exists and can cause some confusion if you don't know there is a difference.   The question is about the sound difference and if the effects loop is better so I didn't get into specifics about the physics.  You can change the tonal output/distortion of a distortion pedal using various boosts, EQ and buffers, so depending of what specific sound you like about the FX loop it is possible to get an acceptably similar sound from a pedal chain.
Answer: While better is of course subjective and there are exceptions (yes you are right about the MT-2 Metal Zone that is the ONE 'dirt' pedal aside from a few 'few amp in a box pedals' that tend to work better if you plug input direct into them and then take output into the fx return bypassing the preamp completely. I wouldn't say its horrible in front, but it gets a bad wrap because most people dial the settings poorly although a few component changes can make it sound real good... check out Colin of CSGuitars videos on youtube about the pedal) 99% of the time you put tone effects in front of the amp and time based effects in the fx loop.  Look for "perfect pedal order" by Steve Vai on YouTube for example. He is a certifiable expert.
The main exception to this is if you are getting your entire distortion tone from pedals in which case you can start putting things like the chorus pedal after that but in front of the amp... ultimately you can do anything, but if you are using a distortion circuit / tube clipping in the preamp it will be applied to your modulated signal which is usually not what you want, and in the case of delays and reverbs likely not what you want ( since the softer repeats and reverberations will likely barely be distorted and sound almost clean compared to fx loop were you are repeating and reverberating a possibly heavily distorted sound)
In a nutshell : mono/poly/harmonic octave generation, wah, compression, fuzz, distortion, od, boost, eq, and for many a phaser will go in front often in that order.
Chorus, possibly phaser, flanger, tremolo/vibrato, delay, reverb will come in the fx loop.  The order is not as clear here other than that delay and reverb are generally last and in that order. but there's a lot of choices here and variants as people perform modulation on the delays.
Insert volume pedals where ever you like many different feelings on that. Some people have multiple so they can alter delay volumes as well as input volume first (instead of from knob on the guitar)
I'm honestly not sure where one puts a ring modulator and some of the wacky synth stuff but I presume that falls under the same category as octave generators where you send a in a clean signal to it.   but I don't have that problem to deal with lol
thats a pretty good guide for a beginner, once you start to embark into stereo, multiple amps and such things start to get more complicated but again the rules still generally apply and almost become stricter as you might have a split going on in the FX section where the main amp is dry and the 'wet' mix goes entirely into the FX return of another amp so that the combination of signals doesn't overwhelm your power amp or generate unintended power amp distortion, stereo chorus often puts original signal to one and effected signal to the other, delays can be ping ponged back and forth for stereo movement or kept entirely on a seperate wet amp, similar for the other effects.
more of note is that using bass amp your upper frequency sound may or may not sound good. the quintessential guitar amp design by fender was actually a bass amp originally so its not unheard of, but if you aren't downtuning a lot you may find guitar amp suits you better than a bass amp. this of course has no impact on your question though


Properties of the generalized centering matrix

Question: Let $L_{\boldsymbol{\Delta}}\in \mathbb{R}^{M \times M}$ be the generalized centering matrix given by:
$L_{\boldsymbol{\Delta}} = \boldsymbol{\Delta} - \frac{1}{\text{tr}(\boldsymbol{\Delta})} \boldsymbol{\Delta} \boldsymbol{1}_M  \boldsymbol{1}_M^\top \boldsymbol{\Delta} \in \mathbb{R}^{M \times M}$, where $\boldsymbol{\Delta} \in \mathbb{R}^{M \times M}$ is a diagonal matrix of strictly positive entries and $\boldsymbol{1}_M = [1, \ldots, 1]^\top \in \mathbb{R}^{M}$.
For $\boldsymbol{\Delta}  = I_{M}$, we recover the classical centering matrix (https://en.wikipedia.org/wiki/Centering_matrix).
I have four questions:

Can we show that $L_{\boldsymbol{\Delta}}$ is semi-positive definite?

Can we get its eigenvectors and eigenvalues?

Any hope to get a simple expression for the precision matrix $(\boldsymbol{X} \boldsymbol{L}_{\boldsymbol{\Delta}}\boldsymbol{X}^\top)^{-1}$?

How do we show that $L_{\boldsymbol{\Delta}}$ removes a weighted average of the samples?

Thank you for your help,
Comment: You asked too many questions in one post.  One for each post is more appropriate.
Comment: Sorry, this is my first time posting on this forum
Answer: Just some hint to the first question, the remaining three are tractable, but require more context and answers to them are probably more verbose.  As I commented, it is better to ask them in separate posts and show some of your own work.
You can write $\Delta$ more explicitly as $\Delta = \operatorname{diag}(a_1, \ldots, a_M)$ with $a_i > 0, i = 1, \ldots, M$. Denote the trace of $\Delta$ by $T = a_1 + \cdots + a_M$.  It can be shown that $L_\Delta = \Delta^{1/2}C\Delta^{1/2}$, where
\begin{align}
C = I_{(M)} - \frac{1}{T}vv', \quad v = \begin{bmatrix}
\sqrt{a_1} \\
\sqrt{a_2} \\
\vdots \\
\sqrt{a_M}
\end{bmatrix}.
\end{align}
Therefore to show $L_\Delta$ is PSD, it suffices to show $C$ is PSD.  But $C$ is symmetric and idempotent, hence PSD.  This completes the proof.
Comment: Thank you so much for your answer. This really helps me to answer to questions 1,2, and 4. I will follow-up in a new post with more context for question 3.
Comment: So $C$ is an orthogonal projector. From your decomposition, $L_\Delta$ applies a diagonal scaling, removes the weighted mean, and scales again. I will follow-up in a new post with more context for question 3.
Comment: @Mathieu Yes.  By the way, if you found any answer is useful, you can accept it by clicking the check mark.


Subgroups of the elementary abelian group $E_8$ (or is it?)

Question: I am computing the Galois group of $(x^2-2)(x^2-3)(x^2 -5)$. I found it to be:
$$G = \{1,a,b,c,ab,ac,bc,abc\}$$
where:
$$a: \begin{cases} \sqrt2 \to \sqrt 2 \\ \sqrt 3 \to \sqrt 3 \\ \sqrt 5 \to -\sqrt 5 \end{cases}, b:\begin{cases} \sqrt2 \to \sqrt 2 \\ \sqrt 3 \to -\sqrt 3 \\ \sqrt 5 \to \sqrt 5 \end{cases}, c:\begin{cases} \sqrt2 \to -\sqrt 2 \\ \sqrt 3 \to \sqrt 3 \\ \sqrt 5 \to \sqrt 5 \end{cases}$$
notice that $a^2=b^2=c^2 = 1$, that the group is commutative and that it is generated by $a,b,c$.
Hence, I found the subgroups to be:
$$1,\langle a\rangle, \langle b\rangle, \langle c \rangle, \langle ab\rangle, \langle ac\rangle, \langle bc\rangle, \langle abc \rangle, \langle a,b \rangle, \langle a,c \rangle, \langle b,c\rangle, \langle a,bc\rangle, \langle ab,c\rangle, \langle ac,b \rangle, G $$
these are $15$. Looking at this page, I see that this group must be the elementary abelian group $E_8$. However, I am missing a subgroup (they should be $16$). Can someone point out to me what subgroup I am missing? I have been staring at this for too long, and I can't see what I am missing. Maybe I went wrong somewhere?
Answer: As each finite Abelian group is self-dual, it has as many groups of order $d$ as of order $|G|/d$. You have seven subgroups of order two, but only six of order four. There is a subgroup of order four missing. It is
$\left<ab,ac\right>$.
Comment: I see why this is the missing subgroup, but I don't understand the first bit. Could you please explain?
Comment: The dual group of $G$, viz., $\widehat G=\text{Hom}(G,\Bbb Q/\Bbb Z)$ is isomorphic to $G$. Subgroups of $G$ naturally correspond to quotient groups of $\widehat G$.
Comment: Thanks a lot. Really nice information.


Correctly integrating the product of two functions

Question: I'm trying to determine the probability of finding a function inside another function.
While plotting the results do give me a sign of there being an actual non-zero number, integrating to determine the number gives me zero.
Plotting these two functions gives me
$$ f(x) = \left( \frac{1}{\pi } \right)^{1/4} e^{- \frac{1}{2 } x^2} $$
$$ f_{1}(x) = \left( \frac{1}{ 2\pi} \right)^{1/4}   x\ e^{- \frac{1}{4} x^2} $$

Which obviously shows that a portion of is found inside the other function.
When integrating over all space defined as
$$ \int_{-\infty}^{\infty} f_{1}^{*}(x)\ f(x) dx $$
(in which after integrating, I take the square modulus to determine the probability)
$$ \int_{-\infty}^{\infty} \left( \frac{1}{2\pi} \right)^{1/4} \left( \frac{1}{\pi }\ \right)^{1/4} e^{ -\frac{3}{4 } x^2}  x\ dx = 0 $$
Is this due to me integrating over all space? In which judging by my plot, I should change my bounds to 0 to $\infty$ instead?
Comment: can you say something to the variables?
Comment: Yes, I just added that. $\hbar$=$\omega$=$m$=1
Comment: is your integral of the form $$\int x\exp(x)^2dx$$
Comment: You haven't plotted $f_1$ for values less than 0 but it goes negative there! That's why he product integrates to zero. Change your limits to $\int_0^\{infty}$
Answer: Your orange plot is incomplete.  If you were to plot your $f_1$ over the interval $[-15,15]$ you would see that it has a negative lobe that is symmetric with the positive lobe you plotted.  There are two ways to fix this:
1)  Do what you suggest and only integrate over the intersection of the supports of the two functions.  In this case, $[0,\infty)$.
2) Actually write down your $f_1$'s complete definition:  $$f_1(x) = \begin{cases} \frac{ \left( \frac{m \omega}{\pi \hbar} \right)^{1/4} }{\sqrt{\hbar m \omega}}\  x\ e^{ \frac{m \omega}{4 \hbar} x^2} ,& x \geq 0 \\ 0 ,& x < 0  \end{cases} \text{,}  $$ so that the product of your functions is actually zero to the left of zero.


Mac USB to XLR output?

Question: I have two powered Yamaha HS8 monitors, that I'd like to use to use for playback via USB from my Mac. A couple of inputs would be nice as well. I've seen products like this before, but they only have TRS outputs and no XLR outs. I have heard that XLR is superior to TRS due to grounding, and thus wanted to use the XLR inputs on my HS8s, but I can't seem to find any kind of USB Audio Interface that supports XLR out. Does this exist, or am I being stupid and should just use TRS? I need to run up to about 20ft of cable for each monitor. Thanks!
Comment: Whatever you heard about grounding isn't quite right. **Balanced** TRS outputs (like the ones on the linked product) are basically identical to XLR outputs except they take up less panel space. A couple TRS male to XLR male adapter cables would help you get connected just fine if you bought the linked item.
Answer: The TRS jack connectors on your speakers are electrically identical to the XLR ones.  What matters is that the connection is balanced, not what type of connector is used.   And, to be honest, on a Line level connection to a powered speaker, it doesn't matter THAT much whether it's balanced.  If you haven't got a TRS jack to TRS jack cable handy (that's what is loosely referred to as a "stereo jack cable", though "stereo" is a misnomer in this case) use a normal TS jack cable.  It will work just fine.  If, and only, if there's a hum problem, lay out on a TRS jack cable.
Comment: So he's ok with the M-Track interface mentioned.
In a domestic situation the main feature of a locking connector is that a cable yank will GUARANTEE the speaker is pulled off its shelf and damaged!
Comment: Yes, you want a USB audio interface with balanced line outputs. If you want the locking feature of XLR, you can use cables with balanced TRS on the audio interface side and XLR on the speaker side.
Comment: As the equipment won't be powered up while you're connecting cables, who cares?
Comment: Thanks! I do have TRS cables handy so that's not an issue. What would you think about doing TRS->XLR cables and always plugging in the XLR side last? I like the idea of XLR being grounded the second it's connected rather than after the cable is fully inserted.

Mac USB to XLR output?


How do I find the difference between the gradients of two lines represented by an equation

Question: I want to find the difference between the gradients (or slopes?) of two lines. The equation of the lines is $$x^2(\tan^2 \theta+\cos^2 \theta)-2xy\tan\theta+y^2 \sin^2 \theta=0$$
I have assumed the gradients are $m_1$ and $m_2$.
I compared the given equation with the general equation $ax^2+2hxy+by^2$ and found out the respective values and plugged it in the formula I know ie
$m_1-m_2=2\sqrt{h^2-ab}=2\sqrt{\tan^2 \theta-(\tan^2 \theta+\cos^2 \theta) \sin^2 \theta}$
Proceeding from here is difficult and the answer given in my book is $2$. How do I do this or is that an error? Thanks in advance!
Comment: What is $m_{1,2}$, among others? Could you give more information about the question?
Comment: Please edit your question and give more information about the question. The notations h,a,b are not clear and it is not very clear what you are asked...
Comment: Notice that you have no $x^2$ in your equation, thus you can't use the mentioned formula. It is still not clear what is your question. Would you mind to upload it?
Comment: Even though i am here for a while this editing always makes things difficult for me if you see any editing mistakes you are welcomed to edit it
Comment: @ArchisWelankar, the argument under the square root can be reduced to $\sin^4(\theta)$. Could you please upload the full question, so we could give you more assistant?
Answer: I'm going to guess wildly at what you actually mean because you do not write what you actually mean.  I expect that if you clarify your question and it turns out I have guessed wrongly, others will vote this answer into oblivion, as they should...
Say we have two lines in point-slope form: $$\begin{align}
    y &= m_1 x + b_1 , \\
    y &= m_2 x + b_2 .
\end{align}$$  We may combine these equations as $$
    (y-m_1 x - b_1)(y - m_2 x - b_2) = 0
$$ and expand, giving $$
    y^2 -(m_1 + m_2) x y + m_1 m_2 x^2 -(b_1 + b_2) y + (b_1 m_2 + b_2 m_1) x + b_1 b_2 = 0 .
$$  Writing your equation in the same form, we have $$
    y^2 -2\frac{\tan \theta}{\sin^2 \theta} x y + \frac{\tan^2 \theta + \cos^2 \theta}{\sin^2 \theta} x^2 - 0 y + 0 x + 0 = 0 .
$$  Comparing coefficients of the constant and linear terms, we discover $b_1 + b_2 = 0$ and $b_1 b_2 = 0$, so $b_1 = b_2 = 0$.  Comparing coefficients of the degree $2$ terms, we find the system $$\begin{align}
    m_1 + m_2 = 2\frac{\tan \theta}{\sin^2 \theta} \text{ , and} \\
    m_1 m_2 = \frac{\tan^2 \theta + \cos^2 \theta}{\sin^2 \theta} .\\
\end{align}$$
It's a useful fact that $(m_1 + m_2)^2 - 4 m_1 m_2 = (m_1 - m_2)^2$, which one can easily verify by expanding.  Therefore, $$\begin{align}
    (m_1 - m_2)^2 &= 4 \frac{\tan^2 \theta}{\sin^4 \theta} - 4 \frac{(\tan^2 \theta + \cos^2 \theta)}{\sin^2 \theta} \\
        &= 4 \frac{\tan^2 \theta - \sin^2 \theta (\tan^2 \theta + \cos^2 \theta)}{\sin^4 \theta} \\
        &= 4 \frac{\tan^2 \theta (1 - \sin^2 \theta) - \sin^2 \theta \cos^2 \theta}{\sin^4 \theta} \\
        &= 4 \frac{(\tan^2 \theta - \sin^2 \theta) \cos^2 \theta}{\sin^4 \theta} \\
        &= 4 \frac{(\sec^2 \theta - 1) \cos^2 \theta}{\sin^2 \theta} \\
        &= 4 \frac{\tan^2 \theta \cos^2 \theta}{\sin^2 \theta} \\
        &= 4 ,
\end{align}$$ as desired.
Comment: Nice this is what i mean actually what i mean !! Impressive 1st statement;)


GET Pedido WooCommerce com critério por Data - restSharp

Question: Pessoal estou fazendo uma integração com C# e a API WooCommerce, minha situação é a seguinte, eu consigo me comunicar com a API através do restSharp e consigo buscar os pedidos do site através da URL de acesso, até aqui tudo bem! Mas, quando tento inserir um critério para a busca (como a data de criação por exemplo), a API me retorna erro 401 - Sem autorização.
Alguém teria uma idéia de como resolver isso? Ou como posso fazer para inserir esse parâmetro na minha requisição?
Adicionando mais informações:
A documentação para Woocommerce que sigo é esta no link: http://woocommerce.github.io/woocommerce-rest-api-docs/?php#authentication-over-http
E abaixo segue código q estou usando e esta me retornando erro 401:
<code>  var client = new RestClient(URLAPI/orders?after=2018-05-13T16:28:02");
            var request = new RestRequest(Method.GET);
            request.BuildOAuth1QueryString(client, "CK", "CS");
            var response = client.Execute(request);
</code>
Comment: Nesse caso, teria que ver a documentação da API. se tiver acesso à documentação e quiser postar o trecho que fala de como se autenticar, podemos te ajudar a passar os parâmetros. Não é necessário informar seu dados de autenticação aqui.
Comment: Marcelo a documentação que sigo esta neste link: [link](http://woocommerce.github.io/woocommerce-rest-api-docs/?php#authentication-over-http) "var client = new RestClient("http://meusite.com/wp-json/wc/v2/orders?after=2018-05-13T16:28:02");
            var request = new RestRequest(Method.GET);
            request.BuildOAuth1QueryString(client, "ck_527f2f46c5e8ef9adeaad1", "cs_ea20c7b891e31e042fbe2");
            var response = client.Execute(request);"
Answer: Pessoal descobri como resolver minha situação, no meu código faltou inserir a linha AddQueryParameter, ficando conforme abaixo:
<code>var client = new RestClient("URL/orders");
            var request = new RestRequest(Method.GET);
            request.AddQueryParameter("after", "2018-05-13T16:28:02");
            request.BuildOAuth1QueryString(client, "CK", "CS");
            var response = client.Execute(request);
</code>


Find the distance between two sets in $\mathbb E^2$

Question: Find the distance between two sets in $\mathbb E^2$:
P = {$(x,y): x+2y = 4$} and K = {$(x,y): $$(x+1)^2 + (y+1)^2 = 1$}
Need some help with this one
Comment: Hint: $P$ is a line and $K$ a circle.
Answer: The hint:
It's just
$$\frac{|1\cdot(-1)+2\cdot(-1)-4|}{\sqrt{1^2+2^2}}-1.$$
Answer: We can make a change of variables and replace $x$ by $x-1$ and $y$ by $y-1$.
$$x \mapsto x-1$$
$$y \mapsto y-1$$
Now the problem becomes find the distance between the line $x+2y=7$ and the circle $x^2 + y^2 =1.$
The line through the origin and perpendicular to this line intersects the line and the circle at the closest points.  This line is $2x-y=0.$
Solving the system of two lines yields $x=7/5$ and $y=14/5.$
This point is a distance $7/\sqrt{5}$ from the origin and $7/\sqrt{5}-1$ from the circle.
Distance between the sets  is $7/\sqrt{5}-1$.


"Non-ASCII character" error when running Python program from Windows Powershell

Question: I am seeing the following error:
SyntaxError: Non-ASCII character '\x93' in file new2.py on line 2, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

when I run this code:
print("Welcome")
 guess = 0
 while guess != 5:
    g = input("Guess the number: ")
    guess = int(g)
    if guess == 5:
    print("You win!")
    else:
    if guess > 5:
    print("Too high")
    else:
    print("Too low")
print("Game over!")

Comment: Your endquote is correct `"` but your opening quotes are both *smart quotes*, which aren't ascii. Did you copy & paste this from some other application, that may be the cause of your errors.
Comment: What symbol is `"` supposed to be? On the line `g = input("Guess the number: ")` - same in your `print` statements
Answer: You need to use proper double quotes, not the " sign, which is not ASCII.
Comment: It's mentioned in comments but these are also known as "smart quotes". Almost always the problem created by code biting from a website.


HTML Footer that sticks to the bottom of browser unless scrollbars needed

Question: On some webpages, the content is minimal and the footer scrunches up near my header.  In these cases, I want the footer stuck to the bottom.  I can do this easily in CSS with:
#footer {
    position: fixed;
    width: 100%;
    bottom: 0;
}

Works great.  However, sometimes the content is not minimal and you might even need to scroll to see the content at the bottom.  In those cases, I want the footer to simply come right after the content, like normal.
Is there a way to do this in just CSS?  Do I need a Javascript hack?  If I need a hack, is there a good library?  I'm using jQuery already, so happy to use something depending on it.  I don't care about IE8 or below.  I only care about IE9+ and recent versions of Chrome, Firefox, Safari and Opera.
Answer: I used the technique described here:
http://www.cssstickyfooter.com/
Seems to work perfect aside from the requirement that you know the height of your footer ahead of time...
Answer: Since the functionality you require is about having whitespace in the content container when the content is less than enough to fill the browser's height, you could use a min-height for the content div to make it force the footer to about the bottom of the page.
ie. If your html was:

 header

 some content here

 footer

then by applying a css like:
.content { min-height:300px; }

the content will always occupy at least that height and not stick up close to the header.
You could also try margin-bottom for the content if that is more likeable
Comment: Problem is I don't know how high to make the content... unless I use javascript.  Which of course I can do, but looking for a CSS solution or javascript/jquery library.


var_export to string

Question: I have an HTML code in the $output string, this code is delimited by two terms: -startMiniPreview- and -endMiniPreview-.
I need to take the part wrapped between those two terms and save it in another string variable.
This is what I thought was the right way to do so, but it's not working at all. The HTMLCODE part is getting exported succesfully but not as a string
    $output="-startMiniPreview-HTMLCODE-endMiniPreview-EXTRA";
    preg_match( '/\-startMiniPreview-(.*?)\-endMiniPreview/', $output,$vt);
    $cutOut=strval(var_export($vt[1]));

Answer: The documentation clearly gives the answer:
$cutOut = var_export($vt[1],true);

EDIT: That said, why are you doing this? Just $cutOut = $vt[1]; is all you need.
Comment: This actually worked.
Silly me, I'm still a newb at PHP, but I just found out it's not complicated as I guessed.. thanks for the help!
Answer: The second parameter to var_export makes it return the string instead of outputting it directly http://php.net/manual/en/function.var-export.php
mixed var_export ( mixed $expression [, bool $return = false ] )

So your code should look like
$cutOut=strval(var_export($vt[1]), TRUE);

var_export to string


Prove ${n \choose k}={n-1 \choose k-1}+{n-1 \choose k}$ using any particular number

Question: I didn't really understand this proof to begin with, and I really don't understand what this question is asking. Do I just let n equal some number and then try to prove it from there, how do I begin the proof?
Comment: Related: http://math.stackexchange.com/questions/20475/proving-pascals-rule-n-choose-r-n-1-choose-r-1n-1-choose
Comment: Have you ever work with the Pascal's triangle?
Comment: Didn't understand what proof? Did you try some method and get stuck? Or perhaps are confused about the notation?
Comment: What's your definition of $\binom{n}{k}$?
Comment: The question asks you to verify that the formula holds for particular values of $n$ and $k$.  For instance, if $n = 5$ and $k = 3$, you must show that $\binom{5}{3} = \binom{4}{2} + \binom{4}{3}$.  You can prove *Pascal's formula* $$\binom{n}{k} = \binom{n - 1}{k - 1} + \binom{n - 1}{k}$$ using an algebraic argument or a combinatorial argument such as that given below by Laars Helenius.
Answer: Clearly the left hand side counts the number of possible sets of size $k$ chosen from $n$ objects.
Now select one of the $n$ objects as special. Each set of size $k$ either contains our special object or it doesn't. There are $\binom{n-1}{k-1}$ sets of size $k$ that include our special object and $\binom{n-1}{k}$ that do not.


Javascript, advancing date range

Question: I using a date-picker to set a start and end date. The date range is supposed to move forward each day for a two week, sliding window of entry.
My code below works but I would like to refactor it into something that is easier to maintain. What techniques or tools can I use to improve this code?
var challenge_start = new Date(2015,04,04);
var challenge_end = new Date(2015,06,12);
var range_start;
var range_end;
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(today<challenge_start) {
    range_start=challenge_start;
    range_end=challenge_start;
}
else if(today>challenge_start && today<challenge_end) {
    if(mm==5) {
        if(dd<18) {
            range_start=challenge_start;
            range_end=today;
        }
        else {
            range_start=today.setDate(today.getDate()-14);
            range_end=today;
        }
    }
    else if(mm=6) {
        if(dd<29) {
            range_start=today.setDate(today.getDate()-14);
            range_end=today;
        }
        else {
            range_start=today.setDate(today.getDate()-14);
            range_end=challenge_end;
        }
    }
    else if(mm==7) {
        if(dd<12) {
            range_start=today.setDate(today.getDate()-14);
            range_end=challenge_end;
        }
        else {
            range_start=challenge_end;
            range_end=challenge_end;
        }
    }
}
else if(today>challenge_end) {
    range_start=challenge_end;
    range_end=challenge_end;
}

$("#date").datepicker({
    startDate: range_start,
    endDate: range_end,
    keyboardNavigation: false,
    autoclose: true,
    todayHighlight: true,
    language: langdate,
    disableTouchKeyboard: true
});

Comment: How is it failing for you? What is it doing / failing to do?
Comment: Thanks for looking. I guess I'm just looking for a more efficient way to do this. Something easier to change/manage when the start and end dates are different.
Comment: You may want to look at moment.js; it simplifies dealing with dates and includes (for instance) a method for 'add one week'.
Comment: More precise in what the author is asking for
Answer: I may not understand what you are striving for, but I'm pretty sure you don't need to have those if(mm==x) and if(dd=x) statements.
Perhaps this is what you want:
var challenge_start = new Date(2015,04,04);
var challenge_end = new Date(2015,06,12);

function getDatePlusDays(origDate, days) {
    var date = new Date(origDate.getTime());
    date.setDate(date.getDate() + days);
    return date;
}

function getClosestDateInRange(startDate, endDate, date) {
    return (date < startDate) ?  startDate
            : ((date > endDate) ? endDate: date);
}

var today = new Date();
var range_start = getClosestDateInRange(challenge_start, challenge_end, getDatePlusDays(today, -14));
var range_end = getClosestDateInRange(challenge_start, challenge_end, today);

jsfiddle
Comment: Thanks. This helps! The sliding entry window is backward in time so a quick switch should do it.
Comment: A user can't make an entry beyond today ... but can make an entry for up to 14 days prior.
Comment: @user1964234 - Is it correct now? Or should that be `-13`, so it is a two week period?

Javascript, advancing date range


Let $A\subseteq B$ be a finite ring extension. Let $M$ be a Noetherian/Artinian $B$-module. Show $M$ is a Noetherian/Artinian $A$-module.

Question: Let $A\subseteq B$ be a finite ring extension. Let $M$ be a Noetherian/Artinian $B$-module. Show $M$ is a Noetherian/Artinian $A$-module.
Let $M_0\subset M_1\subset...$ be an ascending chain of $A$-submodules. We have $BM_0\subset BM_1\subset...$, which  has to stablize and $BM_i=BM_{i+1}$. But how to conclude $M_i=M_{i+1}$ from here? I think we could use Nakayama, but it is not obvious how to use it. This question is mentioned in this answer but no reference is given. Could anyone give a hint how to use the finiteness condition? Thanks.
Comment: For the Noetherian part, I think we can reduce to the following situation: provided that $B$ is a Noetherian ring and finite over some subring $A$, then show that $A$ is Noetherian. Is this known?
Comment: @imtrying46 Let $M$ be Noetherian generated (over $B$) by $n$ elements. Let $N \subset M$ be the submodule generated by the $(n-1)$ first generators. Then $M/N$ is the quotient of $B$ by some ideal $I$, and it is Noetherian as a $B$-module. Therefore, $B/I$ is a Noetherian ring. I claim that $B/I$ is a Noetherian $A$-module (which is necessary for $M$ to be a Noetherian $A$-module) iff $A/A\cap I$ is a Noetherian ring (and $A/A\cap I$ is a subring of $B/I$ over which $B/I$ is finite).
Comment: @Aphelli Could you elaborate on how this reduction may work?
Comment: @Aphelli Yeah I think your reduction works and we are done since what you are asking for is precisely
[Eakin–Nagata theorem](https://en.wikipedia.org/wiki/Eakin%E2%80%93Nagata_theorem).
Answer: For the Noetherian part, let me assemble the argument from the comments by @Aphelli and @metaverse to make it available directly in this answer. All the credit goes to them!
Let $M$ be a Noetherian $B$-module; let us prove by induction on the number of generators $n$ of $M$ that $M$ is also $A$-Noetherian. The claim is trivial if $M=0$, i.e. if $n=0$. Let us assume the statement for modules generated by $n-1$ elements, where $n\geq 1$, and let $m_1,\ldots,m_n$ be generators of $M$. Let $N=\sum_{1\leq i<n}Bm_i$, then we have a SES
$$
0\rightarrow N\rightarrow M\rightarrow M/N\rightarrow 0.
$$
By induction hypothesis, $N$ is $A$-noetherian, so we are left to prove that $M/N$ is $A$-Noetherian. As $M/N$ is generated by $m_n+N$, it is isomorphic to $B/I$ for some ideal $I\subseteq B$. As $M$ is $B$-Noetherian, $B/I$ is as well, and hence $B/I$ is a Noetherian ring. Notice that we have an injection of rings $A/I^c\hookrightarrow B/I$ where $I^c=I\cap A$, which is still a finite extension. By the Eakin-Nagata theorem, $A/I^c$ is hence Noetherian. As $B/I$ is finitely generated over $A/I^c$, it is a Noetherian $A/I^c$-module. Hence it is also $A$-Noetherian, so by induction we are done.
Let us now prove the case where $M$ is finitely generated Artinian. By this post, this is equivalent to assuming that $M$ has finite length.
So let $M$ be a finite length $B$-module. Via a filtration of $M$ we can reduce to prove that $B/\mathfrak{m}$ is a finite length $A$-module, where $\mathfrak{m}\subseteq B$ is maximal. Let $\mathfrak{m}^c=\mathfrak{m}\cap A$, then $A/\mathfrak{m}^c\subseteq B/\mathfrak{m}$ is still finite, and $A/\mathfrak{m}^c$ is a domain, while $B/\mathfrak{m}$ is a field. It is elementary to show that then $A/\mathfrak{m}^c$ must be a field to. In particular, $B/\mathfrak{m}$ is a finite dimensional $A/\mathfrak{m}^c$-vector space, and in particular an $A$-module of finite length. Hence $M$ is of finite length over $A$ as well.
I'm not sure though that any Artinian $B$-module is also an Artinian $A$-module, and neither is this claimed in the post you're referencing. But I don't have a counterexample right now; I'll think a bit.
Comment: Could you elaborate on the Noetherian part? If $M$ is a Noetherian $B$-module, then every $B$-submodule is of finite type, then every $B$-submodule is of finite type over $A$… but this doesn't tell us that every $A$-submodule of $M$ is of finite type over $A$, is it?
Comment: @Aphelli You're right... I edited the post, at least the finite length part survives.


Setting default website redirect to virtual directory IIS 7

Question: I have a virtual directory in IIS 7 called Foo. There is a URL that you can access at mydomain.com/Foo/test.aspx
I would like mydomain.com to automatically go to mydomain.com/Foo/test.aspx but can't seem to find a way to do this in IIS 7 with redirects (it keeps redirecting to mydomain.com/Foo/test.aspx/Foo/test.aspx/Foo/test.aspx/.... if I redirect to Foo/test.aspx or the full address mydomain.com/Foo/test.aspx.
How is this done?
Answer: One option is to have a default document with the following content in the site root:
<%@ page language="c#" %>


How to check the user already offer the ride using LINQ in NET Core

Question: public class User
    {
        [Key]
        public Guid Id { get; set; }

        [Required(ErrorMessage = "Name is required")]
        [MaxLength(250, ErrorMessage = "Username exceed 15 characters")]
        public string UserName { get; set; }

        public ICollection<Vehicle> Vehicles { get; set; }

    }

 public class Vehicle
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        [Required]
        public string Name { get; set; }

        public string Brand { get; set; }

        public string PlateNumber { get; set; }

        public Guid UserId { get; set; }
        public User User { get; set; }

        public ICollection<Ride> Rides { get; set; }
    }

public class Ride
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public DateTime RideStartTime { get; set; }

        //Relationship
        //Ride has One Vehicle
        public int VehicleId { get; set; }
        public Vehicle Vehicle { get; set; }

        //Ride has One Vehicle
        public int EventId { get; set; }
        public Event Event { get; set; }

    }

// In RideRepository
    public bool IsRideExists(int vehicleId, int eventId )
        {

            return RepositoryContext.Rides.Any(ride => ride.VehicleId.Equals(vehicleId) && ride.EventId.Equals(eventId));
        }

One to many
My relationship in ef core
One user can have many vehicle
One vehicle has one user

One to many
One ride can have one vehicle
one vehicle can have many ride

Many to many
one ride can have one event
one event can have one ride

User can offer ride only with one vehicle per event. 1 event for 1 user.
  My problem is when i offer ride with another vehicle, the offer ride is successfully entered the database, i want to use LINQ command to check if one vehicle already offer the ride, the user cant offer ride using other car for the same event
Answer: change your is exist to something in this line, add userId in your paramter
RepositoryContext.Rides.Any(ride => (ride.VehicleId.Equals(vehicleId) && ride.EventId.Equals(eventId)) || (ride.Vehicle.UserId == userId && ride.EventId.Equals(eventId)));

this || (ride.Vehicle.UserId == userId && ride.EventId.Equals(eventId)) will check if the user and this event already exists


Alias link doesn't open correct page

Question: Here is the full config of my site:
server {
    server_name back.mysite.com;
    access_log /home/django/mysite/deploy/logs/nginx_acc.log;
    error_log /home/django/mysite/deploy/logs/nginx_err.log;

    location / {
        uwsgi_pass      unix:///tmp/uwsgi.sock;
        include         uwsgi_params;
    }

    location /media/sites/ {
      alias /web/mysite.com/sites/;
    }

    location ~ ^/(static|media)/ {
      root /home/django/mysite/mysite;
      expires max;
      log_not_found off;
    }

    location /sites/ {
      root /web/mysite.com;
    }
}

Now I can open the first link, but can't open the second of the following
http://back.mysite.com/sites/files/somefile.jpg
http://back.mysite.com/media/sites/files/somefile.jpg #<---can't open but need to

Please, tell me what am I doing wrong.
Answer: I would think is a problem with your path.
When you request /media/sites/files/somefile.jpg that alias convert the URL to /web/mysite.com/sites/files/somefile.jpg, so check if the path do exists.
Also check: http://nginx.org/en/docs/http/ngx_http_core_module.html#alias
Comment: Nginx Wiki [has better explanation](http://wiki.nginx.org/NginxHttpCoreModule#alias) but is still a bit confusing. `A request [url] for "/i/top.gif" will instruct Nginx to serve the file [system path] "/spool/w3/images/top.gif".` That means that **alias** directive accepts system file path as an argument, not the url part.

Alias link doesn't open correct page


Execute PHP on every page?

Question: I'm writing a custom module for Drupal 6, and I need to execute some PHP no matter which page the user accesses.
I see that hook_init is not run on cached pages, so that's a no-go. hook_boot doesn't work, either, because it happens too early in the page request to let me use some functions I need.
So I was thinking about specifying a block using hook_block, but that hook just makes me pull my hair out:
function test_block($op='list', $delta=0, $edit=array()) {
  switch ($op) {
  case 'list':
    $block['test_block']['info'] = t('Test block');
    return $block;
  case 'view':
    $block = array();
    switch ($delta) {
    case 'test_block':
      $block_content = '<?php echo \'WTF?\'; ?>';
      $block['subject'] = NULL;
      $block['content'] = $block_content;
      break;
    }
    return $block;
  }
}

This renders as:

Obviously, that's not what I'm looking for, either. Why does the PHP in that block not render correctly? Even if I end up using something other than a block to solve my problem, this issue really sparks my curiosity.
If I can't use hook_block, how do I get some PHP to run on every page? I need access to the $user global and to the drupal_goto() function.
Answer: What is wrong with your code is that the code that outputs the content of the block needs to be executed, such as:
$block['content'] = test_get_block_content();

This code makes sense if the code needs to output something in the page; if the code that needs to be executed doesn't need to output anything in the page, then hook_exit() is always executed, even on cached pages. The documentation for that hook reports the following text:

Only use this hook if your code must run even for cached page views. If you have code which must run once on all non cached pages, use hook_init() instead.

As for redirecting a user, you can look at what the Redirect module does to redirect a user. The purpose of the module is to redirect a user basing on the currently visited page, and on settings decided from an administrator user. The code that redirects the user is contained in this function, which is the implementation of hook_init():
/**
 * Implements hook_init().
 */
function redirect_init() {
  if (!redirect_can_redirect()) {
    return;
  }

  $current_path = current_path();
  $current_langcode = $GLOBALS['language']->language;
  $current_query = drupal_get_query_parameters();
  if ($redirect = redirect_load_by_source($current_path, $current_langcode, $current_query)) {
    redirect_redirect($redirect);
  }
  // …
}

The redirect_redirect() function is a wrapper for redirect_page_cache_clear().
function redirect_page_cache_clear($redirect = NULL) {
  if (!variable_get('redirect_page_cache', 0)) {
    return;
  }

  if (isset($redirect)) {
    $path = url($redirect->source, array('absolute' => TRUE));
    // Use a wildcard to catch paths with query strings.
    cache_clear_all($path, 'cache_page', TRUE);
  }
  else {
    // Clear the entire page cache.
    cache_clear_all('*', 'cache_page', TRUE);
  }
}

If you would implement a way to redirect a user through drupal_goto(), you should consider implementing hook_init() in a similar way.
Comment: Moving my block content into a function doesn't change anything at all, and hook_exit() happens too late to be of any use. I need to conditionally invoke drupal_goto(), but that doesn't appear to work in hook_exit().
Answer: You can include files whose functions you need to access in hook_boot()
e.g.
require_once DRUPAL_ROOT . '/' . 'includes/path.inc';
require_once DRUPAL_ROOT . '/' . 'includes/locale.inc';

Obviously this means a bit more digging to determine precisely which files you need to include, but it can be done without too many headaches...
Comment: Why do you think the OP needs the "path.inc" file, or the "locale.inc" file?
Comment: Are there any drawbacks to this? Is there any reason not to use the drupal_load() function over require_once? Also, I'm having trouble seeing if I have access to the $user global in hook_boot(). Any ideas there?
Comment: Alright, I've tried using require_once and drupal_load(), but neither work. drupal_goto() just doesn't seem to do anything regardless of what I try to include beforehand.
Answer: I ended up using hook_init, since pages aren't cached for authenticated users. I only needed this PHP to run for authenticated users. I really wish I could have figured out what was going on with hook_block, though...

Execute PHP on every page?


Using the string table and printing sections names

Question: We recieved a homework assignment in which we need to take an ELF file and print its sections' names.
We are supposed to do all that using only the data we receive directly from the ELF header,
meaning we can't use any "high level" procedures - we need to go directly to the data we need.
So, im trying to print the first section's name. I know the names are supposed to be in the string table. This is what I have so far:
I'm getting the start of the ELF file using mmap...
elfhead =(Elf32_Ehdr *) mmap...

I'm getting the section offset using the members in the ELF header
sectionoffset = elfhead->e_shoff

then
section = (Elf32_Shdr*)(elfhead + sectionoffset)
nameoffset = section->sh_name
stringoffset = elfhead->e_shstrndx;

To be clear -

in elfhead i have the elf header
in section i have the section header
in stringoffset i have the index inside the section table where the
string table is supposed to be
in nameoffset i have the index in
the string table where the first section name is suppose to be.

How do I go to the first name and print it, given the code above?
Comment: possible duplicate of [getting the sh_name member in a section header elf file](http://stackoverflow.com/questions/10863510/getting-the-sh-name-member-in-a-section-header-elf-file)
Answer: Well first off you'd have to have access to the section's String Table, and since the header is the first thing in the ELF file:
char* stringTable = elfhead + (section + header->stringoffset)->sh_offset;

Once you have that, all you really have to do is print the first one using the nameoffset you already obtained, like so.
char* name = stringTable + nameoffset;
printf("%s\n",name);

FYI, printing the rest of the names would be a simple loop:
for(i=0;ie_shnum;i++){
        char* name = stringTable + nameoffset;
        printf("%s\n",name);
        section++;
    }