How to return to outer (local) scope with SSH in Bash?

Question: I'm executing commands remotely through ssh. Is it possible to send output or commands to my own shell? (not to remote)
For example, I wanted to use colored output. I didn't mind having the echo's executed remotely too. But it seems like remote machine does not recognize the syntax and throws errors.
ssh myapp "
    mkdir /some/dir || echo "$(tput setaf 1)ERROR$(tput sgr0)"
"

Answer: Maybe you need to escape your remote command correctly:
ssh myapp "
    mkdir /some/dir || echo '$(tput setaf 1)ERROR$(tput sgr0)'
"


PHP/SQL Activity/News Feed a.k.a Recent Activity Feed

Question: I need to learn how to create a well-designed PHP "recent activity" feed
which displays the recent activity of users on the site along with their
username, activity, and how long ago they performed that action/activity.
I have no progress so far, any help is appreciated.
Comment: When you say no progress... Do you have a site already?
Comment: Yes I have a site, It is a subdomain, I'm getting all the code needed then release it so its not much right now, the site
Answer: This is not just a PHP specific problem. Take a look at this to understand about the architecture of Activity Feeds.
Also, my suggestion is that activity feeds can be better implemented in PHP using NoSQL based database solutions (like MongoDB, CouchDB) rather than using MySQL.


Want to learn Blues Guitar

