Regression analysis not showing the first level of treatments

Question: I have my data that looks like this:
data:
or in R:
df <- read.table(header= TRUE, text=
                   "Location    treatment   block   chem1   chem2   chem3   yield
Monmouth    CC  1   1.600946    6212.631579 810.3024    5156
Monmouth    CC  2   1.474084    5641.403509 835.5242    5157
Monmouth    CC  3   1.433078    4392.280702 856.206 5551
Monmouth    CC  4   1.532492    7120.701754 759.8589    5156
Monmouth    CCS 1   1.198667    5305.964912 726.2298    4804
Monmouth    CCS 2   1.193193    4936.842105 792.6472    4720
Monmouth    CCS 3   1.214941    5383.157895 724.7165    4757
Monmouth    CCS 4   1.360981    6077.894737 699.3266    5021
Monmouth    CS  1   1.853056    5769.122807 500.9153    4826
Monmouth    CS  2   1.690117    5016.842105 634.8698    4807
Monmouth    CS  3   1.544357    5060.350877 637.9536    4475
Monmouth    CS  4   1.825132    5967.017544 648.8814    4596
Monmouth    SCS 1   1.695409    5576.842105 641.0357    4669
Monmouth    SCS 2   1.764123    5174.035088 623.3822    4588
Monmouth    SCS 3   1.903743    5655.438596 555.2834    4542
Monmouth    SCS 4   1.538684    5468.77193  520.8119    4592
Urbana  CC  1   0.845077    7933.333333 683.3528    5583
Urbana  CC  2   1.011463    7000    595.9173    5442
Urbana  CC  3   0.857032    6352.982456 635.4315    5693
Urbana  CC  4   0.989803    8153.684211 672.4234    5739
Urbana  CCS 1   0.859022    6077.894737 847.2944    5045
Urbana  CCS 2   0.919467    4939.649123 745.5665    4902
Urbana  CCS 3   1.01717 5002.807018 778.3548    5006
Urbana  CCS 4   0.861689    6489.122807 735.8141    5086
Urbana  CS  1   0.972332    4694.035088 395.2647    4639
Urbana  CS  2   0.952922    5901.052632 570.4148    4781
Urbana  CS  3   0.804431    4303.859649 458.0383    4934
Urbana  CS  4   0.742634    6768.421053 535.3851    4857
Urbana  SCS 1   1.195837    6159.298246 678.0293    4537
Urbana  SCS 2   1.267285    6090.526316 670.7419    4890
Urbana  SCS 3   1.08571 4939.649123 335.2923    4842
Urbana  SCS 4   1.20097 5262.45614  562.5674    4608
")

I would like to see if any of the variables (including factors in my treatment have significant effect on yield. So here, I am running stepwise regression which determined the best fit glm(formula = yield ~ treatment + Location, family = "gaussian",
    data = df). However, I am not sure why it is not showing the results from summary(glm.step) for CC and Urbana from variables treatment and Location. Can someone please explain me the reason?
n = nrow(df)
null = glm(yield ~ 1, data = df, family = "gaussian")
full = glm(
  yield ~ .,
  family = "gaussian",
  data = df
)

glm.step <-
  step(
    null,
    scope = list(lower = null, upper = full),
    direction = "forward",
    k = log(n),
    trace = 0
  )
glm.step
summary(glm.step)

Comment: Hopefully, the current answer by @Patrick provides an answer to your question.  But I'll add that the results you may be seeking might be produced by e.g. `library(car); Anova(Model)`, where *Model* is your final glm model.  From there, you can use the *emmeans* package to further compare the treatments or interactions of treatments.
Answer: R is using "LocationMonmouth" and "TreatmentCC" as reference levels and the other levels are given as relative to that.
By way of example let's consider a simple model that only has the location variable in it:
What you want to see is:

$yield = intercept + Monmouth*IndicatorMonmouth + Urbana*IndicatorUrbana$

Where $IndicatorMonmouth$ is 1 if the data point is from Monmouth and 0 if it's not.
But as you can see we have 3 variables to solve for now and our data only has two levels in it. So we have more unknowns than knowns and thus it is impossible to solve that formulation of the model. So what R and glm() is doing for you is rephrasing your model as follows:

$yield = intercept + (Urbana - Monmouth)*IndicatorUrbana$

Now there are only two parameters to estimate: intercept and (Urbana - Monmouth). So it has chosen to put the Monmouth data into the intercept term and present you with a parameter that is actually the difference between Urbana and Monmouth. Unfortunately R doesn't rename the variables or explicitly tell you it did that.
Same thing applies for TreatmentCC. I hope this also clears up how to interpret the output from the glm function.
Reference: https://stats.idre.ucla.edu/r/modules/coding-for-categorical-variables-in-regression-models/


Logoff Script on Remote Desktop Services

Question: I have a Remote Desktop services running on Server 2012 R2. My timeout settings have been set to end a session 60 minute after disconnection. This is good for all the users except one: That user's session has to be ended as soon as he is disconnected. I can't create another collection because I don't have a second session host.
I decided to create a super simple batch file that will force the user to log off such as this:
<code>@ECHO off
logoff f
</code>
This, for some reason requires admin privileges and brings up the elevation prompt. So it's out of question at this point.
Then I created a powershell script such as this:
<code>If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))

{
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}
import-module RemoteDesktop
$name = [Environment]::Username
$session = get-rdusersession | Where-Object -Filter {$_.username -eq $name}
Invoke-RDUserLogoff -UnifiedSessionID $session.SessionID -HostServer $session.HostServer -Force
</code>
Again, this requires administrator rights and will not work too. It works for admins but nor for regular users.
Does anybody have an idea how I can accomplish this?
Answer: It turned out to be a lot simpler than I thought. Simply using and publishing "logoff.exe" under sys32 does the exact same job without the complexity of permissions.
Answer: Couldn't you add this to the task scheduler with elevated privileges and then execute it when needed? Use an event id that is triggered when the user disconnects from Term services to run your script.  That should execute after they disconnect and because you can provide admin credentials to the task, it should run without triggering a UAC prompt..
Not sure what that event ID is, but I am sure there is something in the logs that records it, but you might have to enable logging..
Comment: Well, it isn't a scheduled task, but rather an eventid driven task.  Keep in mind the task scheduler can do things that aren't just timed, but rather triggered from other events.  (Like logon, pc startup, events in the event log etc).  So if you wanted to do something after they are disconnected, you'd need a trigger to do that.  This is one way to make that happen.  Most people overlook the power of Scheduled tasks and assume it can only do things based on time.
Comment: Wow, ok downvote awesome.  Let me try to explain it again.  The user isn't necessary in the Event Log for my method.   Simply filter your Get-RDSession command to filter for the user you want to logoff, and for the session to be disconnected {$_.SessionState -eq 'STATE_DISCONNECTED'}.  Even if the trigger fires when normal users log off, the Powershell would ignore active sessions.  It is same script you have now, only add a filter for disconnected so if the user is logged in a second time or if they are connected, it doesn't log the disconnected session off.
Comment: Event ID won't be specific to that user.
Comment: Sorry I did not want to downvote. I just can't take it back now.

Also, running this as a scheduled job is simply a bad idea, sorry.
Comment: @user2629636 you can take it back.  Just click the down vote button again.


Cartesian equations from given hyperplanes

