When I double-click on the publish content tab on a node, I end up on /user instead

Question: This is on a pretty complex site so I have no idea where to start looking for the cause of this behavior.
I have installed the Publish Content module, which creates a little "publish" or "unpublish" tab along with "view" and "edit" on each node, for users that have permission to publish or unpublish content.
When I click on "publish" or "unpublish" once, the expected behavior follows. But if I double-click on that tab (and only that tab – others don't do this), then instead of reloading the node, it takes me to my user page.
I tried disabling the devel module but that didn't help. It happens on multiple content types, and as multiple different users with different roles, including user 1.
I have a couple of rule-based redirects in place, but they're only for specific roles and they're only triggered on particular actions (such as login). And none of them redirect to the user's own page.
I have no idea where to start debugging this.
Comment: You have the link that the Publish tab points to? For example: `/publishcontent/publish/node/1234` You get this link, paste it once on your browser, this will publish the unpublished node, then paste it again. This might be the problem, i.e. when you try to publish an already published node, you get redirected to your user page. Just guessing.
Comment: You are right. I don't know why it would redirect to the user page rather than to the node page, though. Thanks.
Comment: That was a pretty wild guess but I guessed right :D
Answer: It's because when you double click, most probable case is the second click will try to use an already used token.
Or it could also be due to the fact that the second click might be translated as trying to publish an already published node.
Comment: Technically I believe it's because when I double-click, it will try to re-use an already-used token, but it's the same effect.
Comment: That's probably also another good case. I'll update the answer.


Advanced javascript image slider

Question: I've been googling away for a while now and realized I need help.
I'm developing something for a friend of mine in PHP, but he wants a really difficult to program (to me) image slider on his website. I'm Dutch and the example I'm gonna show you is of a Dutch website: http://www.iboma.com/index.asp. I'm trying to make a similar animation but in javascript/jQuery.
Only instead of the sample wood being shown I need to fade in an image and corresponding link. The text might be an extra. It's not needed.
Standard sliders aren't good enough so I need custom code. No fancy animations, crossfade would be ideal. After the image has faded in I need an image+link faded in overlaying the background image, also while maintaining the overlaying logo also shown on the example website.
I'd like to feed the image slider images from PHP either with JSON array or pre-loaded on the website (probably search engine friendlier).
var photos = [  { image: "http://localhost/foobar.png", logo: "http://localhost/foobar.png", link: "http://www.stackoverflow.com/" },
{ image: "http://localhost/foobar.png", logo: "http://localhost/foobar.png", link: "http://www.google.com/" },
{ image: "http://localhost/foobar.png", logo: "http://localhost/foobar.png", link: "http://www.yahoo.com/" }];

Could you guys help me on my way developing such an animation?
Starting off:
I'm trying to figure out how to have an efficiënt infinte loop stacking the various events I have to trigger to animate the thing.
First off, do I use normal javascript or am I better off using jQuery regarding load and speed?
Secondly, what's a good implementation of such code for the infinite loop?
Third, how do I trigger various events after each other not disrupting the flow of the animation.
Fourth, while animation the background-image change how do I make sure the overlaying logo doesn't disappear behind the animation? This is one of my main issues. Using fadeIn and fadeOut the logo disappears behind my images.
Suggestions?
Comment: _"Could you guys help me develop such an animation?"_ not with a payment, so no, this is not the site.
Comment: I meant it more like: Could you guys help me on my way?
I've never written code that loops infinitely. All the stuff I find I don't understand.
Comment: Define help please... There isn't a question, so you want us to do it for you. my simple logic.
Comment: This isn't a question, its a work request.
Answer:
I've been googling away for a while now and I'm starting to realize I
  really need some help.

The above line does not make sense to me.
I typed in "image slider" into google, and here is the very first result.
http://nivo.dev7studios.com/
Which looks very much like what you are asking.
There's free code of an image slider, that comes with links, texts, pretty much everything you need.
Answer: OK, as far as you already know, that nobody will write it for you ;) I can recommend you slideshow which will be simply able to do it - http://jquery.malsup.com/cycle/
$('#slideshow').cycle({
   fx: 'fade',
   after:   onAfter
});

function onAfter(curr, next, opts, fwd) {
     //here you make animation with text and small image
};

It's so simple, that all you have to do, is make fun with all animations. Try it. It's no need to write your own slideshow code for this what you need.
Answer: Finally got what i needed.
Sófka you helped me on my way. Thx for that. It was what i needed.
Oh and I used the Orbit image slider for this.
The following code works for me:
(function($) {
        $(document).ready(function() {
            $('#featured').orbit({
                animation: 'fade',                  // fade, horizontal-slide, vertical-slide, horizontal-push
                animationSpeed: 500,                // how fast animtions are
                timer: true,                        // true or false to have the timer
                advanceSpeed: 6000,                 // if timer is enabled, time between transitions
                pauseOnHover: true,                 // if you hover pauses the slider
                startClockOnMouseOut: true,         // if clock should start on MouseOut
                startClockOnMouseOutAfter: 0,       // how long after MouseOut should the timer start again
                directionalNav: false,              // manual advancing directional navs
                captions: false,                    // do you want captions?
                captionAnimation: 'fade',           // fade, slideOpen, none
                captionAnimationSpeed: 800,         // if so how quickly should they animate in
                bullets: false,                     // true or false to activate the bullet navigation
                bulletThumbs: false,                // thumbnails for the bullets
                bulletThumbLocation: '',            // location from this file where thumbs will be
                beforeSlideChange: function() {
                    $('#featured a, #featured span').fadeOut(400, function() {
                        $('#featured a, #featured span').remove();
                    });

                    $('#featured img').each(function(index) {
                        if ($('#featured img').eq(index).css("z-index") == 3) {
                            var className = $('#featured img').eq(index).attr("class");
                            $('#featured img')
                                .eq($('#featured img').index(this))
                                .after($('#featured-images .' + className)
                                    .clone()
                                    .css("z-index","4")
                                );
                            $('#featured a.' + className)
                                .css("top",$("#featured img." + className).height() - $("#featured img." + className).next("a").height() - 50)
                                .css("left",$("#featured img." + className).width() - $("#featured img." + className).next("a").width() - 50)
                                .fadeIn(1000, function() {
                                    $('#featured a.' + className + ' img').fadeIn(1500);
                                    $('#featured img')
                                        .eq($('#featured img').index(this))
                                        .after($('#featured-text .' + className)
                                            .clone()
                                            .css({"z-index":"4",
                                            "top":"0px",
                                            "display":"block",
                                            "opacity":0,
                                            "position":"absolute",
                                            "left":$("#featured img." + className).width() - $("#featured img." + className).next("a").width() - 70 - $('#featured-text .' + className).width()})
                                        );
                                    $('#featured span.' + className).animate({
                                        opacity: 1,
                                        top: $("#featured img." + className).height() - $("#featured img." + className).next("a").height() - 25
                                    }, 1000);
                                });

                        }
                    });
                }
            });
        });
    }(jQuery));

Needed to add some event hooks to the code libary myself but that wasn't too hard.
HTML:

                Dit is een test

Comment: you should really give sofika the check

Advanced javascript image slider


EC2 instance running apache tomcat not accessible from outside

Question: I have a  64-bit amazon linux instance in aws. I am running apache tomcat6 running on that machine.
sudo fuser -v -n tcp 8080
                     USER        PID ACCESS COMMAND
8080/tcp:            tomcat     2492 F.... java

If I do
curl ec2-54-2xx-1xx-1xx.us-2.compute.amazonaws.com:8080

I do get the following web page.

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <title>Apache Tomcat</title>

      Apache Tomcat

                  Administration

                    Status

                    Tomcat Manager

                  Documentation

                    Release Notes

                    Change Log

                    Tomcat Documentation

                  Tomcat Online

                    Home Page

                    FAQ

                    Bug Database

                    Users Mailing List

                    Developers Mailing List

                    IRC

                  Miscellaneous

                    Servlets Examples

                    JSP Examples

                    Specifications

If you're seeing this page via a web browser, it means you've setup Tomcat successfully. Congratulations!

As you may have guessed by now, this is the default Tomcat home page. It can be found on the local filesystem at:

$CATALINA_HOME/webapps/ROOT/index.html

where "$CATALINA_HOME" is the root of the Tomcat installation directory. If you're seeing this page, and you don't think you should be, then you're either a user who has arrived at new installation of Tomcat, or you're an administrator who hasn't got his/her setup quite right. Providing the latter is the case, please refer to the Tomcat Documentation for more detailed setup and administration information than is found in the INSTALL file.

NOTE: For security reasons, using the manager webapp
            is restricted to users with certain roles such as "manager-gui".
            Users are defined in $CATALINA_HOME/conf/tomcat-users.xml.

Included with this release are a host of sample Servlets and JSPs (with associated source code), extensive documentation, and an introductory guide to developing web applications.

Tomcat mailing lists are available at the Tomcat project web site:

tomcat-users for general questions related to configuring and using Tomcat

tomcat-dev for developers working on Tomcat

Thanks for using Tomcat!

            Copyright © 1999-2014 Apache Software Foundation

            All Rights Reserved

However If I try to access the webserver from a web browser from an external machine I get the following error.
Google Chrome's connection attempt to ec2-54-2xx-1xx-1xx.us-2.compute.amazonaws.com was rejected. The website may be down, or your network may not be properly configured. Currently I have configured my security groups (both inbound and outbound) to allow all traffic from any machine.

I am trying to access this from my company network.Is it possible that my company firewall is blocking it? If yes why would that be?
In the security settings I have the following values for Inbound and Outbound.
Type           Protocol         Port Range           Source
All traffic      All               All             0.0.0.0/0

I am still not able to access the web server from outside.
Answer: Login to the AWS console and goto the Security Groups section
Here, in the Inbound add a new rule named Custom TCP Rule and enter the custom port range 8080.
Now goto enter the {ipaddress}:8080 in the browser
Answer: By default, AWS instances start off without any ports accessable from the internet. You need to alter your VPC's security group settings. To do this, select your instance in the EC2 list. In the frame under where you select your instance, you'll see a bunch of random data about the instance, including the "Security Group" - click on this to modify that instances firewall rules. Please be aware that depending on how your VPCs are setup, that altering these rules may affect more than one instance.
It's pretty obvious once you know about it's existence and can poke around.
Comment: Please see updated security settings above
Comment: It's definitely possibly that a corporate admin blocked it since 8080 is frequently used for non-squid http proxies. Can you try port-scanning your ec2 vm from https://w3dt.net/tools/portscan ? That should tell you if the problem is your internet connection.


FileDepot upload file sizes restriction

Question: I am using the filedepot module. With this module I am able to upload files. I want to allocate space for "uploading files" to individual registered users basing on user roles.
Is it possible? How do I do it?
Answer: I don't believe that is possible with filedepot without patching the module. However it is readily available within the IMCE module if you are able to change modules.  From the project page:

Basic file operations: upload, delete
Image(jpg, png, gif) operations: resize, create thumbnails, preview
Support for private file system
Configurable limits for user roles: file size per upload, directory
  quota, file extensions, and image dimensions
Personal or shared folders for users
Permissions per directory
Ftp-like directory navigation
File sorting by name, size, dimensions, date
Tabbed interface for file operations
Keyboard shortcuts(up, down, insert(or enter), delete, home, end,
  ctrl+A, R(esize), T(humbnails), U(pload)).
Built-in support for inline image/file insertion into textareas
Multiple file selection(using ctrl or shift)
Ajax file operations
Themable layout using tpl files

For a better idea of what you are looking at, take a look at a screenshot of filedepot.
Everything you need is already built in to IMCE.

FileDepot upload file sizes restriction