Question: I want to learn guitar and I listen to the blues with just a little bit of folk and rock.
When I talk to people at local businesses, they ask me whether I want to learn Classical or Rock guitar. Frankly, neither interests me all that much, as I love my some Blues. If I tell them that I want to learn to play Blues, I usually get a long-suffering sigh or get the question repeated back to me in short simple words.
What answer must I provide these people so that I can get my message across?
Answer: Are you asking how to learn blues guitar or how to explain to people that you want to learn blues guitar?  Your question seems to be asking the latter, but your title suggests the former.
When most people lump guitar playing into classical and rock; or classical, rock, and folk; they are not really making a distinction between the musical styles as much as between the playing styles.
Think of it this way: imagine seeing three pictures: a person playing classical guitar, a person playing rock guitar, and a person playing folk guitar. Are your mental pictures different - to the point where it is easy to recognize which is which? That is because each style is defined partially by its component physical techniques and disciplines, such as sitting/standing position, right-hand technique, and the type of guitar used.
Now imagine seeing two more pictures: a person playing rock guitar, and a person playing blues guitar. Are these two different enough to easily recognize which is which? If they are, I would bet it is less due to actual differences in the style and more due to stereotypes we hold about the styles. For example, some electric guitars seem less bluesy than others, and although I would certainly say that some guitars have tone/action better suited to blues, our stereotypes tend to center more around body style.
In this regard, playing blues guitar is playing rock guitar - just with an emphasis on certain techniques and a certain musical style. Many rock bands are really just playing blues riffs and blues solos with slightly less bluesy drums & vocals.
For most people, the specifics of what you are playing is less distinctive. For example, if I were to play a Bach lute suite on an electric guitar, using a pick and the whole nine yards, most people would say I was playing rock guitar, not classical guitar. The visual aspects are more relevant to the opinion than the aural aspects.
So basically, if telling people you want to play blues is not clear enough, then telling them you are interested in rock guitar will get your point across. You would not be lying and it really doesn't commit you to playing anything you don't choose to play regardless.
Comment: Another approach to take would be to give the shop examples of the musicians whose style you want to learn to emulate.  This may help them guide you toward specific guitars and amplifiers that will help shape your sound into what you want (though as with any instrument it's really down to the player to make it sound "right").
Comment: +1 for giving examples.  I did not put 2 and 2 together to realize that the business mentioned in the question might actually be music/guitar shops.  I would hope that anyone working in a guitar shop would understand the different nuances between rock and straight blues, but you never know...
Comment: Unfortunately Dumb and Dumber seems to be the hiring standard at music shops these days - at least the big ones. If you have a small local shop that actually knows... *anything* ...then you're ahead of the game :-)
Comment: NOTE: there is no one defining guitar type for playing the blues. You can play blues on any guitar although the typical ones are: flat top with steel strings, solid body electric (strats, SG, PRS), semi-hollow body electric (Lucille), slide guitar both electric and non-electric, doboro.
Comment: @tpburch Yes to both, I suppose. I want to learn to Play The Blues and I need to figure out how to beat that concept through the pretension exhibited at the guitar shops. +1 for 'Lucille' style. I love me some BB King.
Comment: Some sales people can be intimidating, not to worry. Seek out pro players in your area, get to know these guys. Some teach, some don't, you will have to explore. They will guide you to the right gear. Start slow, it can get expensive, but does not have to be that way. Think of Harry Potter and his wand, the wand chooses you, so too the right guitar will speak to you when you are ready to listen.
Answer: If you want to learn blues guitar the first thing a teacher is going to ask you is what experience do you have on the guitar so far? Maybe you've never picked up a guitar, then they might ask you if you have any musical training: piano? voice? other?
Then they might want you to show them some of the more interesting things you can play, just some samples so they can evaluate your present skill level.
The next thing they should ask you is what Blues Guitarists are you interested in?
This could be anyone from Robert Johnson to Albert Collins.
If you don't know? Then you need to do some homework. Here's a list of blues musicians you should listen to and get a sense of history (1920's to present) not just guitar either.
Pine Top Smith (piano, boogie),
Robert Johnson,
Lead Belly,
T-Bone Walker,
Freddy King,
Muddy Waters,
BB King,
John Lee Hooker,
Albert King,
Albert Collins,
Eric Clapton,
Jimi Hendrix,
Stevie Ray Vaughan,
Bonnie Raitt,
and Buddy Guy
And this is just the tip of the ice berg, here's some more:
http://www.digitaldreamdoor.com/pages/best_bluesguitar.html
In any musical instrument instruction you should receive some of the following: theory, history, technical exercises (such as scales, arpeggios, chords, positions), expressive elements (vibrato, tremelo, bending notes, slurs, slides, hammer ons, etc), learn pieces, play duets, play solos, play in an ensemble.
Update and edit based on comments:
"Love me some BB King and John Lee Hooker." and
"...still read sheet music for choral singing having done a few years of cornet in middle school..."
So you have some great tools to work with and a focus on who you like.
Some people will argue that reading music will not help, but since there are a number of people who have transcribed earlier blues music your sight reading skills will be of use.
For the most part, like folk music, much of blues is by ear, so ear training is recommended as well. Don't worry, any good music school would teach both ear training and reading skills. Most professional blues players that I know personally play by ear, and can learn a 'riff' or 'lick' almost instantly. A combination of good ear training, knowing the guitar finger board up and down, and some natural talent is all good.
Your experience with the cornet will assist in understanding melody and expression.
Comment: Another concept to learn about playing blues with an electric guitar is that the vast majority of pro players will tell you that your 'tone' is based on the combination of both the guitar and the amp--most lean heavily on the old Fender tube amps from the 50's and 60's. There are a ton of folks that are building replicas of these today. Tube amp building is not a lost art, it has exploded in the last 20 years.
Comment: Thanks! Love me some BB King and John Lee Hooker. The stuff after 1970 is okay, but it doesn't reach in and grab me.
Comment: And yeah, I can mostly still read sheet music for choral singing having done a few years of cornet in middle school. But that's about it.
Comment: oops, an omission. Blues 'tone' for an electric guitar is based on the three things: Guitar, Amp, and your touch!

Want to learn Blues Guitar


Android Flip View Animation is Jerky

Question: This is an odd one. I have an animation that flips a view 90 degrees and then another view is rotated the other 90 degrees, allowing for a change of image. This works and it works well on the Samsung Tab 3; however on the Tab 4 the animation is very jerky. If I turn on the 'Profile GPU rendering' - 'Show on screen as bars' the animation becomes very smooth.
I have put debug logs in the animation, and I have found the applyTransformation function is called the same amount of times, when it is jerky and when it is smooth. So all I can put it down to is the screen isn't being refreshed enough. Any thoughts would be great.
Here is the Flip animation.
    public class AnimatedFlip  extends Animation
    {
        private final float mFromDegrees;
        private final float mToDegrees;
        private final float mCenterX;
        private final float mCenterY;
        private Camera mCamera;
    public AnimatedFlip(float aFromDegrees, float aToDegrees, float aCenterX, float aCenterY)
    {
        mFromDegrees = aFromDegrees;
        mToDegrees = aToDegrees;
        mCenterX = aCenterX;
        mCenterY = aCenterY;
    }

    @Override
    public void initialize(int aWidth, int aHeight, int aParentWidth, int aParentHeight)
    {
        super.initialize(aWidth, aHeight, aParentWidth, aParentHeight);
        mCamera = new Camera();
    }

    @Override
    protected void applyTransformation(float aInterpolatedTime, Transformation aTransformation)
    {
        float degrees = mFromDegrees + ((mToDegrees - mFromDegrees) * aInterpolatedTime);

        final Matrix matrix = aTransformation.getMatrix();

        mCamera.save();
        mCamera.rotateY(degrees);
        mCamera.getMatrix(matrix);
        mCamera.restore();

        matrix.preTranslate(-mCenterX, -mCenterY);
        matrix.postTranslate(mCenterX, mCenterY);
    }
}

The animation is started with this call.
mFlipStartingView.setVisibility(View.VISIBLE);
applyRotation(0, 90, mFlipStartingView);

This is the function that does the animation.
private void applyRotation(float aStart, float aEnd, final ImageView aView)
{
    // Find the center of image
    float centerX = aView.getWidth() / 2.0f + aView.getX();
    float centerY = aView.getHeight() / 2.0f + aView.getY();

    // Create a new 3D rotation with the supplied parameter
    // The animation listener is used to trigger the next animation
    final AnimatedFlip rotation = new AnimatedFlip(aStart, aEnd, centerX, centerY);
    rotation.setDuration(mFlipAnimationTime);
    rotation.setFillAfter(false);
    rotation.setInterpolator(new LinearInterpolator());
    rotation.setAnimationListener(new Animation.AnimationListener()
    {
        @Override public void onAnimationStart(Animation animation)
        {
        }

        @Override public void onAnimationEnd(Animation animation)
        {
            if (aView != mFlipView)
            {
                mFlipStartingView.setVisibility(View.INVISIBLE);
                mFlipView.setVisibility(View.VISIBLE);
                applyRotation(-89, 0, mFlipView);
            }
        }

        @Override public void onAnimationRepeat(Animation animation)
        {
        }
    });
    aView.startAnimation(rotation);
}

Answer: So I have added a Hack to make this work on the Tab4. At the end of the apply rotation function I have added the following code.
    // hack for the Tab 4
    new CountDownTimer(mFlipAnimationTime * 2, 33)
    {
        @Override public void onTick(long millisUntilFinished)
        {
            mFlipView.refreshDrawableState();
            mFlipView.invalidate();
            mFlipStartingView.refreshDrawableState();
            mFlipStartingView.invalidate();
        }

        @Override public void onFinish()
        {
            mFlipView.refreshDrawableState();
            mFlipView.invalidate();
            mFlipStartingView.refreshDrawableState();
            mFlipStartingView.invalidate();
        }
    }.start();

This makes sure the animation views are updated at least ever 1/30 of a second.
Comment: hii i need this type Rotate Animation

Android Flip View Animation is Jerky


How to select multiple Build Target in Eclipse for Android project?

Question: I am reading Teach Yourself Android Application Development in 24 Hours, in Hour 6, it asks me to create a new Android Project with:
Build target: Android 2.1 + Google APIs
However, I can only select Android 2.1 OR Google APIs, not both, under the list of build targets, when I hover above Google APIs, it says Android + Google APIs, does it mean if I select Google APIs as build target, it will include Android (which version?)? But what if I want to use, say, "Real3D Add-On" as well as "Google APIs", as there is no way to select multiple targets?
Thanks!
Answer: You can only select one target in eclipse.
If you select Google APIS it will be for the Android API version for the Google API listed (and below). If you want the LG Real3D Addon target (I'm guessing it's LG) then it will be for whatever API version it shows.
Comment: Thanks, but what if I want to use Google API as well as Real3D Addon?


Instalação OpenBDT

Question: Começando nessa vida hoje, foi disponibilizado como ferramenta de trabalho o OpenBDT, ao final de toda a instalação, não estou conseguindo criar um projeto, alguém consegue me ajudar?
Answer: Dar uma olhada neste link para ver se resolve seu problema...
https://github.com/OpenBDT/archetype/wiki/gerando-meu-primeiro-projeto-com-o-archetype-OpenBDT
Comment: Adriano, você inseriu no campo resposta um comentário, assim que tiver reputação necessária poderá publicar nos comentários.

Instalação OpenBDT


Hibernate search - match null date field

Question: I want to implement a query to get entities where endDate is null, for this i use indexNullAs because null values are not indexed
@Field(analyze = Analyze.NO, indexNullAs = Field.DEFAULT_NULL_TOKEN)
@DateBridge(resolution = Resolution.DAY)
private Date endDate;

but i have the below error

HSEARCH000325: The 'indexNullAs' property for field 'endDate', with
value 'null', has invalid format: HSEARCH000293: The 'indexNullAs'
property for fields indexed as Longs must represent a Long number.
Could not parse 'null'.
Answer: Pretty much what the error says. Internally, dates are indexed as long numbers, so your null token must be a long too.
Just pick a long value that will not conflict with the representation of valid dates, for example 9223372036854775807 (the maximum long value).
Alternatively, if your project is not too large (yet), you can also migrate to Hibernate Search 6 (in beta, but well tested and approaching the final release). Among many improvements, Hibernate Search 6 has an exists predicate, making the "null token" unnecessary.


RegisterTypeLibForUser call doesn't seem to work - any ideas?

Question: This is really a follow on question to a previous one, where I need to register applications, TLBs and OCXs per user, rather than into HKLM. I have written the following code - based on answers here and elsewhere, BUT the TLB is not registered - no error is thrown, just nothing happens (this code snippet comes from the Embarcadero website.
procedure RegisterTypeLibrary(TypeLib: ITypeLib; const ModuleName: string);
var
  Name: WideString;
  HelpPath: WideString;
  RegisterTypeLibForUser : function(tlib: ITypeLib; szFullPath, szHelpDir: POleStr): HResult; stdcall;
  res : HResult;
begin
  Name := ModuleName;
  HelpPath := ExtractFilePath(ModuleName);
  res:=RegisterTypeLib(TypeLib, PWideChar(Name), PWideChar(HelpPath));
  if res <> S_OK then begin
    @RegisterTypeLibForUser:=GetProcAddress(GetModuleHandle('oleaut32.dll'), 'RegisterTypeLibForUser');
    if (@RegisterTypeLibForUser <> nil) then begin
      res:=RegisterTypeLibForUser(TypeLib, PWideChar(Name), PWideChar(HelpPath));
    end;
  end;
  //MessageBox(GetForegroundWindow, PChar(IntToHex(res, 8)), nil, MB_OK);
  OleCheck(res);
end;

Anyone got any pointers as I am now lost.
Update :
Thanks for all the help and suggestions, so to clarify ...
As I understand that it, I shouldn't require elevated permission, and so this should to be working, but I could be wrong. Application isn't virtualized (or at least it isn't meant to be), at the moment it just calls the above code, and nothing else.
If I run it as admin it works (or at least doesn't throw an error), not elevating it gives me an error. So can I just not do this at all, or am I doing it wrong? This is the same as when I register it via regsvr32, although that is a slightly different question - how to do the same, but for OCX controls.
Answer: You still need to setup the reg keys as shown in the QC (your link) but this time in the current user registry.
Comment: You mean change and recompile the VCL - or can it be done as local copies of the files do you think ?
Comment: No, I mean you need to register your COM Class in the registry. RegisterTypeLib does not create reg keys for your CoClasses in CLSID.
Answer: You also have to run with elevated privileges when running against Vista or Win7.
-EDIT-
The problem could be that your application is being virtualized and the initial call to RegisterTypeLib IS returning OK, but its only valid for the current session.  Either eliminate that call completely, or only perform the call if your running on an OS version prior to vista.
Comment: It is intended to work without Admin rights since the function access only HKCU hive.


XML data binding in C++ with simple \ non intrusive data model

Question: I'm evaluating looking for an XML binding tool for C++.
I'm interested in a tool that uses separate data model and parser. that is:

a "simple" class that only holds the data from the XML, without having the parsing logic. preferably a class without external dependencies - just using simple types, structs, and arrays.
a separate class that handles the parsing.

This is somewhat similar to what happens with the XSD tool for .net.
Answer: I have regularly used the XSD library from CodeSynthesis.  This has a very simple getter interface code generated from the specification.  I specifically use it for the SAX binding, which optimises access and use parse-time, for fastest XML processing.  You can use the SAX tool and dispatch to your own handler, or the DOM tool object-maps the XML directly, revealing simple accessor functions.
Comment: This is what I started using. But I prefer something where to final data classes are cleaner, and don't depend on the parsing infrastructure. (like in XSD\Tree). on the other hand, I don't want to handcraft the data model (like in XSD\Parser)


I have a problem with substr function(C++)

Question: Well, I am supposed to return the middle character of a word. If the word's length is odd, return the middle character. If the word's length is even, return the middle 2 characters.Like "test" => "es" or "testing" => "t".
And This is what I have wroten =>
#include <iostream>
std::string get_middle(std::string input)
{
  if (input.length() % 2 == 0) {
    return input.substr(1,2);
  } else {
    return input.substr(1,1);
  }
};

int Main() {
  get_middle("test");
  get_middle("testing");
}

Comment: And a [`substr`](https://en.cppreference.com/w/cpp/string/basic_string/substr) reference might be good to read as well.
Comment: What is the first argument to `substr`? Why do you always pass `1`, however long the string is?
Comment: And what is the problem you have? Please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Then [edit] your question to include a description of the problem you have, what result you get, what result you expected.
Comment: `int Main() {` I assume that was a typo since `c++` is case sensitive.
Answer: Look at your code, does it return the middle character (in either case)? No because you say input.substr(1,..) it returns the second character.
What you want is this for the odd case
return input.substr(input.length()/2, 1);

I'll leave you to work out the even case.
In fact if you are very clever about it you can have the same formula for both odd and even cases.
Answer: I am guessing the problem is that you are not getting the middle characters (please state the problem next time). The first parameter should be the index of the first character you want in the substring, meaning input.length() / 2
Answer: First, your main method is incorrect. Kindly change "Main" to "main" and return some integer value.
This is how you can achieve your task.
    #include <iostream>
using namespace std;
std::string get_middle(std::string input)
{
    if (input.length() % 2 == 0) {
        return input.substr((input.length() / 2)-1, 2);
    }
    else {
        return input.substr(input.length()/2, 1);
    }
};
 int main() {
    //cout<<get_middle("test")<<endl;
    cout<<get_middle("testing")<<endl;
    cout << get_middle("test") << endl;

    return 0;
}

Output:
t
es
Now first output is for odd string, and second is for even string.
Comment: Thanks man,Your code worked.
Comment: I am glad that you find it useful. Kindly give me thumbs up and accept as answer.


Regression: Estimated Partial Effect of Regressor? (Need help understanding answer)

Question:
Estimate a linear regression model for log wages on education, experience, and experience squared. Report Results.
Predict the effect on average log wage of increasing everybody's education level by one year.

Hint: If the regression model is
$$
\log (\text { wage })_{i}=\beta_{0}+\beta_{1} \times \text { educ }_{i}+\beta_{2} \times \operatorname{exper}_{i}+\beta_{3} \times \operatorname{exper}_{i}^{2}+\varepsilon_{i}
$$
then the effect of increasing education level of individual $i$ by one year is
$$
\theta_{i}=\beta_{1}-\beta_{2}-\beta_{3} \cdot\left(2 \cdot \operatorname{exper}_{i}-1\right)
$$
because one year additional education implies one year less work experience.
The effect we are interested in is:
$$
\theta=\beta_{1}-\beta_{2}-\beta_{3} \cdot(2 \cdot \overline{\operatorname{exper}}-1)
$$
After you defined this, compare the task to that in lecture 5 where we consider the partial effect in a quadratic model.  In lecture, we saw how this partial effect can be specified as a linear combination of the estimators. I.e. if $c$ is a K x 1 vector of constants then the partial effect can be expressed as a linear combination: $ c' \hat\beta$.  In this regard, please see included answer below.
I do not understand how he came about the vector of constants or the standard error. More specifically, how do you get standard error of .0062 as shown at the very bottom of the answer below?
Any help is much appreciated.
Answer: Original regression points are
$$\log (W_i)=\beta_0+\beta_1\text{educ}_i+\beta_2\text{exper}_i+\beta_3(\text{exper}_i)^2+\epsilon_i$$
After giving everyone one more year of education and decreasing experience by one year, we get
$$\log (W_i)=\beta_0+\beta_1(1+\text{educ}_i)+\beta_2(-1+\text{exper}_i)+\beta_3(-1+\text{exper}_i)^2+\epsilon_i$$
The difference between these two is
$$\theta=\beta_1-\beta_2-\beta_3(2\text{exper}_i-1)$$
For all people, we have
$$\frac{\sum_{i=1}^n(\beta_1-\beta_2-\beta_3(2 experi_i-1)}{n}=\beta_1-\beta_2-\beta_3(2\overline{experi}_i-1)$$
Thus the coefficient of $\beta_0$ is 0, beta1 is 1, beta2 is -1, and beta3 is -(2$\overline{experi}$ -1). We get the vector
$$c=(0, 1, -1, -(2\overline{\text{exper}}_i-1))$$
Apparently the vector defined in the lecture has its fourth term to be the negative of the actual coefficient, or is a typo.
To get the estimated effect, plug in the coefficients as well as the average of the experi's for all the n subjects, to get the estimated effect: .0923-.0791-(-.0020)*(2$\overline{exper}_i$-1).
The standard error can be found by the square root of the following variance:
This is equivalent to $$\begin{split}c'Var(\hat\beta)c&=(0, 1, -1, -2\overline{exper}_i+1)'\begin{pmatrix}.2223^2&\sigma_{12}&\sigma_{13}&\sigma_{14}\\\sigma_{21}&.0076^2&\sigma_{23}&\sigma_{24}\\\sigma_{31}&\sigma_{32}&.0249^2&\sigma_{34}\\\sigma_{41}&\sigma_{42}&\sigma_{43}&.0009^2\end{pmatrix}\begin{pmatrix}0\\1\\-1\\-2\overline{exper}_i+1\end{pmatrix}\end{split}$$
I have never used matlab, but the LinearModel object should have a property called CoefficientCovariance that has what we need.
Comment: Brilliant. I just used that in R and got the right standard error. TY!
Comment: @aisync Gave it my best shot!
Comment: The $\text{Var}(\theta)$ is the step I'm confused with.  In Lecture 5, he goes about deriving the variance of the linear combination $c' \hat\beta$ as $\operatorname{Var}\left(c^{\prime} \hat{\beta}\right)$ = $c^{\prime} \operatorname{Var}(\hat{\beta}) c$. Where did you get that expression for the variance? Also what is $\operatorname{Var}\left(\overline{\text { experi }}_{i}\right)$?
Comment: Think there's a slight error in my post with regards to your comment, $\overline{experi}_i$ should be a constant and no variance. But $c'Var(\hat \beta)c$ should be $.0076^2+.0249^2+.0009^2(4)(2\overline{experi}_i-1)^2$ from the formula $Var(aX+bY)=a^2Var(X)+b^2Var(Y)+2abCov(X,Y)$ (but $Cov(X,Y)=0$ here). See updated post for some more details.
Comment: Fantastic ty! So, average experience here is 13.61176. After plugging that into the formula for the estimated effect: $.0923-.0791-(-.0020)^{*}\left(2 \overline{e x p e r}_{i}-1\right)$ = 0.065647. It seems close enough. But I'm still not able to get the same standard error as in the answer.
Comment: If it helps, here's the matlab output:

[0 1 -1 -26.2235]*[.0494 0 0 0;0 .000057 0 0;0 0 .000622 0;0 0 0 .00000081]*[0;1;-1;-26.2235] = 0.0012
Comment: Can this have something to do with the fact that we're using a semi-log regression? Doesn't the effect need to be exponentiated? Beyond that, How are we sure the covariances are zero?
Comment: Yes, I'm getting that too as the variance, and square rooted gives $0.03513951$. The only possibilities are there is actually some covariance between the estimates, what I'm doing is wrong, or typo in the answer key. Maybe you could ask the TA. I am not sure if the effect needs to be exponentiated as the response is already in log form, and the fact that you got the ballpark for the estimated effect without exponentiating.
Comment: Now that I ran a toy example in R, it seems that the command in R to find the variance covariance matrix of the estimates is `vcov`. Dunno what the command in matlab is, but after you find that out from the fitted model you can set up the matrix calculations to determine the standard error
Comment: I have a question regarding an extension of the problem posted [https://math.stackexchange.com/questions/4119255/wages-regressed-on-education-and-experience-estimate-effect-of-changing-paramet](math.stachexchange.com). Any help would be much appreciated.


Logcat android.database.sqlite.SQLiteException: no such tables

Question: sorry if this is a question that many other ask.. but i checked their question but i can't find the solution (sorry noobs here..)
here is my logcat
11-13 00:38:34.267: E/AndroidRuntime(532): FATAL EXCEPTION: main
11-13 00:38:34.267: E/AndroidRuntime(532): android.database.sqlite.SQLiteException: no such table: translate: , while compiling: SELECT _id,kata_indo,kata_toraja FROM translate WHERE kata_indo = 'anjing' ORDER BY kata_indo
11-13 00:38:34.267: E/AndroidRuntime(532):  at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
11-13 00:38:34.267: E/AndroidRuntime(532):  at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
11-13 00:38:34.267: E/AndroidRuntime(532):  at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
11-13 00:38:34.267: E/AndroidRuntime(532):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
11-13 00:38:34.267: E/AndroidRuntime(532):  at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46)
11-13 00:38:34.267: E/AndroidRuntime(532):  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
11-13 00:38:34.267: E/AndroidRuntime(532):  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
11-13 00:38:34.267: E/AndroidRuntime(532):  at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1315)
11-13 00:38:34.267: E/AndroidRuntime(532):  at kamus.indonesia.toraja.TranslateIndonesia.trans(TranslateIndonesia.java:107)
11-13 00:38:34.267: E/AndroidRuntime(532):  at kamus.indonesia.toraja.TranslateIndonesia$1.onClick(TranslateIndonesia.java:45)
11-13 00:38:34.267: E/AndroidRuntime(532):  at android.view.View.performClick(View.java:2408)
11-13 00:38:34.267: E/AndroidRuntime(532):  at android.view.View$PerformClick.run(View.java:8816)
11-13 00:38:34.267: E/AndroidRuntime(532):  at android.os.Handler.handleCallback(Handler.java:587)
11-13 00:38:34.267: E/AndroidRuntime(532):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-13 00:38:34.267: E/AndroidRuntime(532):  at android.os.Looper.loop(Looper.java:123)
11-13 00:38:34.267: E/AndroidRuntime(532):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-13 00:38:34.267: E/AndroidRuntime(532):  at java.lang.reflect.Method.invokeNative(Native Method)
11-13 00:38:34.267: E/AndroidRuntime(532):  at java.lang.reflect.Method.invoke(Method.java:521)
11-13 00:38:34.267: E/AndroidRuntime(532):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-13 00:38:34.267: E/AndroidRuntime(532):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-13 00:38:34.267: E/AndroidRuntime(532):  at dalvik.system.NativeStart.main(Native Method)

this is my the class that the error occured translateindonesia.java
package kamus.indonesia.toraja;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class TranslateIndonesia extends Activity {

    Button translate, back;
    EditText bhsIndonesia, bhsToraja;
    String translateIndonesia;
    public Cursor mCursor;
    public DbHelper helper;
    public SQLiteDatabase db = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_translateindonesia);

        helper = new DbHelper(this);
        db = helper.getWritableDatabase();
        translate = (Button) findViewById(R.id.btnTerjemahkanIndonesia);
        back = (Button) findViewById(R.id.btnBack);
        bhsIndonesia = (EditText) findViewById(R.id.editTextIndonesia);
        bhsToraja = (EditText) findViewById(R.id.editTextToraja);

        translate.setOnClickListener(new View.OnClickListener() {
            //@Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String search = bhsIndonesia.getText().toString().toLowerCase();
                if (search.contains(" ")){
                    parsing ();
                }
                else{
                    trans ();
                }

            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            //@Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent menu = new Intent(TranslateIndonesia.this,MainActivity.class);
                menu.putExtra("pesan", "From TranslateIndonesia Menu");
                startActivity(menu);
            }
        });
    }

    public void parsing (){
        String search = bhsIndonesia.getText().toString().toLowerCase();
        Cursor cursor = null;
        String temp[] = search.split("[^a-zA-Z]+");
        String tempS[] = search.split("[a-zA-Z]+");
        Boolean char1 = search.substring(0, 1).matches("[a-zA-Z]");
        bhsToraja.setText("");

        for(int i = 0; i < temp.length; i++){
            try{
                db = helper.getWritableDatabase();
                cursor = db.rawQuery("SELECT LOWER(kata_indo) as kata_indo, LOWER (kata_toraja) as kata_toraja FROM translate WHERE Lower(kata_indo) = ?", new String[]{temp[i]});
                int countRow = cursor.getCount();
                cursor.moveToFirst();
                String specialChar = "";
                if(tempS.length > i){
                    specialChar = tempS[i];
                }
                if(!char1){
                    if(countRow > 0){
                        bhsToraja.append(cursor.getString(cursor.getColumnIndex("kata_toraja")) + specialChar);
                    } else {
                        bhsToraja.append(temp[i] + specialChar);
                    }
                } else {
                    if(countRow > 0){
                        bhsToraja.append(specialChar + cursor.getString(cursor.getColumnIndex("kata_toraja")));
                    } else {
                        bhsToraja.append(specialChar + temp[i]);
                    }
                }
                //
            }
            catch(SQLiteConstraintException ex){
                bhsToraja.setText(ex.toString());
            }
        }
        if(tempS.length > 0 && tempS.length > temp.length){
            bhsToraja.append(tempS[tempS.length-1]);
        }
    }

    public void trans(){
        String result = "";
        String search = bhsIndonesia.getText().toString().toLowerCase();
        mCursor = db.rawQuery("SELECT _id,kata_indo,kata_toraja FROM translate " + "WHERE kata_indo = '"+search+"' ORDER BY kata_indo",null);
        if(mCursor.moveToFirst()){
            result = mCursor.getString(2);
            for(;!mCursor.isAfterLast();mCursor.moveToNext()){
                result = mCursor.getString(2);
            }
        }
        if(result.equals("")){
            result = "Kata tidak ditemukan";
        }
        bhsToraja.setText(result);
    }
//end

}

do i need to edit some lines or my DB isn't existed even thou i put one in assets folder?
really appreciate your help.. thanks before..
this is the DBHelper.java
package kamus.indonesia.toraja;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.ContentValues;
import android.content.Context;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class DbHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "dbkamus.sqlite";
    public static final String KEY_ROWID = "_id";
    private static final int VERSION = 1;
    private static File DATABASE_FILE;
    private boolean mInvalidDatabaseFile = false;
    private boolean mIsUpgraded = false;
    private Context mContext;
    private int mOpenConnections = 0;
    private static DbHelper mInstance;

    synchronized static public DbHelper getInstance(Context context){
        if(mInstance == null) {
            mInstance = new DbHelper(context.getApplicationContext());
        }
        return mInstance;

    }

    public DbHelper(Context context){
        super(context, DATABASE_NAME, null, VERSION);
        this.mContext = context;
        SQLiteDatabase db = null;
        try{
            db = getReadableDatabase();
            if (db != null){
                db.close();
            }
            DATABASE_FILE = context.getDatabasePath(DATABASE_NAME);
            if (mInvalidDatabaseFile){
                copyDatabase();
            }
            if(mIsUpgraded){
                doUpgrade();
            }
        }catch (SQLiteException e){

        }finally {
            if(db != null && db.isOpen()){
                db.close();
            }
        }
    }
    @Override
    public void onCreate(SQLiteDatabase db){
        mInvalidDatabaseFile = true;
    }
    @Override
    public void onUpgrade(SQLiteDatabase database, int old_version, int new_version){
        mInvalidDatabaseFile = true;
        mIsUpgraded = true;
    }
    private void doUpgrade(){

    }
    @Override
    public synchronized void onOpen(SQLiteDatabase db){
        super.onOpen(db);
        mOpenConnections++;
        if(!db.isReadOnly()){
            db.execSQL("PRAGMA foreign_keys=ON;");;
        }
    }
    @Override
    public synchronized void close(){
        mOpenConnections--;
        if(mOpenConnections == 0){
            super.close();
        }
    }

    private void copyDatabase(){
        AssetManager assetManager = mContext.getResources().getAssets();
        InputStream in = null;
        OutputStream out = null;
        try{
            in = assetManager.open(DATABASE_NAME);
            out = new FileOutputStream(DATABASE_NAME);
            byte[] buffer = new byte[1024];
            int read = 0;
            while((read = in.read(buffer)) != -1){
                out.write(buffer, 0, read);
            }
        } catch(IOException e){

        }finally{
            if(in != null){
                try{
                    in.close();
                }catch(IOException e){}
            }
            if(out != null){
                try{
                    out.close();
                }catch(IOException e){}
            }
        }
        setDatabaseVersion();
        mInvalidDatabaseFile = false;

    }

    private void setDatabaseVersion(){
        SQLiteDatabase db = null;
        try{
            db = SQLiteDatabase.openDatabase(DATABASE_FILE.getAbsolutePath(), null, SQLiteDatabase.OPEN_READWRITE);
            db.execSQL("PRAGMA user_version = " + VERSION);
        } catch(SQLiteException e){

        }finally{
            if(db != null && db.isOpen()){
                db.close();
            }
        }
    }

    public Cursor getAll(){
        return(getReadableDatabase().rawQuery("SELECT _id, kata_indo, kata_toraja from translate ORDER BY _id ASC", null));
    }

    public void insertKey(String indo, String toraja){
        ContentValues cv = new ContentValues();
        cv.put("kata_indo", indo);
        cv.put("kata_toraja", toraja);
        getWritableDatabase().insert("translate","kata_indo", cv);
    }

    public void delete(long id){
        getWritableDatabase().delete("translate", KEY_ROWID + "=" + id, null);
    }

}

Comment: I don't think your `Activity` is causing any problems because it's a problem of the database not existing, which means it's not getting created (or initialized from the one in the assets folder). What does your DBHelper look like? Specifically the onCreate, since that's where you should create the database.
Comment: Show your database class. Your database is not created.
Comment: @Jakar i added the DbHelper in the question above.. :)
Answer: 1) Start your application.
2)Go to ADM monitor
3)find your app => go to data find your databases and delete
4)restart app
////////////////////
initiliaze your dbHelper beforeonCreate(when you declare)
suggestion: Don't make an object and database have the same name.
Comment: sorry for the noob question but can you show me where is it? ^^ the object and database i mean.. ^^
Comment: it is between sdk manager and question mark at toolbox. You don't need to have Sqlitedatabase object in mainactivity, actually you shouldn't. parsing and trans methods should be in dbHelper class.
Comment: so i just need to move the parsing and trans methods to dbhelper class?
Comment: make a seperated dbHelper class which handles everyting about database. I don't suggest you execute your queries or parse data in an activity.This will make your code better and more readable.