Question: I have a question that says...
Give Cartesian equations of the given hyperplanes:
A. x$  =(-1,2) + t(3,2)$
B. the plane passing through (2,0,1) and orthogonal to the line x=(2,-1,3)+t(1,2,-2)
For part a, I have...
found the normal vector a=(2,-3) since (2,-3) $\cdot$ (3,2) =0 and you want a $\cdot$ x = 0. So the cartesian equation would then be...
$2x_{1} - 3x_{2}$
Is this right or have I misunderstood a concept?
As for part b, I'm totally lost. I know I want my answer to be in similar form as in a, but I'm confused on how you use the information you're given. Could someone please explain.
Comment: You don't have an equation yet. You've got an expression that might be one side of an equation.
Answer: Hints:
(a) Equations need equal signs. What does your expression equal? The constant that you pick will ensure that your hyperplane actually passes through at least one of the points that the given line does (and isn't merely parallel to the given line).
(b) As in part (a), we need the plane's normal vector. This vector should be perpendicular to every vector lying in the plane. But we already know that the given line is orthogonal to the plane, so just use its direction vector. Finally, as in part (a), choose your constant in such a way that your candidate hyperplane actually passes through the given point.
Comment: I guess I'm still confused on how to choose the constant, can you explain a little more. Also in (a) how do I go about determining what my expression is equal to?
Comment: Suppose that $2x_1 - 3x_2 = k$ for some constant $k$. We know that the line passes through a particular point. Plug this point into the equation to solve for $k$.


C++ calling C# options

Question: We have native Win32 C++ code and a set of C# assemblies which we wish to call from the C++ code.  I summaries our optios as:

Use COM.  The C# code would need to be decorated with additional attributes (GUID, COMVisible).  The C# assemblies would need to be registered regasm and would then be available to the native C++ code via COM.
Use a C++/CLI (formerly managed C++) wrapper class.  A C++ class could be added to the native C++ project.  That class would be compiled with /clr.  The native C++ code would call the C++/CLI class which would then call the .Net code.  No COM involved.  The CLR is started by magic as required with marshalling handled by the C++/CLI extenstions.
Host an instance of the CLR in the native C++ code.

I'm going to discount option 3 as I don't see the benefits over option 2 other than we lose the need for a wrapper class.  So the question is, what are the pros/cons of option 1 versus option 2?
Thanks in advance.
Answer: Option 2 will perform the best, and be the most seamless and maintainable, IMO.
There is really no advantage to option 1 that I've found.  Using C++/CLI seems to function much better, work faster, and be much simpler in general.
You also can, btw, just use the C# assembly directly without having a wrapper class.  This does require compiling any files that want to use it with /CLR, but it works quite well.
Answer: For option 1 your main pro would be not having to write a wrapper class which can get hairy depending on your project.
For option 2 you won't have to modify your managed library to facilitate unmanaged use, which is sometimes not an option.
For me it comes down to where you want to make your code changes.
Answer: With option 2 you also have a pretty straightforward way of subsequently convert your whole application to C++/CLI to avoid the managed/unmanaged transitions that you will get. The transitions could be an issue depending on how you use your referenced assemblies i.e. getting a performance hit.
So far I have had only positive experiences with C++/CLI and can recommend going that route.

C++ calling C# options


Histogram in a loop: Leave page blank when no data is found

Question: I am printing a number of histograms in a loop. However when no data is found hist() gives an error "invalid number of 'breaks" and the program terminates. Is there any way I can leave the page blank when no data is found.
Here is  a sample part of the code I currently have
    par(mfrow=c(4,1))
    hist(data1,col="blue")
    hist(data2,col="blue")
    hist(data3,col="blue")
    hist(data4,col="blue")
    mtext(paste(title, "Speed Plot", sep=''),outer=TRUE)

Appreciate any help! Thank you.
Comment: Where is the `loop` here?
Comment: What error message does it return?  What do you mean by "no data found" ?  That is: is there, e.g., an object `data3` which is empty, or is there no such object `data3` ?  In the latter case, why are you writing code like that?  And in any case, please rewrite your code in a loop. Maybe `all_data<-ls(pattern='data[0-9]') ; for (j in 1:length(all_data) hist(eval(as.name(all_data[[j]])),col='blue')` which has the added advantage of not trying to find nonexistent objects.
Answer: Wrap the function in try
try(hist(data1, col="blue"))
To explicitly create a blank plot, you can use tryCatch
tryCatch(hist(data1, col="blue"), error=function(e) plot.new())

Comment: For some reason rCatch() does not work, it gives me the same error as "invalid number of 'breaks". Do I need to use both try and tryCatch subsequently for each plot
Comment: Apologies for the typo tryCatch()
Comment: You need to use `try` or `tryCatch` for each function that may output an error. The error message will still display, but the loop will continue to run.
Comment: You are right. Got this to work! Used try(). tryCatch() was making blank pages for all remaining plots from where it found an error.


Estimating event duration by sampling with a Poisson process

Question: Take an event to be a set $E \subseteq {\mathbb R}^{\ge 0}$. We want to estimate the duration of the event (the measure of $E$) by sampling using a Poisson process $N$. That is, for some experiment duration $D$, at each time $t\leq D$ such that $\forall t'>t\colon N(t')>N(t)$, we check whether $t\in E$. See TagTime: Stochastic Time Tracking for Space Cadets for more context.
Given $n$, the number of sampled times $t\in E$, and $D$, how can we find a $c$-confidence interval for the duration of the event?
In the comment thread in the link above, the following formula is proposed, where $g=1/\lambda$ is the mean interval for the Poisson process, and $Q^{-1}$ is the inverse of the regularized incomplete gamma function (InverseGammaRegularized in Mathematica):
$$\left\{g Q^{-1}\left(n,\frac{c+1}{2}\right),g Q^{-1}\left(n,\frac{1}{2}-\frac{c}{2}\right)\right\}$$
I suspect that the $n$ in the upper bound should be $n+1$, because without this, we get no upper bound if $n=0$.
Not understanding the math well enough to determine which formula is correct a priori, I wonder how we might go about testing a formula like this by simulation. My guess is that we should run many experiments, where in each experiment, we choose a random event $E$ and use a new Poisson process. If the formula is correct, the fraction of runs in which the event duration is within the bounds should be close to $c$. But how should we choose $E$?
Comment: To help you clarify your thinking, please consider this: why do you consider $E$ to be "random"? In your considerations, also ponder whether the set $E$ itself  matters in any of the calculations or simulation, or whether all that matters is the measure of $E$.
Comment: Tom, the randomness is in the Poisson process.  It matters not whether E is fixed or random, provided you do not change its measure.  For a simulation study, then, you would create a sequence of E's of different measures and resample the Poisson process multiple times.
Comment: Having read the link, it's not clear to me whether you really mean "duration" or, alternatively, "long-run fraction of the time the system is in $E$".  The two are different; if the latter, there's a much simpler way of calculating a confidence interval from a sample of Poisson arrivals.
Comment: whuber, I suspected that all that matters is the measure, and so ran experiments where, for each sampling time, the result was a Bernoulli trial with success probability equal to the measure of E divided by the duration of the experiment. I feel that E must be chosen "randomly" each time due to my understanding of the definition of a c-confidence interval. So for example, using Bernoulli trials, I thought it necessary to choose the measure of E randomly. Not sure what you are getting at.
Comment: jbowman, I think we want "long-run fraction of the time the system is in $E$", like you say. I see the formula above as providing an estimate for this given the currently available data (after dividing by $D$). What's the difference? In any case getting estimates of total event duration during an experiment is also useful, but long-run fraction is more useful.
Comment: The difference can be illustrated by example:  $E$ occurs every 9 seconds and has a duration of 1 second, or $E$ occurs every 90 seconds and has a duration of 10 seconds.  Either way, the long run fraction of the time the system is in $E$ is 1/10.
Comment: Note that in those cases, $E$ should be defined to be the union of the segments. In the first case, $E=\left[0,1\right]\cup\left[9,10\right]\cup\left[18,19\right]\ldots$. So the measure of $E$ (what I call the 'duration') in either case is the same. With the experiment duration $D$ involved, you might get different answers for each, but as $D$ increases, we should approach the long run fraction if we just divide the bounds from the formula above by $D$. Right?
Comment: Since Poisson Arrivals See Time Averages (PASTA) the fraction of Poisson arrivals (i.e., your samples) that see the system in state $E$ will indeed converge to the long-run fraction of the time the system is in state $E$ as the sample size and duration $D$ go to infinity.  If the system starts out in a steady state then the fraction of samples that see the system in state $E$ is an unbiased estimate of the long run fraction of the time the system is in $E$.
Answer: Each Poisson arrival will see the system either in state $E$ or not-$E$ ($\bar{E}$).   One might suspect that we can construct confidence intervals for the long run fraction of the time in state $E$ (LRF($E$)) by treating this as a sample from a binomial distribution for which we are estimating the probability $p$, but there are two problems with a straightforward approach.  First, the constructed CIs only apply to the sample interval $D$.  To see why this is a problem, imagine we sample over 10 seconds, 5 of which are in $E$ and 5 not.  Increasing sampling frequency while holding $D$ fixed will cause our estimate to converge on 0.5 and our naive confidence intervals to shrink towards a width of 0, but the long run fraction of the time spent in $E$ might well not equal 0.5; $D$ is itself a sample from the long run.
Second, successive observations are not independent.  Imagine the system alternates between one hour in $E$ and one hour in $\bar{E}$, forever.  If the sample interval $D$ is, say, 10 seconds long with start time uniformly distributed over (0,2) hours, with high probability we will see exactly 1 sojourn in either $E$ or $\bar{E}$ and we will estimate LRF($E$) to be either 0 or 1.  Our estimate will be very inaccurate, even with a sample size of 1000 over the 10 seconds.  If, on the other hand, we have the duration of $D$ = 10 years with 1000 samples in $D$, we will see close to 500 sojourns in $E$ and 500 in $\bar{E}$ and our estimate will be close to 0.5.  Our estimate, although based on the same sample size as the previous example's estimate, will be much more accurate.  In either case, though, the estimator is unbiased, as its expected value is 0.5.
The other factor (besides sample size) that counts for constructing CIs for the LRF($E$) is evidently the number of distinct sojourns in $E$ and in $\bar{E}$ we see while in the steady state.  The ideal case is when the sojourns are tagged, so our sample not only counts the frequencies of $E$ and $\bar{E}$ but the number of distinct sojourns into each state.  Otherwise, if the mean duration of a sojourn in $E$ and $\bar{E}$ is much shorter than the mean inter-arrival time of our Poisson process, then we can assume almost all the observed $E$s are distinct sojourns, in which case the Binomial sampling approach gives only slightly too small confidence intervals.  (This is pretty much the best case; it wastes the fewest Poisson samples.)  If, on the other hand, the mean duration of a sojourn in $E$ and $\bar{E}$ is much longer than the mean inter-arrival time of our Poisson process, we might assume that a run of observed $E$s represents a single sojourn of the system in $E$, and likewise for $\bar{E}$.
Either way, by adding the sojourn counts for $E$ and $\bar{E}$ together, we get an estimated (or calculated) total number of sojourns $N$.  Let $\hat{p}$ be the fraction of samples which saw state $E$; if $N\hat{p}$ and $N(1-\hat{p})$ are both large enough, say > 5 (rule of thumb), then we can construct an approximate CI using $\hat{p}$ and $\sqrt{\hat{p}(1-\hat{p})/N}$ as the mean and standard deviation in a Normal distribution, similar to what we would do with true Binomial sampling.  Otherwise, we can construct an approximate CI using $N$ and, for the lower bound, $x = \lfloor N\hat{p} \rfloor$, for the upper bound, $x = \lceil N\hat{p} \rceil$, and pretending that we observed $x$ from a Binomial sample of size $N$.
Note that neither of these approximate CIs is likely to be any good when you're not in one of the two extreme cases described above.
Here's a little simulation that will illustrate the point.  The duration of $E$ and $\bar{E}$ are both distributed Exponential(1), so the long run fraction of the time the system is in $E = 1/2$.  I sample 1000 times at rates of every 0.01, 0.1, 1, and 10 time units, and repeat 1000 times, estimating $p$ (the long run average) each time.  In the first case, it's easy to see we expect to see about 5 sojourns each in $E$ and $\bar{E}$ for an effective sample size of 10; in the latter, about 500 each, for an effective sample size of 1000.  Here's the code and results:
MTBSamples <- c(0.01, 0.1, 1, 10)
EOn <- function(st) {min(which(E.Transition>st)) %% 2 == 1}
      # Odd = not in E, Even = in E

phat <- matrix(0, nrow=1000, ncol=length(MTBSamples))
for (i in 1:length(MTBSamples)) {
  for (j in 1:nrow(phat)) {
    E.Transition <- cumsum(rexp(20000,1))   #20000 = Lots more than needed
    SamplePoints <- cumsum(rexp(1000,1/MTBSamples[i]))
    phat[j,i] <- mean(sapply(SamplePoints, EOn))
  }
}

# Mean of estimates of long run occupancy fraction
colMeans(phat)
[1] 0.530586 0.502062 0.500477 0.499649

# Standard deviations of estimates of long run occupancy fraction
apply(phat,2,sd)
[1] 0.15686412 0.05153150 0.02290577 0.01681441

# (Estimated) effective sample size
0.25/apply(phat, 2, var)
[1]  10.15998  94.14438 476.48622 884.25297

Comment: I had a hard time getting through the first paragraph: it looks like something got garbled during the posting. Do you think you could clean that up?
Comment: @whuber ... or I was channeling James Joyce when I wrote it.  I'll redo the post later today (it's midday my time.)  BTW +1 for the tact!
Comment: Thanks for the detailed answer. This is much more complicated than I expected, and still (at least!) a little above my head.
Comment: I'm left wondering whether the model you use for the events is good for the specific application — for example, after thinking about it more, it seems hard to define the "long-run fraction of the time" in this case, since the events are all finite (i.e. the system is in state "dead" almost all of the time). I suppose you have to model the data as if your current behavior were extended forward through all time? Going to go ahead and mark this as the best answer now, though.


