"from A import B.C" in Python?

Question: In Python, it is possible to import dotted submodules explicitly:
import os.path

makes os.path available.
Now, is there a simple way to do the same thing for sub-submodules? The following code (which does not work) illustrates what I am looking for:
from A import B.C
from A import B.D

(from A import B does not import B.C and B.D, for the module A.B that I need to use). I would like to do this because:

I prefer to not pollute the main namespace with C and D (from A.B import C, D) because they do not have very specific names.
I would like to keep the sub-submodules inside their B namespace (for the same reason).

One solution is to do:
from A import B
from A.B import C, D
del C, D

which loads and defines B.C and B.D. However, this is (1) quite unusual, (2) somewhat obscure (side effect of defining B.C and B.D) and (3) maybe with a result that's not guaranteed (will B.C and B.D always be defined with this code?).
Is this method of putting only B.C and B.D (and B) in the namespace acceptable? is there a better method? maybe it actually best to find some legible compromise like from A.B import C as B_C, D as B_D?
While I am stuck with Python 2.6 (no importlib module…), for this problem, I am also curious about any modern solution.
Comment: Yes, but `A` is in practice quite a long name and the calls are numerous, so I'd like to find a compromise between legibility and convenience, especially since the name `B` is in my case quite specific . Using `B.C` and `B.D` would be a nice compromise.
Comment: @JayanthKoushik: Furthermore, `A.B` is not imported by `import A`, so this does not work.
Comment: Why not `import A` and use `A.B.C` and `A.B.D`?
Answer: For the intent of from A import B.C, I would suggest doing this:
from A.B import C as B_C

Answer: importlib can help here
import importlib
from A import B
importlib.import_module('A.B.C')
importlib.import_module('A.B.D')
# Now you can use B.C & B.D

On Python2.6 you should be able to
from A import B
import A.B.C
import A.B.D
del A

Comment: While it does not work in my case (Python 2.6), it's cleaner than my `from A import B; from A.B import C; del C` (which I'm not sure is guaranteed to work, i.e. to always make `B.C` available).
Comment: Thanks. Do you know if Python the less elegant `from A import B; from A.B import C, D; del C, D` guarantees that `B.C` and `B.D` are created (and kept around)? A reference would be much appreciated, if you have any! Similarly, a reference showing that `import A.B.C` makes `B.C` available would be great!
Answer: One of the cleanest ways is to do what Jayanth mentioned. If A is a long name then you can rename using the 'as' keyword
import A as X

Comment: This is definitely not an answer to the question…
Comment: This answer was only provided as an improvement to Jayanth's comment. Down voting such a suggestion can certainly not be a civil practice. The only reason I did not post a comment was simply because I did not have enough reps to post it as a comment.