Regular expression to find @Razor references in HTML

Question: I have some text resources that contain HTML and @Razor markup. They are fed through a razor engine, so references like @Model.FirstName are replaced with the appropriate value. Also, the razor references are in HTML tag contents as well as attributes of the HTML.
e.g.
Hello @Model.FirstName, I believe your surname is @Model.Surname.
In this instance I want a regular expression to detect @Model.FirstName and @Model.Surname, but ignore the text between them, and also ignore the @Model.TheParam, because it is within the HTML attribute.
The regex I have managed so far is @?Model.[a-zA-Z]*, which matches all 3 model+parameter pairs, but I only want the last 2.
freeformatter.com
Thanks in advance for any help.
Comment: Please edit your question to provide the desired output given the provided input.
Comment: Show what have you done to solve you problem, and then we help you according to your hitches
Comment: Why don't you use the *razor* engine for this, and pass a Model object instead of replacing those references? Which razor engine are you using?
Comment: I have to feed the whole thing through AWS translation, which helpfuly ignores the tag itself, but translates the contents into @Model. 名字, which breaks the later razor rendering. I don't know which razor engine is in use.
Answer:
(?<=>.)@\sModel.[a-zA-Z]*

 try this


Random Chord Progression Based on Circle of Fifths

Question: Given a melody (say in the key of C, we then know G, F, and Am around C), could any chord progression (always starting with tonic) randomly from I, IV, V and vi still work or work most of time? (e.g. I, vi, IV, V, I, vi, V, I, IV, ..., I)
Why would I ask this question? As I am playing songs by ear, while I am trying to figure out the melody on the right hand, I like to do the chord on the left hand. I will have some idea about the key, major or minor. Thus, I probably know the key and hence I am able to start with tonic. Then I will have to randomly (,or by my instinct,) try the chord. It sounds okay I might say. However, I would like to learn more theory about if it "works" or how well it may work?
Comment: Those chords are not really based on the circle of fifths - they're chords which are diatonic to a key - quite different.
Comment: What has the circle of fifths got to do with the question? Did you accidentally write the wrong title for this one?
Comment: I suspect the OP is just starting out learning harmony, and they don't know "diatonic chords" but instead read about "circle of fifth" as a chord progression.
Comment: @piiperiReinstateMonica I think maybe the OP means "G is one fifth above C; F is one fifth below it; and Am is the relative minor." But this doesn't really have so much to do with "the circle"; any relationships between any chords can be plotted on it, without implying any cabalistic causal significance.
Comment: Without clarifying what you mean by "work", this can't be answered objectively. Even with clarifying it, it still probably doesn't have an objective answer. Or maybe another way to look at the question is that there is some concept of "working" that would make any chord and melody combination valid.
Answer: It will fail most of the time, which is why composers exist — to find ways to make otherwise random sounds "work". It's also why music theorists exist — to understand what allows some combinations of sounds to "work" and others "not work".
The first problem, of course, is that what "works" is purely subjective. So, in that sense, a random harmonization of a melody "works" every single time as long as there's someone who thinks so.
But on the other hand, here's a simple example of something that doesn't "work".