logistic regression continuous independent variable interpretation in R

Question: I have age as a covariate in my material. A continuous variable. The age varies between 18-70 years.
I'm into a logistic regression and do not really know how to treat the variable. As a linear effect or as a polynomial?
   gender       passinggrade age    prog
1    man          FALSE      69     FRIST
2    man             NA      70     FRIST
3 woman             NA       65     FRIST
4 woman           TRUE       68      FRIST
5 woman             NA       65     NMFIK
6    man          FALSE      70     FRIST

my model;
mod.fit<-glm(passinggrade ~prog+gender+age,family=binomial,data=both)

summary(mod.fit)
Coefficients:
            Estimate Std. Error z value Pr(>|z|)
(Intercept)  2.42653    0.28096   8.636  < 2e-16 ***
progLARAA    0.44931    0.25643   1.752 0.079746 .
progNASTK   -0.15524    0.26472  -0.586 0.557597
progNBFFK    0.12091    0.65460   0.185 0.853462
progNBIBK   -0.18850    0.37656  -0.501 0.616659
progNDATK   -2.84617    0.73077  -3.895 9.83e-05 ***
progNFYSK    0.64391    0.19634   3.280 0.001040 **
progNMATK    0.18424    0.16451   1.120 0.262733
progNMETK    0.22433    0.29086   0.771 0.440554
progNMFIK    0.38877    0.42152   0.922 0.356373
progNSFYY    0.97205    0.29320   3.315 0.000915 ***
progSMEKK   -0.58043    0.18185  -3.192 0.001414 **
genderman   -0.05623    0.10477  -0.537 0.591496
age         -0.11780    0.01028 -11.462  < 2e-16 ***