Why are p-values from the binomial test in R non-monotonic in trials?

Question: The R function binom.test performs an exact binomial test. In trying to understand how many trails would be necessary to reject the null given zero successes, I plotted the p-value as follows
p <- 1/16
n <- seq(2,200)
pval <- lapply(n, function(x) binom.test(0,x,p)$p.value)
plot(n, pval)

I was surprised to see that the p-value did not decrease monotonically with increasing trials. What is the reason for this and is it accurate?
Comment: It is because the binomial random variable is discrete. See my paper in the American Statistician coauthored with Christine Liu.  "The Saw-toothed Behavior of Power vs Sample Size and Software Solutions: Single Binomial Proportions using Exact Methods. American Statistician (2002) Vol. 56 pp 149-155.
Comment: I took @StephanKolassa advice. I realize that the result is counter-intuitive and it may take more detailed accounts in the references to get a clear picture.
Answer: The reason is because the binomial random variable is discrete. My paper in The American Statistician [1] explains this and gives other references. Think about the fact that the planned type 1 error cannot be achieved exactly for all values of the sample size n.
[1]  Michael R Chernick & Christine Y Liu (2012). The Saw-Toothed Behavior of Power Versus Sample Size and Software Solutions. The American Statistician, 56:2, 149-155, DOI: 10.1198/000313002317572835


Show that this function has derivative only in $x=0$

Question: Show that $f(x)= \begin{cases} x^2, \ x \in \mathbb{Q} \\ 0, \ x \not\in \mathbb{Q} \end{cases}$
has derivative only in $x=0$.
Answer: First show that the function is not even continuous at $x \neq 0$.
Consider $x \neq 0$.
If $x \in \mathbb{Q}$, consider the sequence $r_n = x - \dfrac{\sqrt{2}}n$. Show that even though $r_n \to x$, $$f(r_n) \to 0 \neq x^2 = f(x)$$
If $x \in \mathbb{R} \backslash \mathbb{Q}$, consider the sequence $q_n = \dfrac{\lfloor 10^n x \rfloor}{10^n}$. Show that even though $q_n \to x$, $$f(q_n) \to x^2 \neq 0 = f(x)$$
Hence, we can hope for a derivative to exist only at $x=0$.
At $x=0$, we have $$\dfrac{f(h) - f(0)}h = \begin{cases} h & \text{if }h \in \mathbb{Q}\\ 0 & \text{else}\end{cases}$$
and hence $$\left \vert \dfrac{f(h) - f(0)}h\right \vert \leq \vert h \vert$$ to conclude that the derivative exists and is $0$.
Answer: Since
$$\frac{f(x)}{x}=
\begin{cases}
x & \text{ if $x\in\mathbb{Q}$} \\
0 & \text{ if $x\notin\mathbb{Q}$}
\end{cases}$$
So $\lim_{x\to 0} f(x)/x=0$, and $f$ is differentiable at 0. And it is easily shown that $f$ is discontinous at for all $x\in\mathbb{R}-\{0\}$.
Comment: Does $f$ has limit at any $x\in\mathbb R$?Thanks
Comment: @Babak Sorouh $f$ has a limit only at $x=0$.


Javascript change name of file on download