The melody is "Twinkle, Twinkle Little Star" in C major.
The harmony is the IV chord played for the entire song.

This is certainly one possibility that could emerge from a "random" selection of chords, and I think it's reasonable to suppose that most people familiar with the original song would say it sounds "wrong" or "doesn't work."
Another possibility:

The melody is "Twinkle, Twinkle Little Star" in C major.
The harmony is a random assortment of I, IV, V, and vi chords, all played very quickly to accompany only the first note of the song, then nothing after that.

Again, I think it's fair to say that such a harmonization wouldn't "work".
So, not only does "workable" music require that chords be carefully selected ("carefully" relative to "randomly choosing from infinite combinations"), but they also have to be placed appropriately in relation to the melody.
There are infinite moments at which a chord could be placed and thus infinite possibilities for chord progressions. Compared to infinity, the number of choices that "work" is, I think, vanishingly small.
Answer: My answer comes from a slightly different vantage point, but: this absolutely happens, and it's one of the hallmarks of French impressionism (especially Debussy).
We call this pandiatonicism (literally "across the tones" or "through the tones"), and it arises when we use a given pitch collection (like your C major) but we don't necessarily follow the rules of Classical-era harmonic syntax (like predominants moving to dominant moving to tonic, etc.).
As an example, here's an excerpt from Debussy's famous "The Engulfed Cathedral":

We start with a C chord moving to Dm moving to G: a standard tonic, predominant, dominant progression. But then we move V–IV–iii–ii–vi–iii, which is rather backwards from a "textbook" harmonic progression. But in this style, it works.
A similar term here is planing, where we use the same chord shape and move it in parallel motion within the key. Notice that the left hand planes these second-inversion triads while the right hand planes root-position triads.
Answer: Surprisingly many chords will "work" in many situations, depending on your definition of what it means to "work". If the audience expects a certain chord progression and will not accept anything else, then I guess nothing else will work than what they expect.
If your chords have been selected from chords usually used in songs in that key, almost anything will work at least to some degree. Maybe it won't tell a certain story that someone expects, but if you have the possibility to force-feed that slightly weird combination to people enough times, it will become familiar and thus expected and thus GOOD, however strange it is. Repetition is the most important thing in making hits.
I tried many different chords for backing Twinkle, Twinkle, Little Star, and I more or less liked all of these variations. Will you like them? Will someone else like them? You decide. Music is mostly about skill, much less about knowledge, so play, listen and experiment to improve your harmony skills.

As you see some of the chords aren't even "in the key" like Bb. But it's still nice, it just creates a chromatic alteration for the B scale degree, which the melody doesn't use, so there's no clash.
What doesn't work for me is if there are certain clashes, particularly in the bass range, but as I've got older, I've become much more tolerant for all sorts of note combinations.
Answer: The wording of your question could be clearer, but I think I understand it.

Given these diatonic chord: I IV V vi...
If you always start with I...
Can the following chords be IV V vi in any order...
And still "work" ..."most of the time?"

Yes and no. The problem is using vague expressions like "work" and "most of the time."
Just about anything will work most of the time for some reason... but that is not very informative. To me, the question is "do you want to understand functional harmony or do you just want a random chord progression generator?"
The permutations of your set up are:

I IV V vi
I IV vi V
I V IV vi
I V vi IV
I vi IV V
I vi V IV

All of those will "work" at least "most of the time." But how does that help anyone? Work how? Work when? To continue from there will just be more groping around.
The way to better understand these progressions is through an understanding of functional harmony and the effects of following and not following standard function.
Function harmony is the flow of chord functions: pre-dominant (or subdominant) to dominant to tonic. In Roman numerals that is typically IV V I or ii V I where IV or ii both can fulfill the pre-dominant/subdominant function. Following that standard function is described in various ways like "strong", "moving to home", or "works."
But you don't always have to follow the functional flow. Other progressions can be used. They have different "feels" and can be describe with various musical terms. Let's list a few using just enough chords to illustrate the ideas:

IV V | I the standard functional progression, V | I is a "closing" gesture, it's an ending
I V is tonally strong, but is an "opening" gesture, it leads to a continuation
V IV | I swaps the subdominant and dominant positions, but still has all the primary tonal chords so is tonally clear, could be called less emphatic than the standard functional progression, plagal is the label for the IV I progression at a cadence or important structural point like repeating the progression, heard in rock and 12-bar blues
V vi is a deceptive progression the "deception" being rather than V moving to I it goes to vi and deceives our expectation.
I vi pairs the tonic and relative minor, the chords are similar as both contain the ^1 tonic and ^3 mediant scale degrees, this progression can be a prolongation of the tonic chord (pairing vi IV is similar but it prolongs the pre-dominant function)

We need to make a general caveat that "functional" and "strong" should not be misunderstood as "good" and "non-functional" and "weak" means bad. "Strong" and "weak" progressions are just the standard descriptions used for progressions in music theory. Each type can be employed for various effects. Weak progression could be used to create a gentle mood for example. Also, "functional harmony" and "tonal harmony" are often used synonymously, and when harmony is non-functional it is sometimes called "modal." (Modal harmony is where the plagal term comes from.) Modal harmony can be just as effective as tonal harmony, but I think that get's beyond the scope of the question, so we should move on.
Now let's list the permutations of I IV V vi again, but with comments and in roughly descending order from most functional...
I vi IV V - standard functional progression, first three chords by roots descend by third, nicknamed "doo-wop" progression.
I IV vi V - same as "doo-wop" but flip the order of the two pre-dominant chords which does not undermine the essential functional flow
I IV V vi - standard functional progress, but ends with deceptive progression
I V vi IV  - starts with an opening gesture, continues with a plagal move, nicknamed "Axis of Awesome", with no "ending" gesture it lends itself to repeating
I vi V IV - tonic prolonged, then a plagal move
I V IV vi - opening gesture, then weak ascending thirds, least emphatic, tentative
All of those could be made to "work" but they have different potentials.
I would add in a few more ideas to replace "random" chord use with deliberate handling...