As a matter of fact @vonPetrushev answer which is upvoted is very similar to my answer.
Comment: Sorry, I realize now that I had missed the "do what Jayanth mentioned" part. I removed my downvote (I still understand that downvoting an answer that's not an answer is appropriate, since answers can even be flagged if they fail this test—but this does not apply here). If you flesh out your answer and make it more explicit and self-contained, I will consider upvoting it, because the idea is good.

"from A import B.C" in Python?


AJAX modify field with multiple values

Question: I'm trying to use ajax to update the value of a field that can have unlimited values. I am using hook_form_FORM_ID_alter() to add a button to the form with an ajax callback. The problem is that I can not simply set $form['field_name']['language'][0]['value'] because I need to add/remove items from the fields value. So what I've done is change the value on the entity in $form_state and build the form again. The field reloads with the items that I set but when I save the entity only the first new item saves. For example, if the field has no value then only one of the items will save; if the field has one item already then the first two items will be saved; etc.
In my specific example I am using the commerce_product form with a commerce_installments field. However, I have created a simplified example using nodes. I have created a content type called testing with an integer field called field_int_test that has a cardinality of -1 (unlimited). The "Calculate" button I have added to the form uses ajax to generate a random number of items from 2-10 for the field and sets each item's value to a random number. Clicking "Calculate" reloads the field with the new items, but saving exhibits the same behavior as above. I have checked the post and all of the values are being posted back to the web server so I am not sure where the problem is. Also, I feel that there has to be a better way to do this but I have not come up with one.
I have included the code used for the simple node example below. Any help is greatly appreciated.
/**
 * Implements hook_form_FORM_ID_alter().
 */
function mymodule_form_testing_node_form_alter(&$form, &$form_state, $form_id) {
  $form['calculate'] = array(
    '#type' => 'button',
    '#value' => t('Calculate'),
    '#ajax' => array(
      'callback' => 'mymodule_testing_ajax',
      'event' => 'click',
      'wrapper' => 'testing-replace',
      'method' => 'replace',
      'effect' => 'fade',
    ),
    '#weight' => 1,
  );

  $form['field_int_test']['#prefix'] = '
';
  $form['field_int_test']['#suffix'] = '
';
}

/**
 * AJAX callback that will generate a random number of items for the
 * field_int_test field wiht random values for each and re-build the form with
 * the new values.
 */
function mymodule_testing_ajax(&$form, &$form_state) {
  $total = mt_rand(2, 10);
  for ($i = 0; $i < $total; ++$i) {
    $form_state['node']->field_int_test['und'][$i]['value'] = mt_rand();
  }

  $form_state = array(
    'build_info' => array(
      'args' => array($form_state['node']),
    ),
    'node' => $form_state['node'],
    'cache' => FALSE,
    'no_cache' => TRUE,
    'input' => array(),
  );
  $form = drupal_build_form('testing_node_form', $form_state);

  return $form['field_int_test'];
}

I have made some changes to the callback function (see below) that now allows the field to be saved but any changes made to the node form after clicking "Calculate" will not be saved; this includes changes to the field being calculated. I have modified the callback to save the new form and form state into new variables, replace $form['field_int_test'] with the value in the new form and update the form's cache.
/**
 * AJAX callback that will generate a random number of items for the
 * field_int_test field wiht random values for each and re-build the form with
 * the new values.
 */
function mymodule_testing_ajax(&$form, &$form_state) {
  $total = mt_rand(2, 10);
  for ($i = 0; $i < $total; ++$i) {
    $form_state['node']->field_int_test['und'][$i]['value'] = mt_rand();
  }

  $field_form_state = array(
    'build_info' => array(
      'args' => array($form_state['node']),
    ),
    'node' => $form_state['node'],
    'cache' => FALSE,
    'no_cache' => TRUE,
    'input' => array(),
  );
  $field_form = drupal_build_form('testing_node_form', $field_form_state);

  $form['field_int_test'] = $field_form['field_int_test'];
  form_set_cache($form['#build_id'], $form, $form_state);

  return $form['field_int_test'];
}

Answer: I found that my problem was both how I was rebuilding the form and when. Instead of doing a full build I needed to call drupal_default_form() to build just the field's form and I had to do it during mymodule_form_testing_node_form_alter() rather than the ajax callback. My completed code is below:
/**
 * Implements hook_form_FORM_ID_alter().
 */
function mymodule_form_testing_node_form_alter(&$form, &$form_state, $form_id) {
  // If this form has been submitted by "Caluculate" button then we need to
  // process the form to calculate and add the integers based on the user's
  // input.
  if (isset($form_state['triggering_element'])
    && $form_state['triggering_element']['#value'] == 'Calculate') {
    _mymodule_calculate_process_form($form, $form_state);
  }

  $form['calculate'] = array(
    '#type' => 'button',
    '#value' => t('Calculate'),
    '#ajax' => array(
      'callback' => 'mymodule_testing_ajax',
      'event' => 'click',
      'wrapper' => 'testing-replace',
      'method' => 'replace',
      'effect' => 'fade',
    ),
    '#weight' => 1,
  );

  $form['field_int_test']['#prefix'] = '
';
  $form['field_int_test']['#suffix'] = '
';
}

/**
 * AJAX callback to reload the integer test field with calculated values.
 */
function mymodule_testing_ajax(&$form, &$form_state) {
  return $form['field_int_test'];
}

/**
 * Process a form submitted via AJAX to calculate the value of the integer test
 * field.
 */
function _mymodule_calculate_process_form(&$form, &$form_state) {
  // We don't care about the installment field values that were submitted with
  // the POST.
  unset($form_state['values']['field_int_test']);
  unset($form_state['input']['field_int_test']);

  $items = array();
  $total = mt_rand(2, 10);
  for ($i = 0; $i < $total; ++$i) {
    $items[$i]['value'] = mt_rand();
    $form_state['node']->field_int_test['und'][$i]['value'] = $items[$i];
  }

  // Update the items count for the field.
  $form_state['field']['field_int_test']['und']['items_count'] = count($items);

  // Build the field form with the new values.
  $language = $form['language']['#value'];
  $field = $form_state['field']['field_int_test'][$language]['field'];
  $instance = $form_state['field']['field_int_test'][$language]['instance'];
  $field_form = field_default_form('node', $form_state['node'], $field,
    $instance, $language, $items, $form, $form_state);

  // Replace the field form that was built previously with the new one.
  dpm($field_form['field_int_test']);
  $form['field_int_test'] = $field_form['field_int_test'];
}

I will refrain from accepting my own answer for the time being, in case somebody else has a better solution.

AJAX modify field with multiple values


Can someone motivate gamma function to me not as an extension of factorial but as something important on its own?

Question: Suppose I have to explain the exponential function to someone. Then I would not say something like "Well, we want to extend the $a^n=a\cdot a\cdot a\cdot a.......$ definition to non-naturals. So we do this trick which preserves the properties and so on....".
Instead, what I would say is: 'We have many intuitive real-world problems where the rate of change of a quantity is proportional to its current value, say population." Using those real world problems, I would get to the exponential function as $\lim_{n\rightarrow \infty} (1+\frac{x}{n})^n$. Using this, I would further derive properties like $exp(x+y)=exp(x)exp(y)$. Then I would conclude that this otherwise very important function happens to be the same as repeated multiplication when the argument is a natural number.
I would just like to have a similar presentation of the gamma function. Is there any intuitive real world problem which can be used to motivate the gamme function?
Comment: @ProfessorVector My advice for you would be to stop giving advices as you are not good at it
Comment: My general advice to anybody who needs a special motivation to study something is: don't study it. Try gardening, nice flowers, tasty fruit or vegetables,... ;-)
Comment: @ProfessorVector you should delete your first comment. It is utter nonsense
Comment: @Gerry Myerson What are the odds the OP would consider the Riemann zeta function an "intuitive real world problem"? Whatever that means.
Comment: Lots of interpretations [here](https://math.stackexchange.com/q/3444/272831).
Comment: The best motivation for the Gamma function is the functional equation for the Riemann zeta function, $$\zeta(s)=2^s\pi^{s-1}\sin(\pi s/2)\Gamma(1-s)\zeta(1-s)$$ https://en.wikipedia.org/wiki/Riemann_zeta_function#Riemann's_functional_equation
Comment: @GerryMyerson That's interesting but it's beyond my level. Can this function be motivated by some differential equation describing a physical phenomenon ?
Comment: It's a functional equation for the zeta function. The zeta function is tied to the deepest properties of the prime numbers. The prime numbers are as real world as anything.
Comment: @RyderRude I do not want to start a debate whether there are physical applications. Only one advice : Do not trust blindly mathematical models for physics. Fascinating theories need not be real theories. Better focus on mathematical motivations.
Comment: There is a nice motivation from Gauss sums. However, seeing how the OP responds to the zeta function comment, I hesitate to post an answer, as Gauss sums are probably not "real world" for the OP.
Comment: @WhatsUp Your comment confuses me. The OP responded to the zeta function comment by saying "it's beyond my level"; it didn't seem he/she was saying it's not real world enough....
Comment: @mathworker21 I meant the response "motivated by some differential equation describing a physical phenomenon". The OP probably has background in physics/analysis etc., but not in number theory. And Gauss sums are certainly more on the number theory side.
Comment: @WhatsUp thanks. my comment was stupid
Answer: Start with perhaps a discussion on mean time between failures and how it is used in engineering. Then talk about probability density function and how that describes rate of failures of components in a complex system and analysis of that requires a tool. That tool is the Gamma function, $\Gamma (x)$.

Problem: Suppose that a cell phone battery charge has the gamma distribution with shape parameter $k \in (0, \infty)$ and scale parameter $b \in (0, \infty)$. The probability distribution is given by
$$f(x) = {1 \over \Gamma (k) b^k}x^{k-1}e^{-x/b}, x \in (0, \infty)$$
Derive this formula and then calculate the probability that the battery charge will last more than 24 hours under what parameters $k$ and $b$. Ask them how this helps in the real world.
If you need more examples, you could talk about arrival times in Poisson processes, how Poisson processes have a gamma distribution. Most people should be able to relate to waiting in line in a queue or being put on hold by a call center. These are due to underlying gamma distributions. Modeling them requires using $\Gamma (x)$.

Problem: Suppose that customers arrive at a service station according to the Poisson model, at a rate of 10 per hour. Relative to a given starting time, find the probability that the second customer arrives sometime after 1 hour. Derive the formula and then link it back to gamma distributions.

Then you can take a short detour in explaining recursive processes and that is where the factorial comes in.
For integers $n! = n \times (n-1) \times \dots \times 2 \times 1$.
The factorial function has a recursive property: $n! = n \times (n-1)!$.
What happens if $n$ is not integer or a real number or a complex number? It so happens that $\Gamma (x)$ is a function on complex numbers (which also covers reals) that behaves similar to how the factorial function behaves for integers.
$$\Gamma (x) = x \Gamma (x-1)$$
Notice how this is analagous to the factorial function.
But, what would be the definition of $\Gamma (x)$.
It so happens that $\Gamma (x)$ can be defined analytically as:
$$\Gamma (x) = \int_{0}^{\infty} t^{x-1} e^{-t} dt  $$
This function can be extended to negative real numbers and complex numbers as long as their real part $Re(x) \ge 1$.
You can then give a beautiful equation such as
$$\Gamma \left({1 \over 2}\right) = \sqrt \pi$$
and ask them to derive it.
If we just stop here, we can recap the concepts needed for understanding $\Gamma (x)$.

Integers
Factorial function
Reals
Complex numbers
Integrals, areas under curves
Analytical definition of $\Gamma (x)$
Discussion on why $Re(x) \ge 1$


Math rock chord progression analysis

Question: I have been given this chord progression I have to analyse:
Em9 | F#m7 F#7/G | Bm11 | Cmaj7 Dmaj7 | Em9 (loop around)

I've got around to set E as the root since it's the one that sounds the most stable. So the progression would go like this:
Im9 | IIm7 II7 | Vm11 | bVImaj7 bVIImaj7 | Im9

Excluding the colours, and taking F#7/G as a passing chord, I'd say this is in E aeolian (or E natural minor). But the 7ths are inconsistent and getting me confused.
How would you analyse this?
Answer: Without listening to the actual song it is hard to tell where the tonal center lies.
In E minor you wouldn't have neither F#m nor Dmaj7, but rather F#dim/F#m7b5 and D7.
If you look at it from a D major / B minor perspective, everything is diatonic besides the passing F#7/G and the Cmaj7, that can be seen as a bVII / myxolidian moment or as a simple planing back to D.
(btw in roman numerals notations you use small letters in favour of the m modifier, so v over Vm.)
Comment: I know there are two "schools" about roman numeral notation... This is how I've learned it so that's the way I usually write them :)
Comment: Maybe I can get an audio sample or sheets... I'll try to get some.
Comment: There are many types and styles of roman numeral notation, and none is "the right one". Some books don't use the "small letter" approach at all.
Answer: I would view this as a chord progression in E minor (or, given the C#'s, could be in Dorian mode) which, put in Roman numerals, would be
i-ii-V7/v-v-VI-VII

Math rock chord progression analysis


Distinguished triangle in abelian triangulated categories

Question: I know that an abelian triangulated category is semisimple,i.e.,any exact sequence splits.But Why does any distinguished triangle  is isomorphic to a triangle of the form $X \stackrel{f}{\rightarrow} Y \stackrel{g}{\rightarrow} \operatorname{ker} f[1] \oplus \operatorname{coker} f \stackrel{h}{\rightarrow} X[1]$?(cf. Gelfand and Manin, Exercises to IV 1).
My try:Let $X \stackrel{f}{\rightarrow} Y \stackrel{g}{\rightarrow} Z \stackrel{h}{\rightarrow} X[1]$ be a distinguished triangle.Then we have $ \operatorname{coker} f \stackrel{g}{\rightarrow} Z \stackrel{h}{\rightarrow} \operatorname{ker} f[1]$.If it is exact at $\operatorname{coker} f,Z$,and $\operatorname{ker} f[1]$,then we have $Z\cong \operatorname{ker} f[1] \oplus \operatorname{coker} f$.But I can't show the exactness.
Comment: If the category is semisimple, then split the map $f:X\to Y$ so that you have $X=Ker(f)\oplus X'$ and $Y=Coker (f)\oplus Y'$. Noe you can describe your map $f$ as being 0 on its kernel and an iso from $X'$ into Coker(f). The result now folllows from the fact that direct suma of tengles are rriangle
Answer: Split $X=Ker(f)\oplus X'$,  and $Y\cong X'\oplus Coker(f)$. Now you can describe $f$ as a direct sum of 3 maps,  zero in $Ker(f)$,  identity on $X'$ and $0\to Coker(f)$. The result follows from the fact that identity fits into a triangle with 0 as cone, and the zero maps similarly (just traslate the triangle with identity)


How to export each row from two data frames (which share same identifier) in a separate pdf file?

Question: I am strugling to find a way to compose a pdf for each row which would contained only one row from two data frames. Both dataframes has two common identifiers which are unique for given dataframe. Say I have these two data frames:
    country number  Particip    EvoParticip
    CZ  2   731658.000000000    26.100000000
    CZ  4   696739.000000000    -13.200000000
    CZ  5   580834.000000000    -10.500000000

and
    country number  Trans EvoTrans
    CZ  2   1656171.000000000   12.900000000
    CZ  4   16265.000000000 26.000000000
    CZ  5   17302.000000000 26.200000000

The desired result is to have for each row single pdf containing records from both dataframes like this:
    country number  Particip    EvoParticip
    CZ  2   731658.000000000    26.100000000

    country number  Trans EvoTrans
    CZ  2   1656171.000000000   12.900000000

So far I was able to produce a single pdf with complete dataframes but I don't know how exactly proceed to get desired result. Heres my code:
figs = [fig,fig1]
fig
the_table = ax.table(cellText=df2.values,colLabels=df2.columns,loc='center')

fig1
the_table = ax.table(cellText=df3.values,colLabels=df3.columns,loc='center')

with PdfPages("foo.pdf") as pp:
    for figy in figs:
        pp.savefig(figy, bbox_inches='tight')

Answer: You can adjust your code by creating multiple PdfPages objects :
from matplotlib.backends.backend_pdf import PdfPages

for i, (r1, r2) in enumerate(zip(df1.to_numpy(), df2.to_numpy()), 1):
    with PdfPages(f"file_{i}.pdf") as pp:
        fig, axes = plt.subplots(2, 1)

        for ax, (row, header) in zip(
            axes, [(r1, df1.columns), (r2, df2.columns)]):
            ax.table(
                cellText=[row],
                colLabels=header,
                cellLoc="center",
                loc="center"
            );ax.axis("off")

        pp.savefig(fig, bbox_inches="tight")

NB : This assumes that the two dataframes are correctly sorted and have the same shape.
Comment: You can replace `f"file_{i}.pdf"` with `f"{'_'.join(map(str, r1[:2]))}.pdf"`.
Comment: That works great, thanks! Is it possible to add to this code a line which would just name the produced pdf file sccording to first two columns?


Music Theory, Circle of Fifth, Chords that fit, Exceptions and example song "Sultans of Swing"

Question: I am playing guitar for some years now, mostly self-taught. I had never studied that much of music theory, but now as I want to improve my improvisation skill and want to get a broader view I started learning music theory. One of the first things I learnt is that certain chords match naturally, and these could be read off from the circle of fiths, for example in C major common chords around the key are C, G, F, d, a, e. Most songs I looked at indeed just use chords which lie near its key in the circle of fifths.
Now I started to take a closer look at the famous "Sultans of Swing" by Dire Straits. According to wikipedia it is in the key of d minor, and the chord progression basically is

d C Bb A

in the verse and

F C Bb D (+C Bb in short sequence at the end).

in the chorus.
But when I look at the circle of fifth the A major chords is quite far from the d minor, almost on the opposite side of the circle of fifth. I am wondering, this contradicts the theory (at least what I have read about it), but it still sound greats?
Is there an explanation of this phenomen. As I said I still started with my studies in musical theory, so maybe I got something wrong about chords and the circle of fifth, but when I look at my resources they almost always say something like the chords nearest to a key fit the best?
Comment: Your D in the chorus should be d.
Answer: The A major chord is the dominant chord (V) of D minor. The chord leads very well back to the Dm chord because of the use of the leading tone (C#) and it's very typical to use in a minor key even thought it's not in the natural minor scale (it is however found in the harmonic and melodic minor scales).
There are many questions that go into more detail about this including:
Understanding minor key harmony
Why are the harmonic and melodic minor scales called what they are?
Is Emajor the dominant chord in the key of A minor?
Answer: If I remember my circle of fifths, the relative major key of D Minor is F Major, which has an A major chord in that key, so it makes sense that it fits with the rest of the song.
Also if I remember correctly, the exact opposite key on the circle of fifths is that key's relative major/minor, so that's likely why it is on the opposite side.
You are correct in saying that modulating to the next nearest key generally sounds best, but remember this is all in the eye of the beholder and good music is subjective too!
Comment: In what sense has an F major and A major chord in it? The notes of an F major chord are f,a,c, those of an A major are a cis,e, which for me seem t be quite different?
Comment: @Stefan sorry I mean the key of F Major has the notes F, G, A, B♭, C, D, and E so therefore it has an A Major chord if you are playing in that key.
Comment: But where is the cis from the A major? I see just a c, so still It is not clear to me in what sense we have an A major chord in these notes?
Comment: if you're playing an A Major chord in open position on the guitar, you will be playing the notes A, E, A, C#, E. When playing the harmonic minor, you raise the C to C#
Comment: Ah, guess I know what is it about, it has to do with the harmonic minor scale. Just to note because I think you have a different drawing of the circle of fifth in your mind: When I speak about opposite side I refer to pictures like the one here on wikipedia: https://en.wikipedia.org/wiki/Circle_of_fifths Here F major and d minor are not on opposite sides, they are at the same point (major are outside of the circle and minors are inside), but you wrote the F major is opposite, but in the linked picture it is not, exactly opposite to d minor/F major is gis minor/B major.
Comment: yeah you've got it, I always forget the difference between harmonic and natural minors... Ahhhh, I definitely forgot my circle of fifths then haha, the ones I learnt from had the relative minor on the opposite side if I'm right, it's been a long time though!
Comment: @JamieBrace - as Dom states, there is no A maj. chord in F maj. key. Lack of C# in F key prohibits it. However, the relative min. (Dm) often needs its dominant of ,usually, A maj. Which has the C#. This does not mean that A maj. chord belongs in key F though!
Answer: The chord sequence d, C, B♭, A has an interpretation as the "Andalusian Cadence" (although it's not really a cadence as such.) It can be read as ⅰ, Ⅶ, Ⅵ, Ⅴ in a minor key or as ⅳ, Ⅲ, Ⅱ, Ⅰ in a major or ⅵ, Ⅴ, Ⅳ, Ⅲ in another key. This pattern is common in Flamenco and some other Spanish style music. There is a Wikipedia page for it.
Among the popular songs using this pattern are "Hit the Road Jack," "Good Vibrations," and "Runaway."


Drupal 7 Bartik Menu tabs changed to vertical list - how to reset back to tabs?

Question: I have been working on a Drupal 7 install with the Bartik theme.  Today, the main menu tabs changed to a vertical list on the left side of the page.  I do not know how to reset this.  I checked the theme folder and none of the css files have been changed, so I suspect it is either a module I installed today or something I did when I was working on editing some pages.  Any suggestions would be aprpeciated.
Comment: I'm not that good with CSS.  The site is www.vcapares.org.
Comment: The Responsive Menus Module was the culprit.  The module was disabled and the Bartik Main Menu displays in tabs again.
Answer: Try turning off "Aggregate and compress CSS files" on /admin/config/development/performance and then use right-click inspect element (firefox or chrome) to identify the css responsible for the override. You should see the bartik css files overridden by something else causing this behavior.
A clear cache never hurts either.
Comment: Those options were already turned off.  I'm not that good with CSS, but will look through to see if I can figure out anything.


Proof of semicontinuity

Question: Given Definition:
Function $f$ on metric space $M$ is called lower semicontinuous if, for each real $a$ the set $\{x \in M: f(x) \leq a\}$ is closed in $M$.
Question:
Prove that $f$ is lower semicontinuous if and only if $f(x)\leq \liminf\limits_{n\rightarrow \infty} f(x_n)$ whenever $x_n\to x$ in $M$.
Hint Given:
For the forward implication, suppose that $x_n\to x$ and $m = \liminf\limits_{n\rightarrow \infty} f(x_n) \lt \infty$.
Then, for every $e>0$, the set $\{t \in M: f(t) \leq m + \epsilon \}$ is closed and contains infinitely many $x_n$.

This is from a Real Analysis book I am trying to read.
I would really like to share my work on this, but I do not seem to understand how to even begin with the proof, using only the definition given.
Answer: Suppose that $x_n$ converges to $x$. Let $\epsilon>0$, then the set $G_\epsilon=\{y\in M:\, f(y)>f(x)-\epsilon\}$ is open in $M$. Since, $x_n$ converges to $x$ there is an $m$ such that $x_n\in G_\epsilon$ for every $n\geq m$. In other words,
$$f(x_n)>f(x)-\epsilon$$
for every $n\geq m$. This implies that $\liminf_{n}f(x_n)\geq f(x)-\epsilon$. As $\epsilon>0$ was arbitrary we have $\liminf_nf(x_n)\geq f(x)$. For the converse, suppose that whenever $x_n\to x$ we have $\liminf_{n}f(x_n)\geq f(x)$. We wish to show that the set $$F_\alpha=\{x\in M:\, f(x)\leq \alpha\}$$ is closed in $M$. We will show that the complement $$M\setminus F_\alpha=\{x\in M:\, f(x)>\alpha\}$$ is open. Pick $x\in M\setminus F_\alpha$, then $f(x)>\alpha$. If there is no $\rho>0$ such that the ball centered at $x$ with radius $\rho>0$ belongs to $M\setminus F_\alpha$ then we would be able to find a sequence $x_n\in F_\alpha$ such that $x_n\to x$. Then, since $x_n\in F_\alpha$ means that $f(x_n)\leq \alpha $ for all $n$, which implies $\liminf_n f(x_n)\leq \alpha$, contradicting our hypothesis $\liminf_n f(x_n)\geq f(x)>\alpha$. Hence, there is a $\rho>0$ such that the ball centered at $x$ with radius $\rho$ is contained on $M\setminus F_\alpha$. This means, that $M\setminus F_\alpha$ is open, hence $F_\alpha$ is closed as we wanted.
Comment: This is an excellent answer. And I am very happy that this is the way I tried to go about it, but I was uncertain if $f(x_n) > f(x) - \epsilon$ actually implied that $\liminf\limits_{n\rightarrow\inf} f(x_n) \geq f(x) - \epsilon$. But seeing it now, it makes sense, since $x_0 \rightarrow x$. Thank you
Comment: There are some useful characterizations for inequalities like $\liminf_n y_n\geq y$ where in general $y_n$ is a sequence on $\mathbb{R}$. For example, $\liminf_n y_n \geq y$ if and only if for every $\epsilon>0$ the set $\{n\in \mathbb{N}:\, y_nf(x)-\epsilon$ for all $n\geq n_0$. In other words, the set $\{n\in \mathbb{N}:\, f(x_n)<f(x)-\epsilon\}$ is finite. Thats how we conclude $\liminf_n f(x_n)\geq f(x)$.

Proof of semicontinuity


Nested query using while condition - sql

Question: I need to do a nested query on a single table. Each row is potentially the parent or child of another row.
Is it possible to do this with a single select statement? Ive started using this statement but it only goes down one level.
select * from myTable where parent_id in
   (select id from myTable where name = 'manager' )

This select however will only go down one level. If a row has multiple children they will be ignored. (In the table each row has an Id field , if a row has a parent then the parents Id value will be in the child's parent_Id field. )
If i could include a while loop in the SQL which would always check to see if the returned Id was a parent or not and if it was check and see if any other row was its child by checking the other rows parent_Id. However i m concerned this would take alot of cycles to eventually find all parent child relationships. Any suggestions? Thanks
using Oracle db
Comment: Which DB are using? Different DB's have different solutions for this particular problem.
Comment: Could you give an example of a query you are trying to write - what fields you want returned for example.
Answer: I think you are looking for a hierarchical query like this:
select * from mytable
connect by prior id = parent_id
start with name = 'Manager';

(A "nested table" is something else entirely.)
Comment: I'm going to assume that this is Oracle specific code for dealing with Hierarchies. SQL Server of course has its own methods. For a more generic approaches, check out http://www.amazon.com/Hierarchies-Smarties-Kaufmann-Management-Systems/dp/1558609202/ref=sr_1_3?ie=UTF8&qid=1306168577&sr=8-3
Comment: @Tom, correct this is an Oracle-specific answer; the question is tagged for Oracle.
Comment: Thank you, this query will dive into every parent child relationship where it finds a value of "Manager" in a field named name, therefore if the value is not unique then i will start to see lots of duplicates. Thanks


Uso do getter e setter no Dart

Question: Não consigo entender a real necessidade de se utilizar o Get/Set para o caso abaixo, o qual, mesmo eu deixando a variável comodo privada, eu ainda poderia modificá-la pelo uso de métodos.
class casa {
  int comodos = 4;
}

void main() {
  casa casa1 = casa();
  casa1.comodos = 10;
  print(casa1.comodos);
}

Answer: Caro @Aiken, se fizer pelos métodos poderá modificar por métodos você pode tratar a entrada, permitindo somente valores "corretos" para a regra/lógica da classe. Isso é o básico em "visibilidade" em OO com variáveis (leia sobre a visibilidade de variáveis no final da resposta, acaso não saiba como usar em Dart).
Você é quem irá definir o que pode e o que não pode, já se for public poderá "tudo" (no caso qualquer valor int valido/aceitável). Agora imagine que sua intenção de comodos fosse um range limitado de 5 a 50, qualquer um poderia colocar 4 ou menos, 51 ou mais, como valores se for "public", se fizer privado e tratar por um método set da vida então uma IF será mais que o suficiente para resolver a regra do range de limite, exemplo:
import 'dart:io';

class casa
{
  int _comodos = 4;

  setComodos(int value) {
    if (value < 2 || value > 50) {
        throw("Numero de comodos inválidos");
    } else {
        _comodos = value;
    }
  }

  comodos() {
    return _comodos;
  }
}

void main() {
    casa casa1 = new casa();
    casa1.setComodos(10); //10 comodos
    print(casa1.comodos());
}

Se fizer assim:
    casa casa1 = new casa();
    casa1.setComodos(100); //100 comodos
    print(casa1.comodos());

Vai emitir uma exception customizada que informa que o numero de comodos é invalido, algo como:
Unhandled exception:
Numero de comodos inválidos
#0      casa.setComodos (file:///home/brcontainer/prog.dart:9:6)
#1      main (file:///home/brcontainer/prog.dart:22:11)
#2      _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:261)
#3      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:148)

Sobre privado (visibilidade de variaveis) no Dart
Dart tratará variaveis como privadas desde que a classe esteja em uma lib a parte e use o underline/underscore como prefixo, se a classe for declarada no mesmo documento do main(), veja a documentação:

https://dart.dev/guides/language/language-tour#libraries-and-visibility
Comment: Caro @user140828 não ficou claro para mim que o autor falava de *métodos especiais* , me pareceu que ele falava de Get e Set no sentido de setar por métodos ao invés de diretamente, aliais se for o caso dos *métodos especiais* ai seria outra coisa, mas como não esta bem descrito na pergunta se são eles ou se não então não vou editar a resposta ainda. Não entendi sobre o mal conselho, expliquei a questão de quando necessidade de uma regra faria sentido usar privado, quando não tiver "regra" então não tem porque.
Comment: Dart não usa getters e setters como Java, Dart usa getters e setters como C#. Se mais tarde o programador quiser adicionar uma regra de validação, ele poderá sem ter refatorar o código para receber os valores através de métodos. Não acredito que exista razão para criar getters e setters em Dart quando você não precisa de validação, talvez tenha sido um mal conselho.
Answer: Bom dia,
No dart a refatoração é simplificada, entendo que caso não exista nenhum controle sobre o acesso ao membro, poderá tratá-lo como público, e se a regra de negócio se alterar ao longo do tempo, poderá transformar o membro em privado e declarar os métodos acessadores.
1º Podemos começar com o atributo Público
class casa
{
  int comodos = 4;

}
void main() {
    casa casa1 = new casa();
    casa1.comodos = 10; //10 comodos
    print(casa1.comodos);
}

2º Após podemos refatorar e modificar a sua visibilidade, sem alterar as dependências.
class casa
{
  int _comodos = 4;

  set comodos(int value) {
    if (value < 2 || value > 50) {
        throw("Numero de comodos inválidos");
    } else {
        _comodos = value;
    }
  }

  int get comodos => _comodos;

}

void main() {
    casa casa1 = new casa();
    casa1.comodos = 10; //10 comodos
    print(casa1.comodos);
}

Uso do getter e setter no Dart


Como imprimir itens selecionado?

Question: while(resposta == "sim"){
            System.out.println("Informe o Numero do Produto desejado");
            System.out.println("15 - Parafuso, 20 - Porca, 30 - Arruela");
            cod_peca = scn.nextInt();

                switch(cod_peca){
                    case 15:
                        System.out.println("Voce selecionou Parafuso");
                        break;
                    case 20:
                        System.out.println("Voce selecionou Porca");
                        break;
                    case 30:
                        System.out.println("Voce selecionou Arruela");
                        break;
                }
            System.out.println("Informe a Quantidade do Produto");
            qtdeProd = scn.nextInt();
        }
System.out.println("Numero do Produto comprado: "+cod_peca);

Comment: Qual o problema em concreto que está a encontrar? Tente ser um pouco mais especifico no que não está a funcionar, e de como queria que estivesse a funcionar
Comment: Me desculpe, entrei  aqui hoje e estou postando aqui pela primeira vez. Meio perdido ainda kkk enfim, estou para ler 3 produtos e que a pessoa selecione apenas os produtos selecionado e que o imprima. Eu nao sei, a opcao seria fazer por switch case ou por if else.  poderia me dar um help?
Comment: Possível duplicata de [Entrada não executada](https://pt.stackoverflow.com/questions/101036/entrada-n%c3%a3o-executada)
Comment: Uma coisa é que comparação de `String` se faz com `equals`: `"sim".equals(resposta)`
Comment: vlw galera! deu certo
Answer: Só há um pequeno problema que faz com que o Compilador não compile. O operador == serve apenas para comparar números no Java (ex.: 2 == 4, 5 == 5; já 3 == "ola" não), mas em outras linguagens como Python serve para comparar números e strings.
Para comparar strings com números ou strings com strings não usamos == no Java, mas usamos equals resposta.equals("sim");
Faça isso que o programa vai rodar.
Comment: Obrigado! deu certo!

Como imprimir itens selecionado?


Why is the circle $S^1$ semi-locally simply connected?

Question: I am working from the definition in these notes, but they seem to agree with other things I read:

$X$ is semi-locally simply connected if for all $x_0 \in X$, there is some neighbourhood $U$ of $x_0$ such that any loop $\gamma$ based at $x_0$ is homotopic to $c_{x_0}$ as paths in $X$.

(edit: this should specify that $\gamma \subset U$)
I'm not sure why $S^1$ meets this defition, since any nonconstant loop must wind the circle completely and is never going to be contractible. The only thing I can think of is: for any $x_0 \in S^1$, there is a neighbourhood $U$ where the only possible loop you can draw is the constant loop. So trivially this is homotopic to the constant loop. Is that the correct interpretation?
Thank you!
Comment: The notation $\gamma \in U$ means that $\gamma$ is a **point in $U$**. You mean that the image of the loop $\gamma$ is a subset of $U$.
Comment: Yes sorry I meant $\subset$, have changed it
Answer: The statement in the notes is incomplete: it should specify that the loop $\gamma$ is supposed to be in $U$. Then, the complete definition reads that the neighbourhood $U$ must be such that all loops in it are homotopic as paths in the larger space $X$ to the constant path (though not necessarily as paths in the neighbourhood). Namely, a homotopy between $\gamma$ and $c_{x_0}$ may partially range outside $U$, although this isn't the case for $S^1$ because it's locally simply connected.
Comment: Thank you, you are right about $\gamma \in U$, I but this is not quite my question. A fully winding loop in $S^1$ is not contractible in $S^1$. So is my hypothesis (that the only loops in $U$ are the constant loops) the correct reason?
Answer: That is not the correct interpretation, because it is not true that "any nonconstant loop must wind the circle completely". A loop can go back and forth over a small neighbourhood of $x_0$.
Comment: Ah! That was my misunderstanding - very silly of me. Thank you!


So many users and their access

Question: I am developing a site which has around 46 departments and each dept has three Access types. Say Dept Admin, Dept Lead, and Dept Agent.
Site will use taxonomy terms Confidential, Restricted, Private and Public. Rules goes like this..

Dept Admin has access upto Confidential level records, Dept Lead->upto Restricted, Agents upto Private records.
dept records should be accessible by only that dept users and goes its content moderation
I am planning to use taxonomy access control along with Workbench Moderation. But this requires me to set up 46 * 3 (138) roles for my site. I am a bit concerned that it might reach some php memory limit, not sure.

Any advice is appreciated.
Answer: finally went with duo Organic Group + OG Taxonomy permission modules

So many users and their access


How to show the equivalence between these two definitions of an ordered field?

Question: Definition 1: A field $F$ is an order field if
a) $x,y,z\in F,y \lt z \to x + y \lt x + z$
b) $x,y\in F, x \gt 0, y \gt 0 \to xy \gt 0$
Definition 2: A field $F$ is called an ordered field if there exists $P \in F$ s.t.
a) $x,y \in P \to x+y \in P, xy \in P$
b) $x \in F\to x \in P$ or $x=0$ or $-x\in P$
I think a logical path would be to use the properties of a field to derive the set P from the conditions in definition 1. However, I'm not really sure if that is entirely correct or even how to go about it. Any help would be appreciated.
Comment: Hint: "$P$" stands for "positive numbers".
Comment: @MJD I understand that part, but I'm still not sure where to go. I currently still trying to find an answer based off of Hayden's hints.
Answer: Hint: For the implication $(1) \implies (2)$, show that the set $P=\{x \in F \mid x>0\}$ gives you what you want in $(2)$.
For the implication $(2) \implies (1)$, define $<$ by saying that $x<y$ if $y-x \in P$, then show that $<$ is a strict total order which satisfies the conditions of $(1)$.
To understand the intuition, the idea is that $P$ is the set of positive elements.
(Also, strictly speaking, in definition $(2)$ the set $P$ should be supplied from the get-go (in the same way $<$ is provided in definition $(1)$).  Otherwise I'd say that $F$ isn't ordered, but is orderable since a priori there's no reason to suspect $P$ is unique.)
Comment: I think I see what you're saying. It seems like $2 \to 1$ might be easier. However, I'm not really sure how you could confirm the properties of definition 1. Could you elaborate a bit more.
Comment: Suppose $y<z$, so $y-z \in P$ by definition.  Then $y-z=(y+x)-(z+x) \in P$, so...


Infer limits of unscaled values from their standardized values - Clustering

Question: I am working on a clustering problem and I have some skewed variables.
So, I log transform them and use them in clustering.
However, instead of multivariate clustering, I do multiple univariate clustering (1D clustering of multiple variables seperately) using jenks approach.
My question is,
a) The pattern/cluster/cluster labels found based on standardized/log transformed values also apply to the original values? For ex: using log transformed values (and their correspondimg customer id), I can get their raw values. So, can I say that raw data from 100 to 102 belongs to cluster 0 and 800 to 804 belongs to cluster 1?
Meaning, the pattern found for normalized/standardized/transformed value are also applicable to raw values? Or is it like pattern found in log transformed values is kind of fake (amd not mecessarily apply to raw values)
my sample data looks like below. This is just dummy data to help understand the question (so transformation values may be incorrect)
Comment: How do you combine the univariate results into an overall cluster solution?
Comment: @whuber - based on some biz logic.
Comment: if variable 1 value belongs to Cluster 1 and variable 2 value belongs to Cluster 3  and variable 3 value belongs to cluster 4 then final_cluster_label = "High spenders"

if variable 1 value belongs to Cluster 3 and variable 2 value belongs to Cluster 4  and variable 3 value belongs to cluster 1 then final_cluster_label = "Champions"
Comment: something like above...here by variable value, I meam log transformed values.
Comment: since number of variables is low, we write a if else logic.. we have 5 variables (5 1d clustering)..
Comment: This is my related post on the same topic - https://stats.stackexchange.com/questions/576980/multiple-univariate-clustering-vs-single-multivariate-clustering
Answer: If I understand correctly, you're basically asking: "If I apply a log-transform to my data and run a clustering analysis on the transformed data, are the conclusions/patterns valid for the original data?"
Maybe someone can provide a formal theoretical/mathematical answer, but from a practical point of view, what you're describing is quite common and generally considered valid.
You should be a bit careful when you do this (especially in settings like regression - if you do regression on log-values, don't forget that the coefficients are for log-values and you have to do some transformation to get the raw value interpretation).
In your case, though, I think this kind of approach is perfectly fine. But you should think about the problem from a "common sense" standpoint and understand why your clustering might be better/different with the log-transformed values than with the original values, and whether that makes sense in your context (presumably, the log "squeezes" your skewed distribution, making sparse points closer together and therefore easier to cluster).
Comment: Thanks. When log squeezes the raw data (so its able to find patterns because points are squeezed), how can that be applicable for their raw data (which can far from each other)?
Comment: for ex : if I say cluster 1's limit is from 800 to 804? Is that correct? (Though the cluster label was actually found using log values)
Comment: @TheGreat I'm not sure it's really correct to define the "limits" of a cluster (unless the algorithm somehow explicitly provides that, which I'm not sure really exists). I think you can only really say that points 1, 5, 12, 15 etc. belong to cluster 1.
Comment: Sorry, may I know which points are you  referring as `1,5,12 and 15`? because I don;t have such points in my sample above?
Comment: Additionally, can I know why not the limits be inferred? because, we agree that these points/customers belong to cluster 1. Now, I investigate each of these clusters (using their customer ids) and find out the variable (original variable - unstandardized) minimum value for ex: 10, and max limit for the same variable is 120. In this can't we consider that any customers with a variable value of this limit, would fall into cluster 1
Comment: Because if we tell to a business user that this cluster is created on salary value limit of 1.3 to 3.3 (which are standardized/log transformed values). This wouldn;t make sense to them. Whereas if we could get the corresponding original/raw value using the customer id, then it would make more sense to the business. Isn't it?
Comment: can you also given me an example scenario if possible to illustrate why this may not work?
Comment: @TheGreat Sorry, "points 1, 5, 12 and 15" are not from your data, that was just a random example of the output of some clustering.
Comment: @TheGreat I think this depends on your algorithm (whether you can use such ranges). I had never heard of Jenks Natural Breaks classification (if that's what you're using), and for that algorithm, it might actually be possible to do what you're saying. However, the basic reason is this: You're saying if cluster 1's biggest salary value is 3.3, that means that the max boundary for this cluster is 3.3 . However, this isn't necessarily the case - what if you add a new point to your data with salary 3.31 - are you sure this point will be classified in a different cluster?
Comment: @TheGreat If it's put in the same cluster, suddenly your range is now 3.31, not 3.3 . Then you get a new point that's 3.35 - are you sure that won't be put in this cluster? I hope you understand what I'm illustrating. Especially in the multivariate case, generally there aren't "hard" boundaries - when you add new data, it also changes the distribution, which might change your clustering. If you keep adding data, you'll find 'boundaries" change, new clusters might appear etc. Because of this, I don't think it really makes sense to put boundaries on your clusters.
Comment: @TheGreat Imagine you have two clusters - cluster A has max salary at 3.3 and cluster B has MIN salary at 5.1 . So, where is the boundary? You might say boundary for cluster A is at 3.3 , but how do you know? The boundary could be anywhere between 3.3 and 5.1, maybe you just don't have any data points in that range to demonstrate the "actual" boundary. Maybe the "true boundary" is at 4.2 , but you just don't know. Yet at the same time, if you start adding points at 4.2, maybe there'll eventually be a cluster C from 3.9-4.2 . I hope this shows why conceptually I don't think this makes sense.
Comment: Yes. Correct So, everytime, we hve new data points, shohldn't we rerun the cluster? And get new cluster limits? The limit that we see right now is applicable only for this execution...once I get the clustering oytput, i investigate them and find out this characteristics.. but later after 2 months, if I get new 1000 points, tgen I will rerun clustering. Which will hve new limits later on..So, am I right to understand this way?
Comment: In addition to tge above, my qiestion  mainly on raw values and standardized values. As you might know for kmeans clustering, we are expected to stabdardize our input data etc. So, lets take I standardize my salary variable..and cluster 1 has salary value from 1.1 to 3.4..but when I communicate the characteristocs of this cluster 1, i cannot tell them this cluster 1 has salary range (with this data run) of 1.1 to 3.4..it may not make semse to them. So, they my question is,instead of 1.1, can I say raw value whoch is 100 usd to 3.4 (whoch is 300 usd) the range of salary value for this cluster?
Comment: @TheGreat Yes, I would re-run the clustering when you have significant updates of data. However, my point was to explain that saying "Cluster A's boundary is at 3.1" just doesn't make sense because you don't know where the exact boundary/cutoff is. The most you can say is "We can see that the salaries of all members of cluster B are from 1.1 to 2.0 " . But that doesn't mean that the cluster is *defined* by those values
Comment: @TheGreat Yes, that's exactly correct. You standardize the values, then you run clustering, then to give salary ranges for the clusters, you simply undo the transformation you used to standardize them. So if the cluster has salaries from 1.1 to 3.4, you convert that back to, for example, 100 usd to 300 usd and report that


What does one need to teach Mathematics in American schools with a BSc Mathematics degree?

Question: I'm graduating with a math degree next year (BSc Maths - more theoretical than applied) from an African university, and am going to the US next year to visit a friend for a few months. However, I'd like to do something to make a little money while he's at work/school - particularly in Maths.
What can I do with this qualification in the US?
Could I teach high school kids or primary kids? What am I qualified to do? I'd really rather not end up waiting tables at McDonalds or whatever it is that people end up having to do..
Also I want to figure out, during my time there, exactly what I want to do with my maths education and I am really interested in learning what schooling systems work/don't work, all over the world.
Would I need to do additional studying to gain access to this career?
Comment: Note that while you are visiting on a tourist visa (which is my assumption), you cannot seek employment opportunities. Even if you get a job interview, you __have__ to exit and re-enter the US. You can't work (even at McDonalds) unless you have a work visa (and they won't issue you a work visa to for McDonalds). You could tutor students, as that is an underground market in most countries.
Comment: This might be a good resource to look at: http://education.uky.edu/AcadServ/content/50-states-certification-requirements.
Comment: A degree in mathematics generally cannot be used to teach primary (below middle school) because the math they learn in those grades is very sample, and the kids don't have a specialized "math teacher" yet in those early grades
Comment: Thank you, @CameronWilliams. That was very helpful!
Comment: Thank you very much @CalvinLin. I'm usually very clueless about all this visa business...
Answer: Almost universally, to teach in a public school (and virtually all private schools) one needs to obtain teaching certification within the state one wants to teach, and almost universally, this requires successful completion of a teacher education program. At many Universities, you can earn "post-bachelor" certification in math (secondary level), provided you have a bachelor's degree in math, and/or have completed a degree with the required math one usually completes if earning a degree to teach math at the secondary school level. These post-bachelor certification programs typically run $1 - 2$ years, largely comprised of education focused coursework and courses in teaching methodology, in addition to a semester of full-time, supervised "student teaching". (Some two-year programs also include a combination of "teacher-certification" requirements, and the coursework required to earn, additionally, a master's degree in teaching.)
To teach at an elementary school typically requires successful completion of a bachelor's degree in Education.
What I'd suggest is looking into tutoring math, and/or working in the standardized  test-preparation industry (tutoring and preparing students for the SAT, ACT, Praxis tests, for example). Those sorts of jobs do not necessarily require teaching certification.
Comment: Well, you make it sound like you have to wait two years to start teaching. I know in my state first you land the teaching job then you complete the coursework during your first year teaching, so effectively the coursework isn't a barrier to entry. And when it comes to math, it's not hard to find "stressed" districts.
Comment: Even so, @TBrendle, the OP has said that he'll be in the US for "a few months". That would severely limit the prospect of "landing a job." I looked into pursuing teaching after having earned a Master's degree in two different disciplines, including math, and the shortest program I came across was a year-long certification program that was pretty intense.
Comment: No. Many states have an "Alternative Route to Certification" that doesn't require additional coursework.
Comment: That's rare, @TBrendle, and it would be even more unlikely to qualify for an alternative route if one has earned one's bachelor's degree outside the US, with a very very short time of residency in the US. Perhaps some states are more lenient than others, but in Wisconsin, such alternative routes require simultaneous coursework, and are available only in "stressed" school districts. (This is also true of Minnesota, Missouri, Michigan, etc.)
Comment: There was an "urban fellows" program I looked into where you started from the get-go teaching (supervised by a school mentor) at a very modest stipend, for one school year, full-time 8 - 4:00 p.m. at a "financially stressed school-district" and then attended college-level classes between 4:30 p.m. and 8:30 p.m. During the following summer, one attended classes full-time, then, if successful, one was licensed. One had to commit to teaching minimally 3 years in the same district following licensure.
Comment: @amWhy Thanks for the information! Its all very helpful. (I'm a "she" btw)
Comment: You're welcome, Siyanda!
Comment: @amWhy: I liked this answer and there always exceptions, so there are some that will nitpick these things to no-end! +1


REDCap SQL query filtering on Instances

Question: In a REDCap (EAV table) project each record is a testing site.
Project is divided into two instruments. Instrument 1 will have information on the testing site (Address, DAG associated).
Instrument 2 is a repeatable instrument. Each instance will represent a date where testing is offered at that site.
I am trying to filter out sites using a sub query depending on the date testing is offer, i.e. the site will show on the list when we are between today and the testing date. I manage to filter out a whole record but I do not know how to filter only an instance of the record.
SELECT
  value
FROM redcap_data
WHERE
  project_id = 80
  and
  field_name = 'concat_site_date'
  and
  record in (
    SELECT
      record
    FROM redcap_data
    WHERE
      project_id = 80
      and
      field_name ='date'
      and
      value >= date(now())
  )

This filter out the record that has at least one instance where date >= date(now()) and shows both testing dates. However, one of the two instances is in the past and I wish to hide it. How best to add instances to filter in sql queries?
Comment: Hi @nadha, welcome to Stack Overflow!  I don't understand your question after "I manage to filter out a...".  If you don't get a good response consider (1) editing your post so it has a ([non-PHI](https://www.hhs.gov/answers/hipaa/what-is-phi/index.html) example) of what you want vs what you're getting or (2) asking your REDCap admin to post the question on the private [REDCap Community site](https://community.projectredcap.org/).
Answer: You want to know which sites have testing dates that are in the future, right?
I'd pull out the instance values that meet the time criterion (i.e. are in the future) and join that to a subquery that gives you the site-level data you want (i.e. fields form the non-repeating form):
select instancedata.record, sitefield1, sitefield2, instance, testingdate
from (
  select record, coalesce(instance, 1) as instance, value as testingdate
  from redcap_data
  where project_id=12188
  and field_name='testingdate'
  and now() < value
  group by project_id,record,event_id,instance
) instancedata
inner join (
  select record
  , group_concat(if(field_name='sitefield1',value,null)) as sitefield1
  , group_concat(if(field_name='sitefield2',value,null)) as sitefield2
  from redcap_data
  where project_id=12188
  group by project_id,event_id,record
) recdata
on instancedata.record=recdata.record

REDCap SQL query filtering on Instances


Duvida Básica variaveis JavaScript

Question: Boa tarde!
Estou com uma duvida de iniciante e infelizmente não estou conseguindo achar um tutorial da linguagem (quem tiver um link por favor coloque nas respostas), eu quero apenas mostrar os conteúdos das variáveis 'ping' e 'connectado' porem não estou conseguindo fazer isso.
/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow
 */

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View, Button} from 'react-native';

import NetInfo from '@react-native-community/netinfo';
import Ping from 'react-native-ping';

var ping, connectado;

export default class App extends Component {

  render() {
    NetInfo.isConnected.fetch().then(isConnected => {
      console.log("Is connected", isConnected);
      connectado = isConnected;
    });

    (async () => {
      try {
        ms = await Ping.start('www.google.com.br',{ timeout: 1000 });
        console.log(ms);
        ping = ms;
      } catch (error) {
        console.log('special code',error.code, error.message);
      }
    })();

    return (
        <View style={styles.container}>
          <Text style={styles.welcome}>MS: {ping}</Text>
          <Text style={styles.welcome}>Connectado: {connectado}</Text>
        </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

Answer: Fala Gustavo. Você precisa colocar isso dentro do state. O React é reativo, então ao mudar o state ele "reage" e renderiza novamente os componentes.
export default class App extends Component {
state = {
  ping: "",
  connectado: "",
};
  render() {
    NetInfo.isConnected.fetch().then(isConnected => {
      console.log("Is connected", isConnected);
      this.setState({ conectado: isConnected });
    });

    (async () => {
      try {
        ms = await Ping.start('www.google.com.br',{ timeout: 1000 });
        console.log(ms);
        this.setState({ ping: ms });
      } catch (error) {
        console.log('special code',error.code, error.message);
      }
    })();

    return (
        <View style={styles.container}>
          <Text style={styles.welcome}>MS: {this.state.ping}</Text>
          <Text style={styles.welcome}>Connectado: {this.state.connectado}</Text>
        </View>
    );
  }
}
...

Talvez eu só colocaria as funções no componentDidMount
Answer: Gustavo, é necessário criar um estado (State), no React-Native. Também tem o React Hooks, que nos permite trabalhar com estados em componentes no formato de função.
state = {
  ping: "",
  connectado: "",
};

Para alterar o State use o setState

Duvida Básica variaveis JavaScript


Solving infinite sums with primes.

Question: Let $p_n$ denote the $n$'th prime number.
How would one go about proving that infinite products like: $$\prod_{k=1}^\infty1 - \frac{1}{(p_k)^2} = \frac{6}{\pi^2}$$
or
$$\prod_{k=1}^\infty\frac{{p_k}^2}{{p_k}^2 - 1} = \frac{\pi^2}{6}$$
are correct?
Is there any way to prove it except by exhaustion?
Comment: @anorton that would be exhausting, but probably not very effective in proving the result.
Comment: How do you mean "by exhaustion"?
Comment: @IgorRivin As in "proof by exhaustion"
Comment: "Proof by exhaustion" consists of trying every case.  That is, you would find every one of the infinite number of primes, and then compute the product, and show that it is equal to that number.  That is probably not what *you* mean, but it is what "proof by exhaustion" means in this case.
Comment: @IgorRivin True--but that's the only remotely possible interpretation of "by exhaustion" I can think of in this instance... ;)
Comment: Four color theorem was proven by exhaustion, using 1936 cases...
Comment: @JohnWO Yes, but how would you even break these products into cases?  You cannot list all the primes...
Comment: @anorton "You cannot list all the primes" - Exactly, thus the question...
Answer: Well,
$$\prod_{k=1}^\infty \frac{1}{1-1/p_k^2} = \prod_{k=1}^\infty \sum_{j=0}^\infty \frac1{p_k^j}.$$ Formally expanding the product of sums, and using the fundamental theorem of arithmetic, you get
$$\sum_{n=1}^\infty \frac1{n^2} = \frac{\pi^2}6.$$
But maybe that is an argument by exhaustion?
Comment: This is **exactly** what I thought of when I saw that exhaustion thing in the OP and comment,  yet the OP liked it, so...
Comment: No, it's perfect! Thank you so much!
Answer: As @doppz mentioned in the comments, the way to prove these identities is to realize the requested product is requesting the value of $\zeta(2)$.  Then, you can use any of the multitude of proofs that $\zeta(2) = \frac{\pi^2}{6}$.
For example proofs: http://math.cmu.edu/~bwsulliv/MathGradTalkZeta2.pdf
Comment: That was not the question...
Comment: @JohnWO Um... yes it was, but perhaps I didn't explain every step.  Igor's answer shows that the second product in your question can be changed into a sum.  That sum happens to be $\zeta(2)$.  Thus, if you can prove the value of $\zeta(2)$, you have proven the value of the product.
Comment: "Igor's answer shows that the product can be changed into a sum." Which answered my question...
Comment: @JohnWO You asked how to *prove*, not how to change into a sum.  I agree Igor answered the question (by giving one proof), but I answered with an additional, general outline of another approach, which is a bit more "Googleable" if you want more information about the problems.
Answer: In general we have the following Euler formula
$$\zeta(s) = \sum_{n=1}^\infty\frac{1}{n^s} = \prod_{p \text{ prime}} \frac{1}{1-p^{-s}}$$
and since $\zeta(2)=\frac{\pi^2}{6}$ , see this wiki
so we get the result.

Solving infinite sums with primes.


Find the number of ways that 5 boys and 6 girls can stand in a row if no boy stands next to another boy

Question: The question is:

Find the number of ways that 5 boys and 6 girls can stand in a row if no boy stands next to another boy

In which the correct model would be:
$$\square g \square g \square g \square g \square g \square g\square$$
Therefore:
$$6!\cdot 7P5=1\space 814\space 400$$
However, I noticed that if you take one of the girls away (let's label her as $^a$), we have:
$$^a\square^ag^a\square^ag^a\square^ag^a\square^ag^a\square^ag^a\square^a$$
We see that no matter where girl$^a$ stands, the boys will still not stand next to each others:
$$5!\cdot6P5\cdot12=1\space036\space800$$
Update
Also, if we are to swap it around:
$$\square b \square b \square b \square b \square b \square$$
We now have:
$$5!\cdot6!=86\space 400 $$
What is wrong with the last two logic?
Thank you.
Comment: I don't understand your question. The question is not how many ways there are to arrange 5 boys and 6 girls in a line such that no boy is next to another boy. It's about an error in your reasoning, which uses a notation that I don't know what means.
Answer: Your first computation is correct.
The latter two computations undercount because they start off by taking one of the girls away. Let's say they always take girl #1 away.
Now if we number the girls, those solutions can contain as an example $1 2 b 3 b 4 b 5 b 6b$. But they can not contain $1b2b34b5b6b$. We've lost the ability to have two girls next to eachother without involving girl #1.
Comment: Now, I know the last two undercount somehow but I can't see why  '$1b2b3b4b5b6b$' is not possible in the second case?
Comment: @HuyTranVan You misread. I said $1b2b34b5b6b$. There is no $b$ between $3$ and $4$.
Comment: I wanted to know why $12b3b4b5b6b$ is possible but then $1b2b34b5b6b$ (no b between 3 and 4) is not counted in the second case. According to you when one of the girls stands next to another, the question changed?
Answer: In the last one you're obviously leaving some out, which could be gotten by swapping some of bg combinations in either sum.
In the second, also, you're not counting certain arrangements,  which could be gotten by,  again, swapping at least some bg combinations.
The right one is the first.


Linear Algebra basis. What does it mean?

Question: Could someone check if my understanding is correct for:
(c) find a basis from vectors that span a subspace,
(d) find a basis for a column space of a matrix,
(e) find a basis for the row space of a matrix,
(f) find a basis for a nullspace of a matrix,
My attempt at understanding:
c) is basically like column space? Let A be a m x n matrix, Let H be the RREF of A so A ~ H. According to the pivots of H the basis would be {Ac1,...,Acn} (Depending on where the pivots are on H) (c means column)
d) Same as c?
e) Let A be a m x n matrix, Let H be the RREF of A so A ~ H. According to where there are nonzero values in the rows of H we can just let the basis be {Hr1, Hr2,...,Hrn} (r means row)
f) Is this like the homogeneous space? If the matrix has a Ax = 0 solution what the values of coefficients would also be the null set?
Another question: How do I check my basis for rowspace / column space because the answers arent unique?
Answer: Much of what you say is correct.
(c) You can think about a subspace formed from a set of vectors without worrying about them being the image of a linear map. Given a set of vectors $V = \{v_1,v_2,\ldots,v_p\}$, a basis for the subspace spanned by them is a collection of linearly independent vectors in $V$. For example if the first $q$ are linearly independent, then the subspace generated by $V$ would have a basis $\{v_1,v_2,\ldots,v_q\}$. You can find the linearly independent vectors of $V$ by forming the matrix $A = [v_1 | v_2|\ldots |v_p]$ with the column vectors of $V$ and row-reducing. The nonzero pivots give the linearly independent vectors.
(d and e) - I think your understanding is good. It is possible to consider it more abstractly if you want but what you wrote down is correct.
(f) The nullspace of a matrix $A$ is the space $N = \{x \in \mathbb{R}^n | Ax = 0\}$. This is the set of vectors which map to $0$ under the action of $A$. This can be found by row-reducing the augmented matrix $[A|0]$. Depending on whether or not you want to know what it is or how to compute it, the following question may prove helpful: How to solve this to find the Null Space.
You can check your bases by seeing if they do what you claim they do. For the column space, can every column vector in your matrix be written as a linear combination of the vectors you wrote down as the basis? Is this also true for the rows of your matrix and the basis for the row space?
Comment: could you give an example for :  "You can check your bases by seeing if they do what you claim they do. For the column space, can every column vector in your matrix be written as a linear combination of the vectors you wrote down as the basis? Is this also true for the rows of your matrix and the basis for the row space?"?
Comment: Consider the matrix

$$A = \begin{pmatrix} 2 & 1 & 1\\ 4 & 2 & 2 \\ 1 & 0 & 0 \\ 3 & 1 & 1\end{pmatrix}.$$ The row space of $A$ is spanned by the vectors $\{v_1,v_2\} = \{ (2,1,1), (1,0,0)\}$. Find the constants $c_1$ and $c_2$ such that $(2,1,1) = c_1 v_1 + c_2 v_2$. Then find the constants $d_1, d_2$ such that $(4,2,2) = d_1 v_1 + d_2 v_2$, then find $e_1, e_2$ such that $(1,0,0) = e_1 v_1 + e_2 v_2$ and the constants $f_1,f_2$ such that $(3,1,1) = f_1 v_1 + f_2 v_2$.
Comment: Okay i get it thx
Answer: (C) Put your vectors as the rows of a matrix.  Use Gauss-Jordan to get it into RREF.  Then the nonzero rows will be a basis.
(D) Find the RREF.  Take note of the columns with pivots in them.  Then the columns of your original matrix corresponding to those same pivot-columns in your RREF will be a basis.
(E) Find the RREF.  The nonzero rows will be a basis.
(F) Let's look at an example so that it'll hopefully be more clear.  Take this RREF matrix $$\begin{bmatrix} 1 & 0 & 0 & 1 & -2 \\ 0 & 0 & 1 & 0 & 5 \\ 0 & 0 & 0 & 0 & 0 \end{bmatrix}$$

First just count the columns without pivots.  That's the number of vectors you should have in your basis.  In this case that's three.
Create one vector for each of the columns of your RREF without a pivot in it (that'll be the 2nd, 4th, and 5th columns in this case).  It should have the same number of rows as your matrix has columns.  Fill it with all 0's: $\begin{bmatrix} 0 \\ 0 \\ 0 \\ 0 \\ 0\end{bmatrix}$, $\begin{bmatrix} 0 \\ 0 \\ 0 \\ 0 \\ 0\end{bmatrix}$, $\begin{bmatrix} 0 \\ 0 \\ 0 \\ 0 \\ 0\end{bmatrix}$.
Add a 1 in to each vector in the row corresponding to the column number of the nonpivot column: $\begin{bmatrix} 0 \\ 1 \\ 0 \\ 0 \\ 0\end{bmatrix}$, $\begin{bmatrix} 0 \\ 0 \\ 0 \\ 1 \\ 0\end{bmatrix}$, $\begin{bmatrix} 0 \\ 0 \\ 0 \\ 0 \\ 1\end{bmatrix}$.
For any nonzero number in the nonpivot columns of your RREF, add the negative of that number to the row with the corresponding pivot column number.  So for instance, that $5$ in the RREF becomes $-5$ and goes in the vector we're constructing that's associated with the last column.  And it'll go in the 3rd row because the row that $5$ is in is the one with the pivot in the 3rd column (does that make sense?).  Doing the same for the $1$ and $-2$, we get: $\begin{bmatrix} 0 \\ 1 \\ 0 \\ 0 \\ 0\end{bmatrix}$, $\begin{bmatrix} -1 \\ 0 \\ 0 \\ 1 \\ 0\end{bmatrix}$, $\begin{bmatrix} 2 \\ 0 \\ -5 \\ 0 \\ 1\end{bmatrix}$.

Those are then the basis vectors.


Fatal error: Cannot redeclare class

Question: I am trying to convert a module from D7 to D8. I had parts of it working earlier but after some code edits i now get:
Fatal error: Cannot redeclare class Drupal\delete_fields\DeleteFields in E:\www\drupal8\sites\all\modules\delete_fields\src\Form\DeleteFields.php on line 128

when i try to do a cache clear.
128 is the last line of the file and i have searched entire project and the only place DeleteFields shows up is in my module's class file:
namespace Drupal\delete_fields;

/**
 * Defines a class to build a listing of fields.
 *
 * @see \Drupal\field\Entity\Field
 * @see field_ui_entity_info()
 */
class DeleteFields extends ConfigEntityListBuilder
{

My guess is the class is getting cached somewhere and there is a bug in Drupal which is not letting this get cleared.
I tried drush rr but it seems like this is no longer available (i get command not found); even though it is listed in drush docs.
Answer: Given your question's source code and namespace, the class code you pasted is located in delete_fields/src/DeleteFields.php.
When you compare this with the error message, you will find that you have another DeleteFields class in delete_fields/src/Form.
Despite the possible different sub-namespaces, this doesn't work in Drupal, as it autoloads all classes of enabled modules into the \Drupal namespace and having two loaded classes with the same name within a namespace is not possible in PHP. Therefore your class names should be unique.
Rename your form class to DeleteFieldsForm or DeleteFieldsSettingsForm, or whatever name describes its purpose and reason for existence best.
Comment: It's not exact that it doesn't work in Drupal: It doesn't work in PHP. In all the files that PHP loads, there cannot be two classes with the same name. There is a work-around to that, but it cannot be used in all the cases.
Comment: @kiamlaluno: Ok, maybe not expressed well enough. I edited my answer to reflect your concerns. But: Same names are possible. Just not within the same namespace. ;)
Comment: I didn't mean it was wrong. I only wanted to be sure users who read this don't think Drupal is a special case for handling PHP classes/interfaces.
Comment: No, the only file that existed when this error occurred was in delete_fields/src/Form. I uninstalled the module and reinstalled and the issue resolved itself. Hence the bottom part of my question - this doesn't have to do with my code it has to do with how Drupal caches classes and doesn't properly clean them up. I am not sure if there is a drush command to clean this up. Drush docs stats thee is a RR command but i have also seen this has been replaced with CR (which did not fix the issue).
Comment: Ok, glad you found the cache-rebuild command for Drush. So additionally keep the above advice in mind. This is why it is best practice also to let your form class names end on `Form`. And if you state that it was the only class present at that time, check your namespace. For your forms it should be `Drupal\delete_fields\Forms`. The `Drupal\delete_fields` namespace applies to custom classes placed within your `delete_fields/src` folder only.
Comment: Correction: The namespace must be `Drupal\delete_fields\Form`. Had an `s` too much in my above comment. (Still early here.)

Fatal error: Cannot redeclare class


Game in browser. Where process events: in frontend or in backend?

Question: I develop simple game for browsers. In this game main is shuffling quadrates (5x5).
When user press some buttons (WASD or arrow buttons) state of quadrates is changing.
I try to choose between frontend (javascript) and backend(java) for processing key events.

Frontend (Javascript)
In this case key events will be processed in client browser. So it will be fast.
But, will it safe? Can user somehow change my JS code/file on his own???
Backend (Java)
In this case key events will be processed on server.
Will it fast enough? In this case I will transfer data via ajax request.

I will be glad to hear which approach is better and why? Which approach is the most common?
PS. I know Java better than Javascript
Answer: If you decide on the frontend, your user can of course change the JavaScript and manipulate it in any way they want. If this is absolutely not wanted by you you can either do it in your backend, or, what I would suggest in a case like this, do it on both sides. Meaning frontend for the user-experience and also on the backend via ajax call. The result can be displayed immediately from the frontend code, but the definitive result comes from the server. The logic to implement such a behavior is not simple, but may be worth it. Usually such an approach is used when the user can change resources for other users as well as for themselves. Here the immediate response in the frontend gives the user a good UI-Experience, while the server can still reject conflicting edits. In your case you just need some validation code.
But if this is just a game, why not do it only frontend? Do you care if a user manipulates this? Or is there a server side component anyway?
Answer: The short answer is that if you're worried about the security of your code the best way to completely avoid that is to write it in backend.  Here's a full rundown of some of the other things you probably should take into consideration:
Speed:
Like you identified in your post, front-end code will almost always be faster.  This is in large part because network requests take time to get a response.  If you decide to go with a backend implementation you can try to make it faster by reducing the size of your packets, but unless you start looking into professional hosting you'll be limited by your host internet connection speed and you'll always be limited by your client's connection speed.
Server Load:
Another thing to consider is how powerful your server is.  If it has to make all the decisions of whether a particular move is legal or not, will it be able to keep up?  For a small project this won't be too big of an issue, but it would be good practice to start keeping this in your mind.  As long as your game logic isn't massive then having it all in the front-end code would help lower the demands placed on your server and network connection.
Security:
Like I said above, implementing all your logic server-side is the best way to keep your logic code out of the hands of would-be hackers, but there's a bit more to the story here.  How motivated would someone be to cheat on your game?  Is there any reward or high-score status they would want to get?  If your game is multiplayer or there is any sort of reward for winning (other than just a "you win" screen or some cool animation/sounds) then I would implement it server-side.  If not then I don't see a reason to be worried about code security, because there's a far lessened incentive to cheat.  Also if there's no high-score list or any other way that one player could know the outcome of another player's game, then what effect would their cheating have on you or on the other players?  In most cases: nothing.  The cheater can feel good about their being "smart" or whatever it is they want, they may even brag to their friends about it, but in most cases it doesn't matter to the integrity of your game as a whole because they didn't actually play it: they hacked it.  They lose the experience of winning a game in favor of the experience of hacking code.  Your average player wouldn't try this, and a lot of those who do won't continue to find pleasure in it because there is no longer a point to playing.
Overall:
Either way would probably be fine for a small game.  Ultimately it's up to you; make a decision based on what your personal goals are with this project.  But there is one more thing to be said . . . Your options are not merely limited to server-side or client-side.  You could always try to implement both.  This would not be a project for the faint-of-heart beginner, and it would probably involve some careful planning and dedicated debugging, but it would give great advantages in a lot of areas.  You could have client-side logic that instantly responds to your user's actions and provides the speed of not relying solely on network requests, but at the same time have it send asynchronous requests to the server to validate every user action.  At the end of the game the server compares its simulated result with the client's result, and if they match (and no rules were broken) the game is valid and can be scores.  You'd get good speed, and good security.  Is there any downside to this?  Other than the fact that it would be more difficult and involved to implement it would also produce more load for your server, as each move would have to be validated along with the end result.  But if you have a good enough server, this likely is the "best of both worlds" solution.


Selecting children without a certain index (Jquery)

Question: I want to select some children without a certain index like using eq() in reverse.
I also need to be able to pass the index number as a variable .
Answer: $("#foo").children().not(":eq(2)");

This selects all children of foo except the 3rd one (eq() uses Javascript's 0-based indexing).
Example.
To pass a variable to it:
var num = "2";
$("#foo").children().not(":eq(" + num + ")").css("background-color", "red");

Example
Comment: Thanks , How would I pass it a variable ?
Comment: What do you mean? The number in the `eq()` will be a variable?


Can I use Lasso before Random Forest?

Question: I have a small dataset with 160 obs. and >50 candidate predictors, some of which are correlated.
I would like to fit  a classification model for prediction, and to identify the best set of predictors.
I was thinking to use a Lasso Regression for feature elimination and then fit a random forest with the selected covariates.
Would this be legitimate, or should I use the random forest directly?
Do you think that some other classification algorithm would be more suitable (eg boosting trees?)
Thank you.
Answer: The boruta algorithm is a feature selection method that is based on random forest. The reason you might care is that lasso is a linear model, so anything that matters for your outcome that isn't linear in the parameters under estimation is at risk of getting eliminated. An example: What are disadvantages of using the lasso for variable selection for regression? In other words, the disadvantage to lasso for feature selection of a nonlinear model is that the lasso assumes a more restrictive set of assumptions than the random forest.
Additionally, one might wonder why you care about feature selection at all. Random forest has a kind of feature selection built in, in the sense that it selects the best features to split on when building trees (but this isn't foolproof).
In the particular case you outline, where you have a small number of observations overall, a random forest is probably not the right tool. Overfitting is a very real risk. Regularized regression might be the best you can do.

Can I use Lasso before Random Forest?


How do you cause a program to wait for a set amount of time?

Question: How do you make a program that waits for 3 seconds?
I've heard about this:
obj.wait()

But I'm just wondering about the syntax and what to import?
Comment: `obj.wait()` is to wait until another thread calls `notify()`.  Nothing directly to do with "waiting for X amount of time".
Answer: This will put your code to sleep for 3 seconds:
try{
Thread.sleep(3000);
} catch(InterruptedException e) {
 Thread.currentThread().interrupt();
}

Answer: You have to import `

java.util.concurrent.TimeUnit

`and the you can run:
TimeUnit.SECONDS.sleep(100);


A correct mixed model for longitudinal data

Question: This is a rather basic question and there are many similar questions here on CV. However, I could not find answers to my specific three questions below. My apologies, if I may have overseen something.
Quite often, I see the following experimental setup: "we have measured weight over time in a number of mice, and we want to know whether there exists a difference between the genotypes, i.e. between knockout (KO) and wild type (WT)."
Usually, I think of a simple linear mixed effects model with a random intercept for each individual. Or, if they suspect different time trends, a random slope as well, which often might be a bit too much due to the rather small data sets. Further, I point them towards nlme since they won't stop asking for p-values ...
However, I'm afraid this model is actually too simplified. My concerns are:

Since my weight at one day depends on my weight of the day before, do I need to take some kind of autocorrelation into account? Is maybe some kind of ARIMA more appropriate?
A colleague of mine told me to always use the measurement at baseline time as covariate. While I see that this makes sense in a standard regression model, I'm wondering whether this is required when I already have a random intercept which is supposed to cover differences at baseline?
What to do if lineary does not seem to hold at all? For example, due to a strong peak in the middle with equally strong descend afterwards. Again, a colleague modelled time with restricted cubic splines (rcs) to account for this, but I'm afraid it may be an overkill to make inference from a mixed effects model in which each individual has its own rcs ... however, I do not have a better suggestion.

Just as illustration, an example for the first two questions:
library(dplyr)
library(nlme)
library(ggplot2)

# simulate some data to get started
set.seed(16)
n=10
baseline <- c(rnorm(n=n,mean=20,sd=1),rnorm(n=n,mean=23,sd=1))
data <- data.frame("weight"=baseline, time=0, "Genotype"=c(rep("WT",n), rep("KO",n)), "ID"=1:(2*n))
for(i in 1:20){
  next.week <- baseline + rnorm(n=20,mean=rnorm(1),sd=3)
  new.data <- data.frame("weight"=next.week, time=i, "Genotype"=c(rep("WT",n), rep("KO",n)), "ID"=1:(2*n))
  data <- rbind(data, new.data)
  baseline <- next.week
}

# plotting the data
data %>% group_by(time, Genotype) %>%
  summarise(mean.weight = mean(weight, na.rm=T)) %>%
  ggplot(., aes(x=time, y=mean.weight, colour=Genotype)) +
  geom_line(aes(y = mean.weight, x=time)) +
  theme_bw() + scale_y_continuous(limits = c(15,40)) +
  xlab("Time") + ylab("Weight in g")

The data often looks a bit messy and not perfectly linear, although the assumption of linearity does not seem completely off since one would expect a more or less linear increase of weight over time in general.
# a simple random intercept model
lme.model <- lme(weight ~ Genotype + time, random = ~ 1|ID, data=data)
summary(lme.model)

Random effects:
 Formula: ~1 | ID
        (Intercept) Residual
StdDev:    9.203191   6.1417

Fixed effects: weight ~ Genotype + time
                Value Std.Error  DF   t-value p-value
(Intercept) 22.958956  2.982354 399  7.698268  0.0000
GenotypeWT  -4.040438  4.159205  18 -0.971445  0.3442
time         0.245306  0.049491 399  4.956556  0.0000

We see a significant increase of weight over time, and no significant difference between the genotypes (due to the small sample size in this simulated example). From the random effects, we could calculate the ICC and see that there is a lot of variance captured by the random intercept, so we should not use a model without a random intercept.
Answer:
Since my weight at one day depends on my weight of the day before, do I need to take some kind of autocorrelation into account? Is maybe some kind of ARIMA more appropriate?

An AR(1) structure is often a very good idea in these types of models, of the data support such a model, although in practice there is often insufficient data to estimate the AR parameter. So I would suggest trying it.

A colleague of mine told me to always use the measurement at baseline time as covariate. While I see that this makes sense in a standard regression model, I'm wondering whether this is required when I already have a random intercept which is supposed to cover differences at baseline?

Yes, your intuition is correct here.

What to do if lineary does not seem to hold at all? For example, due to a strong peak in the middle with equally strong descend afterwards. Again, a colleague modelled time with restricted cubic splines (rcs) to account for this, but I'm afraid it may be an overkill to make inference from a mixed effects model in which each individual has its own rcs ... however, I do not have a better suggestion.

Looking at your plot, I don't see a big problem with linearity, but if you do have a clear non-linear pattern ("trong peak in the middle with equally strong descend afterwards"), then a good starting point is to simply add a quadratic term - you may also want to consider centering the data in this case.
Comment: Ahh, I can feel your pain. I have been there. The struggle is real ! :)
Comment: Thank you very much. Regarding the AR(1) structure, would the following model be correct? `lme(weight ~ Genotype + time, random = ~ 1|ID, correlation=corCAR1() data=data)`
I am actually impressed by how much the p-value of time is changed by adding the AR(1) structure.
Comment: Missing a comma of course after `corCAR1()`. And regarding the non-linearity, I must admit my example was formulated in an unfortunate way because it clearly indicated a quadratic relation. My apologies. If you see data in which there really isn't any simple transformation to achieve linearity, what would you recommend then? Or is it maybe a better option then to stick away from modeling the complete longitudinal data, and rather focus on specific (predefined) time points without consideration of the course over time?
Comment: Yes, that looks right, although questions about software are off topic here. As for linearity, I'm not suggesting a transformation (although that is another option), rather the addition of nonlinear terms.  Splines would be a logical next step. Also, try not to be too concerned with p values.
Comment: Thanks! I always try to convince my clinical colleagues not to use p-values but it's an extremely difficult discussion. Often I achieve that they at least add confidence intervals to their papers :)


problem in enabling Commerce 2CheckOut module

Question: I have added 2CheckOut module to drupal 7, then go to drupal admin and modules page to enable it, the module tell me it needs  Commerce_paymen as required module "Requires: Commerce_payment (missing)".
So, how can I find this module "Commerce_payment" I searched it out but without luck.
this what i got in modules page:
Commerce 2CheckOut  7.x-1.x-dev Payment using 2checkout
Requires: Commerce_payment (missing), Rules (disabled), Entity tokens (enabled), Entity API (enabled)
Answer: The commerce_payment module is bundled with Drupal Commerce. Please feel free to contact 2Checkout tech support at firstname.lastname@example.com for assistance with your integration.


Thanking for answers

Question: I know other SE sites actively discourage this, how is it for Econ SE? I frequently have to suppress the urge to write another thank you comment
Answer: Per rules in our help center this is not an appropriate use of comments. As our help center states:

When shouldn't I comment?

...

Compliments which do not add new information ("+1, great answer!"); instead, upvote it and pay it forward;

See also this Meta thread for more info on the subject.
This being said, if you feel strong urge to thank someone in comments and you do so you will not get into any serious problems from us moderators, unless it is somehow disruptive (e.g. long never-ending thank you messages), then the worst thing that could happen is that the comment will get deleted if someone flags it.

Thanking for answers


Diminuir o tamanho de um arquivo(imagem) no PHP

Question: Possuo uma página que faz upload de imagens. Porém existe um tamanho máximo, um número de KB máximos que ao ser ultrapassado não deixam terminar o upload. Gostaria de saber se há alguma forma de diminuir o tamanho das imagens(Em KB) ao serem escolhidas.
Quando a imagem for selecionada, antes de transferir o arquivo para a pasta do meu servidor, gostaria que fosse reduzido o tamanho da imagem, pois ao exceder certo tamanho de KBytes, o upload não é finalizado.
 Obrigado pela atenção.
Comment: Eu acredito que esta resposta que fiz deve resolver o seu problema: http://pt.stackoverflow.com/a/63018/3635
Answer: Não é possível manipular a imagem que está no lado cliente, dentro do contexto da pergunta.
Plugin
Uma opção seria instalar algum plugin no browser do cliente e então por meio desse plugin seria executado um software de edição de imagem previamente instalado no cliente. Isso é uma opção que pode ser viável para um públcio alvo pequeno e restrito. Para um ambiente público é inviável. Para ambos os casos, o custo é alto pois precisa desenvolver um plugin ou talvez consiga encontrar uma solução paga.
Software no cliente
Numa segunda opção instalar um software no cliente somente para fazer o upload das imagens. E nesse software faria a comunicação com algum editor de imagens ou dentro do proprio software possuir tal recurso. Assim com a primeira opção, pode ser dsenvolvido do zero ou comprar soluções prontas.
Processo manual
Outra opção é o usuário usar um software editor de imagens para tornar a imagem dentro do padrão exigido. Ou seja, um processo manual.
Esse é o mais usado por ser simples e sem custos adicionais.
Comment: No google, pesquise por "plugin image editor client side" e termos semelhantes a isso.
Comment: Entendi.. Você poderia me enviar algum exemplo dessa primeira opção que você citou? Seria de grande ajuda. Obrigado.
Answer: É possivel sim manipular a imagem no lado cliente e reduzi-la antes de enviar para o servidor, você usar JavaScript com File API + Canvas API (HTML5) e Ajax (o Ajax é opicional, você pode salvar os dados da imagem gerada em um <input> ou <textarea> oculto também), como descrevi nesta resposta https://pt.stackoverflow.com/a/63018/3635

    <input type="file" value="" id="input">
    <input type="button" value="Enviar" id="send">

O PHP deve ser algo como (https://pt.stackoverflow.com/a/50982/3635):
<?php
define('PASTA_UPLOAD', '/home/user/projeto/data');

if (isset($_GET['filename']) && $_SERVER['REQUEST_METHOD'] === 'POST') {
    $cl = (int) $_SERVER['CONTENT_LENGTH'];

    $tmpFile = tempnam(sys_get_temp_dir(), '~upload-');

    $file = fopen($tmpFile, 'w');
    $fh   = fopen('php://input', 'r');
    if ($file && $fh) {
        $data = '';
        while (FALSE === feof($fh)) {
            $data .= fgets($fh, 256);
        }
        fwrite($file, base64_decode($data));
    }

    if ($file) {
        fclose($file);
    }

    if ($fh) {
        fclose($fh);
    }

    echo 'OK';
    copy($tmpFile, PASTA_UPLOAD . '/' . $_GET['filename']);
} else {
    echo 'Requisição inválida';
}


Interpreting Random v. Fixed-effect Difference-in-Difference equation (+Stata version)

Question: When I compute:
xtset panelid year
xtreg y i.treat##i.post i.year

Versus:
xtset panelid year
xtreg y i.treat##i.post i.year, fe

1. What is the actual difference-in-differences (DiD) regression I'm calculating for each?
2. The coefficient 1.treat should be for the treatment group-- do I need a control group dummy?
3. In the fixed effect model, 1.treat is omitted due to collinearity, which is to be expected because it is being absorbed by the FE. Correct?
Answer: The difference-in-differences equation you are running is
$$
y_{it} = \alpha_i + \gamma \text{post}_t + \beta (treat_i \cdot post_t) + \epsilon_{it}
$$
where $\alpha_i$ are the individual fixed effects (which absorb the treatment dummy), $\text{post}_t$ is the post-treatment time dummy, and $\beta$ estimates your difference-in-differences parameter. When you type in Stata i.treat##i.post then this includes a dummy for i1.treat, i1.post, and their interaction 1.treat#1.post which is equivalent to generating the corresponding dummies by hand and including them - though again the treatment indicator is unnecessary because it is going to be in $\alpha_i$.
You don't need a control dummy because treatment is either 0 or 1, hence a dummy for the control group would be perfectly collinear with the treatment dummy.
Your second specification is correct. Difference-in-differences is essentially a fixed effects regression using the within group variation in the outcomes pre- and post the intervention/treatment. In case you have multiple periods, you can accommodate for this by replace the post dummy with time dummies.
Comment: If I have the same treatment that occurs for two groups (a & b), and I calculate the beta using the fixed effect DiD equations here (second specification) for both groups combined (average treatment effect for a&b) versus a control, and then I calculate the beta or treatment effect of just one of the groups, group b, versus the control; can the difference in betas be interpreted as the multiplier on the treatment effect of remaining group a? i.e., combined treatment effect = .30. Treatment effect for group b = .32. So, the treatment is 1.06x as effective, on average, for group b?
Comment: Not sure how this relates to the original question but if a and b receive the same treatment then what makes them different? You can run your regressions separately for the sample using group and the control group, and for group b and the control group. If you run the difference-in-differences regression for the whole sample, the $\beta$ will be a weighted average of the treatment effects in the two groups.
Comment: @EconoQ did this answer your question?
Comment: It did, thank you. I do have multiple periods, so I should replace the post dummy with time dummies for t-2, t-1, t0, t+1, t+2

This would make the equation:
`y=α+β(treat⋅post)+ γpost + calendarFE + groupFE + ϵ`

Which in Stata is:
`xtset panelid time`
`xtreg y i.treat##i.post i.time i.calendaryear, fe`

I will get coefficients for each time period -- how is this typically reported?
Comment: I also added the follow-up here: [http://stats.stackexchange.com/questions/268692/calendar-year-fe-versus-treatment-year-fe-in-difference-in-differences]