Question: The files on the server are renamed with a random id and the original name is stored in the database when they are uploaded using php for security reasons (client's request).  For the files to be downloaded I was just using a page listing the files using this

    name_from_db

and that worked well but now the client would like a dialogue box asking for the name of the downloader before the file download begins...

   name_from_db
   <input type="hidden" name="file_path" value="path_to_file/temp_name" >

$('#results_table_div').on('click', '.image_list', function(){
    var file_id = $(this).attr('id');
    var path = $(this).siblings('input[name="file_path"]').val();
    swal({
      title: 'Please enter you name',
      input: 'text',
      type: 'question',
      showCancelButton: true,
      inputValidator: function (value) {
        return new Promise(function (resolve, reject) {
          if (value) {
            resolve()
          } else {
            reject('Please enter your name to download the file')
          }
        })
      }
    }).then(function (result) {
        jQuery.ajax({
        type: "POST",
        url: "ajax/log_download.php",
        dataType: "json",
        data: 'file_id='+file_id+'&name='+result,
        cache: false,
        success: function(response) {
            if(response.success === 'yes'){
                var orig_name = (response.name_from_db);
                window.location.href=path;
            }
        }
    });//end ajax
});

This works well except the file is downloaded using the random id for the name rather than the original name stored in the database.  How can I get the file to be downloaded using the orig_name value?
Comment: change the `download` attribute to the new name?
Comment: @charlietfl I had considered that but I'm having a brain stutter and can't work out how to do it
Answer: Ok, sussed it...
in the ajax success I did this...
if(response.result === 'success'){
    var orig_name = (response.name_from_db);
    var downloadLink = document.createElement('a');
    downloadLink.href = path;
    downloadLink.download = orig_name;
    document.body.appendChild(downloadLink);
    downloadLink.click();
}

This triggers the file download and changes the file name


ng-repeat too powerful, need to dumb it down

Question: Newbie here, trying to figure this out.
I have an object literal that i want to have filtered down and shown in a textarea.
This fiddle shows what I want to achieve
http://jsfiddle.net/DrmVC/3/

I have {{friends.length}} friends. They are:

    [{{$index + 1}}] {{friend.name}} who is {{friend.age}} years old.

  <textarea> [1] John who is 25 years old.
[2] Mary who is 28 years old. </textarea>

Ng-repeat doesn't work for textareas. From my limited understanding, ng-repeat is instantiating new scope every time it repeats, while the textarea just wants some string to show -- how can i get it this string?
Maybe a working example of angularForeach filtering down an object literal (like ng-repeat does) and outputting to a string i can bind to like {{foreachStringOutput}}
Thanks in advance.
Comment: Do you want to do a ng-repeat inside a textbox?
Comment: Inside of a textarea. Textbox would probably be the same thing though, i imagine.
Comment: I just added an example, Your question wasn't entirely clear but I assume you were just trying to populate a value in the textbox based on the array which you would really want to do with the use of a controller to define the value.  Check it out.
Answer: I think what you are going to want to do is create a function to produce the text you want and bind that to your text area.  In the example below friendText() is looping over the array and producing the text.
Here is a quick demo: http://plnkr.co/edit/fmiSg3uJChy00bgffe9I?p=preview
var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {

  $scope.friends = [{name:'John', age:25}, {name:'Mary', age:28}];

  $scope.friendText = function() {
    var text ='';
    angular.forEach($scope.friends, function(value, key){
     text = text + '[' + (key+1) + '] ' + value.name + ' is ' + value.age + ' years old.'
    });

    return text;

  }

});

and the markup (the ng-app is defined at the html area and left off here but is on the demo link)

    I have {{friends.length}} friends. They are:

        [{{$index + 1}}] {{friend.name}} who is {{friend.age}} years old.

      <textarea> {{friendText()}}</textarea>

Comment: Thank you very much! I might have confused a little by asking for a filtered ngrepeat, but didn't show that in the jsfiddle. Within the foreach I imagine I can use if statements to further define what I want to return as text. E.g. if i wanted only friends <26. Thanks again for your answer, I appreciate it very much :)


Move Comment form/render out of node.tpl.php (into separate region)

Question: I would like to render the <code>$content['comments']</code> part of my nodes outside the node.tpl.php file, and have the comments (form and all) rendered in a separate region of my theme.
I've noticed Commentsblock does something along these lines, but requires a view, and doesn't seem too complete.
Could I do some preprocessing to get the entire comments renderable array into the page.tpl.php template if I'm on a certain node type page, rather than rendering the array inside node.tpl.php? (Something along the lines of this comment).
Also, wouldn't simply not rendering the $content['comments'], and then getting the comments via some other method (like a View, how Commentsblock works) cause Drupal to load all the comments twice (worse performance, etc.)?
Answer: You should be able to just hide() the comments in the node template and then create a block that calls menu_get_object() and then prints them there...
Comment: I've decided to go this route, documenting my method here: http://www.midwesternmac.com/blogs/jeff-geerling/moving-comments-block-drupal-7
Comment: Would this still create a bit of overhead, though? (See the last paragraph in my question).
Comment: I _think_ the render would only happen once since it _should_ all be cached :)
Answer: I would suggest you use Panels for this.

Activate the Panels module
Enable the node override, "node_view"
Add a variant, go through the wizard. Most important part in the wizard is creating a selection rule with "Node:type" being the node type you want to do this on. Or any other selection rule that matches your critera.
Click the little cog in a suitable region for the node content -> Node -> Node Content.
Click the little cog in a suitable region for the comment form -> Node -> Comment form.

Other resources: Video introduction to Page Manager and Panels
Comment: Panels is complicated, that is true. But the the "overhead" statement, I've never seen substantiated. Quite the contrary, we use it on essentially every one of our sites, and many of them are high traffic. Add that it also offers nice caching. To each his own though.
Comment: I've seen this solution presented elsewhere, but I'm definitely not going to be using Panels on this site; it needs to be lean and mean, as it has almost 100% authenticated traffic. Page Manager/Panels is a lot of overhead, and a lot of complicated mess, imo, unless you truly use it for everything.
Comment: Giving an upvote because it's definitely a solution; just not my favorite (b/c of the complexity, mostly).


Marginal Probability Density

Question: if $A=1$, $y \sim N(1,\sigma^2)$
if $A=2$, $y \sim N(2,\sigma^2)$
$Pr(A=1)=0.5$
$Pr(A=2)=0.5$
Given $\sigma = 2$, I'm trying to find the formula for the marginal probability density function for y.
What I've done
$$f(y | A, \sigma ^2) = f(y | A, 4) = \frac{1}{2\sqrt{2\pi}}exp(-\frac{(y-A)^2}{8})$$
is this the marginal probability density function for y?
If not.
What I've tried next but not sure
According to Wikipedia
$$p_Y(y) = \int_X p_{X, Y}(x,y) dy = \int_X p_{X|Y}p_X(x)dx = E_X[p_{X|Y}(x|y)]$$
However, I'm not sure how to apply this formula on $f(y|A, \sigma^2)$
Thanks in advance!
Answer: $f(y)=\frac{1}{2\sqrt{2\pi \sigma^2}}(e^-\frac{(1-y)^2}{2\sigma^2}+e^-\frac{(2-y)^2}{2\sigma^2})$
You are overthinking.
Comment: Thanks so much for the answer! However, can this formula be used to calculate for example $Pr(A=1 | y=1) = \frac{Pr(y=1 | A=1)Pr(A=1)}{Pr(y=1} =  \frac{0.5 * Pr(y=1 | A=1)}{Pr(y=1}$? Then I need to use the formula by #heropup right?
Comment: Let $g(A,y)=e^{-\frac{(A-y)^2}{2\sigma^2}}$, then $Pr(A=1|y=1)=\frac{g(1,1)}{g(1,1)+g(2,1)}$.
Answer: $$f_Y(y) = \sum_{a = 1}^2 f_{Y \mid A}(y \mid A = a) \Pr[A = a],$$ where $$f_{Y \mid A}(y \mid A = a) = \frac{1}{\sqrt{2\pi \sigma}} e^{-(y - a)^2/(2\sigma^2)}, \quad a \in \{1, 2\},$$ and $$\Pr[A = a] = \begin{cases}1/2, & a \in \{1, 2\} \\ 0, & \text{otherwise} \end{cases}.$$

Marginal Probability Density


Subversion: blame for past revision

Question: I love Subversion's blame operation (which shows for each line in the source file the commit that last changed it). Alas, we recently reformatted our entire source code, and now blame just shows that every line was last modified by this commit.
Can I somehow instruct blame to show me when the line was last changed before that commit? Bonus points if I can do it without leaving eclipse (we use subclipse).
I tried switching the working copy to a revision prior to the reformat, and do "show annotation" there, but that still shows every line last modified by the reformat ...
Answer: God I am slow today. At the bottom of the dialog that "Show Annotate" pops up is the setting "To revision". Setting that to the last revision prior to the reformat performs as expected, i.e. blame reports the commits before the reformat.
I guess since I never before needed to change the defaults in that dialog, I automatically skipped it ...
Answer: You can open history of the file and then you can show annotation on specific revision using context menu in history view.
Comment: That doesn't change the "To Revision" setting in the "Show annotation" dialog. Unless I change that setting, the newer commit is still reported (I agree it's not intuitive, but it's the way my subclipse works...)

Subversion: blame for past revision


a unique limit point in a set

Question: Give an example of a sequence $\{a_n\}_{n\ge 1}$ which has no limit, but such that the set of real numbers $E=\{a_n:n\ge 1\}$ has a unique limit point.
Any help to this problem will be appreciated.
Answer: HINT: Try a sequence whose odd-numbered terms are all the same and whose even-numbered terms converge to some other number.
Answer: A different hint: Begin with any convergent sequence, and ,,spoil'' it in sufficiently many places to ensure it does not converge, but take care not to create additional limit points that way. You could for instance take the convergent sequence $c_n := \frac{1}{n}$ and define:
$$
 a_n = \begin{cases}
 n & \text{if $n$ is a power of 2} \\
 \frac{1}{n} & \text{if $n$ is not a power of $2$}
\end{cases}
$$

a unique limit point in a set


How do I theme the user register page?

Question: In Drupal 7, we can use a template file like page--user--register.tpl.php, or we can register our own template file.
How we can do it in Drupal 8?
Answer: This is similar in D8. Copy page.html.twig from core or base theme and put it into the template directory of your theme.
Rename it like this:
<code>page--user--register.html.twig
</code>
Comment: How do I access the user account fields in this template. I want to access the email, picture and some other fields that I have added to the Drupal user account ?

How do I theme the user register page?


Correct approach to testing for homogeneity of variance across ~2000 conditional distributions?

Question: I'm not well-versed in statistics so apologies in advance for struggling to ask this question the right way.
Essentially, I have 1836 timeseries of stock prices. For each of these timeseries, I am analyzing eleven separate technical indicators (i.e. applying some sort of filter/transformation to the timeseries, returning either True or False) on each date. If the signal is True, then I throw that day's return into a bucket.
The resulting dataset is a 3-dimensional matrix with an x-axis of 1,836 securities, a y-axis of 24,541 dates, and a z-axis of 11 technical indicators.
Across each technical indicator, and for each security, I want to test homogeneity of variance between the unconditional distribution of returns (any given day), and the distribution of returns contingent upon the technical indicator signaling True for that day.
The complexity of this problem is sort of throwing me off since I don't have any experience with statistics beyond an introductory level. If I test each individual security, I will have 1,836 p-values. This seems like a weird output. I could flip it around and test each 11 technical indicators, but then I still have 2 * 1,836 = 3,672 distributions to worry about.
So far I've looked at F-tests and the related Levene's, Bartlett's, and Brown-Forscythe tests, but I'm not sure how I'd implement them across this dataset. Would greatly appreciate any advice!
Edit:
Here's a slice of the DataFrame for one technical indicator 'MA_a':
<code>container['MA_a']

Out[13]:
10006    {'all': [-0.002045, -0.004098, 0.004115, -0.01...
10030    {'all': [0.002907, 0.0, 0.0, 0.02029, 0.005682...
10049    {'all': ['C', '-0.010135', '0.013652', '-0.016...
10057    {'all': [0.014184, 0.020979, 0.006848999999999...
10078    {'all': [-0.009569, 0.0, -0.009662, 0.029268, ...
10102    {'all': [0.0073170000000000015, 0.016949000000...
10104    {'all': [0.065041, 0.045801999999999995, 0.021...
10107    {'all': [-0.016055, -0.030303, 0.004808, 0.005...
10108    {'all': [-0.012077, -0.043521, 0.0623720000000...
10137    {'all': ['C', '-0.010453', '0.035211', '-0.008...
10138    {'all': [-0.064706, 0.018868, -0.037037, -0.04...
10145    {'all': ['C', '0.000000', '-0.006579', '-0.005...
10147    {'all': [0.005814, 0.00578, 0.011494, -0.01136...
10153    {'all': ['C', '0.002688', '-0.013405', '-0.010...
10161    {'all': [0.0, 0.056995, 0.02451, 0.014354, -0....
10196    {'all': [0.0, 0.035714, -0.034483, 0.0, 0.1428...
10209    {'all': [0.01626, 0.004, 0.007968000000000001,...
10225    {'all': ['C', '0.003293', '0.000000', '-0.0010...
10233    {'all': ['C', '0.004630', '-0.004608', '-0.011...
10241    {'all': ['C', '0.009402', '-0.010161', '-0.010...
10276    {'all': [0.010638, 0.0035090000000000004, -0.0...
10292    {'all': [0.004425, 0.004405, 0.006579000000000...
10299    {'all': [-0.095455, -0.021357, 0.026958, -0.01...
10321    {'all': [0.0, 0.045455, -0.043478, 0.068182, 0...
10324    {'all': [0.013848, 0.005464, 0.016811000000000...
10353    {'all': [0.030709, 0.011459, -0.009063, 0.0, -...
10364    {'all': ['C', '-0.013841', '-0.014912', '-0.01...
10372    {'all': ['C', '0.003272', '0.000000', '0.00000...
10401    {'all': [0.004933, 0.009116, -0.003475, 0.0013...
10436    {'all': [0.007298999999999998, 0.0072459999999...

91650    {'all': ['C', '0.015024', '-0.006540', '-0.037...
91668    {'all': [0.0054670000000000005, 0.004284000000...
91849    {'all': [0.018185, -0.002576, 0.00645699999999...
91883    {'all': [-0.040653, -0.017822, -0.009274, 0.03...
91926    {'all': [-0.00928, -0.011151, 0.013532, 0.0302...
91937    {'all': [-0.06662, -0.051089, -0.035629, -0.03...
92121    {'all': ['C', '-0.030182', '-0.027747', '-0.01...
92156    {'all': ['C', '-0.008984', '-0.019061', '0.002...
92157    {'all': ['C', '-0.007756', '-0.005295', '0.009...
92239    {'all': [0.029039, -0.043172, 0.007373, -0.030...
92293    {'all': ['C', '-0.015368', '-0.036298', '0.016...
92322    {'all': [0.013966999999999999, -0.013871000000...
92402    {'all': [0.006794, -0.001295, -0.016791, 0.017...
92602    {'all': ['C', '0.000000', '0.012455', '-0.0031...
92611    {'all': [-0.01641, -0.013827, -0.001738, 0.001...
92614    {'all': [-0.000442, -0.007809, 0.015296, 0.004...
92618    {'all': [-0.038554000000000005, -0.006266, -0....
92655    {'all': [0.01385, 0.0, -0.027322000000000003, ...
92709    {'all': ['C', '0.050314', '-0.009820', '-0.027...
92772                    {'all': [0.035791], 'signal': []}
92778    {'all': [-0.020438, 0.029663, 0.000244, -0.005...
92890    {'all': [0.018072, 0.019882, 0.004177, -0.0036...
92988    {'all': ['C', '-0.062814', '0.002145', '-0.010...
93002    {'all': [0.017063, -0.007947, 0.025515, -0.005...
93089    {'all': [0.005807, -0.003439, 0.004068, -0.010...
93096    {'all': [-0.0248, -0.016817, -0.02795199999999...
93132    {'all': [-0.030789999999999998, 0.036696, 0.00...
93159    {'all': [-0.037385, 0.009203, -0.002736, 0.017...
93422    {'all': ['C', '-0.009894', '-0.028946', '0.048...
93429    {'all': [0.019218, -0.013241, 0.000256, -0.003...
Name: MA_a, Length: 1830, dtype: object
</code>
Zooming in further, here's a slice of the technical indicator 'MA_a' for one security '10006':
<code>{'all': DATE
 1957-03-01   -0.002045
 1957-03-04   -0.004098
 1957-03-05    0.004115
 1957-03-06   -0.012295
 1957-03-07   -0.008299
 1957-03-08   -0.006276
 1957-03-11   -0.006316
 1957-03-12    0.006356
 1957-03-13    0.012632
 1957-03-14   -0.002079
 1957-03-15    0.000000
 1957-03-18   -0.006250
 1957-03-19    0.023061
 1957-03-20    0.016393
 1957-03-21    0.008065
 1957-03-22   -0.020000
 1957-03-25   -0.004082
 1957-03-26    0.004098
 1957-03-27    0.000000
 1957-03-28    0.004082
 1957-03-29    0.012195
 1957-04-01    0.006024
 1957-04-02    0.001996
 1957-04-03   -0.003984
 1957-04-04    0.008000
 1957-04-05   -0.003968
 1957-04-08   -0.009960
 1957-04-09    0.002012
 1957-04-10   -0.008032
 1957-04-11    0.004049

 1984-05-17   -0.002375
 1984-05-18   -0.002381
 1984-05-21    0.002387
 1984-05-22   -0.002381
 1984-05-23    0.002387
 1984-05-24   -0.002381
 1984-05-25    0.002387
 1984-05-29   -0.002381
 1984-05-30    0.000000
 1984-05-31    0.004773
 1984-06-01    0.007126
 1984-06-04   -0.002358
 1984-06-05    0.004728
 1984-06-06   -0.002353
 1984-06-07    0.000000
 1984-06-08    0.000000
 1984-06-11    0.009434
 1984-06-12   -0.004673
 1984-06-13    0.002347
 1984-06-14    0.000000
 1984-06-15    0.000000
 1984-06-18   -0.002342
 1984-06-19    0.002347
 1984-06-20    0.002342
 1984-06-21   -0.002336
 1984-06-22   -0.011710
 1984-06-25    0.009479
 1984-06-26    0.000000
 1984-06-27    0.011737
 1984-06-28    0.000000
 Name: 10006, Length: 6864, dtype: float64, 'signal': 9024     0.014463
 9025     0.002037
 9026    -0.002033
 9027    -0.002037
 9037     0.006135
 9046     0.012397
 9078    -0.013972
 9079     0.004049
 9080     0.000000
 9081    -0.012097
 9082    -0.004082
 9086     0.008333
 9087     0.001033
 9088     0.001032
 9190    -0.005970
 9191    -0.006006
 9194     0.015528
 9195    -0.009174
 9200    -0.012121
 9239    -0.002899
 9240     0.000000
 9241    -0.005814
 9242     0.005848
 9243     0.000000
 9244     0.000000
 9245     0.000000
 9246     0.000000
 9247     0.002907
 9248     0.000000
 9249     0.002899

 15608   -0.048951
 15609    0.000000
 15610    0.000000
 15611   -0.003676
 15612    0.000000
 15613    0.011070
 15614    0.000000
 15615   -0.007299
 15616   -0.007353
 15620    0.026119
 15621   -0.010909
 15624    0.007407
 15625   -0.003676
 15626    0.014760
 15627   -0.003636
 15702    0.000000
 15703    0.002488
 15704    0.000000
 15705    0.000000
 15706   -0.002481
 15707   -0.002488
 15708    0.000000
 15709    0.004988
 15710   -0.004963
 15711   -0.002494
 15712    0.000000
 15714    0.007595
 15717    0.015228
 15838   -0.011710
 15840    0.000000
 Length: 785, dtype: float64}
</code>
As you can see, there are two subsamples, 'all' and 'signal'.
Comment: Why do you want to do this? What is your goal, your real problem? Looks like the https://en.wikipedia.org/wiki/XY_problem
Comment: For an academic thesis, I am testing the null hypothesis that securities' returns are not affected by my filter rules. (I.e. that the distributions are the same.) I am not concerned with median/mean return since those should theoretically be the same, but rather volatility (variance/stdev) which I believe drops substantially on days where my signals flash True.
Comment: Van YouTube please add extra informatikk AS editor to YouTube Q?
Comment: Could you please ask your question another way? I'm having a hard time understanding what you're asking but I'd be glad to upload whatever you need to YouTube.
Comment: Hmm... I'm quite surprised tosee that comment underwriten by me. Not sure how that come to be. Starting some RussianCollusion investigations right now .. . (and then deleting)
Comment: Good luck with that!
Answer: It depends on what you want to do with the results.
Even if none of the 1,836 raw time series really had different variances from their filtered counterparts, you would find about 5% of your p-values being significant using a standard significance level of 0.05.
I assume you are trying to make money by investing based on your results. In that case, one approach would be to determine how much money you will lose by concluding that the variances are not the same when, in fact, they are. Based on this you can set whatever significance level you want based on how often you can afford to be wrong, that is based on how large of losses you can absorb.
Comment: Benjamini Y, Hochberg Y. Controlling the false discovery rate: A practical and powerful approach to multiple testing. J R Stat Soc 1995, 57, 289-300.
Comment: There are maybe two things you might be interested in. One is saying that overall, across all the time series, you don't have equal variances. This would be related to controlling what is called the family-wise error rate. The other is that you want to say which particular time series don't have equal variances. This would be related to controlling what is called the false discovery rate, and this is a common procedure in genomics.
Here is a website to give you a broad overview:
http://www.biostathandbook.com/multiplecomparisons.html
Comment: Thanks for the answer! Actually this research is for an academic thesis — I am completely indifferent as to whether or not the results generate an actionable trading opportunity. I'll take what you said at face value because the math behind it eludes me, so with that in mind, can you think of another way I can group the data that would allow me to make a statistical conclusion?


How to iterate a RDD and remove the field if it exist in a list using PySpark

Question: I have a list which contains a couple of string values/field names. I also have a Spark RDD, I'd like to iterate the rdd and remove any field name that exists in the list. For example:
field_list = ["name_1", "name_2"]

RDD looks like this:
[Row(field_1=1, field_2=Row(field_3=[Row(field_4=[Row(name_1='apple', name_2='banana', name_3='F'), Row(name_1='tomato', name_2='eggplant', name_3='F')])]))]

I'm not very familiar with RDD, I understand that I can use map() to perform iteration, but how can I add the conditions, if it finds "name_1" or "name_2" which exists in the field_list, then remove the value and the field, so the expected result is a new RDD looks like:
[Row(field_1=1, field_2=Row(field_3=[Row(field_4=[Row(name_3='F'), Row(name_3='F')])]))]

Answer: You could recreate the whole structure, but without fields which you don't need. I'm not sure, maybe there's a better method, but looking at the Row documentation we see that it's limited on methods.
Inputs:
from pyspark.sql import Row
rdd = spark.sparkContext.parallelize([
    Row(field_1=1, field_2=Row(field_3=[Row(field_4=[Row(name_1='apple', name_2='banana', name_3='F'), Row(name_1='tomato', name_2='eggplant', name_3='F')])]))
])
print(rdd.collect())
# [Row(field_1=1, field_2=Row(field_3=[Row(field_4=[Row(name_1='apple', name_2='banana', name_3='F'), Row(name_1='tomato', name_2='eggplant', name_3='F')])]))]

field_list = ["name_1", "name_2"]

Script:
F4 = Row('field_4')
F3 = Row('field_3')
F2 = Row('field_1', 'field_2')
def transform(row):
    f3 = []
    for x in row['field_2']['field_3']:
        f4 = []
        for y in x['field_4']:
            Names = Row(*(set(y.asDict()) - set(field_list)))
            f4.append(Names(*[y[n] for n in Names]))
        f3.append(F4(f4))
    return F2(row['field_1'], F3(f3))

rdd = rdd.map(transform)

print(rdd.collect())
# [Row(field_1=1, field_2=Row(field_3=[Row(field_4=[Row(name_3='F'), Row(name_3='F')])]))]

Comment: thanks @ZygD the issue is that my actual data is much bigger and have more that those four fields, i guess with this solution I'll have to do something manual to include each one of them in the function?...
Comment: That's true. With this solution it would require quite much effort. I'd love to see a better approach too.


Como fazer um método de análise de string funcional?

Question: Estou criando uma aplicação que simula um sistema de biblioteca, e para começar, é necessário efetuar o login inserindo um nome no campo de texto.
Esse é o método que eu criei para analisar o nome (eu quero apenas letras maiúsculas e espaços nessa string):
public boolean nomeConfere(String nome) {
    for(int n = 0; n < nome.length(); n++){
        if(nome.charAt(n) != 'A' || nome.charAt(n) != 'B' || nome.charAt(n) != 'C' ||
                nome.charAt(n) != 'D' || nome.charAt(n) != 'E' || nome.charAt(n) != 'F' ||
                nome.charAt(n) != 'G' || nome.charAt(n) != 'H' || nome.charAt(n) != 'I' ||
                nome.charAt(n) != 'J' || nome.charAt(n) != 'K' || nome.charAt(n) != 'L' ||
                nome.charAt(n) != 'M' || nome.charAt(n) != 'N' || nome.charAt(n) != 'O' ||
                nome.charAt(n) != 'P' || nome.charAt(n) != 'Q' || nome.charAt(n) != 'R' ||
                nome.charAt(n) != 'S' || nome.charAt(n) != 'T' || nome.charAt(n) != 'U' ||
                nome.charAt(n) != 'V' || nome.charAt(n) != 'W' || nome.charAt(n) != 'X' ||
                nome.charAt(n) != 'Y' || nome.charAt(n) != 'Z' || nome.charAt(n) != 'Ç' ||
                nome.charAt(n) != ' '){
            JOptionPane.showMessageDialog(null, "O nome não foi digitado corretamente.", "Erro", JOptionPane.ERROR_MESSAGE);
            return false;

        }
    }
    this.nome = nome;
    return true;
}

Mas ele não está funcionando. Eu testei a aplicação após implementá-lo e coloquei um nome apenas com esses caracteres no campo de texto, mas o JOptionPane de erro que não deveria aparecer, apareceu. Alguma solução?
Comment: Alguma resposta ajudou a resolver o problema e pode sanar dúvidas similares de outros usuários? Caso positivo não esqueça de marcar a resposta como aceita. Pra fazer isso é só clicar no ✓ do lado esquerdo da mesma (abaixo do indicador de up e down votes).
Answer: O problema é que você está usando OR na condição. Ou seja, se o nome for diferente de A OU diferente de B ...
Considere que o nome é ADAO.
"A" != "A": false
"A" != "B": true

A comparação com C não é nem realizada, pois false || true resulta em true.
Você pode resolver isto mudando para AND ao invés de OR. Assim somente se todas as condições forem verdadeiras é que seu bloco será executado.
Se quiser deixar o código mais enxuto você pode usar Regular Expressions para fazer está verificação.
Answer: Utilizando expressão regular o seu código ficará mais simples e fácil de dar manutenção. Ficaria da seguinte forma:
public Boolean nomeConfere(String nome) {
  if (!nome.matches("^[A-Z ]+$")) {
    JOptionPane.showMessageDialog(null, "O nome não foi digitado corretamente.", "Erro", JOptionPane.ERROR_MESSAGE);
    return false;
  }

  return true;
}

A expressão regular acima testa se a String tem caracteres de A à Z maiúsculo ou espaços do inicio ao fim.


How do I use Onclick in a div on mobile devices

Question: I am using an asp repeater to generate a list of items, each row is a new div. When I click on the row,the row to becomes highlighted and a checkbox that is hidden is checked. To do this I have an id on the div that contains the Container.ItemIndex so it looks like:
<div class="cartItemInfo" id="cartItemInfo_<%# Container.ItemIndex %>"  OnClick='<%# "return highlightrow(" + Container.ItemIndex +")" %>'>

The onclick calls this:
function highlightrow(RowNum) {
    //alert(RowNum);
    if ($("input[id$=cbRowSelected_" + RowNum + "]").is(':checked')) {
       // alert('IF');

        $("input[id$=cbRowSelected_" + RowNum + "]").attr("checked", false);
        $("div[id$=cartItemInfo_" + RowNum + "]").each(function () { $(this).css("background-color", "inherit") });

    }
    else {
        //alert('ELSE');
        $("input[id$=cbRowSelected_" + RowNum + "]").attr("checked", "checked");
        $("div[id$=cartItemInfo_" + RowNum + "]").each(function () { $(this).css("background-color", "#8A8A8A") });
    }
}

When clicked, it calls my jquery and looks to see if the check box is checked, and does what I expected it to do. When I try this  on a phone I run into a problem. When the alerts are uncommentd and I touch the areas on in the list it will tell me the correct row number but would not go into either the if or else.
I've read that onclick doesn't work on mobile, and I should you ontouchstart instead. But I haven't been able to get that to even tell me what row number I am in.
If I can provide any more info to help solve my problem I will do my best to do so.
Comment: It's been a little bit since I did WebForms, but shouldn't `OnClick` be `OnClientClick`?
Edit: Nevermind, that's for ASP Server Controls... on the same thought though, does `return highlightrow(" + Container.ItemIndex +")` need to be wrapped in `<%# %>`?
Comment: Do you have an example of `onclick` not working?
Comment: @neilsimp1 yes, other wise it will pass a string of _+Container.ItemIndex+_
Comment: @Soren I do not, is there something you were looking for in particular?
Comment: @JoeW How do you know that you have a problem to solve?  Have a look here; http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/
Comment: @Soren I knew that I had a problem to solve because the function in question was working on all desktops while it did not on any mobile devices. I should have realized the problem wasn't with onclick itself being it would tell me a row number, but we all make mistakes
Answer: The problem was not with onclick, it was the jquery reference. The server that I was on was using https while the reference to the jquery file was http. On mobile devices it would not load the jquery file because of the difference in security. Was able to find the problem after learning of Google Remote Debugging, and gaining access to the developer console.


Limiting distribution of Markov chain; two approximations

Question: Just for simplicity let's look at Markov states with a finite number of states.  So we have a sequence of random variables $(X_t, t\in \mathbb{N})$.  Let's assume that there is a steady state distribution $\pi$ to which the chain converges, i.e. as $X_t \to \pi$ when $t \to +\infty$.
Analysing this definition it can be seen that we look at the distribution of the states $X_\infty=\pi$ (a simplified notation is used here). It is known that this limit distribution of the states does not depend on the initial state.  So if I simulate the chain $N$ times, starting from $x_1=1$ I well obtain realisations $x_1, x_2^{(k)}, x_3^{(k)}, \dots x_\infty^{(k)}$, for $k=1, 2, \dots N$.
The limiting distribution of the chains is the distribution of $x_\infty^{(k)}, k=1,2, \dots N$ as $N\to\infty$.
In MCMC on the other hand, one does not take a very large number $N \to \infty$ of realisations of the chain, but one (or a limited number of) realisation(s) of a very long chain, i.e. e.g. $x_1, x_2^{(1)}, x_3^{(1)}, \dots x_\infty^{(1)}$ and then $\pi$ is approximated by the relative frequencies of the states in this one long run.

So either I take an infinite number of realisations of the chain and
  approximate $\pi$ as the distribution of $x_\infty^{(k)}, k=1,2, \dots
N$, where $N \to \infty$ or I approximate it as the distribution of
  the states in one very long realisation i.e. $x_1, x_2^{(1)}, x_3^{(1)}, \dots x_\infty^{(1)}$.

Is there a theorem that states that the latter also converges to $\pi$ ?
Answer: I think you wrote down the answer to your question in the first line when you said

Let's assume there is a steady state distribution to which the chain
  converges, i.e., $X_t \to \pi$ when $t \to +\infty$.

This is the Ergodic theorem that states that if the Markov chain is aperiodic, irreducible and Harris recurrent, then the chain converges to $\pi$ in total variation sense as $t \to \infty$. This result is irrespective of the starting value of the Markov chain. You can find more info in Meyn and Tweedie.


How to get count of the listview items at Xamarin UITests

Question: Suppose I have Xamarin application and page that show list of specific items.
I want to write UI test that will add some items and than try to count them.
I have next listview:
<ListView x:Name="ListView" Grid.Row="1" ItemsSource="{Binding Items, Mode=TwoWay}" HorizontalOptions="StartAndExpand" VerticalOptions="StartAndExpand"
              BackgroundColor="Transparent" SeparatorVisibility="None" HasUnevenRows="false" Margin="20, 0">
        <ListView.ItemTemplate>
            <DataTemplate>
                    <ViewCell.View>
                        <Grid AutomationId="ListItem" VerticalOptions="FillAndExpand">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="1" />
                                <RowDefinition Height="*" />
                                <RowDefinition Height="1" />
                            </Grid.RowDefinitions>
                            <StackLayout Orientation="Horizontal" HorizontalOptions="StartAndExpand" VerticalOptions="Center" HeightRequest="38" Grid.Row="1">

                            </StackLayout>
                        </Grid>
                    </ViewCell.View>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

And my UI test:
[Test]
    public void CheckForSamplesAdding()
    {
        app.Tap(c => c.Id("PageWithList"));

        // add some items:

        app.EnterText("inputForItem", "item001");
        app.PressEnter();

        Task.Delay(250);

        app.EnterText("inputForItem", "item002");
        app.PressEnter();

        Task.Delay(250);

        app.EnterText("inputForItem", "item003");
        app.PressEnter();

        Task.Delay(250);

        // assert to check count of listView items
    }

I think I have to add automationId to the listview and somehow describe checking of count of items in this listview.. But I can't find any information how to perform it..
Thank you in advance.
Answer: It's easy to count the number of elements displayed on screen with something like that :
app.Query(x => x.Marked("ListView").Child()).Length

if you want to count all the elements you should use a backdoor like in this answer.
An other solution could be to scroll to try to find the element in the list (useful if you want to test more than just the element creation).


What set of notes does a singer typically do compared to the set of notes instruments play in popular music?

Question: Suppose a song is "in C" and a guitar and bass are playing the notes of C major.
If the singer sings only thirds above the instruments and we isolate the voice, we hear a melody in E Phyrgian:
C  D  E  F  G  A  B   Instrumentals: C major sound if C is emphasized
E  F  G  A  B  C  D   Voice?: E Phyrgian sound!

I doubt this happens often, because:

vocal melodies are often hummable and the hummed melodies rarely sound Phyrgian
I'd expect the vocal melody to sound like it's in the same key as the harmony, so the singer probably has to not always sing thirds, but also find a way to emphasize the tonic (at least in general)

Or, if the singer sings the same mod a third above the instruments then most of the notes they sing will clash:
C  D  E  F  G  A  B   Instrumentals: C major sound if C is emphasized
E  F# G# A  B  C# D#  Voice?: E Major sound

So how does choice of set of notes in harmony actually work? I know it's hard to make generalizations, but what are some general ways for the people making music together to do so such that:

the different voices (such as guitar and vocals) are in different frequency bands
the different voices sound like they are in the same key
the different voices have similar modes (probably one won't do minor thirds while the other does major thirds)
there are pleasing intervals within each voice (for example, both a guitar and a voice each get perfect fourths and fifths in their respective melodies)

I'm sure there are multiple ways of satsifying at least several of these loose constraints: what are some popular solutions?
Comment: That's not how vocal melodies are written. In terms of popular ways of writing melodies above chords, all you have to do is study or learn some songs or pieces on whatever genre you are most interested in and each song will demonstrate how melodies interact with harmonies.
Answer: The short answer to your question is: They both use the same notes. If a song is in the key of C, the harmony, instrumental melodic backgrounds and melody will all use notes in the C scale for the most part.
Regarding your example, if a singer sings a C scale from E to E against a C chord it will not sound like E Phrygian but rather like a melody in C that starts and ends on E. Unless you want a dissonant or avant-garde sound you would not sing an E major scale in the key of C, those melody notes would clash against the harmony. Moving up and down a scale is an extremely basic view of what a melody is, a melody is artistic, not mechanical. It combines different rhythms, consecutive notes combined with leaps, changes of direction and other techniques. It does not have to follow the contour of the background and it does tend to emphasize the tonic in general.
My comments are related to diatonic music, or music that does not stray from the notes of the given key. If either a melody or harmony moves outside of the original key then the other will generally do the same to match it. The important thing is that they both do the same thing to maintain a cohesive and consonant sound. For example, if in the key of C is the song goes to a Bb chord, the melody will almost certainly use a Bb note instead of a B natural at that moment.
If you learn about and understand basic harmony you will realize that it is not necessary to think too much about the specific notes different instruments play together. As long as they are playing the same chords or notes that match those chords the end result will generally work pretty well.
Answer: This is being asked under false premises. In a song, everyone plays and sings in the same key. It's never that some play in a key, and others play in a mode - not even a mode of that same parent key.
I think you're asking about harmony between , say, a guitar line and a vox line. Thirds is probably the most common, and easiest to perform. Two voices will be the same. Some of those intervals will be m3, some M3. If we take a very simple example, (in key C) one singer sings CDEF, the other, in 3rds, to harmonise, will song EFGA. So, the 1st harmony is M3, 2nd m3, 3rd m3 and 4th M3. There's no problem doing that, for a lot of folk it comes naturally, and will inevitably sound right. Right because all the notes are diatonic - from the same key (C).
You mention P4 and P5. These would often be the next harmony used. Along with the base note, they will often sound hard - so usually get used as well as the 3rds, for what we call 3-part harmony.CG, DA etc sound quite harsh, so where your notion of 'pleasing' comes from, not sure.
Simplest way to understand what I'm trying to get at is to use the white keys on the piano. Two notes with a white key in between are 3rds m/M, and will always sound harmonious together. Notes with 2 or 3  white keys between won't, but play that 3rd as well and it'll come together.
Answer: If you take a C major melody, and sing a new melody which is a diatonic third higher, does it sound like E Phrygian?  No, really, try it, with a few different melodies.  It is very likely that your new melody will also sound like it's in C major.  Additionally, if you were to have two people sing both melodies at once (the new one and the one it's based on), it will almost surely sound like it's in C major- we generally interpret a set of notes played together as having one tonality based on all the notes we are hearing.  It would not sound like a C major melody played alongside an E Phrygian melody.
Suppose, though, that you did come up with a vocal melody that truly sounded like it was in E Phrygian.  If you sang that alongside a convincingly C major accompaniment, the result would probably sound like C major, because the accompaniment generally has more influence over the perceived tonality of the song than the melody does, and the melody is being heard in the context of the C major harmony.  You shouldn't be worrying about one instrument sounding like it's in a different mode than the rest.
Quite simply, if the song is in key N, then all instruments (including vocals) will primarily use the notes in key N, with few exceptions.


Adding coefficients to obtain interaction effects - can I add standard errors?

Question: I posted this question earlier and am rewriting it in hopes of getting some guidance. I am using a weighted regression (after propensity score matching) to obtain estimates of the effects of a treatment (treat) on an outcome for different income quintiles. I include interactions of the quintiles and the treatment in the equation, as shown below:
<code>regress outcome treat quintile2 quin3 quin4 quin5 treatXquin2 treatXquin3 treatXquin4   treatXquin5
</code>
To obtain the effect of treatment on quintile2 I add the coefficient on treat with the coefficient on treatXquin2. However, what do I do with the standard errors? Can I simply add the standard errors on the two coefficients together? Can I add the t-stats together?
Any advice would be much appreciated. I'm not a stats expert but have a basic understanding of econometrics.
Comment: Please explain why you're not satisfied with suncoolsu's answer to your previous version of the same question http://stats.stackexchange.com/q/3653/449.
Also please indicate what software the code in your question is for, and add an appropriate tag. That way, someone with experience in that software package might be able to suggest explicit code.
Comment: You can edit the question by clicking 'edit' which appears below the tags for your question. If some aspect of suncoolsu's answer is not clear you should ask for clarification via commenting to the answer. There is a 'add comment' link below every answer.
Comment: Sorry for posting a second question. For some reason I can't figure out how to edit my question. I'm new to the site so please bear with me. And thanks again to those who have provided answers so far!
Answer: Your impact of treatment depends updon quintile status. After running the 'regress' command, type, 'lincom treat + treatXquin2' to get the impact of treatment for those in quintile 2. This gives a confidence interval and standard error for this estimate. Note that, as is typical in Stata, using the names of the variables in a post-estimation step refers to the coefficients on those variables, not the variables themselves.
Comment: Thanks so much Charlie. This is very useful. I didn't know about the lincom command so this will save me lots of time. However, if I want to get the impact of treatment on the outcome for quintile 2 should i not add treat + treatXquin2 (rather than adding quintile2 + treatXquin2)?
Comment: Charlie - I'm not sure I understand why you multiply treatXquin2 by 1 for yes. If that is the case then how do I obtain the estimate for the untreated (i.e. treat==0)? It doesn't seem to make sense that I would multiply treatXquin2 by 0. Any help would be much appreciated!
Comment: Sorry, I misread your question. Add treat + treatXquin2 for the treatment impact for those in quintile 2. I have updated my answer.
Comment: Thanks Charlie - Is it possible to get the estimates separately for treated and untreated? The lincom command only gives me the difference between treated and untreated.
Comment: Estimates of what? Using a version of the lincom statement above, you get the treatment effect for each quintile (the estimate of the treatment effect for the first quintile is just the coefficient on treatment).
Answer: I am quite sure it's fine to just add the standard errors on the two coefficients together. I can then obtain the t-stats by dividing the sum of the coefficients by the sum of the standard errors. If anyone disagrees please let me know.
Comment: I did not downvote but you should see the other thread as to why this is not ok.
Comment: Sorry, I downvoted this answer to indicate I disagreed, but that was an inapproriate and lazy way of doing so, for which I apologise. I'm afraid it's now too late to undo my vote, however - the software won't allow it, for reasons I don't entirely understand. I've just upvoted your question in an attempt to balance things out, though I realise two wrongs don't make a right.
Comment: If you want to add the standard errors together you need to square them then sum them then sqrt them. i.e. s.e(X+Y)=  sqrt(s.e(x)^2 + s.e(Y)^2)


How can I create correct breadcrumbs when using Views arguments?

Question: I have a custom CCK content type for blog posts and I'm using Views to display the posts in an archived list. I use two arguments—Node: Created year and Node: Created month—to generate the monthly and yearly archive pages, which allows me to build URLs like http://example.com/blog/2011 and http://example.com/blog/2011/08. This is pretty standard stuff and really easy to do with Views.
While the actual archive list pages are working just fine, the breadcrumbs that views generates are not. With the URL http://example.com/blog/2011/08, instead of creating this kind of trail (which seems correct):
Titles = Home > Blog  > 2011       > August
URLs   = /      /blog   /blog/2011   /blog/2011/08

… Views generates this incorrect breadcrumb trail:
Titles = Home > Blog  > 2011  > August
URLs   = /      /blog   /blog   /blog/2011

The URL generated for the year argument points to /blog rather than /blog/[yyyy], and the generated URL for the month argument only points to the year, not the month.
Going down one level (to http://example.com/blog/2011) creates a similar problem:
Titles = Home > Blog  > 2011
URLs   = /      /blog   /blog

In both cases, none of the arguments' URLs are output correctly, while their titles are.
My settings for each of the arguments in Views are pretty standard: Month settings and Year settings. The path for the page display is just "blog".
What is causing this strange breadcrumb behavior and how can I fix it?
Thanks!
Answer: Check out the Custom Breadcrumbs module which has support for Views. Here is a quote about it (from the module's project page):

Custom Breadcrumbs 1:
Allows administrators to set up parametrized breadcrumb trails for any node type. This allows CCK-style node types to have "Home > User Blog > 2005 > January" style breadcrumbs on the node view page itself, synchronizing cleanly with custom views or pathauto aliases. Breadcrumb visibility can be customized via a php snippet.
Custom Breadcrumbs 2:
Many new features have been added including support for Views, Panels, Taxonomy vocabularies and terms, paths, and a simple API that allows contributed modules to enable custom breadcrumbs for module pages and theme templates. These are implemented using optional, independent submodules that depend on the main Custom Breadcrumbs module.
In addition to breadcrumb visibility, this version provides an option permitting PHP code snippets to be used in forming the breadcrumb titles and paths. Multiple languages are also supported.
Comment: I use that for my regular, non-Views pages. I've been playing with its Views support, but I can't get it to work well either. When I set custom breadcrumbs for my blog views page, both the month and the year links show up on both month and year views (when month shouldn't show up on the year view)
Answer: The title and breadcrumb settings are often very confusing. Drupal usually does not display current page as part of breadcrumbs and that is how Views' settings work, too.
You need to keep in mind you're setting the title for the current page, but breadcrumb for the previous one in the hierarchy. That means: "what do I want to be added to breadcrumbs when this page is displayed".
Node: Created year
Title: Blog archive for %1
Breadcrumb: Blog
Node: Created month
Title: Blog archive for %2 %1
Breadcrumb: %1
This should fix the label-link matching for you. If you're using a theme that adds current page to breadcrumbs, or want current page's link in breadcrumbs anyway, let me know more about the theme you're using and if it's okay to have the current page's title there. Cheers!
Comment: Found it. It was Menu Trails, which I use to set breadcrumbs for nested menu items (like subpages of pages). Now I just need to find a way to fix the pages.
Comment: Ooh. That's so close. I don't mind not having the current page title or link in the breadcrumb—I'm using a Zen subtheme and have that option turned off in the theme configuration already.
Comment: However, I now have duplicate entries in my breadcrumbs: Home > Blog > Blog > 2011. Fortunately all the links are correct, but I can't figure out how to get rid of the extra "Blog" link.
Comment: I wonder where the extra breadcrumb comes from, but it's not the views. Could you have added it there through a different module/theme for that particular path (blog)? It could be [Custom breadcrumbs 2](http://drupal.org/project/custom_breadcrumbs) or a PHP snippet somewhere.
Comment: Sweet! So is it all sorted at the views pages?


C# FluentMigrator.Runner console app set transaction per session

Question: I am using FlunetMigrator.Runner.3.2.1 and see that this version no longer defaults to rolling back all migrations is one of them fails. It says they are per migration file! which doesn't add value. Is there a way to set the transaction per session whilst running it as a .net core console app.
I see there is this link https://fluentmigrator.github.io/articles/runners/runner-console.html but says use the Migrate.exe file which we are not using, we are using a console. Can I set the transaction level to session in the code?
Why would anyone want to run it and only have some of the changes done, All or nothing is a much better approach
Answer: I found the answer! you set it in the Runner options as shown below
opt.TransactionPerSession = true;

Full method that create the IServiceProvide
private static IServiceProvider CreateServices(string connectionString,
    CommandLineArguments commandLineArguments)
{
    return new ServiceCollection()
        // Add common FluentMigrator services
        .AddFluentMigratorCore()
        .ConfigureRunner(rb => rb
            // Add SQL Server support to FluentMigrator
            .AddSqlServer()
            // Set the connection string
            .WithGlobalConnectionString(connectionString)

            // Define the assembly containing the migrations
            .ScanIn(typeof(Program).Assembly).For.Migrations().For.EmbeddedResources())
        // Enable logging to console in the FluentMigrator way
        .AddLogging(lb => lb.AddFluentMigratorConsole())
        .Configure<RunnerOptions>(opt => {
            opt.Tags = commandLineArguments.Tags.ToArray();
            opt.TransactionPerSession = true; })
        // Build the service provider
        .BuildServiceProvider(false);
}

The below code is the full example that uses the IServiceProvider
var serviceProvider = CreateServices(connectionString, commandLineArguments);

// Put the database update into a scope to ensure
// that all resources will be disposed.
using (var scope = serviceProvider.CreateScope())
{
    try
    {
        UpdateDatabase(scope.ServiceProvider, commandLineArguments);
    }
    catch (Exception e)
    {
        Console.WriteLine("There was a problem with the migration: " + e.Message + "\n" +
                          e.StackTrace);
    }
    migrationRun = true;
}

The Update Database code:
private static void UpdateDatabase(IServiceProvider serviceProvider, CommandLineArguments commandLineArguments)
{
    // Instantiate the runner
    var runner = serviceProvider.GetRequiredService<IMigrationRunner>();

    if (commandLineArguments.Downgrade)
    {
        runner.MigrateDown(commandLineArguments.Version != -1 ? commandLineArguments.Version : 0);
    }
    else
    {
        if (commandLineArguments.Version != -1)
        {
            runner.MigrateUp(commandLineArguments.Version);
        }
        else
        {
            runner.MigrateUp();
        }

    }
}

Comment: This is correct - thanks for adding value to FluentMigrator by updating your post here in StackOverflow with the answer!


The integral $\ J(m,n):=\int_0^1 \frac{x^m}{x^n+1}dx$

Question: Here
Is there a general formula for $I(m,n)$?
I asked for a general formula for $\ I(m,n):=\int_0^{\infty} \frac{x^m}{x^n+1}dx$.

Is the formula $$I(m,n)=\frac{\pi}{n\sin((m+1)\frac{\pi}{n})}$$ true for every pair $\ (m,n)\ $ of real numbers with $\ 0\le m\le n-2\ $ and not just for non-negative integers with $\ 0\le m\le n-2\ $ ?

I wondered whether there is a similar closed form for
$$J(m,n):=\int_0^1 \frac{x^m}{x^n+1}dx$$
I figured out that $$I(m,n)=J(m,n)+J(n-2-m,n)$$ holds for $0\le m\le n-2$.
For $\ m=0\ $, the first few values are
$$J(0,0)=\frac{1}{2}$$ $$J(0,1)=\ln(2)$$ $$J(0,2)=\frac{\pi}{4}$$ $$J(0,3)=\frac{2\sqrt{3}\pi+\ln(64)}{18}$$ $$J(0,4)=\frac{\pi+2\coth^{-1}(\sqrt{2})}{4\sqrt{2}}$$ $$J(0,5)=\frac{1}{5}\sqrt{\frac{1}{10}(5+\sqrt{5})}\pi+\frac{1}{20}(\ln(16)+2\sqrt{5}\coth^{-1}(\frac{3}{\sqrt{5}}))$$ $$J(0,6)=\frac{\pi+\sqrt{3}\ln(2+\sqrt{3})}{6}$$

Is there a general formula for $\ J(m,n)\ $ and does it hold for all pairs $\ (m,n)\ $ of real numbers with $\ 0\le m\le n-2$ ?
Answer: There does exist a closed form for
$$
J(m,n):=\int_0^1 \frac{x^m}{x^n+1}dx \tag1
$$ in terms of the digamma function $\psi(\cdot)$.

Proposition. Let $m=1,2,\cdots$ and $n=1,2,\cdots$. One has
  $$
J(m,n)=\frac1{2n} \psi\left(\frac{m+n+1}{2n}\right)-\frac1{2n}\psi\left(\frac{m+1}{2n} \right) \tag2
$$

then using
$$
\psi\left(r+1\right)-\psi\left(r \right)=\frac1r,\quad r \in \mathbb{Q}^*,\tag3
$$ and $$
\psi\left(\frac{m}{2n}\right) = -\gamma -\ln(4n) -\frac{\pi}{2}\cot\left(\frac{m\pi}{2n}\right) +2\sum_{k=1}^{n-1} \cos\left(\frac{\pi km}{n} \right) \ln\sin\left(\frac{k\pi}{2n}\right) \quad (m<2n)\tag4
$$ one gets a closed form in terms of a finite number of elementary functions.
Hint. By the change of variable, $x=u^{1/n}$, $dx=\dfrac1n u^{1/n-1}du$, one may write
$$
\begin{align}
J(m,n)&=\int_0^1 \frac{x^m}{x^n+1}dx
\\&=\frac1n \int_0^1 \frac{u^{\frac{m+1}{n}-1}}{1+u}du
\\&=\frac1n \int_0^1 \frac{u^{\frac{m+1}{n}-1}(1-u)}{1-u^2}du
\\&=\frac1{2n} \int_0^1 \frac{v^{\frac{m+n+1}{2n}-1}}{1-v}dv-\frac1{2n} \int_0^1 \frac{v^{\frac{m+1}{2n}-1}}{1-v}dv
\\&=\frac1{2n} \psi\left(\frac{m+n+1}{2n}\right)-\frac1{2n}\psi\left(\frac{m+1}{2n} \right)
\end{align}
$$ then one may conclude with Gauss's digamma theorem.
Edit. For any real numbers $a, b$ such that $a>0$ and $b>0$ we have
$$
\int_0^1 \frac{x^a}{x^b+1}\:dx=\frac1{2b} \psi\left(\frac{a+b+1}{2b}\right)-\frac1{2b}\psi\left(\frac{a+1}{2b} \right)
$$
Comment: Very nice! What about non-integer values ? For which pairs does the formula hold ?
Answer: $\newcommand{\bbx}[1]{\,\bbox[8px,border:1px groove navy]{{#1}}\,}
 \newcommand{\braces}[1]{\left\lbrace\,{#1}\,\right\rbrace}
 \newcommand{\bracks}[1]{\left\lbrack\,{#1}\,\right\rbrack}
 \newcommand{\dd}{\mathrm{d}}
 \newcommand{\ds}[1]{\displaystyle{#1}}
 \newcommand{\expo}[1]{\,\mathrm{e}^{#1}\,}
 \newcommand{\ic}{\mathrm{i}}
 \newcommand{\mc}[1]{\mathcal{#1}}
 \newcommand{\mrm}[1]{\mathrm{#1}}
 \newcommand{\pars}[1]{\left(\,{#1}\,\right)}
 \newcommand{\partiald}[3][]{\frac{\partial^{#1} #2}{\partial #3^{#1}}}
 \newcommand{\root}[2][]{\,\sqrt[#1]{\,{#2}\,}\,}
 \newcommand{\totald}[3][]{\frac{\mathrm{d}^{#1} #2}{\mathrm{d} #3^{#1}}}
 \newcommand{\verts}[1]{\left\vert\,{#1}\,\right\vert}$

$\ds{\mrm{J}\pars{m,n} \equiv \int_{0}^{1}{x^{m} \over x^{n} + 1}\,\dd x:\ ?}$.

\begin{align}
\mrm{J}\pars{m,n} & \equiv \int_{0}^{1}{x^{m} \over x^{n} + 1}\,\dd x =
\int_{0}^{1}{x^{m} - x^{m + n} \over 1 - x^{2n}}\,\dd x\quad
\pars{\begin{array}{l}
\mbox{Multiply numerator an denominator}
\\
 \mbox{by}\ds{\quad 1 - x^{n}}
\end{array}}
\\[5mm] &
\stackrel{x^{2n}\ \mapsto\ x}{=}\,\,\,
{1 \over 2n}\int_{0}^{1}{x^{\pars{m + 1}/\pars{2n} - 1} -
x^{\pars{m + n + 1}/\pars{2n} - 1} \over 1 - x}\,\dd x
\\[5mm] & =
\bbox[#ffe,15px,border:1px dotted navy]{\ds{{1 \over 2n}\bracks{%
\Psi\pars{m + n + 1 \over 2n} - \Psi\pars{m + 1 \over 2n}}}}\qquad
\pars{~\Psi:\ Digamma\ Function~}
\end{align}
We used
the identity $\color{#000}{\mathbf{6.3.22}}$: $\ds{\Psi\pars{z} + \gamma =
\int_{0}^{1}{1 - t^{z - 1} \over 1 - t}\,\dd t\,,\ \Re\pars{z} > 0}$. $\ds{\gamma}$ is the Euler-Mascheroni Constant.


How to check if index + 1 is present in ArrayList to avoid IndexOutOfBoundsException

Question: A list contains several lists. From these, 2 lists are always checked one after the other and values are subtracted from each other. The results are then saved in a new list.
This is how the code looks like:
    List<List<AccountlinevaluesEntity>> a_l_v_eList = new ArrayList<>();
    List<List<AccountlinevaluesEntity>> finalList = new ArrayList<>();

    //creates multiple ArrayLists in List, based on numbers of monthList
    for (int i = 0; i < monthList.size(); i++) {
        a_l_v_eList.add(new ArrayList<>());
        finalList.add(new ArrayList<>());
    }
    //splits original List and seperates lists
    for (AccountlinevaluesEntity a_l_v_E : alveList) {
        for (int k = 0; k < a_l_v_eList.size(); k++) {
            for (int j = 0; j < monthList.size(); j++) {
                if (j == k) {
                    if (a_l_v_E.getPeriod().equals(String.valueOf(monthList.get(j)))) {
                        a_l_v_eList.get(j).add(a_l_v_E);
                    }
                }
            }
        }
    }

    List<AccountlinevaluesEntity> tempList;

    for (int i = 0; i < a_l_v_eList.size(); i++) {
        List<AccountlinevaluesEntity> firstMonth = a_l_v_eList.get(i);

        tempList = a_l_v_eList.get(i + 1);

        for (int j = 0; j < firstMonth.size(); j++) {
            AccountlinevaluesEntity firstMonthElement = firstMonth.get(j);

            for (AccountlinevaluesEntity nextMonthElementTemp : tempList) {
                if (firstMonthElement.getAccountNumber() == nextMonthElementTemp.getAccountNumber()) {
                    nextMonthElementTemp.setAmount(subtractAmount(firstMonthElement.getAmount(), nextMonthElementTemp.getAmount()));
                    finalList.get(i).add(nextMonthElementTemp);
                    break;
                }
            }
        }
    }

In itself, the code makes first what he should.
But logically at some point, it finally bangs in this line:
tempList = a_l_v_eList.get(i + 1);

Can anyone tell me how to check if index + 1 is present?
I am also open for general code improvements.
Answer: Change this line
for (int i = 0; i < a_l_v_eList.size(); i++) {

to
for (int i = 0; i < a_l_v_eList.size()-1; i++) {

That way you will end the loop once no next element is available.
Comment: Okay, great, I get it. Thank´s :)
Answer: You can just check if(list.size() > index + 1)


ImportError: No module named cElementTree - Redhat

Question: I am getting following error while running yum on RHEL. Based on my search on google it seems it is related to python.
anyone has any idea?

Plugin "refresh-packagekit" can't be imported
Loaded plugins: security
Traceback (most recent call last):
  File "/usr/bin/yum", line 29, in
    yummain.user_main(sys.argv[1:], exit_code=True)

......
........
...........

    self.parse(srcfile)
  File "/usr/lib/python2.6/site-packages/yum/repoMDObject.py", line 140, in parse
    parser = iterparse(infile)
  File "/usr/lib/python2.6/site-packages/yum/misc.py", line 1169, in cElementTree_iterparse
    _cElementTree_import()
  File "/usr/lib/python2.6/site-packages/yum/misc.py", line 1164, in _cElementTree_import
    import cElementTree
ImportError: No module named cElementTree
Answer: If its Red Hat Enterprise Linux 6.3
Solution:

The libexpat.so.1 symlink links to libexpat.so.1.5.2 in /lib64/. But
  the directory was renamed to libexpat.so.1.old. Recreating the symlink
  with the correct name solved the issue:

<code># ln -s libexpat.so.1.5.2 libexpat.so.1
# /sbin/ldconfig
</code>
Answer: Check out this Bug 694812. I hope it will be helpful.
Comment: No, not helpful in this case.
Comment: If you have Oracle installed: http://serverfault.com/questions/482280/rhel-6-x-yum-gives-importerror-no-module-named-celementtree-errors


Typescript conditional types aren't inferred as expected

Question: I'm trying to build a simple Select component, that can handle both singular and multiple selections. I want it to be typesafe, so as far as I understood I can set the type of component's value depending on its isMultiselect property, so this is how it went:
The value type:
type ISelectItem = {label: string, value: string};

// conditional type - or at least how I thought it should work
type ISelectValue<TIsMultiselect extends boolean> = TIsMultiselect extends true ? Array<ISelectItem> : ISelectItem;

The component props type:
type IAutocompleteSelectProps<TIsMultiselect extends boolean> = {
    items: ISelectItem[]; // items to select from
    value?: ISelectValue<TIsMultiple>; // component value
    setValue: (value: ISelectValue<TIsMultiple>) => void; // function to set the value from the outside
    isMultiselect: TIsMultiselect; // flag defining if we have singular or multiple values
}

And the component itself (only relevant part):
function AutocompleteSelect<TIsMultiselect extends boolean>({
    items,
    onSearch,
    setValue,
    value,
    isMultiselect,
}: IAutocompleteSelectProps<TIsMultiselect>) {

    const selectValue = (item: ISelectItem) => {
        if (isMultiselect) {
            value
                // TS2345: Argument of type 'any[]' is not assignable to parameter of type 'ISelectValue ',
                // and Type 'ISelectValue ' must have a '[Symbol.iterator]()' method that returns an iterator.
                ? setValue([...value, item])
                // TS2345: Argument of type 'ISelectItem []' is not assignable to parameter of type 'ISelectValue '.
                : setValue([item]);
        } else {
            // TS2345: Argument of type 'ISelectItem ' is not assignable to parameter of type 'ISelectValue '.
            setValue(item);
        }
    }

    // ...rest of the component logic
}

I'd expect TS to understand, that if isMultiselect is true, so the value should contain an array of items, and only one item otherwise. Instead, it tells me that I misunderstood something here, but after extensive googling I can't figure out what :(
Here's the playground
Comment: There is nothing in `IAutocompleteSelectProps` that tells TS that there is a relation between some of the members. Google discriminated unions. That's usually how you handle that sort of thing. Oh, right. That's what the answer is already telling you...
Answer: Instead of using generic, please consider to do the following:
type IAutocompleteSelectSingleProps = {
  isMultiselect: false;
  value?: ISelectItem;
  setValue: (value: ISelectItem) => void;
};
type IAutocompleteSelectMultiProps = {
  isMultiselect: true;
  value?: ISelectItem[];
  setValue: (value: ISelectItem[]) => void;
};

type IAutocompleteSelectProps = IAutocompleteSelectBaseProps &
  (IAutocompleteSelectMultiProps | IAutocompleteSelectSingleProps);

This way, your props will always have IAutocompleteSelectBaseProps, and depending on the value of isMultiselect, you will have either IAutocompleteSelectMultiProps or IAutocompleteSelectSingleProps.
Also, depending on whether you use strictNullChecks tsconfig option or not, you will have to add if(isMultiselect === true) to make it work as expected. You will have to add === in case if you do not use strictNullChecks.
playground


Can I connect to my app engine VM instance from my computer with SSH?

Question: 1) Can I connect to my app engine VM instance from my computer with SSH ?
I speak well about the app engine part of google cloud platform.
For example, in my computer, I open a terminal, I enter
ssh root@ip

How to get the IP and the username for the app engine instance ? I Want to do the same thing that in the GCP Shell, but from my PC..
2) I have app engine and compute engine in my GCP panel. Are them interconnected themselves between or are they independent instances (app and compute) ?
Answer: App Engine Flexible Environment is implemented on Compute Engine as auto scaling, auto updating, container hosting instances. It is reasonable to assume most of GCP uses Compute Engine for... compute, but for this one you see the instances in your project.
You may list instances with the API, and get a shell on them with gcloud  app instances ssh.  There is limited need for this as App Engine's OS environment is updated for you.


Convergence Issues for Bootstrap Distributions

Question: the following is part of a proof from van der Vaarts book on asymptotic statistics:
I want to show that if for a continuous distribution function F
$$P\left(\frac{\hat{\theta}-\theta}{\hat{\sigma}}\leq x\mid P\right) \rightarrow F(x) \mbox{ and } P\left(\frac{\hat{\theta}^\ast-\theta^\ast}{\hat{\sigma}^\ast}\leq x\mid \hat{P}\right)\rightarrow F(x) \text{ (in probability)}$$
$\quad\forall x$
then
$$\sup_x\| P\left(\frac{\hat{\theta}-\theta}{\hat{\sigma}}\leq x\mid P\right)-P\left(\frac{\hat{\theta}^\ast-\theta^\ast}{\hat{\sigma}^\ast}\leq x\mid \hat{P}\right) \| \rightarrow 0 \text{ (in probability)}$$
So i think it should go like this:
Define $X=\frac{\hat{\theta}_n-\theta}{\hat{\sigma}_n}$ and $\hat{X}=\frac{\hat{\theta}^\ast_n-\hat{\theta}_n}{\hat{\sigma}_n}$.
\begin{align*}
&\sup_x \| P\left(X\leq x\mid P\right)-P\left(\hat{X}\leq x\mid \hat{P}\right) \| \\
\leq&\sup_x\left(\| P\left(X\leq x\mid P\right)-F\|+ \|F-P\left(\hat{X}\leq x\mid \hat{P}\right) \| \right)\\
\leq&\sup_x \| P\left(X\leq x\mid P\right)-F \| +\sup_x \|P\left(\hat{X}\leq x\mid \hat{P}\right)-F\|
\end{align*}
and now i'm a bit lost... i think the claim should follow because the sup is attained because F is continous and monotonous or something like that.
i feel it is close but
Answer: Hint: mimic the proof that a tight family of pdf's that converges pointwise to a continuous limit converges uniformly.
(note by OP: german speaking readers may also find a proof of $F_n \rightarrow F$ weakly \Rightarrow \sup \|F_n - F | \rightarrow 0) in Bauer - Mass- und Integrationstheorie)
In my mind, Helly's selection theorem and your problem were related.
Here is a solution. Let $\epsilon \gt 0$. Since $F$ is continuous with limits at infinity, we can find a partition of $\mathbb{\bar{R}}$ of the form: $a_{0} = -\infty \lt a_{1}\lt\dots \lt a_{k}\lt a_{k+1}=+\infty$ such that
\begin{align*}
& F(a_{i}) \lt F(a_{i-1}) + \epsilon
\end{align*}
for $i=1,\dots,k+1$.
We have:
\begin{align}
&\sup_x|\hat{F}(x) - \hat{F}^*(x)| = \max_i \sup_{x  \in  [a_{i-1},a_{i})}|\hat{F}(x) - \hat{F}^*(x)| ,
\end{align}
but for $x \in [a_{i-1},a_i)$ we have
\begin{align}
&-|\hat{F}^*(a_{i-1}) - \hat{F}(a_i)| \le \\
&\le \hat{F}^*(a_{i-1}) - \hat{F}(a_i) \le\hat{F}^*(x) - \hat{F}(x)\le\hat{F}^*(a_i) - \hat{F}(a_{i-1}) \le \\
& \le |\hat{F}^*(a_i) - \hat{F}(a_{i-1})|
\end{align}
so
$|\hat{F}^*(x) - \hat{F}(x)|\le max\{|\hat{F}^*(a_i) - \hat{F}(a_{i-1})|,|\hat{F}^*(a_{i-1}) - \hat{F}(a_i)|\}  \\
\stackrel{(a)}{\le} max\{|\hat{F}^*(a_{i-1}) - F(a_{i-1})|, |\hat{F}(a_{i-1} - F(a_{i-1})|,
|\hat{F}^*(a_{i}) - F(a_{i})|, |\hat{F}(a_{i}) - F(a_{i})|,
|F(a_i) - F(a_{i-1})|\} $.
By the selection of $\{a_i\}_i$,  $|F(a_i) - F(a_{i-1})| \le \epsilon $.
By assumption, for each $i=1,\dots , k$ we can find a value of the of the index that parametrizes the convergence in probability (which is implicit in your statement, and that I will refer to "the sample size") such that beyond that value:
\begin{align*}
& |\hat{F}(a_i) - F(a_i) | \le \epsilon
\end{align*}
for $i=1, \dots ,k$. Therefore, if the sample size is large enough:
\begin{align}
&\sup_x|\hat{F}(x) - \hat{F}^*(x)| \stackrel{(b)}{\le} \max_i \{|\hat{F}^*(a_{i}) - F(a_{i})|\} + 2\epsilon .
\end{align}
Let $\delta \gt 0$. By the assumption of convergence in probability (as the sample size increases), if the sample size sufficiently large:
\begin{align*}
& P( |\hat{F}^*(a_i) - F(a_i) | \ge \epsilon ) \le \frac{\delta}{k}
\end{align*}
for $i=1, \dots ,k$.
Finally,
\begin{align*}
& P( \sup_x|\hat{F}(x) - \hat{F}^*(x)| \ge 3\epsilon ) \le \\
& P( \max_i \{|\hat{F}^*(a_{i}) - F(a_{i})|\} + 2\epsilon \ge 3\epsilon ) = \\
& P( \max_i \{|\hat{F}^*(a_{i}) - F(a_{i})|\} \ge \epsilon ) \le \\
& \sum_{i=1}^k P(|\hat{F}^*(a_{i}) - F(a_{i})| \ge \epsilon) \le
 \sum_{i=1}^k \frac{\delta}{k} = \delta.
\end{align*}
Since $\delta \gt 0$ is arbitrary, this shows:
\begin{align*}
& \lim P( \sup_x|\hat{F}(x) - \hat{F}^*(x)| \ge 3\epsilon ) = 0 ,
\end{align*}
and since $\epsilon \gt 0$ is arbitrary, this show:
\begin{align*}
& \sup_x|\hat{F}(x) - \hat{F}^*(x)| \leadsto 0 .
\end{align*}
Comment: Very nice, thank you. I worked through it and can see what you are doing, but i have some questions:
- With "value of the of the index that parametrizes the convergence in probability" you mean sth like $F_n$?
- the inequality sign marked (a) is a bit of a mystery to me. i toy around with triangle inequality a bit, but i dont get to the other side
- With the one marked (b) it is clear where the 2 $\epsilon$ come from. The other part is somehow because of the monotonicity i guess?

other than than i like the proof.
Comment: You are right, (a) should be corrected. "<= 3*" instead of "<=" seem to work, but then one has to change some constants. In your statement you are taking limit over some index, which is not written explicitly; I mean that index; since your questions is in the context of bootstrap, it is probably the size of the original sample.


Linear mixed-effects models or one-hot encoding for grouped data

Question: I wish to perform linear regression over a data set whose entries can be divided into two or more groups. The groups could be, for example, the date at which observations where taken, or the patient Ids.
It seems that this can be approached by fitting a linear mixed effect model. Or, are there more simpler yet equivalent solutions that only involve standard linear models?
E.g., perhaps by adding  additional binary features for each group that indicate which group an observation belongs to (one-hot encoding) and then fit a linear model (e.g., with lm() in R).
For example let data1 be:
         y          x x1
 -3.669049 -0.3851723  2
 -4.223906 -0.4416519  2
 10.443685  0.9347280  1
 20.530023  2.0341488  2
  2.915306  0.1803468  1
  8.284428  0.7195443  1
 -5.183832 -0.5389523  2
  3.803867  0.3585491  2
  5.212799  0.5110681  0

and the one hot encoded is (data2):
         y          x  x_0    x_1    x_2
 -3.669049 -0.3851723  0    0    1
 -4.223906 -0.4416519  0    0    1
 10.443685  0.9347280  0    1    0
 20.530023  2.0341488  0    0    1
  2.915306  0.1803468  0    1    0
  8.284428  0.7195443  0    1    0
 -5.183832 -0.5389523  0    0    1
  3.803867  0.3585491  0    0    1
  5.212799  0.5110681  1    0    0

is lm(y ~ x + x_0 + x_1 + x_2 - 1, data2) equivalent to
lmer(y ~ x + (1|x1), data1)?
Are the two approaches equivalent or why should I chose one over the other?
Can you please help me  gain some intuition over this, as I would say that they are equivalent, but i cannot obtain the same results with the two approaches.
PS: i found this answer Random effects vs one-hot encoding but that concerns the prediction accuracy, while I am interested in the regression coefficient and their interpretation.
Comment: Does this answer your question? [What is the difference between fixed effect, random effect and mixed effect models?](https://stats.stackexchange.com/questions/4700/what-is-the-difference-between-fixed-effect-random-effect-and-mixed-effect-mode)
Comment: @GeorgeSavva thank you. that link does not really answer my question. Upon reading that answer, in particular definition 1, it looks to me that adding additional binary features as I suggested in the post only helps with a random intercept (not slope), but I am not sure. Is this correct?
Comment: If that link doesn't answer your question then you need to set out your question more clearly.  In particular what you mean by 'one-hot encoding' because my understanding is this means 'encoding the levels as mutually exclusive binary features then estimated a fixed effects model'.  In which case you are asking about the difference between fixed and random effects, but I could be misunderstanding you.
Comment: @GeorgeSavva Ah I see, I now edited the question. Essentially, I am asking if fitting a standard linear model after one-hot encoding can be thought of being the same as  fitting a mixed effect model.
Comment: OK, I think I now understand the difference... and why you suggested that link: In a mixed-effect model the parameters (intercept and slopes) are correlated random variables, which make these suitable for fitting to data where the observations are correlated. Essentially a mixed-model encodes more hypotheses than a standard linear model
Answer:
is lm(y ~ x + x_0 + x_1 + x_2 - 1, data2) equivalent to lmer(y ~ x + (1|x1), data1)?

No, it is not. The model fitted with lm is a fixed effects model, whereas the second model is a linear mixed effects model.
Let's say the grouping variable is a patient ID, as you mentioned.
Since you may have as few as two groups, fitting a mixed effects model is not a great idea. One of the main purposes of a mixed model is to account for correlations within groups (patients in this case). This seems like a natural approach since in many settings you would expect there to be variation among different patients. If there are substantive correlations then the data are not independent. The software you use will attempt to estimate the variance of a random variable having as many observations as there are patients (each patient represents one observation). With very few patients, trying to estimate the variance of a sample with very few observations is folly.
However, all is not lost. If your primary interest is in the fixed effect of the covariate (x in your case), then estimates and standard errors should be very similar in each model. There is no need to do one-hot encoding yourself - the software will take care of that.
Let's look at this further with some simulated data. I will use basically the same structure as your sample data, one fixed effect X, and one grouping variable (eg patient ID) with 3 levels.
The research question here would be to estimate the fixed effects of the covariate X.
library(lmerTest)

set.seed(1)
n_group <- 3
dt <- expand.grid(X = LETTERS[1:3], id = LETTERS[1:n_group], reps = 1:2)
X <- model.matrix(~ X, dt)

dt$Y <- 1
myFormula <- "Y ~ X + (1 | id)"

foo <- lFormula(eval(myFormula), dt)
Z <- t(as.matrix(foo$reTrms$Zt))

betas <- c(1, 0.5, 2) # Fixed effects parameters

u <- rnorm(n_group, 0, 2) # standard deviation of random intercepts

e <- rnorm(nrow(dt), 0, 1)   # residual error

dt$Y <- X %*% betas + Z %*% u + e

So completes the simulation, so that all we need to do now is fit the models and compare the fixed effect estimates:
m0 <- lmer(Y ~ X + (1 | id), data = dt)
summary(m0)

Fixed effects:
            Estimate Std. Error      df t value Pr(>|t|)
(Intercept)   0.4698     0.7462  2.9875   0.630  0.57387
XB            0.3350     0.5560 13.0000   0.603  0.55712
XC            2.2001     0.5560 13.0000   3.957  0.00164 **

...where I have omitted the random effects and other extraneous details.

m1 <- lm( Y ~ X + id, data = dt)
summary(m1)

### Coefficients:
###             Estimate Std. Error t value Pr(>|t|)
### (Intercept)  -0.3657     0.5075  -0.721  0.48390
### XB            0.3350     0.5560   0.603  0.55712
### XC            2.2001     0.5560   3.957  0.00164 **
##  idB           2.1687     0.5560   3.901  0.00182 **
##  idC           0.3378     0.5560   0.608  0.55390

So it is readily apparent that the estimates and standard errors are consistent between models. In the 2nd model. fitted with lm we also have estimates for id which we are not interested in.
However, this is not always the case. This is a balanced design. With very unbalanced data, or groups with widely different variances, and if you have very few levels of the grouping variable, then fit both models and compare. On the other hand if there are plenty of levels (say 20+) then the mixed model should work well. Between, say, 6 and 20, you might attract some criticism. There is no black and white answer to the minimum number of levels of the grouping variable(s) for mixed models.


ASP.NET WebServices authorisation

Question: I have an ASP.NET web service that is accessed by some software application. In my "Login" method I create a FormsAuthenticationTicket, encrypt it, and return to web service user. This ticket is expected to be sent to each of web service methods: GetLicense, CreateProduct, etc.
When ticket is received, it is decrypted and checked if expired.
The problem is "Logout" method: there are no any ability to force ticket expiration. If it is created with 60 minutes length, it can be marked as expired. Nevertheless user called "Logout" method, he still will be able to use the same ticket later.
Please advise, what is a best practice to do authorization for web service users?
Thanks a lot!
P.S. Any thoughts are welcome. Probably I need to use another mechanism (not a FormsAuthenticationTicket object).
Comment: How are you evaluating if the ticket is expired?
Comment: It has 'IsExpired' property and Expiration period is defined by parameters required by object constructor.
Comment: See http://stackoverflow.com/questions/2772648/formsauthentication-signout-on-server/2774379#2774379
Answer: What I see as easy solution is to put ticket into HttpContext.Current.Application collection with
<code>HttpContext.Current.Application.Add(ticket)
</code>
When user is trying to use ticket - check if it is in collection. If yes - check if it is expired...
When user call 'Logout' method - I will just remove ticket from collection...
Seems like a quick workaround to force FormsAuthenticationTicket expiration.
Please let me know if you see any lack of this approach.
Thanks.

ASP.NET WebServices authorisation