harmonic rhythm, how long each chord lasts, the relative importance of a chord in a progression can be altered by duration: one chord per bar, one chord for two bars, two chord per bar, etc.
metrical position, placing a chord on beat 1 emphasizes it, other beat positions or syncopation can de-emphasize a chord
prolongation or auxiliary effects can be made with neighbor chord one step away by root, ex. V IV | V or vi V | vi, etc.
returning to the previous chord will usually repeat one of the devices already covered and add interest to a basic progression
to the extent that the non-functional progressions tend toward "weak" progressions, you can try to strengthen or clarify them by just adding a V at the end, or maybe a plagal IV

So, if you apply some of those ideas to one of the "weaker" progressions, you could take (| means a barline)...
| I | vi | V | IV |
...and modify it to...
| I vi | V | IV V | IV | which subordinates vi and emphasizes plagal IV, when reduced it's mostly | I vi | V | IV | IV | which maintains the original chord order, but changes the relative importance of each chord.
...or it could be modified like this...
| I vi | I | vi V | IV V | which when reduced sort of transforms the original to | I | I | vi | V |
The important idea is to take your initial impulse - "what can I do with just four chord" - and replace the "random" groping around with an understanding of how functional harmony, relative strength of progressions, harmonic rhythm, and metrical placement can be used to make a progression "work" to produce a musical effect.


Making composite of two alpha textures in a glsl shader

Question: I wish to combine two textures in a shader exactly the same as you would expect from a photoshop like program. That is, one texture ontop of another as if layered.
Both textures will have alpha, and the result output should also have the correct combined alpha.
So the goal is;

However despite googleing for the formula, I am not able to get the correct colors out.
Using the referance here: (https://microsoft.github.io/Win2D/html/PremultipliedAlpha.htm) for example produces;

(note difference in grey 50% overlay)
The shader code is set in libgdx to use this as the blending;
  context.setBlending(true,GL20.GL_SRC_ALPHA ,GL20.GL_ONE_MINUS_SRC_ALPHA);

I am fairly sure the mistake is in my fragment shaders blending (see code in image above), because if I pass the input texture (A) straight to gl_FragColor, then I get a pixel-perfect color match of A, including its alpha.
eg;
gl_FragColor =  backdiffuse;
works fine.
=====
edit
Following advice, I have tried using a premulitiplied alpha instead.
context.setBlending(true,GL20.GL_ONE, GL20.GL_ONE_MINUS_SRC_ALPHA);

However, this produces wrong effects even when the shader is just set to output A
backdiffuse = texture2D(u_backSample2D, vTexCoord);
backdiffuse=backdiffuse*backdiffuse.a; //convert to premultiply
gl_FragColor =  backdiffuse;

Am I setting libgdxs blending wrong? This is clearly too bright.

Solution (thanks to accepted answer);
 backdiffuse.rgb *= backdiffuse.a; //premultiply source
 v_backBackColor.rgb *= v_backBackColor.a; //premultiply source

finalCol.rgb = backdiffuse.rgb + (v_backBackColor.rgb * (1 - backdiffuse.a));
finalCol.a = backdiffuse.a + (v_backBackColor.a * (1.0 - backdiffuse.a));

Answer: If you want to use the premultiplied alpha equation, you must also draw the output to screen with the appropriate blend equation: GL_ONE, GL_ONE_MINUS_SRC_ALPHA.
And the destination alpha must also be multiplied by one minus source alpha. So:
finalCol.a = backDiffuse.a + v_backBackColor * (1.0 - backDiffuse.a);

If you want to use the more typical blend equation you are currently using, the equations in the shader would be more complicated. You can find them if you look up alpha compositing on Wikipedia. However, premultiplied alpha is a preferred format since it avoids dark or light fringing on antialiased edges of sprites.
Comment: `backdiffuse=backdiffuse*backdiffuse.a;` should be `backdiffuse.rgb *= backdiffuse.a;` You're accidentally squaring the alpha, which makes it more glowy in premultiplied alpha blend mode.
Comment: Thanks, it seems premultiplied would make things easier anyway reading around. However, I cant seem to get even a unchanged image to look correct using it (see edited question). I think its now treating the whole image as additive :-/
Answer: Here is function that computes the "over" alpha compositing function (without pre-multiplied alpha).
This is only necessary if both colors are translucent. If you are always blending onto an opaque color, you can use a simpler function.
float4 blendOver(float4 a, float4 b) {
    float newAlpha = mix(b.w, 1.0, a.w);
    float3 newColor = mix(b.w * b.xyz, a.xyz, a.w);
    float divideFactor = (newAlpha > 0.001 ? (1.0 / newAlpha) : 1.0);
    return float4(divideFactor * newColor, newAlpha);
}


How to show the given set is convex or not?

Question: The set is defined as
$$ V=\{(1,\cos(\theta),\sin(\theta))|0 \leq \theta \leq \pi/2\}.$$
Using the definition of convex set, this is how I tried.
Let $$ v_1,v_2 \in V,$$ then $$v_1 =(1,\cos(\theta_1),\sin(\theta_1))$$ and $$v_2=(1,\cos(\theta_2),\sin(\theta_2)) $$ where $ 0 \leq \theta_1,\theta_2 \leq \pi/2.$ Now for $0\leq \lambda \leq 1$, $$ \lambda v_1+(1-\lambda)v_2 =(1,\lambda \cos(\theta_1)+(1-\lambda)\cos(\theta_2),\lambda \sin(\theta_1)+(1-\lambda)\sin(\theta_2)).$$
So we need to show either that there exists some $0 \leq \theta \leq \pi/2$ such that $$ \cos(\theta)= \lambda \cos(\theta_1)+(1-\lambda)\cos(\theta_2)$$ and $$\sin(\theta)=\lambda \sin(\theta_1)+(1-\lambda)\sin(\theta_2),$$ or no such $\theta$ exists. How do I proceed from here?
Comment: Isn't this a quarter circle in $R^3?$ This is not convex because you can draw straight lines between points in the set and points on the straight lines are not in the set.
Comment: $@$ Adam Rubinson Yes, it is. But in that case any curve won't be non-convex right? But in $R^2$, we say a curve is convex if the line joining two points on the curve lies above the curve.
Comment: "in $\mathbb R^2$, we say a curve is convex if the line joining two points on the curve lies above the curve" No we do not. For example, the curve $C = \{(x,y) \mid y = x^2 \}$ is not a convex subset of $\mathbb R^2$. You're mixing up the idea of a convex function and a convex set.
Comment: $@$ littleO Thanks, I got it now.
Answer: $(1,0,1) \in V$ (corresponding to $\theta=\frac {\pi} 2$), $(1,1,0) \in V$ (corresponding to $\theta=0$),  but $\frac 1 2 [(1,0,1)+(1,1,0)] \notin V$ because there is no $\theta$ with $\sin \theta =\cos \theta =\frac  12$.
Answer: To answer your concern in the comment section.
In fact convexity of curves and convexity of sets are related in this way:
In $\mathbb R^2$ the function $y=f(x)$ is convex if the set $S(f)=\{(x,y)\mid y\ge f(x)\}$ is convex, that is the region above the curve of $y=f(x)$ is convex.
The convexity of the set is given any two points, the segment joining these two points also belongs to the set (or algebraically $\lambda P_1+(1-\lambda)P_2\in S,\ \forall \lambda\in[0,1]$).
For a convex curve, this segment is above the curve, therefore in the region defined by $S$.
Now if a continuous curve can define $2$ regions in $\mathbb R^2$ (let stay in the simple case where there is no self-intersecting, this is just for illustration anf comprehension) in $\mathbb R^3$ you'll need a surface to achieve that.
But since the first coordinate $1$ is fixed, the resulting curve is just the quarter circle of dimension $1$ and we cannot apply the definition seen above.
Moreover you gave us directly a set and not a curve, so geetha290km counter-example solves the question.
Comment: Thanks a lot for the clarification.


Implement a Facebook login without breaking the privacy

Question: Like everybody knows Facebook ignores the privacy compleatly.
So I would like to allow users to login via Facebook without breaking their privacy or non Facebook users. Curriently I'm using the modules Fbconnect, OpenID Selector, OpenID Selector for Drupal login and OpenID Selector for Facebook Connect.
I want to allow the users to log in like in stack exchange. This works almost so like expected except the Facebook stuff. The plugin adds to everypage a stupid link to Facebook. So Facebook can spy my users. How can I avoid that?
Is it possible that I download the http://connect.facebook.net/xx_XX/all.js script and put it on my space? Or maybe to proxy the access and cache the file. Does anyone know if some of my ideas are forbidden by Facebook?
Comment: Hi Rekire, I can suggest you a module called Janrain engage http://drupal.org/project/rpx , this does not add links, it helps you during the registration to collect the user data as well. Have a look to it.
Answer: I can suggest you a module called Janrain engage drupal.org/project/rpx , this does not add links, it helps you during the registration to collect the user data as well. Have a look to it.
Answer: If you really care about your users privacy then you shouldn't use a third party service like Janrain but instead communicate directly with the Facebook API. You can do that with the OAuth Connector module for Drupal 7 as it supports signing up and logging in through OAuth 2 API:s and specifically states that Facebook is one of the API:s that works with it.
Comment: I will check this. Janrain is not the best option but I doesn't want that the clients will load any ressources from facebook directly.
Comment: Could you add some more advice how to setup that module? The documentation is not very helpful.
Comment: I have no hands on experience with configuring the current version - I built the original version, but others are maintaining it now. The way it used to work was that you added a couple of services you wanted users to be able to log in with and then placed a block with all log in buttons in the place of your site where you wanted them to appear. Any specific thing you're wondering about?


How to send HTTP request issued by cURL through VPN?

Question: Please excuse me for (probably) the noob question, but I am very new to Linux networking. I've already spent a few days searching the Internet for an answer to my question, but was unable to find one, so I decided to post a question over here, because I know a lot of skilled professionals visit this site.
Anyway, on to the actual question: I have two servers available. Let's call them Server-A and Server-B. I have established a VPN tunnel between Server-A and Server-B using OpenSWAN. I have assigned the source IPs: 192.168.2.1 for Server-A and 172.16.2.1 for Server-B. I am successfully able to ping 172.16.2.1 from Server-A and 192.168.2.1 from Server-B, so I'm assuming I've set the VPN up properly and both servers are able to communicate.
Now, on Server-A, I want to be able to do this:
When I issue an HTTP request through libcurl like this:

curl http://checkip.dyndns.com/

I'd want to see the request going out through Server-A's public IP address.
However, when I issue:

curl --interface 192.168.2.1 same_url_as_in_the_above_request

I'd want the request to go through the VPN, in order to use Server-B's Internet access, so I would expect to see Server-B's public IP address listed in the checkip response.
Right now, when I issue a request like this, I'm receiving the following error:

curl(7) : couldn't connect to host

What can I do to achieve this, or am I entirely on the wrong path? All I want to be able to do is use Server-B's Internet access from Server-A for accessing only certain websites, or when the ISP for Server-A is down for some reason. I don't need to automate that, just want to be able to do this manually, whenever needed.
I'd suppose I'd need to do some routing or NAT, but I have no idea what to do exactly - I've read a lot of materials in the past couple of days, but it's hard for me to figure out what the exact iptables and/or route commands should look like, and if at all setting up a VPN is the correct solution.
I'm really hoping someone here will be able to give me an advice, and I'm really sorry if I made you all laugh with a noob question like this.
Best regards!
Comment: What if you specified the VPN interface instead of the IP?
Comment: Same thing: curl(7) : couldn't connect to host
Answer:
for accessing only certain websites

Really the simlpest way to do this would be via HTTP proxies - and push the routing choices out to the berowser via a PAC file.

or when the ISP for Server-A is down for some reason

That needs a very different solution - using routing (either in iptables or via iproute2).
The VPN isn't actually required - but if you want to run a remote HTTP proxy and not have it used by every interent fraudster who can find it (*) then you MUST have some method for restricting client access to the service - a VPN is a good way way to achieve that.
*) I now see more attempts to find open web proxies than open SMTP relays on the boxes I look after
Comment: I prefer not to run an HTTP proxy, so I'm rather pursuing the latter solution. Can you please give me (or link me to) an example on what routing rules I have to use in iptables?
Comment: While it's possible do some routing related stuff in iptables, it's a better idea to use ip route (http://lartc.org/howto/) DO NOT try to partition your web traffic using this method. The proxy is the route to go.


FhirContext reusing best practice returns exception "Timeout waiting for connection from pool"

Question: I am having a jsp which makes 25 hl7 hapi fhir calls asynchronously using dstu2. As suggested in best practices, I am creating the fhir context once using static loading and reusing it in every service call. However, service calls fail intermittently with the below stack trace: (I initialized the fhir context for every service call and this issue gets resolved. However, this is slowing down the calls. Could someone help me with any alternative approaches or tell me what I am doing wrong)
Caused by: org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool
public class MyFHIRContext{
<code>public static FhirContext ctx;

static{
    ctx = FhirContext.forDstu2();
    ctx.getRestfulClientFactory().setSocketTimeout(60 * 1000);
    ctx.getRestfulClientFactory().setConnectTimeout(60 * 1000);
    ctx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER);
}
</code>
}
calling code:
IGenericClient client = MyFHIRContext.ctx.newRestfulGenericClient("server url");
Answer: The exception suggests that your connection pool is not big enough to support that many overlapping requests.
You could either make the pool bigger or, better, reduce the number of requests by issuing them all (or groups of them) as batch requests - see http://hl7.org/fhir/DSTU2/http.html#transaction for the details.
We make extensive use of batch requests in our FHIR clients to good effect.