how would you treat the variable age?
and how should I interpret the results for age?
Answer: When considering age in regression it is important to have some understanding of its relationship across the population. In the context of education, it is possible that the relationship is not linear across a wide age range.
As an example, in children aged 5 - 16, you would expect mathematical ability to increase as older pupils have had greater exposure to teaching. If one extends the age range, it is likely that individuals continue to improve in higher education (college / university). However, it is possible that a point is reached where individuals begin to forget what was learnt at school and college and their numerical skill declines. How many OAPs can still perform complex integration?
A simple solution to assess or control this effect in your data is to group age as a continuous variable into meaningful categories with an appropriate granularity. You may need to 'relevel' this new variable to provide an appropriate reference category.
Comment: Good explanation of why the effect of age may well not be linear; but what could "meaningful categories" be, other than age ranges over which you'd expect the response to be constant, with step changes at the boundaries between categories? How often would modelling age in such a fashion be appropriate? Representing age as a polynomial, as the OP suggests, or as a natural spline, would usually be a more sensible way to deal with curvilinearity - see [here](http://stats.stackexchange.com/questions/68834/).
Comment: Thank you very much. Do you have to change anything in the code when I'll treat it as a polynomial? @Scortchi
Comment: @Malin: See `?poly` & `?I` (for the use of e.g `I(x^2)` in a formula). Some reading up on regression in general might also be useful: [Faraway (2002), *Practical Regression and Anova using R*](http://cran.r-project.org/doc/contrib/Faraway-PRA.pdf).


Android Studio - Input Method Manager

Question: Android studio version :2.3.3
The code below is not working and it should hide the keyboard but its not.Please help.
<code> InputMethodManager imm = (InputMethodManager)
    getSystemService(Context.INPUT_METHOD_SERVICE);
    public void setImm(InputMethodManager imm) {
    this.imm = imm;
    }
    public InputMethodManager getImm() {
    imm.hideSoftInputFromWindow(urledit.getWindowToken(),0);
    return imm;
    }
</code>
Comment: Hi, welcome to stack overflow. Please refer the [ask] link for
more details on how to ask a question and update your question accordingly.
Answer: <code>/**
 * Hides the soft keyboard
 */
 public void hideSoftKeyboard() {
   if(getCurrentFocus()!=null) {
   InputMethodManager inputMethodManager = (InputMethodManager)
 getSystemService(INPUT_METHOD_SERVICE);
   inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
  }
}

 /**
 * Shows the soft keyboard
 */
public void showSoftKeyboard(View view) {
   InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
   view.requestFocus();
   inputMethodManager.showSoftInput(view, 0);
}
</code>
Try this to hide and show keyboard
Answer: Hope to Useful
<code>public static void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
View view = activity.getCurrentFocus();
if (view == null) {
    view = new View(activity);
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
</code>
Answer: Hi use this code and it's working 100%
<code>            View.OnTouchListener disable = new View.OnTouchListener() {
            public boolean onTouch (View v, MotionEvent event) {
            v.onTouchEvent(event);
            InputMethodManager img =
           (InputMethodManager)v.getContext().getSystemService(INPUT_METHOD_SERVICE);
            if (img != null) {
            img.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }
            System.out.println(ee.getSelectionEnd());
            return true;
           }
           };
           //create a Button to setOnTouchListener(event)
           Button ee=new Button(this);
           ee.setOnTouchListener(disable);
</code>

Android Studio - Input Method Manager


`circular dependency` - inheritance in application controller of Rails 4.2.0 engine

Question: When upgrading engine from Rails 3.2 to Rails 4.2.0, the following inheritance in application controller causes circular dependency error in rspec:
class ApplicationController < ApplicationController
end

We have config.eager_load = false for config/development.rb.
The error:
activesupport-4.2.0/lib/active_support/dependencies.rb:492:in `load_missing_constant': Circular dependency detected while autoloading con
stant Authentify::ApplicationController (RuntimeError)

Here is the Rails engine document (ch:4.3.2) explaining this type of code practice. As I understand, the purpose of this inheritance is to allow the engine to access methods in Rails app or other engine which the current engine is mounted to. We would like to do the same in Rails 4.2.0 engine. How to fix this problem?
Answer: your applicationController is clearly trying to inherit from itself, it should rather look like
class ApplicationController < ActionController::Base
Comment: if so, you should add a namespace for your controller, which inherits from ApplicationController, like here -> http://guides.rubyonrails.org/engines.html#using-a-controller-provided-by-the-application, simply you cannot inherit Something from Something, as it will cause circular dependency
Comment: Please refer rails engine document in the post. This is a Rails recommended code practice.
Answer: In rails 4 engine, the right format is:
class ApplicationController < ::ApplicationController
end

assume the class is within module MyEngine. Or
class MyEngineName::ApplicationController < ::ApplicationController
end


Show that $x_1' = x_2, \space x_2' = -2x_1 + x_2(2-5x_1^2 - 3x_2^2)$ has a bounded solution

Question:
Consider the dynamical system :
  $$x_1' = x_2, \space \space x_2' = -2x_1 + x_2(2-5x_1^2 - 3x_2^2)$$
  where $x_1,x_2 \in \mathbb R$. Using the functional $V(x_1,x_2) = \frac{1}{2} x_1^2 + \frac{1}{2} x_2^2$, prove that for any given inital value, the initial value problem has a bounded solution.

Attempt :
Take the derivative over the solution curves for the given functional, which is :
$$\dot{V}(x_1,x_2) = -x_1x_2 + x_2^2(2-5x_1^2-3x_2^2)$$
I will try "increase" this expression via inequalities (which is a standard way of handling such problems), so that I can provide a compact form with $V(x_1,x_2)$ in expressions which we know it's positive :
$$\dot{V}(x_1,x_2)\leq-x_1x_2 + x_2^2(2-3x_1^2-3x_2^2)\leq\frac{1}{2}x_1^2 + \frac{1}{2}x_2^2 + x_2^2(2-x_1^2 - x_2^2)$$
Now, I have reduced the coefficients inside the parenthesis, since $x_1^2,x_2^2 \geq0$ which means that a smaller coefficient leads to a bigger quality. The inequality derived for the expression at front is a simple application of $(x_1+x_2)^2 \geq 0$.
However, I cannot seem how I should continue from now on, since that $x_2^2$ in front of the parenthesis is bugging me and I do not know how to convert it while keeping the inequality in a form that would produce an expression of $V$.
Any tips ?
Comment: Not really. It seems that there is an attracting limit cycle that surrounds the origin. What is repelled from the origin goes on to this limit cycle. The trajectories outside of the limit cycle go to it too. The only reason why I accentuated this is because you have to prove that $\dot{V} \leqslant 0$ holds for all points outside of some circle $x^2 + y^2 = R^2$ and in some other places this inequality doesn't really hold (near the origin, for example). I thought this observation can help you get better estimates, but probably you already had this in mind.
Comment: If $\dot{V} \leqslant 0$ holds everywhere, the origin would be asymptotically Lyapunov stable equilibrium. But it's not true, it's unstable focus. Actually you have to split your analysis of $\dot{V}$ into two separate cases.
Comment: @Evgeny Not interested for stability in this case but just showing that the solutions would be bounded which just relies into  forming an inequality that would involve an expression like $V^k(x)(R-V^n(x))$. Then you'd just take a restriction for $V(x)$ and it would be bounded !
Comment: @Evgeny The fact that every trajectory is repelled though, shouldn't lead to the solutions being not bounded ?
Comment: Did your textbook tell you to consider this function $V$, or did you make a typo?
Comment: @Did It's an exams problem, so this is exactly the functional $V$ that we should use. I accepted the answer below though since it's interesting and works some things around another way. I added a comment elaborating for the method I used but maybe through a weird inequality it could be shown that an $R\geq 2/3$ would be needed.
Comment: It seems the $x_1x_2$ term in $\dot V$ is really ruining things since it dominates $x_2^2$ when $|x_1|$ is much larger than $|x_2|$, so what we would have here is an example of a bad exam problem.
Comment: @Did Yeah indeed. Because even when you apply that : $$-x_1x_2 \leq \frac{1}{2} x_1^2 + \frac{1}{2} x_2^2 = V$$ you still get the issue of $x_2^2$ which you cannot work around simply by increasing it to $$x_1^2+x_2^2$$ exactly because the sign of the parenthesis multiplied is not strict. I was trying to work around a solution to this problem thinking of some inequality but it seems like it's exactly what you mentioned (your contribution has been really interesting and good).
Answer: The functional $V$ is ill-adapted to this system. On the other hand, a one-line computation shows that $$W(x_1,x_2)=2x_1^2+x_2^2$$ is such that $\dot W(x_1,x_2)$ has the sign of $$Z(x_1,x_2)=2-5x_1^2-3x_2^2$$ Thus $\dot W\leqslant0$ on the set $\{W\geqslant3\}\subset\{Z\leqslant0\}$ and $\dot W\geqslant0$ on the set $\left\{W\leqslant\frac23\right\}\subset\{Z\geqslant0\}$.
This shows that every solution starting from $(x_1,x_2)\ne(0,0)$ ends in the annulus $$\left\{\tfrac23\leqslant W\leqslant3\right\}$$ which implies the desired boundedness result. The behaviour we just described is confirmed by the rough figure below.
Comment: Really nice plot there ! It shows exactly the effect of $θ' = -1$ which is interesting to see ! The selection of $W$ really comes in hand in this particular example, I don't know why the exercise implies another functional which makes our life tougher. Truth is though, it's interesting to see that the sign of the expression acts on the annulus. I guess it could be shown in my elaboration too that if you implied $ \geq \dots + (x_1^2 +x_2^2)(2-5x_1^2 - 3x_2^2)$ then you'd need to specify the sign of the parentheses which would lead to the same annulus as yours ! Great stuff again.


How to solve function for x having x and x^2

Question: I want to solve the following funktion for $x$, is that possible? And how woult it look like?
$y = xp -qx^{2}$
Thanks for Help!
Comment: This is not an equation, this is a function.
Comment: use the quadratic formula
Answer: Change the order of the multiplications then add $qx^2-px$ to both sides of the equation to get
$$qx^2-px+y=0$$
This is now a quadratic equation in standard from $ax^2+bx+c=0$, with $a=q,b=-p,c=y$. Substitute those values into the quadratic formula to get your solution(s) to the equation. I'll leave that last part to you.
Answer: Notice, we have $$y=xp-x^2q$$
The above equation can be solved for $x$ in terms of $p$ & $y$. The above equation can be rearranged as follows $$q\color{red}{x^2}-p\color{red}{x}+y=0$$ The above equation is a quadratic equation of form $\color{blue}{Ax^2+Bx+C=0}$ in terms of variable $x$ which can be solved by using quadratic formula.


Current location in not working in Google Map

Question: I have integrated google map in swift 3, when map screen appear than current location in not showing, i have added two keys in .plist file and also set CLLocationManager delegate and requestAlwaysAuthorization
class MapViewController: UIViewController, CLLocationManagerDelegate, UITextFieldDelegate {

    @IBOutlet var mapView: GMSMapView!
    var marker: GMSMarker?

    override func viewDidLoad() {
         super.viewDidLoad()
         self.title = "MapVC"
         self.doSetupUI()
         self.searchLocation()
   }

   override func viewWillAppear(_ animated: Bool) {
         let locationManager : CLLocationManager = CLLocationManager()
         locationManager.delegate = self
         locationManager.requestAlwaysAuthorization()
   }

func doGoogleMapSetup(lat : Double , lng : Double) {
    let camera = GMSCameraPosition.camera(withLatitude: lat, longitude:lng, zoom:16)
    let mapView = GMSMapView.map(withFrame: .zero, camera:camera)
    mapView.isMyLocationEnabled = true

    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: lat, longitude: lng)
    marker.snippet = ""
    marker.appearAnimation = kGMSMarkerAnimationPop
    marker.map = mapView

    let arrPoints : NSMutableArray = NSMutableArray()
    arrPoints.add(UserDefaults.standard.object(forKey: "addressPoints"))

    for i in 0..<arrPoints.count {
        let path : String = (arrPoints.object(at: i)as! NSMutableArray).object(at: 0) as! String
        let route : GMSPath = GMSPath.init(fromEncodedPath: path)!
        let polyLine : GMSPolyline = GMSPolyline.init(path: route)
        polyLine.strokeWidth = 2.0
        polyLine.strokeColor = UIColor.red
        polyLine.map = mapView
    }
}

Comment: please show your code
Comment: Also, what do you mean by "location"? Are you talking about a place mark, indicating user location? Are you trying to get the center coordinates of the map? Or something else?
Comment: hiii, i m trying get user's current location
Comment: func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) {
        let latitude: Double = newLocation.coordinate.latitude
        let longitude: Double = newLocation.coordinate.longitude
        currentlat = latitude
        currentlong = longitude

    }
Comment: @Dhanraj: You shouldn't need the map to get the user's location. locationManager.location should give you the Geocoordinates of your user.
Comment: Hiii, before some days its working perfect and now its not working in my other app have same problem
Comment: What's the problem? What does print(locationManager.location) show in your console? Also, are you running in the simulator?
Comment: i m running in iPod and locationManager.location print "nil"   my app is not listed in Location Service in Settings
Comment: That means that the device is not tracking the user's locations. I've built lots of location-based apps, and the most common problem I run into, is that you call the startLocating method (which prompts the user to allow location tracking) in the same ViewController as the location request. What this can do, is have the app ask the manager for the location, before it has the location. Does that make sense?
Comment: Though there are other reasons why it may come back nil: the app doesn't have permission, the phone/simulator's GPS is broken, for instance.
Comment: when screen appear than allow location tracking prompts comes i have clicked on "Allow" option but there nothing happened
Comment: You should also call locationManager.startLocating(). This tells the device to start tracking the user's current location. I will leave a more detailed answer, describing a race condition I've frequently encountered, as an answer below. It will take me a minute.
Comment: @Dhanraj where are you calling `doGoogleMapSetup`
Comment: @dylanthelion  don't need any `CLLocationManager` in case of google maps. All we just need is to add keys in plist.
Comment: @Dhanraj Check the answer I posted. tested on iOS 10 device, working fine
Comment: Hello all, issue solved, there some code sequence problem and thanks to all for help
Answer: For showing current location we don't need any location manager in case of GoogleMaps. All we need is to add one of the keys or both in the .plist. So make sure the key is there. I have used NSLocationWhenInUseUsageDescription key.
<key>NSLocationWhenInUseUsageDescription</key>
    <string>Allow location</string>

Also make sure that you have called GMSServices provideAPIKey method and replaced with the API_KEY you generated in google developer console. Also all the relevant Google APIs as per requirement should be enabled.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
       // Override point for customization after application launch.
       GMSServices.provideAPIKey("YOUR_API_KEY")
       return true
    }

So, I am assuming you have done all the settings and things right in google developer console.
By just writing the below line in your controller where you have made the GoogleMap can show the location allow/disallow prompt and take the permission of the user.
mapView.isMyLocationEnabled = true

However this will not animate your map to your current location. But you can manually drag the map to check the current location and you will see a blue dot at your current location.
But now we also want to animate to the current location whenever we load that ViewController. Now the need for CLLocationManager arrives. So that in its didUpdateLocation delegate, we can fetch the current location and can just animate the graph to the current location.
So here is my complete controller.
import UIKit
import GoogleMaps

class ViewController: UIViewController,GMSMapViewDelegate,CLLocationManagerDelegate {
    @IBOutlet weak var mapView: GMSMapView!

    var locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        mapView.isMyLocationEnabled = true
        mapView.delegate = self

        //Location Manager code to fetch current location
        self.locationManager.delegate = self
        self.locationManager.startUpdatingLocation()
    }

    //Location Manager delegates
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        let location = locations.last

        let camera = GMSCameraPosition.camera(withLatitude: (location?.coordinate.latitude)!, longitude:(location?.coordinate.longitude)!, zoom:14)
        mapView.animate(to: camera)

        //Finally stop updating location otherwise it will come again and again in this delegate
        self.locationManager.stopUpdatingLocation()

    }
}

Another way of doing is not using the didUpdateLocation and not using the location manager is just by using the GMSMapViewDelegate delegate method mapViewDidFinishTileRendering
func mapViewDidFinishTileRendering(_ mapView: GMSMapView) {
    let location = mapView.myLocation
    let camera = GMSCameraPosition.camera(withLatitude: (location?.coordinate.latitude)!, longitude:(location?.coordinate.longitude)!, zoom:14)
    mapView.animate(to: camera)
}

It will be called everytime the map rendering is finished.
But this comes with a limitation, it will always bring you to the current location whenever you drag/pinch/zoom map as the rendering finish everytime you play with map. So, you can just implement some kind of bool variable logic here.
You can get your location by using
let yourCurrentLocation = mapView.myLocation

Make sure to do this on a device rather than simulator. If you are using simulator, you have to choose some custom location and then only you will be able to see the blue dot.

I already gave this type of answer. Check this Link. But that was in Swift 2.x. The one which I posted in this answer is in Swift 3.x
Comment: Awesome. I would like to know, is it necessary to add the plist `Privacy - ..` if you are using the `didUpdateLocation ` ?
Comment: Yes, it is necessary to add the entries in plist in order to activate the location access for the respective app.
Answer: This is a bit detailed, so I'd like to leave it in a full answer. This is the most common reason I have encountered for a nil location, since figuring out the basics, a few years ago. So, you call CLLocationManager.startLocating(), in your viewDidLoad. Then you call the method that sets up your map. Sometimes this works, and sometimes it doesn't, because of a race condition caused by the amount of time it takes the CLLocationManager to set up permissions, on the one hand, and access the user's location, in another part of the code. Let's look at an order of events, where it doesn't work:
1) you call requestAlwaysAuthroization and startLocating
2) User permissions setup is triggered on one thread
3) In your ViewController, you request the user's location, to set up your map
4) It comes back nil
5) NOW, step 2 finishes, and the app has access to the user's location, but it's too late
The core problem, is that the process that starts with requesting permissions and location, takes more than a few milliseconds. And if your view is already set up, it takes few milliseconds for it to go through the methods in your viewDidLoad. By the time you have the location you need, you've already requested it. This has caused me too many crashes, in my location-based apps.
My workaround, has been to craft a singleton CLLocationManager, make my starting view a delegate, and requestAlwaysAuthorization and startLocating, in that view. That way, when I get to the view that needs the location, the app has already started locating, and the locationManager.location is not nil.
This is an approach that will obviously not work for every app. If you need clarification, let me know, and if you need code, as well. I have a few public iO git repos, with projects where I have encountered and fixed this problem.
Comment: Hiii dylanthelion,  thanks for help. can u plz share demos?
Comment: Check out the Develop branch of this unfinished app:
https://github.com/dylanthelion/AssassinationClient
The two things to look at are LocationManager.swift, which is a singleton and calls the methods you need to start tracking location in startLocating, and LoginViewController, which is the first view in the app, and starts tracking location immediately on app startup.
Comment: Also, it's pre-Swift 3, and may or may not build. But the coding pattern is relatively easy to implement, and should fix the problem I described.
Comment: @dylanthelion We don't need any `CLLocationManager` in case of google maps. All we just need is to add keys in plist.
Comment: @RajanMaheshwari: I see the ViewController is a CLLocationManagerDelegate, which makes me think that the CLLocationManager is active, and should expose the user's location. I'm fine with recommending your GoogleMaps-specific answer, however.


Swift alarm clock UIswitch

Question: when creating an alarm clock, is it the right way to directly display all scheduled local notifications on a table view? If so, After the notifications have fired they disappear. How can i stop them from disappearing so i can use switches to set them on and off?
What i understood about UIswitch is that i have to cancel the notification when off then reschedule them when on.
@IBAction func addAlarm(sender: UIButton) {
    let notification = UILocalNotification()

    notification.fireDate = timePicker.date.fireDate
    notification.timeZone = NSTimeZone.localTimeZone()
    notification.alertBody = "Its time"
    notification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
    notification.hasAction = true
    notification.alertAction = "View"
    UIApplication.sharedApplication().scheduleLocalNotification(notification)
    alarmTable.reloadData()
}

Answer: Use an array to store the times of the alarms.
Then you can easily show and manage them and create UILocalNotifications based on them.
Answer: You have to define the timezone before defining the firedate

Swift alarm clock UIswitch


Pointers and LValue Reference

Question: I have this situation.
 case 1
    void printInt(int & i) {}

    int main () {
       int i=1;
       printInt(&i);

    }

the printInt is expecting a reference, so therefore, inside the main, I call the printInt function and supplied with the reference i. Is this correct.
then I can also do
 case 2
     int main () {
       int i=1;
        printInt(i);    // i is a lvalue, and printInt function is expecting a lvalue

}

so, are case 1 and case 2 seems like conflicting?
Comment: The first case is not correct at all. This sample is not valid C++.
Comment: In "case 1", the two `&` have _two different meanings_: `int & i` (in `void printInt(int & i)`) declares `i` as an `int&` (reference to int), but `&i` (in `printInt(&i);`) means "address of `i`" and gives a pointer to `i` (of type `int*`, pointer to int).
Comment: @ gx_ when you print out int&, reference to an int, what do you get?
Comment: In C you use pointers to pass values by reference, it's not needed in C++ as it ha proper references.
Comment: @lilzz A reference is just an alias. Given `int i=1; int& r = i;` then anything (printing, modification) done to `r` (the "alias") is actually done to `i` (the "original"). It seems that you should read a good C++ introductory book/tutorial. For books, see http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
Answer: The first case doesn't even compile because you are passing int* but the compiler expects reference to a int type.
printInt(&i);

In the above statement, you are passing the address of an integer. So, at the receiving end it should be a pointer.
Edit:
I think you are confused between pointers and references. Think of reference as an alias to a variable. An analogy to some extent would be like a short cut for an application placed on the desktop. When you click the short cut, you are actually running the executable installed in the Applications directory. So, short cut is more like an alias for running the executable of the application.
Can short cut placed for one application be used to open different application ? No. For a similar reason, references can not be reseated.
Comment: In the implementation, a reference is a pointer, all right. On the language level, however, a reference is treated as a value, hiding the fact that actually an address is taken. As such, to call your function, you would have to write `printInt(i)`, because the compiler takes the address implicitely. Confused? That's normal. References are among the most evil inventions ever!
Comment: so what's a reference? it's not an address? then what is it?
Answer: Case 1 is actually incorrect, it is expecting a reference-able integer, however you are supplying it with pointer-to-integer.
Answer: The second case is correct. When you pass by reference a value int x to function foo(int &y) you write:
foo(x);

This is as if you said:
int &y = x;

In the first case you call
foo(&x);

which is as if you said:
int &y = &x;

This is not how a reference is defined.

Pointers and LValue Reference


Creating Parameters for shared datasets using Multidimensional Data

Question: ive been trying to create a SSRS report based off a cube in the system. This report is to use a shared datasource and a shared dataset. So far, ive created a shared datasource and a shared dataset which has about 5 dimensions as parameters in the filter area (with the "equal" operator) with the parameters field checked off and a bunch of dimensions and measures in the results grid.
Now, when i create a report using this shared dataset, the 5 dimensions present in the filter area do appear as parameters, however, when i run the report, these parameters have no values in the drop down lists. Am i missing something in the process? Is this a permissions issue?
When i create a local dataset then things are fine and the parameters are populated with the members of the dimension. but the same doesnt work on a shared data set.
Also, i m using the report builder 3.0.
Has anyone run into the same issue? Please help.
Answer: I think parameters are local to a report and aren't really seen as shared.  You can share a dataaset, but I'm not sure how you would share a parameter as it requires another dataset to populate it. When you create a parameter from an SSAS source, SSRS creates a hidden dataset for each parameter and uses that to populate the parameter drop-down list.
You can write another shared dataset that uses the same MDX as what SSRS normally does for the hidden dataset and make it available to report builders. For instance, here is the MDX that SSRS generates for a calendar year parameter:
<code>WITH
Member [Measures].[ParameterCaption] as [Date].[Calendar Year].Currentmember.CAPTION
MEMBER [Measures].[ParameterValue] as [Date].[Calendar Year].Currentmember.UNIQUENAME
MEMBER [Measures].[ParameterLevel] as [Date].[Calendar Year].Currentmember.LEVEL.ORDINAL
SELECT {[Measures].[ParameterCaption], [Measures].[ParameterValue], [Measures].[ParameterLevel} on COLUMNS,
[Date].[Calendar Year].ALLMEMBERS ON ROWS
FROM [CUBE]
</code>
You can take this and adapt it for whatever dimension hierarchy you need.  If you were doing a product category parameter, you would just replace [Date].[Calendar Year] with [Product].[Category] in the query.
Comment: Hey,Thanks for the response. This approach works in the sense that the second shared dataset does populate the dropdown, however, Since the filtered list is time based (i.e. depending on the time frame of the report the list would vary) , would there be a way for me to constraint the secondary dataset based on the time frame of the report?
Comment: yep, add a where clause to the end of the MDX statement or a crossjoin to the mdx statement and put a parameter in the query whose value would be passed in from a cascading parameter


NoClassDefFoundError thrown when specifying same jar file at compile and run time

Question: Hi I'm trying to use an external Java package from my own code and keep getting NoClassDefFoundError even though I am using the same class path that I compiled with.
For your amusement I have included a bare bones reproduction of what will undoubtedly be a dumb mistake on my part (I've been at this for about 6 hours so far)
/* WTF/WTF.java */
import foo.Bar;
class WTF
{
    public static void main(String[] args)
    {
        Bar dontCare = new Bar();
    }
}

/* WTF/foo/Bar.java */
package foo;

class Bar
{
    public Bar() {}
}

Now from the WTF directory I run the following:

javac foo/Bar.java              [ok]
javac WTF.java                  [ok]
java WTF                        [ok]
jar cf foo.jar foo              [ok]

I remove the WTF/foo directory so there is only WTF/foo.jar available.

javac WTF.java -cp foo.jar      [ok]
java WTF -cp foo.jar            [$!@#$!]

Exception in thread "main" java.lang.NoClassDefFoundError: foo/Bar
    at WTF.main(WTF.java:7)
Caused by: java.lang.ClassNotFoundException: foo.Bar
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)

Any clues greatly appreciated!
Comment: I wonder if this even compiled!
Comment: My mistake Bar was marked public in my test file but I missed it when writing the listing - apart from that yes it does compile
Answer: Wrong arguments sequence? Try:
java -cp foo.jar WTF

Comment: Sounds like it. The parameters after the main classname are parameters to the program. But it should be `java -cp .:foo.jar WTF` to include the local directory if it contains `WTF.class`.
Answer: The directory where is WTF.class should be in the classpath.
Also, Bar should be public.
java -cp foo.jar:. WTF

: is the path separator in Linux, if you are using Windows replace it with ;.
Comment: Not it, according to the error message `WTF` is found, but not `foo.Bar`.
Comment: @christoffer: yes, but the classpath argument must be before the main class name and I think the current directory should be in it


Copy the complete structure of a table in another table

Question: How can I copy the complete structure of a table in another table with PK, FK, etc.
The following query is missing constraints:
<code>SELECT top 0 * INTO [DestinationDB].[tableSchema].[tableName] FROM [SourceDB].[tableSchema].[tableName]
</code>
Comment: Please do not *SHOUT* at us (in your title); it's is considered quite rude.
Comment: See https://dba.stackexchange.com/questions/65734/how-to-create-new-table-with-same-constraints-and-indexes for an explanation and solution
Comment: https://learn.microsoft.com/en-us/sql/relational-databases/tables/duplicate-tables?view=sql-server-ver16 - does this link solve your problem?
Comment: there is no one-line command to do this, as each constraint, index, etc needs names which you would need to supply or automate; basically, the answer here is "manually and carefully, but thoughtfully" - or there might be some tools inside SQL Server Explorer
Comment: Sorry @ThomA  it's the first time I've asked a question so I need to get familiar. In general I am used to always putting the title in capital letters.
Answer: The simplest way is not by construction your own raw SQL, because to recreate keys and other constraints it doesn't have a built in command.
If you have access to SQL Server Management Studio, you can just right click the table and export the definitions, then it will generate all that for you.
https://learn.microsoft.com/en-us/sql/ssms/scripting/generate-scripts-sql-server-management-studio?view=sql-server-ver16#ScriptSingleObject


conversion of Cstring to BYTE

Question: I am using Visual Studio c++ and want to convert the Cstring to Byte. I have written this code but it gave me error in the second line that "data" is undefined.
CString data = _T( "OK");
LPBYTE pByte = new BYTE[data.GetLength() + 1];
memcpy(pByte, (VOID*)LPCTSTR(data), data.GetLength());

Further more I need to convert LPBYTE to const char for strcmp function. I have written the code but I can't find the issue with it.
const LPBYTE lpBuffer;
LPBYTE lpData = lpBuffer;
CString rcvValue(LPCSTR(lpBuffer));
const CHAR* cstr = (LPCSTR)rcvValue;
if (strcmp (cstr,("ABC")) == 0)
{
    ////
}

Comment: This code is not correct at all. It works if the base type for CString is 'char', but if Unicode is enabled, and the base type is wchar_t this is broken. GetLength returns the number of characters (that is the number of char's or wchar_t's), not the number of bytes allocated to hold the string.
Comment: That code is correct (assuming you account for the trailing `0` afterwards.  Please paste the real error you're getting.
Answer: The CString type is a template specialization of CStringT, depending on the character set it uses (CStringA for ANSI, CStringW for Unicode). While you ensure to use a matching encoding when constructing from a string literal by using the _T macro, you fail to account for the different size requirements when copying the controlled sequence to the buffer.
The following code fixes the first part:
CString data = _T("OK");
size_t size_in_bytes = (data.GetLength() + 1) * sizeof(data::XCHAR);
std::vector<BYTE> buffer(size_in_bytes);
unsigned char const* first = static_cast<unsigned char*>(data.GetString());
unsigned char const* last = first + size_in_bytes;
std::copy(first, last, buffer.begin());

The second question is really asking to solve a solved problem. The CStringT type already provides a CStringT::Compare member, that can be used:
const LPBYTE lpBuffer;
CString rcvValue(static_cast<char const*>(lpBuffer));
if (rcvValue.Compare(_T("ABC")) == 0)
{
    ////
}

General advice: Always prefer using the concrete CStringT specialization matching your character encoding, i.e. CStringA or CStringW. The code will be much easier to read and reason about, and when you run into problems you need help with, you can post a question at Stack Overflow, without having to explain, what compiler settings you are using.
Answer: Make sure you include atlstr.h to provide the definition of CString, as below:
#include "stdafx.h"
#include <Windows.h>
#include <atlstr.h>

int _tmain(int argc, _TCHAR* argv[])
{
    CString data = _T( "OK");
    LPBYTE pByte = new BYTE[data.GetLength() + 1];
    memcpy(pByte, (VOID*)LPCTSTR(data), data.GetLength());
    return 0;
}

Comment: Looks like the `pByte` `BYTE` array won't be large enough if Unicode is enabled since each char in the `CString` will take 2 `BYTE`s but `data.GetLength()` will return the number of characters.
Comment: not me!  I think someone else downvoted both of us, at least temporarily.
Answer: I'm fairly certain Jay is correct for your first question. You need to include the right header.
For your second question, why would you expect that code to work? Let's walk through what the code you've written actually does.

Create a char pointer (char *) without initializing it. This leaves lpData/lpBuffer pointing to a random location in memory.
Create a CString and initialize it with this random pointer.
Extract the buffer from the CString and compare it to a string literal.

Keeping in mind that the CString contains random garbage, what exactly do you expect this code to do? (Other than crash horribly? =) )
I also want to point out that you need to be more consistent in your approach to strings. Do you plan to support both char and wchar_t based strings as your use of TCHAR in the first sections suggests? Do you want to work with C-Style strings or do you want to use objects like CString? If you want to work with CString's, just use the Compare function that CString provides. Don't bother with strcmp.
Answer: Probably you didn't include the cruicial header
    #include <afx.h>

    int main()
    {
        CString data = _T( "OK");
        LPBYTE pByte = new BYTE[data.GetLength() + 1];
        memcpy(pByte, (VOID*)LPCTSTR(data), data.GetLength());

        return 0;
    }

This code works fine.
Comment: @gok: There is no `AccessViolationException` in C++, and if there were, the code wouldn't raise it. It still fails to account for size requirements of the character type used by `CString` (could be `char` or `wchar_t`), but that causes the string to get truncated. The behavior is still well defined.
Comment: this throws `AccessViolationException`
Answer: You should rather use
 CString ss = "123ABC";

 BYTE* bp = (BYTE*)ss.GetBuffer(ss.GetLength());

 BYTE expected[16] ;

 CopyMemory(expected,bp,sizeof(expected));

Just using '=' won't work.
Comment: No, that won't work, for many reasons. When calling `GetBuffer()`, you are also required to call `ReleaseBuffer()` (or friends) later on. But you don't need a mutable pointer anyway, so just call `GetString()`. The code also fails to account for Unicode character encoding. `CString` could store characters encoded as UTF-16 (and that's the default in Visual Studio for many years), i.e. 2 bytes per code unit. Your code simply truncates such strings.

conversion of Cstring to BYTE


Curl No URL set! 3 Error

Question: I am using curl to fetch result from Punchtab when I execute this code I am getting curl exception 3 No URL set! can any one help me with it.
$tocken2 =  "https://api.punchtab.com/v1/user?access_token=".$rest->authResponse->accessToken;

  $ch1 = curl_init();
  $ca = 'c:/path/to/ca-bundle.crt';
  curl_setopt($ch, CURLOPT_URL,$tocken2);

    curl_setopt ($ch1, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt ($ch1, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch1, CURLOPT_POST, 1);
    curl_setopt($ch1, CURLOPT_REFERER, "http://locatefirms.in");
    curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);

    $server_output = curl_exec ($ch1);
    $errorMsg = curl_error($ch1);
    $errorNumber = curl_errno($ch1);
    echo $errorMsg;
    echo $errorNumber;
    echo $server_output;exit;

Comment: @Elby - comments to comments please
Answer: Your curl handle is $ch1, but you're setting the URL option to $ch.
$ch1 = curl_init(); // initializing $ch1 here
curl_setopt($ch, CURLOPT_URL,$tocken2); // setting option to $ch

Quite obviously, to fix this, you need to change the statement to
curl_setopt($ch1, CURLOPT_URL,$tocken2);

Curl No URL set! 3 Error


Is conducting an ANOVA comparable to several pairwise comparisons?

Question: I am analyzing the data of a personality survey to see whether the order in which a text was presented to participants has an effect on their scores.
For that, I am performing a one-way independent measure ANOVA with three groups (text presented 1st, 2nd or 3rd), on each of the five personality trait scores I compute from the participants. For 4 traits (i.e. in 4 ANOVAs) no significant effect between the groups can be found (which is what I expected). For one trait, however, I get a significant difference in mean values. I performed a post-hoc pair-wise Tukey HSD test and determined that only one of the three possible pairings shows significant differences.
This is a somewhat weird result, as it suggests that only neuroticism is affected by the order of my text, and only when the text is presented 3rd. I am drawn to the following interpretation: an ANOVA with 3 groups, which shows no significant results, is effectively conducting three pair-wise comparisons. I performed 5 ANOVAs, which would be comparable to 15 pair-wise tests. With an alpha level of 0.05 I would expect to get at least on false-positive in 20 tests, so finding one in 15 would not be unlikely. My conclusion: the effect I found was a random sampling effect, not an effect of group order.
However, I am not sure about my first assumption: "an ANOVA with 3 groups, which shows no significant results, is effectively conducting three pair-wise tests". Is this a warranted interpretation?
Answer: If an ANOVA test showed no statistically significant results, you should ignore any post hoc pairwise tests.
On the other hand, even if an ANOVA test shows statistically significant result, post hoc significance is not guaranteed.
Edit: ANOVA is an omnibus test. Short version: ANOVA tests the average score of each group with the average score of all groups, taking into account the size of each group and the variance of each group. It's just one Sum (Σ). So, due to "blindness/chance/randomness" you would have 5% chance for false positive for each ANOVA.
Comment: ANOVA is an omnibus test. Short version: ANOVA tests the average score of each group with the average score of all groups, taking into account the size of each group and the variance of each group. It's just one Sum (Σ). So, due to "blindness/chance/randomness" you would have 5% chance for false positive for each ANOVA.
Comment: I perform no post-hoc tests on the traits where ANOVA shows no effect. I do find significance in one post-hoc pairwise test on the three means where ANOVA shows an effect.
My interest is rather in how to interpret what an ANOVA does in general: Does it equal to comparing all means to each other pairwise, or not? If this were the case, I would have conducted 15 tests on my data, if not, only 5. This is relevant because seeing one false positive in 15 tests is more probable than seeing one in 5 tests.
Comment: Accepting this answer for the reference to omnibus test in your last comment. Thanks, and feel free to put this as an edit to the actual answer for better visibility. :)
Answer: I don't think that is a valid interpretation, Zakum.  The post-tests compare pair-wise between groups.  The original ANOVA will compare the three groups, in a one way ANOVA, on whether you can say that the means are drawn from different populations.  That is my interpretation.  Adding a second factor, the personality traits, will require additional ANOVA.  In a one-way ANOVA, the F value is computed as (Sum of Squares due to Treatments) / (Sum of Squares due to Error), so I don't think you can make the assumption about the pair-wise.  I think that is what the post-tests are for.
I am by no means a stats expert though.
EDIT: Building on the comment, if I understand what you are asking, there were three ANOVAs performed separately on 1&2; 2&3; 1&3; then I think it entirely possible that you could see one set as statistically different while the other sets have p>.05 and so to your original question, the ANOVA with 3 groups is comparing the 3 means, and the post-tests are 3 pair-wise comparisons.  As far as I understand it, the F value in the original ANOVA with 3 groups is different than the t value in the post-tests, meaning I don't think the F is a compilation of 3 pair-wise t values directly, if that makes sense.  If I understand your experimental design, then the text presented 3rd affects the neuroticism trait statistically different than text presented 1st.  Text presented 2nd may be moving the 'score' of the neuroticism trait, but it is not statistically different until it reaches the 3rd position?  So then I want to ask "what if there was a fourth in order option for the text, would it be more significant?"
Comment: Thanks for your insights. Your words about samples being drawn from the same population makes me wonder: my post-hoc pairwise comparison showed the following results: mean1 and mean2 are from the same population (no significant differences), mean2 and mean3 are from the same population (no significant differences), BUT: mean1 and mean3 are from different populations (P=0.028). Being drawn from the same population should be a transitive property (m1, m2 from same pop and m2,m3 from same pop -> m1,m3 from same pop). Any ideas how to interpret the finding that this doesn't hold?


How to natural sort string with multiple numbers in it

Question: How to do human sorting on below list, I want to sort based on last numbers e.g 45-50, 35-40 etc
list_list= ['N1-1TWS-AD03-____-001N-__45-50',
     'N1-1TWS-AD01-____-001N-__50-54',
     'N1-1TWS-AD01-____-001N-__54-58',
     'N1-1TWS-AD01-____-001N-__58-61',
     'N1-1TWS-AD01-____-001N-__61-65',
     'N1-2TWS-AD01-____-001S-__25-28',
     'N1-2TWS-AD01-____-001S-__25-28',
     'N1-3TWS-AD01-____-001S-__28-31',
     'N1-4TWS-AD01-____-001S-__28-31',
     'N1-3TWS-AD01-____-001S-__31-35',
     'N1-3TWS-AD01-____-001S-__31-35',
     'N1-3TWS-AD01-____-001S-__35-40',
     'N1-3TWS-AD01-____-001S-__35-40',
     'N1-3TWS-AD01-____-001S-__40-43',
     'N1-3TWS-AD01-____-001S-__40-43',
     'N1-3TWS-AD01-____-001S-__43-47']

I tried sort and sorted but it wasn't giving the result I wanted.
I want to sort like
['N1-2TWS-AD01-____-001S-__25-28',
'N1-2TWS-AD01-____-001S-__25-28',
'N1-3TWS-AD01-____-001S-__28-31',
'N1-4TWS-AD01-____-001S-__28-31',
 ...and so on...
'N1-1TWS-AD01-____-001N-__61-65']

Comment: Does this answer your question? [Is there a built in function for string natural sort?](https://stackoverflow.com/questions/4836710/is-there-a-built-in-function-for-string-natural-sort)
Comment: @Ch3steR not quite, his problem is simple without multiple numbers in a single string
Comment: What do you mean by human sorting? Which strings do you want to come first and which next?
Comment: @user13044086 go google natural sorting aka human sorting
Comment: Judging from the way you've provided your ideal sorted result is it only the last two double digits you want to sort by?
Comment: I've put a solution in the answers assuming that's what you mean. Let me know if you mean otherwise!
Comment: @Swazy Is there a way to do it for df column ? I want columns to move as the key column
Comment: Hey @EegiiEnkhtaivan. I'm afraid I'm not quite sure what you mean. Do you mean there is another part of the string you would like to sort by as well?
Comment: @Swazy Hi, Swazy. I mean is it possible to perform below in DF column ?
Comment: Oh you mean sort items in a pandas DataFrame column?
Answer: This should do the trick:
z = sorted(list_list, key=lambda s: (s[-5:-3], s[-2:]))

Comment: Probably `z = sorted(list_list, key=lambda s: s[:-5])` will be enough for the provided data
Comment: Yeah agreed. Thought it might be nice to be explicit about how you could extract specific integers from the string to sort by. Maybe more complicated than necessary though :)


Forecasting Prices vs Returns by Deep Learning

Question: The question: It seems that (univariate) forecasting stock market done by websites using DL and LSTM actually does not work that well if we focus on returns instead of prices. What is a relatively quick fix for that?(or most important fix)
Explanation: There are hundreds of python DL codes in internet trying to forecast stock market(usually S&P 500 ) prices using LSTM and other methods mostly in keras/tensorflow(an example here:[1]).
When I applied and graphed visually it seems to be a good forecast.
On the other hand when we do the same method for "returns" , everything falls apart. Even a constant forecast (For example always predicating that the return will be 0.01 monthly) does better or relatively the same as DL. What is the reason and mostly importantly cure for that?
Comment: If you have a good predictor for stock prices (which is an extraordinary statement), why not forecast the price and then calculate the forecasted return based on it (i.e. $p_t/p_{t-1}-1$)? You wouldn't need a separate model for returns (or am I missing something?). If it doesn't work well, obviously the stock price forecast wast not satisfactory to begin with (a pretty well established fact in financial mathematics).
Comment: I was thinking something along the lines of: if we have a perfect price forecast, it will lead to the perfect return forecast since one follows from the other. You would only need to fit 1 model for the prices. In econometrics/finance returns are not forecasted themselves, since they change drastically (from positive to negative) in very short periods of time, making a return plot look like a dense zig-zag scatter. This is why there are models for their *volatility* instead (periods of larger vs smaller zig-zags) such as ARCH or GARCH.
Comment: I disagree with the answers below, both in conclusion, and motivation.  I watched "think or swim" and paper trading sites, and it seems to me (personally) that owners thereof use them to adaptively extract patterns of investing by individuals.  The non-long market tries very hard to be a zero sum game (Poundstone,2005, p389/827), which means it isn't about value creation but value extraction from the "non-winners".  Buffets non-transitive dice show how with money and nerd you can lock others out of the "winner-pool".  The actual system is the national economy which doesn't move fast, so glass.
Comment: @PaulG Forecasting the price and forecasting the return turn out to be quite different. "When I applied and graphed visually [price]  seems to be a good forecast. On the other hand when we do the same method for "returns" , everything falls apart"
Comment: So the "cure" would be simple: model returns with a different model or model their volatility only, since they're pretty much white noise (i.e. can't be forecasted in the mean precisely).
Answer: What you've outlined is probably the single most common error that machine learning researchers make when analyzing financial data: it's trivial to discover that a great predictor of tomorrow's price is today's price.
The statistical term of art for this phenomenon is "non-stationarity." We have a number of questions about how to test for the stationarity of a time series. One such thread is How to know if a time series is stationary or non-stationary? In the particular case of time series analysis of financial data, it might be helpful to review a high-quality statistical text, such as Statistics and Data Analysis for Financial Engineering, Second Edition (David Ruppert & David S. Matteson). On page 308, we find the remark

As mentioned, many financial time series do not exhibit stationarity, but often the changes in them, perhaps after applying a log transformation, are approximately stationary.

(This is a quite extensive textbook about time series data and financial data, so it's worth reading in some detail if you're interested in how to pursue this project further.)
So to answer your question, the example neural networks that you mention discover that the financial data are non-stationary, and these models make use of that fact when making predictions. But if you look at returns, then the non-stationarity phenomenon disappears, and the model is not able to discover such a simple rule to exploit.
The cure, in some sense, is to discover what drives stock prices, either generally or in the specific case of the equities you're studying. The price changes every second -- why is that? What information could a person have that causes a 0.1% shift from minute to minute, or 1% day to day? It's unlikely that yesterday's price movement, or the price movement the day before, will tell you much of anything about tomorrow's price movement by itself with a high degree of precision -- because, as we know, past performance is no guarantee of future returns.
Framed in this way, the problem is not about choosing a certain kind of neural network, but instead making a neural network that has relevant data to inform its predictions. So, right now, you know that a good predictor of price tomorrow is the price today. To improve on that, you'll have to find timely information that improves upon the "best guess" provided by yesterday's price data.
As an example of what form this information might take, consider pairs trading. In the 1980s, Morgan Stanley quants invented "pairs trading" and the strategy was profitable for a while. The premise is that two highly correlated stocks will tend to move together, so if there is movement in one that's not present in the other, you can make a trade with thesis that eventually the two stocks will return to their equilibrium. So your neural network would use information about one stock to place trades on the second stock, and vice-versa. Naturally, pairs trading is only profitable as long as the premise that the pairs are strongly correlated is true.
Comment: Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackexchange.com/rooms/132112/discussion-on-answer-by-sycorax-forecasting-prices-vs-returns-by-deep-learning).
Answer:
The question: It seems that (univariate) forecasting stock market done
by websites using DL and LSTM actually does not work that well if we
focus on returns instead of prices. What is a relatively quick fix for
that?(or most important fix)

The motivation is quite simple. You can find it in any financial economic/econometrics text. As starting point we can consider stock price (log-price) as described from a Random Walk model (RW):
$p_t = p_{t-1}  + \epsilon_t$
where  $\epsilon_t$ are iid gaussian noise. Then $E[p_{t+1}|I_t]= p_{t}$
$I_t$ stand for information set at time $t$, but it boil down into $p_t$ in this model. So for log return = $r_t = p_t – p_{t-1}$ we have  $E[r_{t+1}|I_t]=0$ for all $t$
Take away:
price is non-stationary series, the best predictor for price in the future is price now, returns are stationary and best predictor for them is zero.
Candidate predictive model have to predict better than RW, using in some way $I_t$. usually is better to try with return than price, because the former are stationary (wide sense).
Under usual definition of predictability, price are quite well predictable while return are hard to predict. So your evidence is usual.
Machine learning can be used fruitfully in finance too, but beat RW is far from simple.
NOTE: Just to note that some comments below touch interesting points as equilibrium or others; however here we have to stay focused on prediction only. Others comment ask empirical support. About the last request, the story above is a model (theory) and, apart any details, it represent the oldest stochastic model in finance (Bachelier model: https://en.wikipedia.org/wiki/Random_walk#Applications; https://en.wikipedia.org/wiki/Bachelier_model ). RW became a benchmark model in stock prediction (and for other assets too). During the decades was proposed, in academy and financial industry, hundreds of predictive models. Models that, looking for predictability, refute the RW condition like $E[r_{t+1}|r_t, r_{t-1} …]=0$. Them searched some model for which $E[r_{t+1}|I_t] \neq 0$ and, then, to beat the RW in MSE or other metrics. However until now no one of them can beat the RW for all dataset (all country/index, all stock/name, all data frequency, ecc). So, it is useless to cite here specific articles that confirm or refuse returns predictability, this is an endless empirical debate.
Moreover, for some RW version we can relax gaussianity and independence with weaker conditions but the basic implications above hold yet.
Finally, the history seems me the strongest as possible empirical confirm that the basic RW implications above, in some extent, is consistent with data. Therefore it remain the more simple and more convincing way for explain why stock price are easily predictable while returns are very hard to predict.
Just another point. Some trader/analyst want to predict directly the prices and not the returns. Them intend this work as predict the direction of price in the next period and not the level (it is easy). These guys use something like "prices paths". This target is not different that predict returns direction. However these guys do not realize that there are no way for infer something from non stationary and non ergodic time series.
Comment: I agree that a RW process with 0-mean noise implies that such a sequence generated by such a process has expected return of 0. However, this observation does not, on its own, establish that stock prices are examples of a RW with 0-mean noise. In other words, why do we know that financial data conforms to the properties of this mathematical model?
Comment: Without some sort of positive drift it makes all stock investing idiotic doesn't it?
Comment: If markets are efficient today is the best predictor of tomorrow. If someone could improve on it they would get rich and certainly not share their secret on a public forum. That said some make large amounts of money investing which is hard to attribute just to luck.
Comment: @user54285 Continuing your argument, if it were possible to predict future price changes profitably then that person who could do it would have done so, moving the current price towards the future price and eliminating the predictable future change.  This is what makes markets efficient and prevents others from doing something similar.
Comment: @Henry I think my quibble with that is the mechanism. It is unlikely that a single investor could move the market. I think the argument for efficient markets would argue that this special knowledge would soon be known to all and that would move the market.
Comment: I'm aware about your point. Indeed I never said that we 'know' that RW is 'conform' with real stock world. RW is entirely an assumption, indeed I said 'we can consider... RW'. Actually RW is not easy to beat for prediction and focus on return is better. I do not said more.
Comment: Fine, but OP already has a model that uses $p_{t-1}$ to predict $p_t$, and has found that the "zero return" model is hard to beat, so how does exposition about RW help OP improve their model?
Comment: @Sycorax, OP ask why to forecast returns is harder than forecast prices. It seems me that my reply help readers to understand the basic behind this fact. I do not try to help OP to improve his model. This would be a non trivial task.
Comment: But if we don't know whether or not  a 0-mean RW process describes stock prices, how does a 0-mean RW process explain why forecasting returns is harder than forecasting prices? This seems to be a case of circular reasoning: first, you suggesting that one should **assume** a RW model, and then show that the returns of the RW model are zero, and then conclude that the RW model must also describe stocks because forecasting prices tends to a model which has 0 expected return.
Comment: Stock prices do share similarities with Brownian motions (random walks) and in financial mathematics they are often modeled as such (not for forecasting, but for distribution reasons to calculate e.g. values at risk). It is important to put things into perspective, since basic financial theory (no arbitrage etc.) says prices are not predictable, while OP states that they could be. If they're not predictable in the first place, neither would the returns.
Comment: @Sycorax 'My' argument (not my but from masters) is consistent. We assume RW and derive conclusions from this, as zero cond mean. After we can check that data match surprisingly well this (basic) assumpion. The last is a very interesting thing, from that popularity of RW. This procedure is basic science. From that is useful to keep in mind the take away of RW. I fear that not all of them are clear for non economist machine learners.
Comment: @PaulG, I agree with you, but predictability concept need definition. From above, predictability is usually write on returns, and price, as opposite, is quite well predictable.
Comment: @markowitz You've got me there, it's not at all clear. Indeed, this theory can't even explain why stocks are traded, because trading has zero expected return.
Comment: @Sycorax, it seems me that rarely we can find simpler, clearer and more useful theory than what I shortly summarized above. Said that, your last point go far beyond the take away of RW, and what OP ask. It touch equilibrium issue. RW theory do not care about that. The more widespread theory of equilibrium in financial markets is CAPM.
Comment: @Cagdas Ozgenc; The asker do not ask why people buy stock. Moreover note that if stock buyer are idiot the gambler are even more, however they are many; gambling is a florent business. Said that. You suggest RW with positive drift. For many people it can seem more reliable hypothesis, but it is so only at first glance. Indeed it imply at least two strong conclusions: 1) the price go up forever, go to infinity in the long run. 2) stock returns are predictable in the same extent of the drift.
Comment: Now, if we consider deflated prices/indexes, conclusion 1 is not easy to support in the data, and in any case long run implication are too strong; conclusion 2 are even even less reliable.
Answer: Advances in Financial Machine Learning is a good reference for practical usage of ML in the context of financial time series.
Basically :

Formulating your label in term of level attained in a given amount of time (see chapter 3 barrier method) will help you build practical and realistic strategies. Unless you are doing some market making, you usually don't care about the price at the next ticker. For application you might need to go further and use meta labeling (ie. two algo, one to predict the trend, one to predict the amount to bet).

Regarding feature building, you often go with stationnary processes, that you obtain trough fractionnal differenciation (getting rid of noise without getting rid of information), instead of integer differenciation (0: price, 1: return). See Chapter 5 - Fractionnaly differentiated features.

As mentionned by others, there are complex mechanisms at play, and numerous actors on the market such that any meaningfull price prediction will be taken advantage of and nearly immediatly corected. That's why whitout external/original/new info, you won't get meaningfull prediction (and why underlying are often modelled as randow walks with drift for option pricing). Basically you need external info to predict price movement. See here for an exemple where tweets are used (financial-tweets).

So generally speaking, when using ML for Finance you just don't predict next price from past prices. There is barely any value in doing that. The exception might be in HFT when you are trying to predict price change from limit order book, but that is a very specific case. And, to answer your general question, there is no easy way to deal with it : any 'easy' solution has already been implemented and has been optimised to the point it is difficult to compete.


I can't upload images in CKEditor

Question: With Drupal 8 I'm having issues uploading images in a node.
I select an image in the upload dialog box and add it, yet when I go to view the page the image is not there? Directory permissions for image uploads are set to read/write.
When I inspect the source, I don't even see an image path.
My upload dialog box looks pretty crappy too.

What am I doing wrong?
Comment: Just to be sure: Is `` allowed for the input format you select? Did you save the node?
Comment: I believe so, this is just the vanilla Drupal 8 install, I've made no configuration changes
Comment: Resolved with update to Drupal 8.0.1
Comment: May I ask which browser this is? Or did you perhaps customize the default admin theme (called 'Seven')? This is *not* what it looks like out of the box on any browser I've ever seen.
Answer: Resolved with update to Drupal 8.0.1

I can't upload images in CKEditor


C# possible to compare string[] to 4 other string[]?

Question: I've been looking around for a while now for some answers, but I cant find it anywhere.
I need a solution for this in C#:
if (stringer[1] == stringer[2]||stringer[3]||stringer[4]||stringer[5])
{

}

this obviously doesn't work in C#, so I need a way to do this.
thank you!
Comment: It's `(a == b || a == c || a == d || a == e)`. `(a == b || c || d || e)` doesn't work _anywhere_ (well, except bool, of course :)
Answer: Try this:
if (stringer.Skip(1).Any(v => v.Equals(stringer[0])) {
    ...
}

This code compares every item in the stringer array from the second on to the initial item, returning true if the initial item is duplicated anywhere else in the array.
Comment: Added ToList(), assuming the original type was an array and not a list, given how the OP intended to access it.
Comment: @MikeAtlas You do not need `ToList` on arrays, the extension method works for arrays and other `IEnumerable` objects as well. Moreover, `T[]` implements `IList`.
Answer: You have to compare each item individually, or use a LINQ solution as above.
if (stringer[1] == stringer[2] || stringer[1] == stringer[3] || and so forth)
Answer: If there is a character that will never appear in the strings, you can use it as a delimiter:
 ( "|" + stringer[2] + "|" + stringer[3] + "|" ).IndexOf( "|" + stringer[1] + "|" ) >= 0

Ugly, ancient technique.


Unable to determine the serialization information for *expression* using .Date

Question: I'm trying to read from a mongoDB collection with a Date filter (only the Date, time should be ignored):
var filterDefinition = Builders<SomeClass>.Filter.Eq(p => p.SomeDateTimeProperty.Date, DateTime.Now.Date);
using (var cursor = await _someCollection.FindAsync(filterDefinition))
{ ... }

SomeClass and the SomeDateTimeProperty property look like this:
[BsonIgnoreExtraElements]
[Serializable]
public class ReportConfiguration
{
    ...
    public DateTime SomeDateTimeProperty { get; set; }
    ...
}

The code throws an InvalidOperationException when trying to .FindAsync():

An exception of type 'System.InvalidOperationException' occurred in
  MongoDB.Driver.dll but was not handled in user code Additional
  information: Unable to determine the serialization information for p
  => p.SomeDateTimeProperty.Date

Everything works if I remove the .Date part in the filter (p.SomeDateTimeProperty.Date, DateTime.Now.Date), but I need a yyyy\mm\dd comparison regardless of the hh\mm.
Stacktrace says:
at MongoDB.Driver.ExpressionFieldDefinition`2.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
at MongoDB.Driver.SimpleFilterDefinition`2.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
at MongoDB.Driver.MongoCollectionImpl`1.CreateFindOperation[TProjection](FilterDefinition`1 filter, FindOptions`2 options)
at MongoDB.Driver.MongoCollectionImpl`1.FindAsync[TProjection](FilterDefinition`1 filter, FindOptions`2 options, CancellationToken cancellationToken)
at MongoDB.Driver.IMongoCollectionExtensions.FindAsync[TDocument](IMongoCollection`1 collection, FilterDefinition`1 filter, FindOptions`2 options, CancellationToken cancellationToken)
at [...]

What could be the issue?
Comment: Have you tried it without the .Date part?
Comment: Yes, removing the .Date part prevents the Exception, I added this clarification to the question - thanks!
Answer: Have you thought about using a between clause using Lt and Gt operators
 var b = Builders<SomeClass>.Filter;
 var date = DateTime.UtcNow.Date;
 var filter = b.And(
        b.Gte(x => x.SomeDateTimeProperty, date),
        b.Lt(x => x.SomeDateTimeProperty, date.AddDays(1))
        );
 ...

Comment: I think because .Date is a c# function on DateTime, that the MongoDb driver is unable to interpret when you try to apply it to a property on your Mongodb model property. You will find a lot of this when using the MongoDb c# driver, particularly when using Linq
Comment: Yes, that's the current solution I approached and it works just fine, it's a good and clear workaround. However I am curious why the first approach doesn't work...
Answer: The Driver doesn't know how to serialize the Eq expression between two Dates.
Try doing ToString() to both Date parts:
var filterDefinition = Builders<SomeClass>.Filter.Eq(p => p.SomeDateTimeProperty.Date.ToString(), DateTime.Now.Date.ToString());

If that doesn't work, try something like this:
var date = DateTime.Now.Date.ToString();
var docs = _someCollection.asQueryable().Where(p => p.SomeDateTimeProperty.Date.ToString() == date);

Comment: Tried the first one, I get the same InvalidOperationException. For the second one I still get:

'An exception of type 'System.InvalidOperationException' occurred in MongoDB.Driver.dll but was not handled in user code. Additional information: {document}{SomeDateTimeProperty}.Date.ToString() is not supported'


Mongoose model function timed out when running inside jest testrunner

Question: I am trying to add testing to the existing codeblock on my backend api using jest framework.
For the function create user

where insertUser is calling save function on db

I get the following test in test file
Answer: so the reason it didn't work was because the mongoose.connect was never called inside the failing test file. Inside the other file was the existing logic for the database, requiring app in user.router.test.js fixed the problem.
alternatively doing the mongoose.connect inside the jest test file also works.
<code>    beforeAll(async () => {
    const url = "mongodb://localhost/crm_ticket_system";
    await mongoose.connect(url);
</code>


How to expand $2^6 \cos^7(x)$

Question: How to expand $2^6 \cos^7(x)$
The answer is $\cos(7x) +7\cos(5x) +21\cos(3x) +35\cos(x)$.
Comment: Is not it following on using de moivre identity to expand $\cos^7x$.
Comment: They may have mistyped the problem statement, it would have worked with $2^\color{red}{6}\cos^7 x$.
Comment: Ye you are right
Comment: Have a look [here](https://en.wikipedia.org/wiki/List_of_trigonometric_identities#Power-reduction_formulae)
Answer: $$2\cos{(nx)}=e^{inx}+e^{-inx}$$
$$(2\cos{(x)})^7=(e^{ix}+e^{-ix})^7$$
$$=(e^{i7x}+e^{-i7x})+7(e^{i5x}+e^{-i5x})+21(e^{i3x}+e^{-i3x})+35(e^{ix}+e^{-ix})$$
$$=2\cos{(7x)}+14\cos{(5x)}+42\cos{(3x)}+70\cos{(x)}$$
Comment: Ye I made a mistake by typing 2^7, it was actually 2^6
Comment: Sorry but can u please tell me how to expand $(e^ix+e^-ix)^7
Comment: $(a+b)^n=\sum_{k=0}^n \binom{n}{k} a^k b^{n-k}$ it's called binomial expansion.

How to expand $2^6 \cos^7(x)$


Jquery validate engine radio css help

Question: I am using jquery validate engine plugin. I got the validation right so the challenge is i need to display the radio buttons on line not as a block . Please advice
Comment: So... what does this have to do with jQuery?
Comment:

 New City:

         ewrw

         Test

Answer: Use display:inline; or display:inline-block;. That should fix your problem if I'm reading your question correctly.
Comment: Please add your HTML and CSS then so I can be more helpful.
Comment:     New City:  ewrw  Test
Comment: input[type="radio"] {
 display: inline;
}
.formular span.radio, .formular .radio {
 display: inline;
}
Comment: .formular radio {
 display : inline-block;
 margin-bottom: 5px;
}

Jquery validate engine radio css help