Using Wolfram Alpha to output table of values over the integers

Question: I'm trying to get Wolfram Alpha to output a table of values over the integers for a given function, in example:

n^2-n
1----1
4----2
9----3
...  ...

(using '-'s instead of whitespace to separate the columns)
Despite being a rather simple thing my searches for a command that does this turn up nothing. Does anyone know how I can get it to do this?
Answer: How about Mathematica commands? This is always the best first idea when using wolfram alpha.
Table[n^2, {n,1,10}]
for instance. If you want the table of the values of $n^2$ with $n$ between $1$ and $10$.
Comment: Perfect, thanks!
Comment: This is good, but how do I get it to display decimal values instead of fractions?
Answer: Try Table[n^2, {n, 1, 5}].
Answer: To get the output exactly in the form you describe in the question, you should use
TableForm[Table[{n^2,n},{n,1,k}]]

For example:

EDIT: Well, it seems this only works in Mathematica proper. Oh well.


Stepwise regression vs. elastic net

Question: I understand that Stepwise regression analysis has lots of limitations, including the assumption that the predictors are not highly correlated with each other. In fact, this limitation was the most important reason that I switched to Elastic Net, as I had 75 predictors in my model, some of which are highly correlated.
Using Elastic Net, I could reduce my predictors to 21. I used these selected 21 variables in a multilinear regression model and calculated the coefficient of determination ($R^2=0.58$).
However, when I used Stepwise analysis on the same data, only 11 variables got selected, while the R-square stayed the same! Does it mean that my results from Stepwise analysis can explain a higher proportion of my outcome? If so, how can I justify the limitations of Stepwise analysis over Elastic Net when I'm getting better results?
Comment: It is not appropriate to enter variables found by lasso or elastic net into a new regression as this ignores shrinkage.  And stepwise regression without shrinkage is invalid.
Comment: Prof @Frank Harrell, I wonder if you could comment further on this statement- there appears to be studies using elastic net regularization as a variable selection method, and then performing PLS regression on the subset of variables.
Comment: I don't understand how anyone would thank that double dipping is valid.  The second step forgets about the shrinkage of the first.
Comment: Thanks for your answer Prof @Frank Harrell. As for the double-dipping, is it necessary that the shrinkage be consistent with the selection of latent variables? I see that there is a discordancy in the criteria for model selection, but in the end a prediction model constructed by separation of the variable selection and latent variable selection can still be a "valid" model?
Comment: _Latent variable_ has a special meaning that I don't think applies here.  Variable selection is frought with difficulties.  The only thing that makes it work is the simultaneous incorporation of shrinkage.  You cannot separate the two.  An alternate approach that preserves the correct amount of shrinkage is _pre conditioning_ (also called _model approximation_) whereby the linear predictor ($\hat{Y}$} from lasso or elastic net or any other method is predicted from individual predictors in an attempt to simplify the properly shrunken model.
Comment: Prof @FrankHarrell, thanks - I had meant latent variables for the case where elastic net was coupled with PLS but not generally. I will read up on pre conditioning and model approximation.
Answer: Your question has an implicit assumption that $R^2$ is a good measure of the quality of the fit and is appropriate for comparing between models.  I think that your background information provides evidence that $R^2$ is not a good tool for what you are trying to do.  After all, you can increase $R^2$ by adding nonsense variables to your model.
Did you take the variables that were found using the elastic net and refit a new regression model using those variables rather than use the estimates from the elasticnet fit?  That is kind of like entering your data into a nice statistical software program and using it to round the data and print it out so you can calculate the mean using an abacus.
If you want the fewest predictors possible (and still get a reasonable fit) then lasso methods will tend to result in fewer predictors than elasticnet methods.  The advantage of the elasticnet method is not in finding the fewest variables, but in finding a good model that takes advantage of the information in the variables and avoids the bias that you get with stepwise models.
A better comparison would be how well they predict a new set of observations, or maybe a press statistic or cross-validation.
Comment: [here](http://faculty.chicagobooth.edu/nicholas.polson/research/papers/Horse.pdf) is a link which compares the shrinkage properties of the lasso and horseshoe.  Similar results would occur for the generalised double pareto, as it has similar tail behaviour.  It has a lot to do with local-global shrinkage rules.  Lasso is just a global shrinkage rule, so it has real difficulty when you have both sparsity and large signals.  I'm pretty sure this is known via the inconsistent parameter estimates for non-zero betas.
Comment: generalised double pareto is better than lasso, as it doesn't shrink the "clearly" non zero betas.  Horseshoe is another.
Comment: Hi @probabilityislogic have you got a link comparing generalized double pareto to lasso? And horseshoe?
Comment: @GregSnow: Thanks so much for you response. I do agree that R^2 is not the right measure to compare the models. However, since that I don't have another set of data, what do you think is the best measure to compare these two models? I was thinking of MSE, but again, I have to get the selected variables from the two models, make a regression, and find the MSE for both, right? Isn't it again using an abacus after using a nice software?
Comment: @GregSnow: Also, my dataset is not very large (n=90). That's why I doubt separating the data into test and train sets. So, I would be grateful if I can find another measure for comparison.

Stepwise regression vs. elastic net


Finding the hyperbolic length in a hyperbolic right triangle

Question: Question: In a hyperbolic right angled triangle, the two legs have hyperbolic lengths of $3$ and $4$. What is the hyperbolic length of the hypotenuse? Is this larger or smaller than $5$?
I'm having trouble with this problem mainly because I am not allowed to use a calculator. So I have to do everything manually.
I used the formula:
$$\cosh(c)=\cosh(a)\cosh(b) $$
where $a=3$ and $b=4$
I know $\cosh(x)=\frac{e^x+e^{-x}}{2}$
With that being said, by plugging the values of $a$ and $b$
$$\cosh(c)=\cosh(3)\cosh(4) $$
and substituting into $\cosh(x)$
$$\cosh(c)=\frac{e^3+e^{-3}}{2} \times \frac{e^4+e^{-4}}{2}$$
I know:
$$\cosh^{-1}(x)=\ln(x+\sqrt{x^2-1})$$
In this case, my $x$ would be $\frac{e^3+e^{-3}}{2} \times \frac{e^4+e^{-4}}{2}$
Plugging into the inverse, I would obtain:
$$c=\cosh^{-1}(x)=\ln\left(\frac{e^3+e^{-3}}{2} \times \frac{e^4+e^{-4}}{2}+\sqrt{\left(\frac{e^3+e^{-3}}{2} \times \frac{e^4+e^{-4}}{2}\right)^2-1}\right)$$
Maybe my algebra is a bit rusty but how would I know if $c$ is less than or greater than $5$. The only way I could think is to simplify above but I'm not sure how to simplify above. It became way too messy. Any help?
If there is an alternate way, I'm open to hear that as well.
Comment: Just calculate(get a advanced calculator) : cosh(3) is a bit more than 10 , cosh(4) is a bit more than 27 so the product cosh(3)cosh(4) is  more than  270, arcosh(270) is more than  6.2 so definitly longer than 5 (notice all values above are rounded down, btw cosh(5) is less than  75  so the hypothenusea is definitly longer in the hyperbolic case.
Comment: Start by noticing that in the Euclidean case, $3^2+4^2=5^2$. So if you only have to decide whether the length is smallr or greater than $5$, you only have to see whether it's smaller or larger than in the Euclidean case. You might find ways to decide this without performing any actual computation.
Comment: alternative calculation forget the $e^{-x} $  so you get $\cosh (c) > e^7 / 4 $ then for arcosh forget everything  after $ \ln(x) $ and you get $\ln(e^7 /4 ) = 7- \ln(4)  = 7 - 1.4  = 5.6 $ still much above 5, ps a hyperbolic length of 3 is really long, hyperbolic geometry has an absolute measure
Answer: \begin{align*}
\cosh\sqrt{a^2+b^2}
&= \sum_{m=0}^\infty \frac{(a^2+b^2)^m}{(2m)!} \\
&= \sum_{m=0}^\infty \sum_{n=0}^m \binom mn \frac{a^{2(m-n)}b^{2n}}{(2m)!} \\
&= \sum_{n=0}^\infty \sum_{m=n}^\infty \binom mn \frac{a^{2(m-n)}b^{2n}}{(2m)!} \\
&= \sum_{n=0}^\infty \sum_{m=0}^\infty \binom{m+n}n \frac{a^{2m}b^{2n}}{(2(m+n))!} \\
&= \sum_{n=0}^\infty \sum_{m=0}^\infty \frac{\binom{m+n}n}{\binom{2(m+n)}{2n}} \cdot \frac{a^{2m}b^{2n}}{(2m)!\,(2n)!} \\
&\le \sum_{n=0}^\infty \sum_{m=0}^\infty \frac{a^{2m}b^{2n}}{(2m)!\,(2n)!} \\
&= \sum_{m=0}^\infty \frac{a^{2m}}{(2m)!} \sum_{n=0}^\infty \frac{b^{2n}}{(2n)!} \\
&= \cosh a \cosh b \\
&= \cosh c
\end{align*}
which since $\cosh$ is increasing on $[0,\infty)$ yields $\sqrt{a^2+b^2}\le c$.  We have equality iff $a=0$ or $b=0$.
Answer: $$\cosh c=\cosh a\cosh b={\cosh(a+b)+\cosh(a-b)\over2}$$
Applying Jensen's inequality to $\cosh\sqrt x$:
$$\cosh\sqrt{a^2+b^2}=\cosh\sqrt{\frac{(a+b)^2+(a-b)^2}2}\le{\cosh\sqrt{(a+b)^2}+\cosh\sqrt{(a-b)^2}\over2}=\cosh c$$
Answer: You have\begin{align}\cosh c&=\left(\dfrac{\exp(3)+\exp(-3)}{2}\right)\left(\dfrac{\exp(4)+\exp(-4)}{2}\right)
\\
&=\dfrac{\exp(7)+\exp(1)+\exp(-1)+\exp(-7)}{4}.\\
&>\dfrac{\exp(7)}{4}\\&>\dfrac{\exp(6)+\exp(-6)}{2},
\end{align}
because
$\exp(7)/[\exp(6)+\exp(-6)]=e/[1+\exp(-13)]$
which is clearly greater than $2$. So $c>6$.


How can you beat the world record in shooting?

Question: I'm not expert in the sport of shooting, but if the world record is calculated by the amount of points, how can you beat a perfect score? You can see the records on the official site for the London Olympics, and there is no mentioning of time. So can a score of 600 still be beaten? And what's with the 704.8?
Answer: In the event you mention (50m Rifle Prone), the 8 best qualifiers shoot in the final event.  In the final event, the scoring zones are divided into tenths, with 10.9 being the maximum score per shot (ten shots are taken).

The final total is the combination of the qualification and final score.

The competition is won by the shooter who reaches the highest aggregate score (qualification + final, maximum 709.0).

So, if you shoot in the final, you could hit a perfect 600 (in qualification) and then your final ten could be anything up to 109, meaning a maximum possible of 709.
You note the 704.8 but actually, in the London 2012 games just now, Sergei Martynov won gold with 705.5.
I'll add also that, once someone shoots 709, that will be the maximum world record (and it will be shared by anyone else shooting 709) unless the scoring changes.
For clarity, this is how the shooting is scored:
Qualification

Number of shots : 60
Score possibilities : 0 to 10 per shot
Maximum possible score : 60 shots scoring 10 = 600

Final (Top 8 qualifying shooters)

Number of shots : 10
Score possibilities : 0 to 10.9 per shot
Maximum possible score : 10 shots scoring 10.9 = 109

Final Score

Qualifying Score + Final Score = Total Score
600 + 109 = 709
Comment: In qualification there are clear zones to hit.  I've added an image above.  You either score 0,1,2,3,4,5,6,7,8,9 or 10 depending on which concentric circle you hit.  In the final, these zones are further divided into tenths.  So, the central "10" ring is divided into ten tenths: 10.0, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8 and 10.9.  It's done to score the final at a more accurate level.
Comment: good extension. Why 10.9 max in final, and 10 in qualification?


Uniform distribution probability?

Question:
Let $X$ and $Y$ be independent random variables, each of which is uniformly distributed between $0$ and $1$. Find the probability that $(X−1/2)^2+(Y−1/2)^2≤1/9$. Give at least $8$ correct digits after the decimal point.

I do not know where to start with this question. Can someone please get me on the right track?
Comment: Why do you need 8 rather than say 6 digits? Why do you need any digits of imprecision? P.S. There is a special tag called `homework` for homework questions.
Comment: Algebraically, $(X-1/2)2+(Y-1/2)2\le 1/9$ is equivalent to $X+Y\le 19/18$, which is a considerably simpler expression. Perhaps you did not write what you intended? Maybe it should be  $(X-1/2)^2+(Y-1/2)^2\le 1/9$? In either case, *draw a picture* of the set of $(X,Y)$ that satisfy the inequality. Then think about how you would find the probability of that set, given that $(X,Y)$ has a constant density.
Comment: I did mean to have (X−1/2)^2+(Y−1/2)^2 ≤ 1/9, sorry. Algebraically, I understand that if there was only one distribution, instead of two added together, then the probability would be 2/3. How would I factor in the second distribution?
Comment: Re the self-study tag:  the exact same question has just been posed an hour ago by a different person at:  http://math.stackexchange.com/questions/775525/probability-of-independent-random-variables   ... Except they list it as their HOMEWORK ... And you are self-studying the exact same question???  Should SE be a homework factory??
Answer: Why don't you draw a picture? Here's a hint:
Comment: Fantastic answer; it shortcuts the whole issue down to a single very clear diagram from which the algebraic answer is instantly obvious, and the approach ('*draw a picture*') is a very useful one on a large variety of problems.
Comment: @baudolino  Pretty picture - what software do you use?
Comment: @wolfies: Geogebra. It's open source and pretty cool.
Comment: @baudolino Cool - many thanks. I will take a look.

Uniform distribution probability?


Effect of differencing on prediction intervals in ARIMA models

Question: I am sitting with a couple of time-series that I am analysing using ARIMA models. I have a question regarding prediction intervals. When predicting using a model that takes a first difference (a SARIMA(1,1,0)x(1,0,0) model), I get an increasing size of the prediction interval. Without I get a very constant and narrow band (see below):

The corresponding results are as follows:

Can anyone explain why the band is so constant? First I thought it was because of a large significant MA coefficient. This, however, I removed and the "problem" persisted. Then I though it was because the ARIMA without differencing automatically included an intercept. However, again, when I specified include.mean = FALSE, nothing changed.
Any help would be appreciated.
Comment: When forecasting, you deal with *prediction intervals* rather than *confidence intervals*. The two are not the same.
Comment: Hey Richard. Thanks for pointing out my imprecision. I have now edited the post accordingly
Answer: In general, forecast intervals from ARIMA models will increase as the forecast horizon increases. For stationary models (i.e., with no differencing), they will converge so forecast intervals for long horizons are all essentially the same. If there is some differencing, you are assuming the series is non-stationary, and the forecast intervals will continue to diverge into the future.
Comment: Hey Rob. Thanks so much for the response. I appreciate the explanation.
Comment: Dear Rob, I have related but little bit advanced issue, maybe you would have some comments on that ? http://stats.stackexchange.com/questions/261102/arima-versus-arma-on-differenced-data-gives-different-prediction-interval


Simplenews unsubscribe link shows a 404 Page

Question: In my Newsletter the unsubscribe URL which is inserted by a token, returns a 404 page.
The URL of my Link is:
http://www.mysite.info/newsletter/confirm/remove/
What could be the issue?
Comment: Drupal 6 or 7??
Comment: If Drupal 6, what version
Answer: There are many issue thread on simplenews about this issue.

Simplenews 404 issues

Currently the only supported version of simplenews is version 6.x-1.3 (May 21st 2010).   Most of the issues have patches that are commit to 6.x-2.x-dev and 6.x-2.0-alpha2.  You might want to upgrade.  If you don't, then you will need to apply one or multiple patches to fix your problem.
Answer: For the Simplenews module, the default is to provide a subscriber with an unsubscribe link when they receive an email from a posting i.e. creating of an issue.  This process of appending the unsubscribe link is a default action of the module.  The typical link which is appended to all sent newsletters includes the standard text, i.e. click here to unsubscribe.
The link provided to the user in the email is similar to
<code>http://www.mysite.info/newsletter/confirm/remove/2210afaa624t1
</code>
... where the id at the end is the one linked to the email address when the user first subscribed.
If you don't include the ID on the URL, then you will get the 404 error.
I have not seen a feature that lets a user come to the site and enter an email address and then click a remove button.  I'm guessing someone would have to add this as a module if desired.
Comment: I'm getting the 404 with the full link you mention.
Comment: Is the link you are trying from an email that was created and sent by the SimpleNews Module.  The link I provided above was only a sample from one of our implementations.

If you Send a newsletter out with SimpleNews to a valid email address, then the link at the bottom of the email should have an unsubscribe link similiar to the one above.
Comment: Indeed, the link from an email created and sent by the SimpleNews Module.


Subclipse integration with Eclipse

Question: I did checkout from  SVN with tortoise SVN client in windows explorer and imported those projects under eclipse.
Now trying to integrate subclipse plugin with Eclipse(Kepler).Successfully installed the subclipse plugin from marketplace and restarted eclipse.
But i don't see the SVN options under Team in eclipse editor.
Do i need to have some settings in eclipse after subclipse installation(i was expecting subclipse will pick up the svn settings automatically
from tortoise svn), though settings suggested  under section "Set Subversion Preferences in Eclipse" at link are in place
Comment: Maybe you need a SVN Connector. What do you have when in Eclipse you go to Window > Preferences > Team > Svn > SVN Connector ?
Comment: There is no element of name SVN Connector  under SVN
Comment: @metaDiego Subclipse don't need connectors it has everything built-in and ready to use.
Comment: Are you right-clicking on a project already using Subversion? It does not appear in projects which haven't been checked out from a Subversion Repository. But you can use "Team -> Share Project..." and an option to publish it in SVN should appear.
Comment: I would add your repo's to your SVN repo list available in eclipse via Subclipse. Check out the projects into your workspace through the repo browser in eclipse. I experience difficulties while mixing tortise and subclipse usage, so try to stick with one.
Answer: Due to the order in which you did things, you have to "connect" the project to SVN.  Right-click and choose Team > Share Project.  Then follow the wizard to select SVN. It should recognize it is an existing checkout and just "connect" it to Subclipse.  After that, the Team options will show up.
Comment: Thanks Mark. But when i di Team >Share Project >SVN i get the below error org.apache.subversion.javahl.ClientException: The working copy needs to be upgraded
svn: The working copy at 'C:\...'
is too old (format 29) to work with client version '1.8.10 (r1615264)' (expects format 31). You need to upgrade the working copy first.
Comment: I came across this thread but i am already using subclipse client version 1.10
Comment: Upgrade TortoiseSVN to SVN 1.8 and then use its option to upgrade the working copy.  That will be easiest solution.
Comment: Mark. I am not sure how tortoise SVN coming into picture here and need of upgrading to 1.8 as mytortoise SVN client is working fine under windows explorer. But i am facing issue when i do Team >Share Project >SVN in eclipse kepler(subclipse installed). Is n't a issue with subclipse ?
Comment: TortoiseSVN enters the picture because you put it there.  You checked out your code using TortoiseSVN 1.7.x.  That means the SVN working copy format is for SVN 1.7.  A SVN 1.8 client cannot use this working copy without upgrading it.  Once it does, a 1.7 client will not be able to use it.  So you should upgrade TortoiseSVN to 1.8 so that all of your SVN clients are the same SVN version.
Comment: Ok now getting you. You mean to say that boyh Tortoise SVN and subclipse should belong to same version either 1.7.x or 1.8.x.Right? I am facing issue becoz Tortoise SVN is 1.7 but subclipse is 1.8. Is that correct?
Comment: Yes. If you want to use multiple SVN clients on the same working copy then they MUST all be from the same SVN major.minor version family. SVN 1.8.x is 18 months old now and the best available version. Also any 1.x client can talk to any 1.y server so there is rarely a good reason not to run the very latest client version.

Subclipse integration with Eclipse


Create node and node reference from URL at one time

Question: I have content type "article", in some cases I need to orginise group of parent and child nodes and to do this I use Node Reference from URL module. Also I need to include parent node in this group of articles because some views and node URL depend on completed field "node reference". So, how it works now:

I create a parent node and leave the node reference field empty as the parent node doesn't exist now;
Save this node
Back to edit page and choose my new parent node from the list of avalible nodes in node reference field;

What I would like to do is to create parent node and fill node reference field at the same time.
I guess that Rules module could help me but It too powerfull module and I do not need it for my website. Is there a more simple solution?
Comment: Why can't you create parent node first?
Comment: yes, I do that at first but I need to have filled node reference field filled with the link to itself. I mean that if parent node "Node 1" the node reference field have the same value "Node 1" it should not be empty
Answer: Depending on which relationship module you're using there are different options.

If you're using Entity Reference there's the Inline Entity Form module:

Provides a widget for inline management (creation, modification, removal) of referenced entities.
  The primary use case is the parent -> children one (product display -> products, order -> line items, etc.), where the child entities are never managed outside the parent form.

If you're using the References module there's References Dialog:

This module extends reference fields like the user and node reference fields by adding links to add, edit and search for references through a dialog. This allows for a workflow where the user can create all the references to a node at the same time as she is creating it, a process which sometimes get's a bit backwards, when a user for instance needs to create all the facts for an article.

Both will allow you to create a referenced node at the same time as creating the referee.
Answer: Use hook_node_insert and update the node.
Comment: OP is not using Drupal 6
Comment: @NikhilM which OP?
Comment: OP one who originally posted the question.
Comment: oh ya ya.. it is different function in 7... sorry but I think he can understand what I want to say.
Comment: I changed it now.. thnx..
Comment: thank you for your idea, what I particular used: `function custom_module_node_presave($node) {
  $node->field_add[LANGUAGE_NONE]['0']['nid'] = $node->nid;
}`
Comment: but now I need to put a condition in this function, to use this presave only if field if empty
Comment: Did you get the nid in `presave`? And you can make a condition check,  `if(!isset($node->field_add[LANGUAGE_NONE]['0']['nid'])){$node->field_add[LANGUAGE_NONE]['0']['nid'] = $node->nid;}`
Comment: yes, it was my mistake it worked as I tried on existing nodes that already have nid..


Finding $\mathbb{E}(X|X<Y)$ where $X,Y$ are iid $\exp(\lambda)$ random variables

Question: Question:
$X,Y$ are two independent random variables following exponential distribution with mean $\lambda$.
$Z$ is another random variable which takes value $1$ if $X<Y$ and $0$ otherwise. Find $\mathbb{E}(X|Z=1)$

So basically I have to find $\mathbb{E}(X|X<Y)$ which is equal to $$\int x.f_{X|X<Y}(x)dx$$
I am having trouble finding this conditional pdf . I tried $$f_{X|X<Y}(x)=\frac{f_x(x)}{P(X<Y)}= 2f_x(x)$$ and if this is correct then the required expectation is $2\lambda$ but I am not sure if this is the correct way to approach the problem.
Any help would be very much appreciated!
Answer: $E(X|X<Y)=\frac 1 {P(X<Y)} E(XI_{X<Y})=\frac 1 {P(X<Y)}\int_0^{\infty} \int_0^{y} x\lambda e^{-\lambda x} \lambda e^{-\lambda y} dx dy$. Can you take it from here?.
Comment: @Prof.Shanku That is the **definition**. $E(X|B)$ is defined as $\frac {EXI_B} {P(B)}$ whenveer $X$ has finite expectation and $P(B) >0$. When $X=I_A$ this reduces to the usual definition $P(B|A)=\frac {P(A\cap B)} {P(B)}$ of conditional probabilities. Note that this definition does not assume that densities exist
Comment: I couldn't understand how did you get the first equality
Comment: thanks for the explanation, sir!


Iptables LOG rule inside a network namespace

Question: I'm trying to setup iptables rules for a docker container. I'm using nsenter to execute the iptables command inside of the container's network namespace:
# log access to port 8080
PID=$(docker inspect --format "{{.State.Pid}}" $ID)
/home/ubuntu/nsenter -n -t $PID iptables -A OUTPUT -o eth0 -p tcp -m tcp --dport 8080 -j LOG

This approach works perfectly except for LOG rules. Those don't seem to log anywhere. Note that the same rule applied to the host system works and logs to /var/log/kern.log.
Where can I find the output of those log rules? Is this a known issue/limitation of network namespaces?
Comment: I did a test using a docker container based on centos 7 and it's works, the host is a centos, the same test with ubuntu Ubuntu 15.04 host and ubuntu 12.04.5 container doesn't works, anyway you need to be sure, the syslog is running in your host.
Comment: I'm using Debian wheezy as a host and Ubuntu 14.04 in a container. There it doesn't work. I'm wondering what is different there.
Comment: @gucki I did not find a solution to get it working inside of the namespace. I moved the logging rules outside of the container.
Comment: Update: I tried `NFLOG` instead but it still won't work
Comment: Did you find a solution to this?
Answer: As Donald mentioned, iptables LOG rules inside containers are suppressed by default.
In kernels <=4.10, this behavior could not be adjusted without patching the kernel.  As agrrd mentioned, a work-around is to run ulogd in each container and use iptables NFLOG (or ULOG) rules instead of LOG rules.
However, as of kernel 4.11, running echo 1 > /proc/sys/net/netfilter/nf_log_all_netns on the host (outside of the container) will cause iptables LOG rules inside all containers to log to the host.  (See this Kernel Commit.)
Answer: The output of iptables LOG targets from inside a network namespace is suppressed by design to prevent containers from DOSing their host by overrunning its log buffers.
commit introducing the change
relevant source code line in the current kernel
Answer: I was able to log iptables rules for docker containers by installing ulogd and replacing "-j LOG" with "-j ULOG". Matched packets are logged to /var/log/ulog directory
Comment: Did you install ulogd on the host (gives no output from the rule) oder inside the container (which does not start)?
Answer: I've seen an example (not kernel related) of using -v /dev/log:/dev/log. I wonder if you need to do something similar..
Also, I see that you are using nsenter rather than docker exec: what version of docker are you running?
Comment: It's also not working when not using docker at all, but netns manually from the command line.
Comment: You'd nee /proc/kmsg or /dev/kmsg and docker effectively prevents you from mounting that in the container.


Sinatra streaming response with headers

Question: I want to proxy remote files through a Sinatra application. This requires streaming an HTTP response with headers from a remote source back to the client, but I can't figure out how to set the headers of the response while using the streaming API inside the block provided by Net::HTTP#get_response.
For example, this will not set response headers:
get '/file' do
  stream do |out|
    uri = URI("http://manuals.info.apple.com/en/ipad_user_guide.pdf")
    Net::HTTP.get_response(uri) do |file|
      headers 'Content-Type' => file.header['Content-Type']

      file.read_body { |chunk| out << chunk }
    end
  end
end

And this results in the error: Net::HTTPOK#read_body called twice (IOError):
get '/file' do
  response = nil
  uri = URI("http://manuals.info.apple.com/en/ipad_user_guide.pdf")
  Net::HTTP.get_response(uri) do |file|
    headers 'Content-Type' => file.header['Content-Type']

    response = stream do |out|
      file.read_body { |chunk| out << chunk }
    end
  end
  response
end

Answer: I could be wrong but after thinking a bit about this it appears to me that when setting the response headers from inside the stream helper block, those headers don't get applied into the response because the execution of that block is actually being deferred. So, probably, the block gets evaluated and the response headers get set before it begins executing.
A possible workaround for this is issuing a HEAD request before streaming back the contents of the file.
For example:
get '/file' do
  uri = URI('http://manuals.info.apple.com/en/ipad_user_guide.pdf')

  # get only header data
  head = Net::HTTP.start(uri.host, uri.port) do |http|
    http.head(uri.request_uri)
  end

  # set headers accordingly (all that apply)
  headers 'Content-Type' => head['Content-Type']

  # stream back the contents
  stream do |out|
    Net::HTTP.get_response(uri) do |f|
      f.read_body { |ch| out << ch }
    end
  end
end

It may not be ideal for your use case because of the additional request but it should be small enough to not be much of a problem (delay) and it adds the benefit that your app may be able to react if that request fails before sending back any data.
Hope it helps.

Sinatra streaming response with headers


Filter ClientDataSet by more than one Field

Question: The following code allows me to filter a clientdataset by one field:
begin
dmodule.cds.DisableControls;
try
  dmodule.cds.Filtered := False;

  dmodule.cds.FilterOptions := [foCaseInsensitive,foNoPartialCompare];
  dmodule.cds.Filter := 'Field LIKE '+ QuotedStr('%'+ editSearch.Text + '%');

  dmodule.cds.Filtered := True;
finally
  dmodule.cds.EnableControls;
end;

I have added in another line after the first 'cds.Filter' line which I thought would allow me to filter by another field but it doesn't bring back any records.
The line is:
dmodule.cds.Filter := 'Field2 LIKE '+ QuotedStr('%'+ editSearch.Text + '%');

Do I need to do something else to allow multiple filtering?
Thanks,
Answer: You just combine them  with and (you can also use or if need be):
begin
  dmodule.cds.DisableControls;
  try
    dmodule.cds.Filtered := False;

    dmodule.cds.FilterOptions := [foCaseInsensitive,foNoPartialCompare];
    dmodule.cds.Filter := 'Field LIKE '+ QuotedStr('%'+ editSearch.Text + '%') +
                          'AND Field2 LIKE ' + QuotedStr(editSearch2.Text) + '%');

    dmodule.cds.Filtered := True;
  finally
    dmodule.cds.EnableControls;
  end;

Answer: Your second line basically replaces the filter in the first line.  You probably want:
dmodule.cds.Filter :=
         'Field LIKE '+ QuotedStr('%'+ editSearch.Text + '%') +
     ' OR Field2 LIKE '+ QuotedStr('%'+ editSearch.Text + '%');

Note: I've assumed you want OR because you were surprised about not returning any data. Of course if you require both conditions to be True to return data, then use AND; but you'll still get no records.